微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何改变乌龟的颜色?

如何解决如何改变乌龟的颜色?

我需要多次更改我的乌龟头的颜色,就像我希望乌龟头撞到墙壁时颜色闪烁多次,但是当我更改屏幕上的颜色时,只为我设置的最后一个颜色龟头可见。

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")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。