【发布时间】:2020-03-17 07:22:31
【问题描述】:
我正在编写一个游戏,其中敌方海龟(在程序中称为 badturt)追逐用户的海龟。用户可以通过发送攻击(另一只乌龟)使其乌龟攻击敌方乌龟。
在 lvl 2 中,有两只敌方海龟在追逐用户的海龟。为了让一只敌方海龟停止移动(在它被攻击/击中后),我尝试重新定义使敌方海龟移动的函数,这是在另一个函数中完成的。 (我将其重新定义为无)
attack = turtle.Turtle()
#...attributes
def turtleattack():
global lvl
global q
global w
global e
#... positioning attack
for i in range(75):
attack.forward(10)
if lvl == 1:
Chase(badturt)
if lvl == 2:
Chase(badturt)
Chase(badturt2)
if lvl == 3:
Chase(badturt)
Chase(badturt2)
Chase(badturt3)
IfAttackHit()
bg.onkeypress(turtleattack, 'z')
bg.listen()
def Chase(bt): #makes bad turt (bt) chase turt
bt.setheading(bt.towards(turt))
bt.forward(11)
def StopChase(bt):
global lvl
global win
#global Chase <---------------- program stops running if I write it in
if lvl == 1:
#...
if lvl == 2:
def Chase(bt):
None
if q == 2 and w == 2:
lvl = 3
writeinfo()
if lvl == 3:
def Chase(bt):
None
if q == 3 and w == 3 and e == 3:
#... (winning the game)
def ChaseAgain(bt): #makes badturt chase again when it moves onto next lvl
def Chase(bt):
bt.setheading(badturt.towards(turt))
bt.forward(11)
Chase(bt)
def IfAttackHit():
global win
global lvl
global q
global w
global e
if lvl == 1:
if badturt.distance(attack) < 20:
badturt.hideturtle()
attack.hideturtle()
badturt.goto(300,350)
q = 1
StopChase(badturt) #<---- doesn't work
if lvl == 2:
if badturt.distance(attack) < 20:
badturt.hideturtle()
attack.hideturtle()
badturt.goto(300,350)
q = 2
StopChase(badturt)
if badturt2.distance(attack) < 20:
badturt2.hideturtle()
badturt2.goto(-300,350)
attack.hideturtle()
w = 2
StopChase(badturt2)
if lvl == 3:
#same format as lvl 2 but with addition of badturt3
while True:
if lvl == 1:
while True:
CheckDamage()
if turthealth == 0:
LOSE()
break
IfAttackHit()
Chase(badturt)
if q == 1:
break
break
if lvl == 2:
ChaseAgain(badturt) #make it move again
ChaseAgain(badturt2)
badturt.goto(300,350)
badturt.showturtle()
badturt2.showturtle()
while True:
CheckDamage()
if turthealth == 0:
LOSE()
break
IfAttackHit()
Chase(badturt)
Chase(badturt2)
break
if lvl == 3:
#same format as lvl 2 but with addition of badturt3
break
这不起作用。是因为它嵌套在另一个函数中吗?从未调用过 StopChase() 吗?函数是否再次被重新定义,以便敌方海龟再次开始移动?
另外,我的老师告诉我,我必须编写 'global Chase' 来在另一个函数中重新定义它,但是当我这样做时,程序会停止运行 - 当我将光标移到海龟屏幕上时,它只是显示加载光标,屏幕上没有任何反应/它冻结。 (这样做是错的,还是我笔记本电脑上的 python 程序有问题?)
我还尝试重新定义 Chase() 以便 badturt 只会向前移动 0(基本上让它什么都不做),但这也不起作用。
请让我知道我做错了什么,或者是否有其他方法可以让 badturt 停止移动。
【问题讨论】: