首页 技术 正文
技术 2022年11月8日
0 收藏 597 点赞 1,520 浏览 2749 个字

这是关于Python的第9篇文章,介绍如何用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的内置库,用它来生成随机数。如:

 import random point = random.randrange(1,7) # random.randrange(1,7)生成1-6的随机数 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版本,3.6;PyCharm版本,2016.2;电脑:Mac

—–   End   —–

作者:杜王丹,微信公众号:杜王丹,互联网产品经理。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,286