0

I was coding a Snake Game using turtle module but when I add this line into my code the turtle screen and python crashed:

turtle.tracer(0)

can somebody help me so I can complete the game? Thanks a lot

my code:

from turtle import Turtle, Screen, colormode


screen = Screen()
screen.bgcolor("black")
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)
x = 0

segments = []

for turtle in range(3):
    turtle = Turtle("square")
    turtle.color("white")
    turtle.penup()
    turtle.goto(0-x, 0)
    x += 20

    segments.append(turtle)


game_is_on = True
screen.update()
while game_is_on:
    for segment in segments:
        segment.forward(20)


screen.exitonclick()
Drownz
  • 1
  • 1
  • What do you want to achieve exactly by this line? – Bemwa Malak Jan 10 '22 at 13:03
  • I want to turn off the animation of each segment in the line( the Snake's body) that I created and then use ```turtle.update()``` to update the screen when my snake move. If you want I can show you my code? Im a beginner and my code only uses the turtle module – Drownz Jan 10 '22 at 13:54
  • it will be a lot better if you can share your code in the question. – Bemwa Malak Jan 10 '22 at 14:11
  • I added my code in. Thanks for your time! – Drownz Jan 11 '22 at 03:28

1 Answers1

1

I think we need to know more about what you mean by 'crashed'. If you mean everything froze, that's the code you wrote. Once you introduce tracer() you need to provide an update() for every change you want the user to see. But you don't have any update() calls in your loop so everything visually remains as it was before the loop. If you want to see the segments move, you need to do something like:

from turtle import Turtle, Screen

screen = Screen()
screen.bgcolor('black')
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)

x = 0

segments = []

for turtle in range(3):
    turtle = Turtle('square')
    turtle.color('white')
    turtle.penup()
    turtle.setx(x)

    x -= 20

    segments.append(turtle)

screen.update()

game_is_on = True

while game_is_on:
    for segment in segments:
        segment.forward(20)

    screen.update()

screen.exitonclick()  # never reached

If you mean by 'crashed' that Python quit back to the operating system, then you need to describe the environment under which you're running this code.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • can you make it clearer for me about "describe the environment under which you're running this code."? I don't really get it. Sorry! – Drownz Jan 11 '22 at 08:03
  • I tested your code and I figured it out, thanks a lot! – Drownz Jan 11 '22 at 10:24