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

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.

Related

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()````

Problem moving snake in a snake game using Turtle

I'm trying to create an easier version of the snake game. Everything in the code looks ok for me but I can't make snake move as I want to. Can you help me why my code doesn't work? I don't understand why my code is not okay.
I searched for some similar games codes, but they all used time. and I couldn't understand the need for that.
Here is the code:
import turtle
import random
window = turtle.Screen()
window.screensize(600, 600)
window.title("Snake Eats Tomato Game")
window.bgcolor("skyblue")
window.tracer(0)
snake = turtle.Turtle()
snake.color("dark blue")
snake.shape("square")
snake.shapesize(1)
snake.speed(1)
snake.penup()
snake.goto(0, 100)
snake.direction = "stop"
def move():
if snake.direction == "up":
y = snake.ycor()
snake.sety(y + 20)
if snake.direction == "down":
y = snake.ycor()
snake.sety(y - 20)
if snake.direction == "left":
x = snake.xcor()
snake.setx(x + 20)
if snake.direction == "right":
x = snake.xcor()
snake.setx(x - 20)
point = 0
point_table = turtle.Turtle()
point_table.speed(0)
point_table.shape("square")
point_table.color("green")
point_table.penup()
point_table.hideturtle()
point_table.goto(-200, 200)
point_table.write(
"POİNT: {}".format(point), align="center", font=("Courier", 25, "normal")
)
def go_left():
if snake.direction != "right":
snake.direction = "left"
def go_right():
if snake.direction != "left":
snake.direction = "right"
def go_up():
if snake.direction != "down":
snake.direction = "up"
def go_down():
if snake.direction != "up":
snake.direction = "down"
window.listen()
window.onkey(go_left, "Left")
window.onkey(go_right, "Right")
window.onkey(go_up, "Up")
window.onkey(go_down, "Down")
tomato = turtle.Turtle()
tomato.penup()
tomato.color("tomato")
tomato.shape("circle")
tomato.speed(0)
tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))
while True:
window.update()
snake.forward(3)
move()
if snake.xcor() < -330 or snake.xcor() > 330:
snake.right(90)
if snake.ycor() < -330 or snake.ycor() > 330:
snake.right(90)
if snake.distance(tomato) < 20:
point += 1
point_table.clear()
point_table.write(
"PUAN: {}".format(point), align="center", font=("Courier", 25, "normal")
)
tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))
There were some 'errors' in your code, I'll try to adres most of them, one by one:
Instead of always calling snake.forward(3) in the while True, change snake.direction = "stop" to snake.direction = "right" as the starting movement. This will keep the while loop clean
The go_left (etc) functions had an if to check the reversed way, I've removed those, just overwrite the direction
snake.xcor() < -330 these won't work, not every screen is 330 pixels. You'll need to use window.window_width() and window.window_height() to get the current window size, and use those values to detect the edges
WHen you detect an edge, don't do snake.right(90), as this will instant move the snake. Just change the direction, to bounce it:
if (curX < window_max_left):
snake.direction = 'left'
if (curX > window_max_right):
snake.direction = 'right'
if (curY > window_max_up):
snake.direction = 'down'
if (curY < window_max_down):
snake.direction = 'up'
Applying those points, fixes the movement, and let the snake bounce of walls
* Please see the example recording below the code
The final code looks like:
import turtle
import random
window = turtle.Screen()
window.screensize(600, 600)
window.title("Snake Eats Tomato Game")
window.bgcolor("skyblue")
window.tracer(0)
snake = turtle.Turtle()
snake.color("dark blue")
snake.shape("square")
snake.shapesize(1)
snake.speed(1)
snake.penup()
snake.goto(0, 100)
snake.direction = "right"
def move():
if snake.direction == "up":
y = snake.ycor()
snake.sety(y + 10)
if snake.direction == "down":
y = snake.ycor()
snake.sety(y - 10)
if snake.direction == "left":
x = snake.xcor()
snake.setx(x + 10)
if snake.direction == "right":
x = snake.xcor()
snake.setx(x - 10)
point = 0
point_table = turtle.Turtle()
point_table.speed(0)
point_table.shape("square")
point_table.color("green")
point_table.penup()
point_table.hideturtle()
point_table.goto(-200, 200)
point_table.write(
"POİNT: {}".format(point), align="center", font=("Courier", 25, "normal")
)
def go_left():
snake.direction = "left"
def go_right():
snake.direction = "right"
def go_up():
snake.direction = "up"
def go_down():
snake.direction = "down"
window.listen()
window.onkey(go_left, "Left")
window.onkey(go_right, "Right")
window.onkey(go_up, "Up")
window.onkey(go_down, "Down")
tomato = turtle.Turtle()
tomato.penup()
tomato.color("tomato")
tomato.shape("circle")
tomato.speed(0)
tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))
window_width = window.window_width()
window_height = window.window_height()
window_max_left = -abs(window_width / 2)
window_max_right = window_width / 2
window_max_down = -abs(window_height / 2)
window_max_up = window_height / 2
while True:
curX = snake.xcor()
curY = snake.ycor()
if (curX < window_max_left):
snake.direction = 'left'
if (curX > window_max_right):
snake.direction = 'right'
if (curY > window_max_up):
snake.direction = 'down'
if (curY < window_max_down):
snake.direction = 'up'
if snake.distance(tomato) < 20:
point += 1
point_table.clear()
point_table.write(
"PUAN: {}".format(point), align="center", font=("Courier", 25, "normal")
)
tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))
move()
window.update()
How it looked:

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

Need a way to add teams and solo gameplay. Also any idea on making a main menu with tkinter for playing as team or solo

I'm new to programming and python. I somehow followed a guide and came up with a snake game. This game runs great. However, there are a few things that I need to add to make it much better.
First of all, I want to add some sort of start screen menu. Want it to have buttons. This start screen will tie into my next addition.
I am wanting to add a scoring system for teams or solo. I need it to selectable from the main screen. Like "play as a team" or "play on your own" buttons. If the team button is pressed then there will be 5 rounds, one round for each player. I have seen somewhat how to make the main menu screen using Tkinter. However, the other part about the teams and solo part seems to baffle me. Can't find a way to do it and can't find a guide online. I'm kinda stuck here.
Any help will be greatly needed and accepted. I will post the game code below so you guys can see it.
Thanks,
# import required modules
import turtle
import time
import random
import tkinter as tk
delay = 0.1
score = 0
high_score = 0
# Creating a window screen
wn = turtle.Screen()
wn.title("Snake Game made by name")
wn.bgcolor("coral")
# 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("black")
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# food in the game
food = turtle.Turtle()
colors = random.choice(['red', 'yellow'])
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("My Score : 0 High Score : 0", align="center",
font=("segoe print", 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'])
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=("segoe print", 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("lime") # tail colour
new_segment.penup()
segments.append(new_segment)
delay -= 0.00001
score += 1
if score > high_score:
high_score = score
pen.clear()
pen.write("Score : {} High Score : {} ".format(
score, high_score), align="center", font=("segoe print", 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=("segoe print", 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