我正在自学Python,我正在尝试使用我熟悉的RPG机制制作一个基本的游戏。我的核心技工看起来是这样的:
while not GameOver(): #checking for one side or the other to be all KO'd
turnbegin() #resetting # of moves per player, etc
while not TurnDone(): #checking to see if everyone's out of moves
for ch in activechars: #going through the players who still have moves
if ch not in defeatedchars: #ignoring the KO'd players
attack(ch,target(ch)) #EVERYONE PUNCH EVERYONE (keeping it simple)
else:
pass我的问题是,这个循环在点击GameOver()之后仍然试图运行目标(Ch)函数。计数器已关闭(每个人的KO'd),GameOver函数似乎工作正常;我检查了。但是GameOver返回True,而then...it只是继续进行攻击(),并返回一个错误,即它没有任何人可以针对,而不是因为它已经结束而停止了。我试着创建一个gameover=GameOver()变量,然后用“但不是gameover”来代替,但是在它说开始第2轮之后,它就被卡在了turn ()中。
谢谢你看了这个!我对此非常陌生,非常感谢你的帮助。
发布于 2022-03-07 15:12:50
“虽然不是GameOver()”只有在它完成运行并需要进入另一个循环时才会被计算。
因为TurnDone()仍然是真的,所以它不会退出循环,并且GameOver()不会被重新计算。
当GameOver()被更新为True时,TurnDone()也应该变成True,以防止它进入另一个循环。
发布于 2022-03-07 15:17:35
您是否尝试过在攻击()之后更新activechars列表?此外,如果在for循环中的攻击()之后没有逻辑,则可以替换
else:
pass什么都没有,就骑上那辆车,否则就无关紧要了。保持密码干净。
您应该使用pep-8 (activechars必须是active_chars,等等)。
发布于 2022-03-07 15:47:17
假设您要将activechars和defeatedchars存储在列表中,也许您可以这样做:
# This boolean will be False if there are no more undefeated active chars.
have_active_undefeated_chars = True
while not GameOver() and have_active_undefeated_chars:
turnbegin()
while not TurnDone():
activechars_not_defeated = list(set(activechars) - set(defeatedchars))
if len(activechars_not_defeated) == 0:
have_active_undefeated_chars = False
break
for ch in activechars_not_defeated:
attack(ch,target(ch))activechars_not_defeated获取activechars中没有失败字符的元素,布尔have_active_undefeated_chars告诉您是否仍然存在未失败的活动字符。break语句将使您脱离这两个while循环,而have_active_undefeated_chars则确保循环不会再次运行。
您还可以为for循环使用activechars_not_defeated列表。
当然,我不熟悉您的整个代码,所以我不知道这在上下文中是否有效。但希望这能帮点忙。
https://stackoverflow.com/questions/71383135
复制相似问题