问题描述
我需要多次更改我的乌龟头的颜色,就像我希望乌龟头撞到墙壁时颜色闪烁多次,但是当我更改屏幕上的颜色时,只为我设置的最后一个颜色龟头可见。
def reset_score(seq):
global score
time.sleep(0.5) # the snake freezes for a moment when hitting a wall then the game resets
head.color("black")
wn.update()
head.color("yellow")
wn.update()
head.color("red")
wn.update()
head.goto(0,0)
head.direction = "stop"
score = 0
for seq in snake_tail:
seq.goto(1000,1000)
score = 0
score_printer.clear()
score_printer.write("score: {} High score: {}".format(score,high_score),align="center",font=("italic",24,"normal"))
snake_tail.clear()
# reset the game when the head hits the wall
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
reset_score("body_parts")
解决方法
正如@Michael Guidry 建议的那样,您需要在每次颜色更改后添加一个小暂停,以便颜色可见。
def reset_score(seq):
global score
time.sleep(0.5) # the snake freezes for a moment when hitting a wall then the game resets
head.color("black")
wn.update()
time.sleep(0.1)
head.color("yellow")
wn.update()
time.sleep(0.1)
head.color("red")
wn.update()
time.sleep(0.1)
head.goto(0,0)
head.direction = "stop"
score = 0
for seq in snake_tail:
seq.goto(1000,1000)
score = 0
score_printer.clear()
score_printer.write("Score: {} High Score: {}".format(score,high_score),align="center",font=("italic",24,"normal"))
snake_tail.clear()
# reset the game when the head hits the wall
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
reset_score("body_parts")