Snake turtle don't grow up - python

I'm beginner and I can't undertstand why my code doesn't work. I make snake game. My snake must grow but it doesn't. You can run the code in python interpreter and see how it works. And when I run a code, head of snake moves slower and slower. It isn't really good. I would be thankful for help. Python version is 3.10(probably it is also important)
import turtle
import random
import time
segments = []
delay = 0.1
score = 0
highest = 0
x = 0
y = 0
cl2 = random.choice(['orange', 'purple', 'blue'])
cl = random.choice(['yellow', 'blue', 'green'])
sh = random.choice(['square', 'triangle', 'circle'])
foodx = random.randrange(-245, 245)
foody = random.randrange(-245, 245)
wn = turtle.Screen()
wn.title('Snake_game')
wn.bgcolor('black')
wn.setup(width=600)
wn.tracer(0)
head = turtle.Turtle()
head.speed(0)
head.color(cl)
head.fillcolor(cl2)
head.shape('square')
head.shapesize(0.80, 0.80)
head.penup()
head.direction = 'stop'
head.goto(x, y)
food = turtle.Turtle()
food.speed(0)
food.shape(sh)
food.shapesize(0.65, 0.65)
food.color('red')
food.fillcolor('orange')
food.penup()
food.goto(foodx, foody)
score_b = turtle.Turtle()
score_b.speed(0)
score_b.shape('square')
score_b.color('white')
score_b.hideturtle()
score_b.penup()
score_b.goto(0, 250)
score_b.write('Score : 0, the_highest_score : 0', align='center', font=('Arial', 19, 'italic'))
def go_up():
if head.direction != 'down':
head.direction = 'up'
def go_down():
if head.direction != 'up':
head.direction = 'down'
def go_left():
if head.direction != 'right':
head.direction = 'left'
def go_right():
if head.direction != 'left':
head.direction = 'right'
speed = 0.1
def move():
if head.direction == 'up':
y = head.ycor()
head.sety(y+speed)
if head.direction == 'down':
y = head.ycor()
head.sety(y-speed)
if head.direction == 'left':
x = head.xcor()
head.setx(x-speed)
if head.direction == 'right':
x = head.xcor()
head.setx(x+speed)
wn.listen()
wn.onkey(go_up, 'w')
wn.onkey(go_left, 'a')
wn.onkey(go_down, 's')
wn.onkey(go_right, 'd')
while True:
wn.update()
move()
if head.xcor() > 300 or head.xcor() < -300 or head.ycor() > 310 or head.ycor() < -310:
time.sleep = 1
head.goto(0, 0)
head.direction = 'stop'
cl2 = random.choice(['orange', 'purple', 'blue'])
cl = random.choice(['yellow', 'blue', 'green'])
sh = random.choice(['square', 'triangle', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segment.clear()
if head.distance(food) < 10:
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
segment = turtle.Turtle()
segment.speed(0)
segment.shape('square')
segment.shapesize(0.80, 0.80)
segment.color(cl)
segment.fillcolor(cl2)
segment.penup()
segments.append(segment)
for i in range(len(segments)-1, 0, -1):
x = segments[i-1].xcor()
y = segments[i-1].ycor()
segment.goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()

Your code has many problems.
You have wrong indentations. You move segments inside if head.distance(food) < 10: - so it moves it only once. You should do it in every loop.
You move segments after moving head - so segment has the same position as head. You should move segments before you get new position for head.
The biggest problem is that your snake moves smoothly using speed = 0.1 so when you create new segment then it may get position head + 0.1 and it displays one segment on another segment and you can't see segments. It would have to get position head + head_size. Maybe first try classic version where snake moves from square to square using speed = head_size
Another problem: in for-loop you get value from segments[i-1] and you set segment.goto(x, y) but it should be segments[i].goto(x, y)
Working example with classic snake which moves from square to square.
import turtle
import random
import time
segments = []
delay = 0.1
score = 0
highest = 0
x = 0
y = 0
cl2 = random.choice(['orange', 'purple', 'blue'])
cl = random.choice(['yellow', 'blue', 'green'])
sh = random.choice(['square', 'triangle', 'circle'])
foodx = random.randrange(-245, 245)
foody = random.randrange(-245, 245)
wn = turtle.Screen()
wn.title('Snake_game')
wn.bgcolor('black')
wn.setup(width=600)
wn.tracer(0)
head = turtle.Turtle()
head.speed(0)
head.color(cl)
head.fillcolor(cl2)
head.shape('square')
head.shapesize(0.80, 0.80)
head.penup()
head.direction = 'stop'
head.goto(x, y)
food = turtle.Turtle()
food.speed(0)
food.shape(sh)
food.shapesize(0.65, 0.65)
food.color('red')
food.fillcolor('orange')
food.penup()
food.goto(foodx, foody)
score_b = turtle.Turtle()
score_b.speed(0)
score_b.shape('square')
score_b.color('white')
score_b.hideturtle()
score_b.penup()
score_b.goto(0, 250)
score_b.write('Score : 0, the_highest_score : 0', align='center', font=('Arial', 19, 'italic'))
def go_up():
if head.direction != 'down':
head.direction = 'up'
def go_down():
if head.direction != 'up':
head.direction = 'down'
def go_left():
if head.direction != 'right':
head.direction = 'left'
def go_right():
if head.direction != 'left':
head.direction = 'right'
def move():
if head.direction == 'up':
y = head.ycor()
head.sety(y+speed)
if head.direction == 'down':
y = head.ycor()
head.sety(y-speed)
if head.direction == 'left':
x = head.xcor()
head.setx(x-speed)
if head.direction == 'right':
x = head.xcor()
head.setx(x+speed)
wn.listen()
wn.onkey(go_up, 'w')
wn.onkey(go_left, 'a')
wn.onkey(go_down, 's')
wn.onkey(go_right, 'd')
# --- all changes are only below ---
speed = 20
while True:
if head.direction != 'stop':
if head.xcor() > 300 or head.xcor() < -300 or head.ycor() > 310 or head.ycor() < -310:
time.sleep(1)
head.goto(0, 0)
head.direction = 'stop'
cl2 = random.choice(['orange', 'purple', 'blue'])
cl = random.choice(['yellow', 'blue', 'green'])
sh = random.choice(['square', 'triangle', 'circle'])
segments = [] # remove all segments
# first add new segment
if head.distance(food) < 10:
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
segment = turtle.Turtle()
segment.speed(0)
segment.shape('square')
segment.shapesize(0.80, 0.80)
segment.color(cl)
segment.fillcolor(cl2)
segment.penup()
segments.append(segment)
# next move segments
for i in range(len(segments)-1, 0, -1):
print('move')
x = segments[i-1].xcor()
y = segments[i-1].ycor()
segments[i].goto(x, y)
if len(segments) > 0:
print('move')
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
# next move head
move()
# --- in every loop ---
# finally display all in new positions
wn.update()
time.sleep(.2) # reduce speed of game

Related

I can't move turtle in python which for snake game

I'm trying to make a snake game just for fun and learning but I can't move my turtle. When I add the border lines and run it. My turtle can't move a bit. Problem pop up when I add the "if turtle.xcor()>290 or turtle.xcor()<-290 or turtle.ycor()>290 or turtle.ycor()<-290:" border lines
import turtle
import time
import random
delay = 0.1
wn = turtle.Screen()
wn.title("snake game")
wn.bgcolor("white")
wn.setup(width= 700, height= 700 )
wn.tracer(0)
turtle = turtle.Turtle()
turtle.speed(0)
turtle.shape("square")
turtle.color("black")
turtlepenup()
turtle.goto(0,0)
turtle.direction = "stop"
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("blue")
food.penup()
food.goto(0,100)
segments = []
def go_up():
turtle.direction = "up"
def go_down():
turtle.direction = "down"
def go_left():
turtle.direction = "left"
def go_right():
turtle.direction = "right"
def hareket():
if turtle.direction == "up":
y = turtle.ycor()
yilan.sety(y + 15)
if turtle.direction == "down":
y = turtle.ycor()
yilan.sety(y - 15)
if turtle.direction == "left":
x = turtle.xcor()
yilan.setx(x - 15)
if turtle.direction == "right":
x = turtle.xcor()
yilan.setx(x + 15)
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
while True:
wn.update()
if turtle.xcor()>290 or turtle.xcor()<-290 or turtle.ycor()>290 or turtle.ycor()<-290:
time.sleep(1)
turtle.goto(0,0)
turtle.direction = "stop"
if turtle.distance(yemek) < 20:
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
#segment ekle
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
x = turtle.xcor()
y = turtle.ycor()
segments[0].goto(x,y)
move()
time.sleep(delay)
wn.mainloop()
I've checked the lines and searched on the internet but i couldn't find a solution. I'm new so the problem might be easy.

Python Turtle Snake Game: Problems With Snake Segments

As you guessed from the title, I'm making a beginner form of a snake game using the turtle module in Python. So far, everything is running smoothly, except for one single detail; the segments are not attached to the head, but they are rather lying inside the head itself on top of each other (you get what I'm saying?).
Here is what I wrote:
os.system("pause")
import turtle
import random
#Game Screen Set Up
screenWidth = 600
screenHeight = 600
screencolor = "#221C35"
screentitle = "Snake Game for Beginners"
screen = turtle.Screen()
screen.setup(width=screenWidth, height=screenHeight)
screen.bgcolor(screencolor)
screen.title(screentitle)
screen.tracer(0)
#Game Objects
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("#FFFFFF")
head.penup()
head.goto(0, 0)
head.direction = "stop"
food = turtle.Turtle()
food.speed(0)
food.shape("square")
food.color("#888888")
food.penup()
food.goto(0, -40)
segments = []
#Game Functions
def upwards():
head.direction = "up"
y = head.ycor() + 20
head.sety(y)
def downwards():
head.direction = "down"
y = head.ycor() - 20
head.sety(y)
def right():
head.direction = "right"
x = head.xcor() + 20
head.setx(x)
def left():
head.direction = "left"
x = head.xcor() - 20
head.setx(x)
#Keyboard Bindings
screen.listen()
screen.onkeypress(upwards, "Up")
screen.onkeypress(downwards, "Down")
screen.onkeypress(right, "Right")
screen.onkeypress(left, "Left")
while True:
screen.update()
if head.distance(food) < 20:
food.goto(random.randint(-290, 290), random.randint(-290, 290))
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("white")
new_segment.penup()
segments.append(new_segment)
for index in range(len(segments)-1, 0, -1):
segments[index].goto(segments[index-1].xcor(), segments[index-1].ycor())
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
if (head.xcor() > 290) or (-290 > head.xcor()) or (head.ycor() > 290) or (-290 > head.ycor()):
head.goto(random.randint(-290, 290), random.randint(-290, 290))
head.direction = "stop"
for segment in segments:
segment.goto(100000, 100000)
segments.clear()
upwards()
downwards()
left()
right()````

How do I end a code using Python Turtle Module when getting a Terminator Error

Hello I'm new to python and I do not really know how to fix terminator errors. I tried some other fixes I found around this website but they end up leading to other errors such as .!canvas being an invalid command name.
This is the code
import turtle as turtle
import time
import random
import keyboard
delay = 0.1
score = 0
high_score = 0
t=turtle
t=turtle.Screen()
t.title("Snake Xenzia [Press Esc to end game]")
t.bgcolor("skyblue")
t.setup(width=600, height=600)
t.tracer(0)
# Head of snake
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# Food
food = turtle.Turtle()
colors = random.choice(['red', 'green', 'blue'])
shapes = random.choice(['square', 'triangle', 'circle'])
food.speed(0)
food.shape(shapes)
food.color(colors)
food.penup()
food.goto(0, 100)
# Adding Segments
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write("Score : 0 High Score : 0", align="center",
font=("candara", 24, "bold"))
# Coding Moving
def wkey():
if head.direction != "down":
head.direction = "up"
def skey():
if head.direction != "up":
head.direction = "down"
def akey():
if head.direction != "right":
head.direction = "left"
def dkey():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycor()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.setx(x-20)
if head.direction == "right":
x = head.xcor()
head.setx(x+20)
t.listen()
t.onkeypress(wkey, "w")
t.onkeypress(skey, "s")
t.onkeypress(akey, "a")
t.onkeypress(dkey, "d")
segments = []
# Main Gameplay
while True:
t.update()
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = "Stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
if head.distance(food) < 20:
x = random.randint(-270, 270)
y = random.randint(-270, 270)
food.goto(x, y)
# Adding segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange") # tail colour
new_segment.penup()
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
# Collision
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
time.sleep(delay)
t.mainloop()
And this is the Terminator error received when I close the widget.
---------------------------------------------------------------------------
Terminator Traceback (most recent call last)
<ipython-input-12-9edf0a070d73> in <module>
182 while True:
183
--> 184 t.update()
185
186 if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
~\anaconda3\lib\turtle.py in update(self)
1301 self._tracing = True
1302 for t in self.turtles():
-> 1303 t._update_data()
1304 t._drawturtle()
1305 self._tracing = tracing
~\anaconda3\lib\turtle.py in _update_data(self)
2644
2645 def _update_data(self):
-> 2646 self.screen._incrementudc()
2647 if self.screen._updatecounter != 0:
2648 return
~\anaconda3\lib\turtle.py in _incrementudc(self)
1290 if not TurtleScreen._RUNNING:
1291 TurtleScreen._RUNNING = True
-> 1292 raise Terminator
1293 if self._tracing > 0:
1294 self._updatecounter += 1
Terminator:

I can't get VSCode to open window for snake script

I am practicing some very simple projects and I found the snake game and handed to try my hand. I am pretty sure I copied the code completely but when I run it a small blue screen pops up for a split second and then disappears and doesn't say what's wrong.
site I am using for the project. https://www.geeksforgeeks.org/create-a-snake-game-using-turtle-in-python/
import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
# creating a window screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("blue")
# Width and height
wn.setup(width=600, height=600)
wn.tracer(0)
# head of the snake
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# food in the game
food = turtle.Turtle()
colors = random.choice(['red', 'green', 'black'])
shapes = random.choice(['square', 'Triangle', 'circle'])
food.speed(0)
food.shape(shapes)
food.color(colors)
food.penup()
food.goto(0, 100)
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write ("Score : 0 High Score : 0", align="center",
font=("candara", 24, "bold"))
You are getting a blue screen because the code is not complete there are some variables missing + the main game loop doesn't exits so the game simply crashes here is the complete code I got it from the same website further down Full Code
# import required modules
import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
# Creating a window screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("blue")
# the width and height can be put as user's choice
wn.setup(width=600, height=600)
wn.tracer(0)
# head of the snake
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# food in the game
food = turtle.Turtle()
colors = random.choice(['red', 'green', 'black'])
shapes = random.choice(['square', 'triangle', 'circle'])
food.speed(0)
food.shape(shapes)
food.color(colors)
food.penup()
food.goto(0, 100)
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write("Score : 0 High Score : 0", align="center",
font=("candara", 24, "bold"))
# assigning key directions
def group():
if head.direction != "down":
head.direction = "up"
def godown():
if head.direction != "up":
head.direction = "down"
def goleft():
if head.direction != "right":
head.direction = "left"
def goright():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycor()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.setx(x-20)
if head.direction == "right":
x = head.xcor()
head.setx(x+20)
wn.listen()
wn.onkeypress(group, "w")
wn.onkeypress(godown, "s")
wn.onkeypress(goleft, "a")
wn.onkeypress(goright, "d")
segments = []
# Main Gameplay
while True:
wn.update()
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = "Stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
if head.distance(food) < 20:
x = random.randint(-270, 270)
y = random.randint(-270, 270)
food.goto(x, y)
# Adding segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange") # tail colour
new_segment.penup()
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
# Checking for head collisions with body segments
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segment.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
time.sleep(delay)
wn.mainloop()
You don't seem to have a game loop, in the tutorial you linked to the code block you have showed was only the setup if you continue scrolling down there is more code containing functions that handle user input as well as a game loop. Here is the full program in their tutorial.
# import required modules
import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
# Creating a window screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("blue")
# the width and height can be put as user's choice
wn.setup(width=600, height=600)
wn.tracer(0)
# head of the snake
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# food in the game
food = turtle.Turtle()
colors = random.choice(['red', 'green', 'black'])
shapes = random.choice(['square', 'triangle', 'circle'])
food.speed(0)
food.shape(shapes)
food.color(colors)
food.penup()
food.goto(0, 100)
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write("Score : 0 High Score : 0", align="center",
font=("candara", 24, "bold"))
# assigning key directions
def group():
if head.direction != "down":
head.direction = "up"
def godown():
if head.direction != "up":
head.direction = "down"
def goleft():
if head.direction != "right":
head.direction = "left"
def goright():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycor()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.setx(x-20)
if head.direction == "right":
x = head.xcor()
head.setx(x+20)
wn.listen()
wn.onkeypress(group, "w")
wn.onkeypress(godown, "s")
wn.onkeypress(goleft, "a")
wn.onkeypress(goright, "d")
segments = []
# Main Gameplay
while True:
wn.update()
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = "Stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
if head.distance(food) < 20:
x = random.randint(-270, 270)
y = random.randint(-270, 270)
food.goto(x, y)
# Adding segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange") # tail colour
new_segment.penup()
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
# Checking for head collisions with body segments
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segment.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
time.sleep(delay)
wn.mainloop()

how can i move my snake head with my arrow keys

what did I do wrong because I cant move the snakehead with my wasd key the error that I keep on getting is turtle.terminator, It also say the problem is at the wn update place can u please tell me the solution this move the sane head i tried every thing this always come up raise Terminator
turtle.Terminator
import turtle
import time
import random
delay = 0.1
# Set up the screen
wn = turtle.Screen()
wn.title("Snake")
wn.bgcolor("blue")
wn.setup(width=600, height=600)
wn.tracer(0)
# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("red")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("green")
food.penup()
food.goto(0, 100)
segments = []
# function
def go_up():
head.direction = "up"
def go_down():
head.direction = "down"
def go_left():
head.direction = "left"
def go_right():
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "left":
x = head.xcor()
head.setx(x - 20)
if head.direction == "right":
x = head.xcor()
head.setx(x + 20)
# key board bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
# Main game loop
while True:
wn.update()
if head.distance(food) < 20:
# move the food to random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
# add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
# move the end segments frist in revrves order
for index in range(len(segments) - 1, 0, -1):
x = segments[index - 1].xcor()
y = segments[index - 1].ycor()
segments[index].goto(x, y)
# segment 0 to where the is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
time.sleep(delay)
wn.mainloop()
This code works for moving. You have to call move() method after go_up etc.
import turtle
import time
import random
delay = 0.1
# Set up the screen
wn = turtle.Screen()
wn.title("Snake")
wn.bgcolor("blue")
wn.setup(width=600, height=600)
wn.tracer(0)
# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("red")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("green")
food.penup()
food.goto(0, 100)
segments = []
# function
def go_up():
head.direction = "up"
move()
def go_down():
head.direction = "down"
move()
def go_left():
head.direction = "left"
move()
def go_right():
head.direction = "right"
move()
def move():
print("Move")
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "left":
x = head.xcor()
head.setx(x - 20)
if head.direction == "right":
x = head.xcor()
head.setx(x + 20)
# key board bindings# key board bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
# Main game loop
while True:
wn.update()
if head.distance(food) < 20:
# move the food to random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
# add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
# move the end segments frist in revrves order
for index in range(len(segments) - 1, 0, -1):
x = segments[index - 1].xcor()
y = segments[index - 1].ycor()
segments[index].goto(x, y)
# segment 0 to where the is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
time.sleep(delay)
wn.mainloop()

Categories