用Python设计一个经典小游戏

来源:互联网 发布:杀网络喷子的动漫 编辑:程序博客网 时间:2024/06/03 22:59

这是关于Python的第9篇文章,介绍如何用Python设计一个经典小游戏:猜大小。

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

游戏规则:

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

玩家选择下注,押大或押小;

输入下注金额;

摇3个骰子,11≤骰子总数≤18为大,3≤骰子总数≤10为小;

如果赢了,获得1倍金额,输了,扣除1倍金额,本金为0时,游戏结束。

程序运行结果是这样的:


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

我们先让程序知道如何摇骰子;

让程序知道什么是大,什么是小;

用户开始玩游戏,如果猜对,赢钱;猜错,输钱;输完后,游戏结束。

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

摇骰子:

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

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

import randompoint = random.randrange(1,7)# random.randrange(1,7)生成1-6的随机数print(point)
print(point)后可以看到打印出的随机数,每次运行结果都是随机的。

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

import randomdef 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 randomdef 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 pointsdef 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   -----

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


1 0