用Python设计一个经典小游戏


Posted in Python onMay 15, 2017

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

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

游戏规则:

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

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

程序运行结果是这样的:

用Python设计一个经典小游戏

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

  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 相关文章推荐
爬山算法简介和Python实现实例
Apr 26 Python
python制作一个桌面便签软件
Aug 09 Python
Python多线程经典问题之乘客做公交车算法实例
Mar 22 Python
浅谈DataFrame和SparkSql取值误区
Jun 09 Python
win8下python3.4安装和环境配置图文教程
Jul 31 Python
python实现对输入的密文加密
Mar 20 Python
python实现维吉尼亚加密法
Mar 20 Python
Django使用中间件解决前后端同源策略问题
Sep 02 Python
Python字典生成式、集合生成式、生成器用法实例分析
Jan 07 Python
tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
Jun 22 Python
python如何编写类似nmap的扫描工具
Nov 06 Python
Python 实现一个简单的web服务器
Jan 03 Python
Python 对象中的数据类型
May 13 #Python
Python中matplotlib中文乱码解决办法
May 12 #Python
使用Python对Csv文件操作实例代码
May 12 #Python
python 读取excel文件生成sql文件实例详解
May 12 #Python
Python实现读取并保存文件的类
May 11 #Python
Python使用defaultdict读取文件各列的方法
May 11 #Python
python中nan与inf转为特定数字方法示例
May 11 #Python
You might like
PHP 之 写时复制介绍(Copy On Write)
2014/05/13 PHP
thinkphp跨库操作的简单代码实例
2016/09/22 PHP
PHP实现计算器小功能
2020/08/28 PHP
javascript下利用arguments实现string.format函数
2010/08/24 Javascript
Jquery实现三层遍历删除功能代码
2013/04/23 Javascript
JavaScript模板引擎用法实例
2015/07/10 Javascript
jQuery实现的网页竖向菜单效果代码
2015/08/26 Javascript
js 判断所选时间(或者当前时间)是否在某一时间段的实现代码
2015/09/05 Javascript
jQuery Validation Plugin验证插件手动验证
2016/01/26 Javascript
基于jQuery和hwSlider实现内容左右滑动切换效果附源码下载(一)
2016/06/22 Javascript
PHP获取当前页面完整URL的方法
2016/12/02 Javascript
js实现日历的简单算法
2017/01/24 Javascript
vue模板语法-插值详解
2017/03/06 Javascript
详解React Native开源时间日期选择器组件(react-native-datetime)
2017/09/13 Javascript
vue2.0组件之间传值、通信的多种方式(干货)
2018/02/10 Javascript
JS实现单张或多张图片持续无缝滚动的示例代码
2020/05/10 Javascript
Element-UI 使用el-row 分栏布局的教程
2020/10/26 Javascript
[51:15]2014 DOTA2国际邀请赛中国区预选赛 Orenda VS LGD-GAMING
2014/05/22 DOTA
[03:53]2016国际邀请赛中国区预选赛第三日TOP10精彩集锦
2016/06/29 DOTA
python利用OpenCV2实现人脸检测
2020/04/16 Python
Python 错误和异常代码详解
2018/01/29 Python
在Mac下使用python实现简单的目录树展示方法
2018/11/01 Python
python实现切割url得到域名、协议、主机名等各个字段的例子
2019/07/25 Python
python的slice notation的特殊用法详解
2019/12/27 Python
python 还原梯度下降算法实现一维线性回归
2020/10/22 Python
Volcom法国官网:美国冲浪滑板品牌
2017/05/25 全球购物
将"引用"作为函数参数有哪些特点
2013/04/05 面试题
英语教师岗位职责
2014/03/16 职场文书
党课培训主持词
2014/04/01 职场文书
青安岗事迹材料
2014/05/14 职场文书
公务员政审材料
2014/12/23 职场文书
公司开除员工通知
2015/04/22 职场文书
《山中访友》教学反思
2016/02/24 职场文书
2016年社区“6.26”禁毒日宣传活动总结
2016/04/05 职场文书
分位数回归模型quantile regeression应用详解及示例教程
2021/11/02 Python
口袋妖怪冰系十大最强精灵,几何雪花排第七,第六类似北极熊
2022/03/18 日漫