源码:
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()