python简单实现剪刀石头布小游戏

源码:

python3:

import random

name = 'player'
hero_name = '' #hero's name
ren_win = 0
com_win = 0
pk_sum = 0

# 1.选择英雄


def selecHero():

    print('=====================welcome to 人机对战===========================')
# 1.变量
    global name

    #利用input通过控制台进行输入
    name = input('欢迎召唤师,输入你的昵称:')
    #选择对战的英雄 序号
    hero = input('请输入对战的hero的序号;1.盖伦 2.卡莎  3.马儿扎哈  4.安妮')
    #通过input接收的所用变量都是字符串类型
    global hero_name

    if hero == '1':
        print('你选择了盖伦')
        hero_name = '盖伦'
    elif hero == '2':
        print('选择了卡莎')
        hero_name = '卡莎'
    elif hero == '3':
        print('选择了马儿扎哈')
        hero_name = '马儿扎哈'
    else:
        print('选择了安妮')
        hero_name = '安妮'
        
# 2.进行pk对战
def ren_com_PK():

    global ren_win
    global com_win
    global pk_sum

    print(pk_sum)
    # 进行pk
    while True:
        pk_sum += 1
        ren_key = int(input('选择你的手势:1.石头,2.剪刀,3.布'))
        if ren_key == 1:
            print('选择了石头')
        elif ren_key == 2:
            print('选择了剪刀')
        else:
            print('选择了布')

        #电脑选择 使用随机数 需要导入随机数模块
        #random.randint(a,b) 随机生产一个[a,b]之间的随机数,闭区间
        #random.randrange(a,b)随机列表
        com_key = random.randint(1,3)

        if com_key == 1:
            print('选择了石头')
        elif com_key == 2:
            print('选择了剪刀')
        else:
            print('选择了布')

        if ren_key==1 and com_key==2 or  ren_key==2 and com_key==3 or ren_key==3 and com_key==1:
            print('win!')
            ren_win+=1
        elif ren_key == com_key:
            print('平局')
        else:
            print('本局输了')
        if pk_sum > 5:
            print('本次对战结束')
            break


# 3.显示结果
def showResult():
    print('=====================结果显示===================')
    print('%s一共进行%d\n你赢了%d局\n电脑赢了%d\n最总结果:'%(name,pk_sum,ren_win,com_win))
    if ren_win > com_win:
        print('大吉大利')
    elif ren_win == com_win:
        print('平局,来日再战')
    else:
        print('输了')
# 4.退出或继续


def exits():
    global pk_sum

    # 是否退出?退出按E键,按任意键继续游戏
    k = input('是否退出?退出按E键,按任意键继续游戏')
    if k == 'E':
        print('游戏结束')
        exit(0)
    else:
        pk_sum = 0
        selecHero()
        ren_com_PK()
        showResult()
        exits()


if __name__ == '__main__':
    selecHero()
    ren_com_PK()
    showResult()
    exits()

结果

在这里插入图片描述

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值