我一直在家里使用Python制作基于文本的游戏,并且在玩家和敌人之间创建一小部分战斗时遇到了麻烦。战斗应该包括两个随机卷;一个为玩家,另一个为外星人。如果外星人高于玩家,那么+1应该被添加到alien_wins计数器,如果玩家获胜+1,则应该添加到player_wins计数器。
我想要发生的是当player_wins计数器达到3时,循环将停止并产生一条消息,当alien_ wins达到3时也应该发生同样的情况,但是我无法完成这项工作。任何帮助将不胜感激,谢谢!
import random
from Tkinter import *
def Fight():
player_wins = 0
alien_wins = 0
while player_wins <= 3:
player_roll = random.randint(0, 10)
alien_roll = random.randint(0, 7)
if player_roll > alien_roll:
contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")
player_wins += 1
return
elif alien_roll > player_roll:
contents.set("The alien reaches out and strikes you with its claws.")
alien_wins += 1
return
elif alien_roll == player_roll:
contents.set("You both grapple eachother and eventually back off.")
return
if player_wins == 3:
contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")
win = True
break
elif alien_wins == 3:
contents.set("You lose the fight to the alien. Game Over!")
break
base = Tk()
contents = StringVar()
display = Message(base, textvariable = contents, relief=RAISED)
display.pack()
enterbutton = Button(base, text = 'Try Again', command = Fight).pack()
base.mainloop()
答案 0 :(得分:2)
当任一条件(点数&lt; = 3)为True
时,您希望继续战斗(循环)。您需要在while
循环中添加另一个条件。
你只想在战斗结束后从该功能返回,所以删除所有这些返回并在战斗结束后添加一个在循环之外。此外,您只想在战斗结束后创建消息。
def Fight():
player_wins = 0
alien_wins = 0
while player_wins <= 3 and alien_wins <= 3:
player_roll = random.randint(0, 10)
alien_roll = random.randint(0, 7)
if player_roll > alien_roll:
contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")
player_wins += 1
elif alien_roll > player_roll:
contents.set("The alien reaches out and strikes you with its claws.")
alien_wins += 1
elif alien_roll == player_roll:
contents.set("You both grapple eachother and eventually back off.")
if player_wins == 3:
contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")
win = True
elif alien_wins == 3:
contents.set("You lose the fight to the alien. Game Over!")
return
答案 1 :(得分:1)
我清理了一些东西。循环的主要问题是通过使循环依赖于player_wins
和alien_wins
<= 3
来解决的。这样,只要其中一个命中3
,循环就会终止,然后控制转到下面的if/else
语句,然后设置获胜者。
def Fight():
player_wins = 0
alien_wins = 0
while player_wins <= 3 and alien_wins <= 3:
player_roll = random.randint(0, 10)
alien_roll = random.randint(0, 7)
if player_roll > alien_roll:
contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")
player_wins += 1
elif alien_roll > player_roll:
contents.set("The alien reaches out and strikes you with its claws.")
alien_wins += 1
else:
contents.set("You both grapple eachother and eventually back off.")
if player_wins == 3:
contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")
win = True
else:
contents.set("You lose the fight to the alien. Game Over!")
你也不需要那些break
语句,因为它完全打破了循环。您可能正在考虑continue
,它将进入循环的下一次迭代,但是即使这样也不需要,因为循环的整个内容只是一个if/else
语句,所以只有一个分支将执行。
此外,您检查两个elif
相等的rolls
是不必要的,因为您已经排除了两者都不大于/小于另一个,因此可以假设它们是等于。同样适用于最终if/else
,您可以在其中检查谁3
。如果我们不在循环中,我们知道某人有3
,所以我们只需要实际检查单个玩家得分。