用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通过PIL获取图片主要颜色并和颜色库进行对比的方法
Mar 19 Python
Python 3实战爬虫之爬取京东图书的图片详解
Oct 09 Python
Python简直是万能的,这5大主要用途你一定要知道!(推荐)
Apr 03 Python
python将字符串list写入excel和txt的实例
Jul 20 Python
对python3中的RE(正则表达式)-详细总结
Jul 23 Python
Python利用requests模块下载图片实例代码
Aug 12 Python
Django 项目重命名的实现步骤解析
Aug 14 Python
python 中的[:-1]和[::-1]的具体使用
Feb 13 Python
python 基于opencv操作摄像头
Dec 24 Python
基于python实现银行管理系统
Apr 20 Python
python 自动刷新网页的两种方法
Apr 20 Python
Python selenium模拟网页点击爬虫交管12123违章数据
May 26 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配置php-fpm启动参数及配置详解
2013/11/04 PHP
Zend Framework入门教程之Zend_Mail用法示例
2016/12/08 PHP
PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】
2017/11/16 PHP
Laravel框架基础语法与知识点整理【模板变量、输出、include引入子视图等】
2019/12/03 PHP
父窗口获取弹出子窗口文本框的值
2006/06/27 Javascript
showModelessDialog()使用详解
2006/09/07 Javascript
基于NodeJS的前后端分离的思考与实践(三)轻量级的接口配置建模框架
2014/09/26 NodeJs
jQuery实现表单步骤流程导航代码分享
2015/08/28 Javascript
深入学习AngularJS中数据的双向绑定机制
2016/03/04 Javascript
jQuery实现点击水纹波动动画
2016/04/10 Javascript
js实现不提示直接关闭网页窗口
2017/03/30 Javascript
javaScript 逻辑运算符使用技巧整理
2017/05/03 Javascript
Vue.js实例方法之生命周期详解
2017/07/03 Javascript
Node.js搭建WEB服务器的示例代码
2018/08/15 Javascript
vue安装和使用scss及sass与scss的区别详解
2018/10/15 Javascript
基于Vue的侧边目录组件的实现
2020/02/05 Javascript
JS forEach跳出循环2种实现方法
2020/06/24 Javascript
vue v-for出来的列表,点击某个li使得当前被点击的li字体变红操作
2020/07/17 Javascript
Python学生信息管理系统修改版
2018/03/13 Python
Python脚本按照当前日期创建多级目录
2019/03/01 Python
Python占用的内存优化教程
2019/07/28 Python
python getpass模块用法及实例详解
2019/10/07 Python
python 检查数据中是否有缺失值,删除缺失值的方式
2019/12/02 Python
python装饰器的特性原理详解
2019/12/25 Python
python GUI库图形界面开发之PyQt5 Qt Designer工具(Qt设计师)详细使用方法及Designer ui文件转py文件方法
2020/02/26 Python
Python延迟绑定问题原理及解决方案
2020/08/04 Python
iRobot官网:改变生活的家用机器人品牌
2016/09/20 全球购物
美国在线精品家居网站:Burke Decor
2017/04/12 全球购物
The Kooples美国官方网站:为情侣提供的法国当代时尚品牌
2019/01/03 全球购物
党员群众路线承诺书
2014/05/20 职场文书
医院安全生产月活动总结
2014/07/05 职场文书
2015驻村干部工作总结
2015/04/07 职场文书
Python中Permission denied的解决方案
2021/04/02 Python
python实战之用emoji表情生成文字
2021/05/08 Python
使用canvas仿Echarts实现金字塔图的实例代码
2021/11/11 HTML / CSS
分享MySQL常用 内核 Debug 几种常见方法
2022/03/17 MySQL