'Whenever I try to run my script it stops working
I am currently making a py game and python stops working every time, but I'm not sure why.
from turtle import Turtle, Screen
screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')
screen.title('Snake Game')
turtles = []
y = 0
x = 0
for _ in range(3):
turtle = Turtle('square')
turtle.color('white')
turtle.penup()
turtle.goto(x, y)
x -= 20
game_is_on = True
while game_is_on:
for seg in turtles:
seg.forward(20)
Solution 1:[1]
You have forgotten to add each turtle to the turtles list. All you need to do is append the turtle in the for loop.
Here is the code:
from turtle import Turtle, Screen
screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')
screen.title('Snake Game')
turtles = []
y = 0
x = 0
for _ in range(3):
turtle = Turtle('square')
turtle.color('white')
turtle.penup()
turtle.goto(x, y)
turtles.append(turtle)
x -= 20
game_is_on = True
while game_is_on:
for seg in turtles:
seg.forward(20)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lecdi |
