标题: 学习Python,个人完善了踢点球小游戏(分享) [打印本页] 作者: changri218 时间: 2017-12-27 13:26 标题: 学习Python,个人完善了踢点球小游戏(分享) 在学习借鉴crossin老师和其他吧友代码的基础上,补充完善了点球小游戏,供交流分享。
本人代码有点长,有其他更精简的语句,求分享。
代码如下:
from random import choice
d=['left','center','right']
c=['up','down']
score=[0,0]
def order(x,y): #决定先踢还是先守门
if x==y:
print('You kick first!')
return 1
else:
print('You save first!')
return 0
def you_kick():
print('YOU KICK:choose one side to shoot')
print('left,center,right')
you=input()
while you not in d:
print('Choose again,spell right!')
you=input()
print('you kicked: '+you)
com=choice(d)
print('computer saved: '+com)
if you!=com:
print('goal!')
score[0]+=1
else:
print('Oops...')
print('Score: %d(you) - %d(com)\n'%(score[0],score[1]))
def you_save():
print('YOU SAVE:choose one side to save')
print('left,center,right')
you=input()
while you not in d:
print('Choose again,spell right!')
you=input()
print('you saved: '+you)
com=choice(d)
print('computer kicked: '+com)
if you!=com:
print('Oops...')
score[1]+=1
else:
print('saved!')
print('Score: %d(you) - %d(com)\n'%(score[0],score[1]))
print('Now start the penerlty!\n')
print('Deside the order to kick')
print('Please choose the side of coin:')#猜硬币,决定踢点球顺序
print('up,down')
you=input()
while you not in c:
print('Choose again,spell right!')
you=input()
ref=choice(c)
print('The side of coin is '+ref)
order=order(you,ref)
print('===Game Start!!!===\n')
for i in range(5):
print('==Round %d==\n'%(i+1))
if order==1: # 猜对硬币先踢
you_kick()
if (abs(score[0]-score[1])>(4-i)): #5轮常规点球内提前结束判定,下同
break
you_save()
if (abs(score[0]-score[1])>(4-i)):
break
elif order==0: #猜错硬币先守门
you_save()
if (abs(score[0]-score[1])>(4-i)):
break
you_kick()
if (abs(score[0]-score[1])>(4-i)):
break
while (score[0]==score[1]): #5轮常规点球若踢平,后续1vs1决胜
i+=1
print('==Round %d==\n'%(i+1))
if order==1:
you_kick()
you_save()
else:
you_save()
you_kick()