1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python简单实现剪刀石头布小游戏

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

时间:2020-06-21 14:32:57

相关推荐

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

源码:

python3:

import randomname = 'player'hero_name = '' #hero's nameren_win = 0com_win = 0pk_sum = 0# 1.选择英雄def selecHero():print('=====================welcome to 人机对战===========================')# 1.变量global name#利用input通过控制台进行输入name = input('欢迎召唤师,输入你的昵称:')#选择对战的英雄 序号hero = input('请输入对战的hero的序号;1.盖伦 2.卡莎 3.马儿扎哈 4.安妮')#通过input接收的所用变量都是字符串类型global hero_nameif 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_winglobal com_winglobal pk_sumprint(pk_sum)# 进行pkwhile True:pk_sum += 1ren_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+=1elif 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 = 0selecHero()ren_com_PK()showResult()exits()if __name__ == '__main__':selecHero()ren_com_PK()showResult()exits()

结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。