M.Naderi
M.Naderi

Reputation: 3

How can I change color of the turtle?

I need to change the color of my turtle head many times like I want the color to blink many times when the turtle head hits the wall, but when I change the color on the screen only the last color that I set for the turtle head is visible.

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

Upvotes: 0

Views: 235

Answers (1)

kinshukdua
kinshukdua

Reputation: 1994

As @Michael Guidry suggested you need to add a small pause after each color change so that the color is visible.

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

Upvotes: 1

Related Questions