Python 之 “猜数字”游戏(附源码)

“猜数字”游戏

创建一个可以猜数字的游戏,它会在1~100以内随机选择一个数,然后让玩家挑战在10轮以内猜出这个数字,每一轮都要告诉玩家正确或者错误,如果出错了,则告诉他数字是低还是高了,一旦玩家猜测正确,或者玩家用完了回合游戏将会结束。游戏结束后,可以让玩家选择重新开始,消耗相应的金币即可以继续游戏。

运行环境

[开发环境]:
python 3.6 运行代码
pycharm 2021 辅助敲代码
第三方模块 pip install 模块名
镜像源安装 pip install -i pypi.douban.com/simple/ +模块名

源码

import random
import time
x1 = ['╠','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','╣']
for i in x1:
    print(i, end='')
    time.sleep(0.02)
print('\n加', end='')
time.sleep(0.05)
print('载', end='')
time.sleep(0.05)
print('中', end='')
time.sleep(0.05)
print('.', end='')
time.sleep(0.05)
print('.', end='')
time.sleep(0.05)
print('.', end='')
time.sleep(0.05)
print('.', end='')
time.sleep(0.05)
print('.', end='')
time.sleep(0.05)
print('.')
print('欢迎来到猜数游戏!')
lives = 10
tax = 1
ranswer = 0
coines = 300
number = random.randint(0, 100)
name = input('请输入昵称!')
if name != '爸爸':
    print('尊敬的用户' + name + '您好')
    print('欢迎来到猜数游戏!')
elif name == '爸爸':
    print('我是你爸爸!')
    print('你是我儿子!')
a = ['第' + str(tax) + '关!']
for l in a:
    time.sleep(0.05)
    print(l)
c = ['现有' + str(coines) + '金币!' ]
for d in c:
    time.sleep(0.05)
    print(d)
while lives > 0:
    print('请', end='')
    time.sleep(0.05)
    print('输', end='')
    time.sleep(0.05)
    print('入', end='')
    time.sleep(0.05)
    print('0', end='')
    time.sleep(0.05)
    print('-', end='')
    time.sleep(0.05)
    print('1', end='')
    time.sleep(0.05)
    print('0', end='')
    time.sleep(0.05)
    print('0', end='')
    time.sleep(0.05)
    print('的', end='')
    time.sleep(0.05)
    print('数', end='')
    time.sleep(0.05)
    print('字', end='')
    time.sleep(0.05)
    answer = input(':')
    ranswer = int(answer)
    if ranswer < number:
        print('''|( ̄Д  ̄)=凸
太小了!''')
        lives -= 1
        time.sleep(0.5)
    elif ranswer > number:
        print('''(︶︿︶)=凸
太大了!''')
        lives -= 1
        time.sleep(0.5)
    if ranswer == number:
        print('''(凸艹皿艹)凸
竟然对了!''')
        tax += 1
        lives = 8
        time.sleep(0.05)
        print('是', end='')
        time.sleep(0.05)
        print('否', end='')
        time.sleep(0.05)
        print('在', end='')
        time.sleep(0.05)
        print('玩', end='')
        time.sleep(0.05)
        print('?', end='')
        time.sleep(0.05)
        print('\n不', end='')
        time.sleep(0.05)
        print('玩', end='')
        time.sleep(0.05)
        print('试', end='')
        time.sleep(0.05)
        print('试', end='')
        time.sleep(0.05)
        print('!', end='')
        print('へ', end='')
        time.sleep(0.05)
        print('(', end='')
        time.sleep(0.05)
        print('`', end='')
        time.sleep(0.05)
        print('_', end='')
        time.sleep(0.05)
        print('ゝ', end='')
        time.sleep(0.05)
        print('´', end='')
        time.sleep(0.05)
        print(')', end='')
        time.sleep(0.05)
        print('▄',end='')
        time.sleep(0.05)
        print('︻',end='')
        time.sleep(0.05)
        print('┻',end='')
        time.sleep(0.05)
        print('┳',end='')
        time.sleep(0.05)
        print('═',end='')
        time.sleep(0.05)
        print('═',end='')
        time.sleep(0.05)
        print('━',end='')
        time.sleep(0.05)
        print('一')
        time.sleep(0.05)
        print('y', end='')
        time.sleep(0.05)
        print('/', end='')
        time.sleep(0.05)
        yn = input('n')
        if yn == 'n':
            print('杀过' + sr(tax) + '关!')
            print('   凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)')
            x1 = ['╠','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','╣']
            for i in x1:
                print(i, end='')
                time.sleep(0.02)
            break
        else:
            print('ヾ(@^▽^@)ノ还是好盆友!')
            lives = 10
            number = random.randint(0, 100)
            a = ['第' + str(tax) + '关!']
            for l in a:
                time.sleep(0.05)
                print(l)
            coines += 300
            c = ['现有' + str(coines) + '金币!' ]
            for d in c:
                time.sleep(0.05)
                print(d)
            continue
    if lives == 0 and coines > 200:
        print('很', end='')
        time.sleep(0.05)
        print('遗', end='')
        time.sleep(0.05)
        print('憾', end='')
        time.sleep(0.05)
        print('!')
        time.sleep(0.05)
        print('机', end='')
        time.sleep(0.05)
        print('会', end='')
        time.sleep(0.05)
        print('耗', end='')
        time.sleep(0.05)
        print('尽', end='')
        print('!')
        print('是', end='')
        time.sleep(0.05)
        print('否', end='')
        time.sleep(0.05)
        print('在', end='')
        time.sleep(0.05)
        print('玩', end='')
        time.sleep(0.05)
        print('(', end='')
        time.sleep(0.05)
        print('消', end='')
        time.sleep(0.05)
        print('耗', end='')
        time.sleep(0.05)
        print('2', end='')
        time.sleep(0.05)
        print('0', end='')
        time.sleep(0.05)
        print('0', end='')
        time.sleep(0.05)
        print('金', end='')
        time.sleep(0.05)
        print('币', end='')
        time.sleep(0.05)
        print(')', end='')
        time.sleep(0.05)
        print('y', end='')
        time.sleep(0.05)
        print('/', end='')
        time.sleep(0.05)
        YN = input('n') 
        if YN == 'y':
            print('ヽ( ̄ω ̄( ̄ω ̄〃)ゝ是条汉子!')
            lives = 10
            coines -= 300
            c = ['现有' + str(coines) + '金币!' ]
            for d in c:
                time.sleep(0.05)
                print(d)
            number = random.randint(0, 100) 
        else:
            tax -= 1
            print('杀过' + str(tax) + '关!')
            print('   凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)凸(艹皿艹凸)')
            x2 = ['╠','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','┅','╣']
            for j in x2:
                print(j, end='')
                time.sleep(0.02)
    elif lives == 0 and coines < 200:
        y = ['金币不足!\n无法复活!']
        for f in y:
            print(f)
            time.sleep(0.05)
            tax -= 1
            print('杀过' + sr(tax) + '关!')
        break

效果展示

游戏规则:猜数字高级版本是一个闯关版本—每10次机会猜,猜对即可获取300金币,猜错即
游戏失败,初始金币为300,每玩一次需要消耗200金币。
(1)第一关卡
ldtx6jue.png
(2)随机截图
ldtx76hg.png

END

原文公众号:Python顾木子吖

posted @ 2023-09-18 17:53  Ivan丶ky  阅读(1001)  评论(0)    收藏  举报