今天,編寫小遊戲用python實作。
1.遊戲要求:
設定球的方向:左中右三個方向,射門或者撲救動作,循環5次,直接輸入方向。電腦随機挑選方向,如果方向相同,那麼電腦得分,如果方向相反,那麼人得分。
2.分析如何寫程式:
1)循環,使用for ..in range()
2) if ..else
3)from random import choice 随機選擇
3.腳本如下:
from random import choice
score_person=
score_com=
location=['left','center','right']
for i in range ():
print ("----Round %d You kicked----"%(i+))
com_choice=choice(location)
print ("Computer's choice is %s"%com_choice)
print ("input what your choice:left/center/right")
you_choice=input()
print ("You have choose:"+you_choice)
if you_choice!=com_choice: # 方向不同,球進!
score_person+= #人得分
print ("Kicked!")
else:
print ("Saved unsuccesfully!") #補救
print ("Score:%d(person)-%d(com)\n" %(score_person, score_com))
print ("----Round %d You saved----"%(i+))
com_choice=choice(location)
print ("Computer's choice is %s"%com_choice)
print ("input what your choice is:left/center/right")
if you_choice==com_choice: #方向相同,球不進!
print ("Saved unsucessfully!")
score_com+= #電腦得分
else:
print ("Kicked")
print ("Score:%d(person)-%d(com)\n"%(score_person, score_com))
這小遊戲的功能類似于猜數遊戲。