用Python设计一个经典小游戏

yipeiwu_com6年前Python基础

本文主要介绍如何用Python设计一个经典小游戏:猜大小。

在这个游戏中,将用到前面我介绍过的所有内容:变量的使用、参数传递、函数设计、条件控制和循环等,做个整体的总结和复习。

游戏规则:

初始本金是1000元,默认赔率是1倍,赢了,获得一倍金额,输了,扣除1倍金额。

  1. 玩家选择下注,押大或押小;
  2. 输入下注金额;
  3. 摇3个骰子,11≤骰子总数≤18为大,3≤骰子总数≤10为小;
  4. 如果赢了,获得1倍金额,输了,扣除1倍金额,本金为0时,游戏结束。

程序运行结果是这样的:

现在,我们来梳理下思路。

  1. 我们先让程序知道如何摇骰子;
  2. 让程序知道什么是大,什么是小;
  3. 用户开始玩游戏,如果猜对,赢钱;猜错,输钱;输完后,游戏结束。

梳理清楚思路后,接下来开始敲代码。

摇骰子:

定义roll_dice函数,3个骰子,循环次数numbers为3,骰子点数points初始值为空,这里的参数传递用到的是之前讲到的关键词参数传递。

随机数生成用import random来实现。Python中最方便的就是有很多强大的库支持,现在我们可以直接导入一个random的内置库,用它来生成随机数。如:

1 import random
2 point = random.randrange(1,7)
3 # random.randrange(1,7)生成1-6的随机数
4 print(point)

print(point)后可以看到打印出的随机数,每次运行结果都是随机的。

接下来我们看下摇骰子这部分的完整代码:

import random
def roll_dice(numbers = 3,points = None):
 print('----- 摇骰子 -----')
 if points is None:
  points = []
  # points为空列表,后续可以插入新值到该列表
 while numbers > 0:
  point = random.randrange(1,7)
  points.append(point)
  # 用append()方法将point数值插入points列表中
  numbers = numbers - 1
  # 完成一次,numbers减1,当小于等于0时不再执行该循环
 return points

定大小:

11≤骰子总数≤18为大,3≤骰子总数≤10为小,代码如下:

def roll_result(total):
 isBig = 11 <= total <=18
 isSmall = 3 <= total <= 10
 if isBig:
  return '大'
 elif isSmall:
  return '小'

玩游戏:

初始本金1000元,默认赔率1倍;赢了,获得一倍金额,输了,扣除1倍金额;本金为0时,游戏结束。

def start_game():
 your_money = 1000
 while your_money > 0:
  print('----- 游戏开始 -----')
  choices = ['大','小']
  # choices赋值为大和小,用户需输入二者之一为正确
  your_choice = input('请下注,大 or 小:')
  your_bet = input('下注金额:')
  if your_choice in choices:
   points = roll_dice()
   # 调用roll_dice函数
   total = sum(points)
   # sum为相加,将3个骰子的结果相加
   youWin = your_choice == roll_result(total)
   if youWin:
    print('骰子点数:',points)
    print('恭喜,你赢了 {} 元,你现在有 {} 元本金'.format(your_bet,your_money + int(your_bet)))
    # your_bet是字符串格式,这里需要转化为int类型进行计算
    your_money = your_money + int(your_bet)
    # 最新本金
   else:
    print('骰子点数:',points)
    print('很遗憾,你输了 {} 元,你现在有 {} 元本金'.format(your_bet, your_money - int(your_bet)))
    your_money = your_money - int(your_bet)
  else:
   print('格式有误,请重新输入')
   # 如果输入的不是choices列表中的大或小,则为格式有误
 else:
  print('游戏结束')
start_game()

到这里,我们就完成了该游戏三大部分的设计,大家一定要仔细思考,梳理设计思路,动手敲出代码才好。

最后,附【猜大小】游戏的完整代码

import random

def roll_dice(numbers = 3,points = None):
 print('----- 摇骰子 -----')
 if points is None:
  points = []
 while numbers > 0:
  point = random.randrange(1,7)
  points.append(point)
  numbers = numbers - 1
 return points

def roll_result(total):
 isBig = 11 <= total <=18
 isSmall = 3 <= total <= 10
 if isBig:
  return '大'
 elif isSmall:
  return '小'

def start_game():
 your_money = 1000
 while your_money > 0:
  print('----- 游戏开始 -----')
  choices = ['大','小']
  your_choice = input('请下注,大 or 小:')
  your_bet = input('下注金额:')
  if your_choice in choices:
   points = roll_dice()
   total = sum(points)
   youWin = your_choice == roll_result(total)
   if youWin:
    print('骰子点数:',points)
    print('恭喜,你赢了 {} 元,你现在有 {} 元本金'.format(your_bet,your_money + int(your_bet)))
    your_money = your_money + int(your_bet)
   else:
    print('骰子点数:',points)
    print('很遗憾,你输了 {} 元,你现在有 {} 元本金'.format(your_bet, your_money - int(your_bet)))
    your_money = your_money - int(your_bet)
  else:
   print('格式有误,请重新输入')
 else:
  print('游戏结束')

start_game()

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持【听图阁-专注于Python设计】!

相关文章

关于pytorch中全连接神经网络搭建两种模式详解

pytorch搭建神经网络是很简单明了的,这里介绍两种自己常用的搭建模式: import torch import torch.nn as nn first: class NN...

python2.7 安装pip的方法步骤(管用)

python2.7 安装pip的方法步骤(管用)

python2.7安装目录下没有Scripts文件夹。这种问题可能是你装的python安装包年代久远了,到官网下载最新的python2.7安装能解决这个问题。python2.7下载地址:...

TensorFlow绘制loss/accuracy曲线的实例

TensorFlow绘制loss/accuracy曲线的实例

1. 多曲线 1.1 使用pyplot方式 import numpy as np import matplotlib.pyplot as plt x = np.arange(1,...

Selenium chrome配置代理Python版的方法

环境: windows 7 + Python 3.5.2 + Selenium 3.4.2 + Chrome Driver 2.29 + Chrome 58.0.3029.110 (64...

用Python进行行为驱动开发的入门教程

用Python进行行为驱动开发的入门教程

为驱动开发(Behavior-Driven Development,BDD)是一种卓越的开发模式。能帮助开发者养成日清日结的好习惯,从而避免甚至杜绝“最后一分钟”的情况出现,因此对提高代...