这是关于Python的第9篇文章,介绍如何用Python设计一个经典小游戏:猜大小。
在这个游戏中,将用到前面我介绍过的所有内容:变量的使用、参数传递、函数设计、条件控制和循环等,做个整体的总结和复习。
游戏规则:
初始本金是1000元,默认赔率是1倍,赢了,获得一倍金额,输了,扣除1倍金额。
- 玩家选择下注,押大或押小;
- 输入下注金额;
- 摇3个骰子,11≤骰子总数≤18为大,3≤骰子总数≤10为小;
- 如果赢了,获得1倍金额,输了,扣除1倍金额,本金为0时,游戏结束。
程序运行结果是这样的:
现在,我们来梳理下思路。
- 我们先让程序知道如何摇骰子;
- 让程序知道什么是大,什么是小;
- 用户开始玩游戏,如果猜对,赢钱;猜错,输钱;输完后,游戏结束。
梳理清楚思路后,接下来开始敲代码。
摇骰子:
定义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)后可以看到打印出的随机数,每次运行结果都是随机的。
接下来我们看下摇骰子这部分的完整代码:
1 import random 2 3 def roll_dice(numbers = 3,points = None): 4 print('----- 摇骰子 -----') 5 if points is None: 6 points = [] 7 # points为空列表,后续可以插入新值到该列表 8 while numbers > 0: 9 point = random.randrange(1,7) 10 points.append(point) 11 # 用append()方法将point数值插入points列表中 12 numbers = numbers - 1 13 # 完成一次,numbers减1,当小于等于0时不再执行该循环 14 return points
定大小:
11≤骰子总数≤18为大,3≤骰子总数≤10为小,代码如下:
1 def roll_result(total): 2 isBig = 11 <= total <=18 3 isSmall = 3 <= total <= 10 4 if isBig: 5 return '大' 6 elif isSmall: 7 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()
到这里,我们就完成了该游戏三大部分的设计,大家一定要仔细思考,梳理设计思路,动手敲出代码才好。
最后,附【猜大小】游戏的完整代码:
1 import random 2 3 def roll_dice(numbers = 3,points = None): 4 print('----- 摇骰子 -----') 5 if points is None: 6 points = [] 7 while numbers > 0: 8 point = random.randrange(1,7) 9 points.append(point) 10 numbers = numbers - 1 11 return points 12 13 14 def roll_result(total): 15 isBig = 11 <= total <=18 16 isSmall = 3 <= total <= 10 17 if isBig: 18 return '大' 19 elif isSmall: 20 return '小' 21 22 23 def start_game(): 24 your_money = 1000 25 while your_money > 0: 26 print('----- 游戏开始 -----') 27 choices = ['大','小'] 28 your_choice = input('请下注,大 or 小:') 29 your_bet = input('下注金额:') 30 if your_choice in choices: 31 points = roll_dice() 32 total = sum(points) 33 youWin = your_choice == roll_result(total) 34 if youWin: 35 print('骰子点数:',points) 36 print('恭喜,你赢了 {} 元,你现在有 {} 元本金'.format(your_bet,your_money + int(your_bet))) 37 your_money = your_money + int(your_bet) 38 else: 39 print('骰子点数:',points) 40 print('很遗憾,你输了 {} 元,你现在有 {} 元本金'.format(your_bet, your_money - int(your_bet))) 41 your_money = your_money - int(your_bet) 42 else: 43 print('格式有误,请重新输入') 44 else: 45 print('游戏结束') 46 47 start_game()
操作环境:Python版本,3.6;PyCharm版本,2016.2;电脑:Mac
----- End -----
作者:杜王丹,微信公众号:杜王丹,互联网产品经理。