
Python Game Programming Tutorial: Snake Game Part 4
🆘 Watch this first and then let me know in the comments below:
❤️❤️ SHOW SOME LOVE AND SUPPORT THE CHANNEL ❤️❤️
Click Join and Become a Channel Member Today!
Channel members can get preferential comment replies, early access to new content, members only live streams, and access to my private Discord.
❤️
Amazon Affiliate Links
💻 My Laptop (Asus Zenbook 13):
🎙My New Microphone (Blue Yeti Pro):
🎙My Old Microphone (Blue Snowball):
🎶My USB Interface (Focusrite Scarlett Solo):
Other Affiliate Links
⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing.
Functions Link:
LINKS
🗄️ GITHUB:
💬 Follow me on Twitter:
📜 Subscribe to my Newsletter:
📝 Check out my Blog:
⬇️ Download Geany Editor:
LEARN MORE PYTHON
➡️Space Invaders:
➡️Snake Game:
➡️Pong:
➡️Space War:
➡️Intro to Python (for Java Coders):
➡️Space Arena – The Ultimate Python Turtle Graphics Game Tutorial:
LEARN MORE JAVA
➡️Basic Java for Beginners:
➡️Intro to AP Computer Science A:
#Python #Tutorial #Beginner
Sir when I run the code the first segment attaches to the head, but when I ate the the second food it stops and it says
Typerror: 'method' object is not subscriptable
Sir…This program is raising "in_incrementudc raise Terminator Turtle. Terminator" error ….Plz help.
(THIS IS NOT AN ERROR SO please try answering it here] i dont understand the part where u adding those tail segments in reverse order… the part where u write segment[index].goto(x,y). So if u want for example, segment 5 to follow segment 4, and u write this, shouldnt it overlap? becuase thats what i understand. Or does it mean that the loop is so quick that by the time the 5th segment replaces 4th one, the 4th one already replaces the 3rd one and so on? and also the special case for the first segment to follow the head, so how doesnt it just overlap the head and manage to go behind it? pls try to answer i really want to understand how this works
hii bro this line (for index in range(len(segments) – 1, 0, -1): ) in my laptop the erroe is coming
saying that
Traceback (most recent call last):
File "C:/Users/tanvi/PycharmProjects/pythonProject/main.py", line 78, in <module>
for index in range(len(segments) – 1, 0, -1):
TypeError: object of type 'Turtle' has no len()
WHAT I DO >>> PLS reply
FAST
Hello again, I have a problem here, for index in range(len(segments)-1, 0, -1) it says its an unvalid syntax, here is the link, https://pastebin.com/wSSke7Nx, Thank you for helping
import turtle
import time
import random
delay = 0.1
#Set up the screen
wn = turtle.Screen()
wn.title("3ess")
wn.bgcolor("black")
wn.setup(width=600,height=600)
wn.tracer(0) # turns off the screen updates
# Snake Head
head = turtle.Turtle()
head.shape("square")
head.color("red")
head.speed(0)
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
segments = []
#functions
def go_up():
head.direction = "up"
def go_down():
head.direction = "down"
def go_right():
head.direction = "right"
def go_left():
head.direction = "left"
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 == "right":
x = head.xcor()
head.setx(x + 20)
if head.direction == "left":
x = head.xcor()
head.setx(x – 20)
#keyborad bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_right, "d")
wn.onkeypress(go_left, "a")
while True:
wn.update()
# check for a collision with the food
if head.distance(food) < 20:
# Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x,y)
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
move()
time.sleep(delay)
#main loop
wn.mainloop()
my segment ( the grey one) appears when i start the program
Hello! Great tutorial. I was wondering if there is a way to make another food item (maybe something like a grape along with the red apple) that would add more segments than the apple would (ex: apple adds 1 segment onto the snake but grape adds 2 segments)? Any help at all would be appreciated! (also having them both be on screen simultaneously)
Hello, nice code but when the head catches the food the game becomes laggy
Thanks again
Hello, thanks for this helpful tutorial but I really didn't understand the code after 3:40, you explained it very fast :/
Why did you put a "-1, 0, -1"?
is the x/y coordinate just (amount of segments -1)?
bro plz help when i run this program till #move where the head is part when i run it and press the keys w,a,s,d my snake doesn't move plz solve it as soon as possible
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/snake game/snake game.py", line 86, in <module>
segments.add(new_segment)
AttributeError: 'tuple' object has no attribute 'append'
link: https://pastebin.com/qZ31SGC6
Hi #Error? Why does it leave blocks behind after the first one https://pastebin.com/87UBBW6Q
Is it possible to keep some space between each segments?
Hi #error It doesn't create segment 1 till the player eats twice
is there a function to say if snake goes left the snake body has img… if snake goed right the snake body has img….?
it shows me the grey segments but they arent at one shape with the head they are just some grey squares at the screen frozen
thats my code
import turtle
import time
import random
delay = 0.1
#set up the screen
wn = turtle.Screen()
wn.title('Snake game by Pete Exarchos')
wn.bgcolor('green')
wn.setup(width=600, height=600)
wn.tracer(0)
#snake head
head = turtle.Turtle()
head.speed(0)
head.shape('square')
head.color('black')
head.penup()
head.goto(0,0)
head.direction = 'stop'
#snake food
food = turtle.Turtle()
food.speed(0)
food.shape('circle')
food.color('red')
food.penup()
food.goto(0,100)
segments = []
#functions
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)
#keyboard 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()
# Check for a collision with the food
if head.distance(food) < 20:
#move food to a random position at the sc
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)
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()
time.sleep(delay)
wn.mainloop()
how do i add another food object that make the body segment loose 1 segment when consumed.
#error new_segment = turtle.Turtle() doesn't work error on the end of this command
your channel is very cool and nice but the square is only one square Why?
after i run it .. i cant move anything like it dosent move
when the snake eats the food there will be a new segment but there's a trail following it for some reason pls help idk if i missed something
edit: nevermind i found the problem, i forgot the penup lol, thanks
this is the error i get pls help me
File "c:UsersashDesktopMySnakeGame.py", line 101
if len(segments) > 0
^
IndentationError: unindent does not match any outer indentation level
PS C:Usersash>
Bro the grey blocks are coming but the first block comes in the middle not on the snakes head Here is my coding
import random
import time
import turtle
delay = 0.1
# setupscreen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)
# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
# Functions
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)
# Keyboard 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()
# Check for a collision with the food
if head.distance(food) < 20:
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 first in reverse 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)
# Move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
move()
time.sleep(delay)
Hi! So it looks like I'm having an indentation error and I think thats because, before nothing would move and I was pressing stuff trying to make it work, but anyway, if you see something other then an indentation error, that would help me alot. Thanks! Here's the code.
import turtle
import time
import random
delay = 0.1
#set up the screen
wn = turtle.Screen()
wn.title("Heidi's Snake Game!!!")
wn.bgcolor("light blue")
wn.setup(width=600, height=600)
wn.tracer(0) #turns off the screen updates
#Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
# Snek foods
food = turtle.Turtle()
food.speed(0)
food.shape("triangle")
food.color("indigo")
food.penup()
food.goto(0,100)
segments = []
#functions
def go_up():
if head.direction != "up":
head.direction = "down"
def go_down():
if head.direction != "down":
head.direction = "up"
def go_left():
if head.direction != "left":
head.direction = "right"
def go_right():
if head.direction != "right":
head.direction = "left"
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)
#keyboard bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "d")
wn.onkeypress(go_right, "a")
#main game loop
while True:
wn.update()
# check for a collision with the food
if head.distance(food) < 20:
#move the food to a 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("circle")
new_segment.color("green")
new_segment.penup()
segments.append(new_segment)
#move the end segments first in reverse 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)
# move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
time.sleep(delay)
wn.mainloop()
I get no error but my program in not attaching the body segment to the head here is my code:
# Simple Snake Game in Python 3 for beginners
# By @TokyoEdTech
# I followed a tutorial tho.
import turtle
import time
import random
delay = 0.1
#Set up the screen
wn = turtle.Screen()
wn.title("Snake game by @TokyoEdTech")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0) # Turns off the screen updates
# Snake Head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
# Functions
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)
# Keyboard binding
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()
#Check for a collision with the food
if head.distance(food) < 20:
#Move food to a 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 segment in reverse 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)
# Move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
time.sleep(delay)
wn.mainloop()
9:49 says "Hope you join me for "Part 4" Problem, this is part 4 XD
Can you please help me? My game prints those cube in their place after the first segment which comes after the head.
Here is my code:
import turtle
import time
import random
delay = 0.1
#Set up the screen
wn = turtle.Screen()
wn.title('Snake Game(By:Yugavartha)')
wn.bgcolor('green')
wn.setup(width=600, height=600)
wn.tracer(0)
#Snake head
head = turtle.Turtle()
head.speed(0)
head.shape('square')
head.color('black')
head.penup()
head.goto(0, 0)
head.direction = 'stop'
#Snake food
food = turtle.Turtle()
food.speed(0)
food.shape('circle')
food.color('red')
food.penup()
food.goto(0, 100)
segments = []
#Functions
def go_up():
head.direction = 'up'
def go_down():
head.direction = 'down'
def go_right():
head.direction = 'right'
def go_left():
head.direction = 'left'
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 == 'right':
x = head.xcor()
head.setx(x + 20)
if head.direction == 'left':
x = head.xcor()
head.setx(x – 20)
#Keyboard bindings
wn.listen()
wn.onkeypress(go_up, 'w')
wn.onkeypress(go_left, 'a')
wn.onkeypress(go_down, 's')
wn.onkeypress(go_right, 'd')
#Main game loop
while True:
wn.update()
#Check for a collision with the food
if head.distance(food)<20:
#Move food to a 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 first in reverse 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)
#Move segment 0 to were the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
For some strange reason the segments keep on popping up even tho i am not picking up apples. If you can help it would be greatly appreciated.
import turtle
import time
import random
delay = 0.1
# Window
wn = turtle.Screen()
wn.title("Snake Game By Ash")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)
# Snake Head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("lime")
head.penup()
head.goto(0,0)
head.direction = "left"
# Snake Food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
# Functions
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)
# Keyboard 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()
#Check For A Collision With The Food
if head.distance(food) < 20:
# Move The Food To Random Place
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("cyan")
new_segment.penup()
segments.append(new_segment)
# Move The End Segments First In Reverse 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)
# Move Segment 0 To Head
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
move()
time.sleep(delay)
wn.mainloop()
When I eat the first apple the application crashes
bro please help me. my snake is getting bigger without eating. it just keeps on going
Did you remember to put the functions video in the description ?
Hi, my problem is that it keeps on popping up with a message saying 'unindent does not match any outer indentation level'. I'm not sure what's wrong with it.
So so so detailed and I love the way you break a game into bite-size chunks so it's easier for ADHD-affected people like me!!!!!!!!!!!!!
the parts are divided so incredibly insightful – different part of the games and it is explain with so much details!
you are a fantastic teacher, thanks so much for this series
it always crashes instead of the snake getting longer please help
NameError: name 'segments' is not defined
pls help me
"Unindent amount does not match previous indentPylance"
this is the error i get in # move segment 0 to where the head is
code :
import turtle
import time
import random
delay = 0.1
# set up the screen/ window
wn = turtle.Screen()
wn.title("snake game by @arjav")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0) # turns off screen update
#snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
#snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
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)
#keyboard binding
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()
# check for a collision with the food
if head.distance(food) < 20:
# move the food to a 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 segments first in reverse 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)
# move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
time.sleep(delay)
wn.mainloop()
the game crashes everytime there snake collides with the food and my body does not grow
import turtle
import time
import random
delay = 0.1
#Score
score = 0
high_score = 0
#setup screen
wn = turtle.Screen()
wn.title("Snake game by Hloho")
wn.bgcolor("black")
wn.setup(width=600, height=600)
wn.tracer(0) #turns off screen update
#snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.penup()
head.goto(0,0)
head.direction = "stop"
#snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score :0 High Score :0",align="center",font=("Arial", 24,"normal"))
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"
#function
def move():
if head.direction == "up":
y = head.ycor() #current turtle cordinate
head.sety(y + 20)
if head.direction == "down":
y = head.ycor() #current turtle cordinate
head.sety(y – 20)
if head.direction == "left":
x = head.xcor() #current turtle cordinate
head.setx(x – 20)
if head.direction == "right":
x = head.xcor() #current turtle cordinate
head.setx(x + 20)
#keyboard bindings
wn.listen() #listen for key press
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
#main game loop
while True:
wn.update()
#check for collision with the board
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"
#hide segments
for segment in segments:
segment.goto(1000,1000)
#clear segments list
segments.clear()
#reset score
score = 0
pen.clear()
pen.write("Score :{} High Score :{}".format(score,high_score),align="center",font=("Courier", 24,"normal"))
#check for collision with the food
if head.distance(food) < 20:
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x,y)
#add segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
#shorten the delay
#increase Score
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score :{} High Score :{}".format(score,high_score),align="center",font=("Courier", 24,"normal"))
#move the end segments first with reverse 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)
#segments 0 moves with the head
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
#check for collision with the snake body
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
for segment in segments:
segment.goto(1000,1000)
#clear segments list
segments.clear()
#reset score
score = 0
#reset delay
delay = 0.1
#update score reset
pen.clear()
pen.write("Score :{} High Score :{}".format(score,high_score),align="center",font=("Courier", 24,"normal"))
move()
time.sleep(d
I really like your tutorials. However, while I sort of understand whats going on with the snake body, i probably wouldnt be able to apply this logic in another project if needed. I am not sure if im stupid or just still inexperienced cause i dont see myself coming up with this at all at the moment. Makes me want to abandon this tutorial as I have not understood everything fully and feel im just copying the code. i dont think its your fault though. I just dont get the logic behind the snake body (yes ive read the explanation youve given somewhere else in the comments)
this is the first episode I didnt 100% understand the logic, the body is confusing
Sorry for asking for help again but I have a bigger problem. when I collide with food the game resets.
here is the code I hope you can help me:
import turtle
import time
import random
delay = 0.1
#Screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)
#Snake Head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#Functions
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 + 15)
if head.direction == "down":
y = head.ycor()
head.sety(y – 15)
if head.direction == "right":
x = head.xcor()
head.setx(x + 15)
if head.direction == "left":
x = head.xcor()
head.setx(x – 15)
#Keys
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()
#Check for collision with the border
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"
#Hide the segments
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
if head.distance(food) < 20:
#Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
#Add a segment
ns = turtle.Turtle()
ns.speed(0)
ns.shape("square")
ns.color("grey")
ns.penup()
segments.append(ns)
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
#Move segment 0 to where the head is
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"
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
time.sleep(delay)
wn.mainloop()
the identations are correct but copying and pasting put it all together
I need help. When I start the game, my snake keeps creating more segments. Is there a way to fix this?
Please help me out by replying to me.
I need help again. So on my python code it says invalid syntax.
The problem is that i don't know what that means.Here's the error,
File "main.py", line 95
new_segment.shape("square")
^
SyntaxError: invalid syntax
here's my code so far.
#Python 3
#Made by JagBains1
#Part 1 – Getting started
import turtle
import time
import random
delay = 0.1
#Set up the screen
wn = turtle.Screen()
wn.title("Snake Game from JagBains1")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)
#Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
#Snake food (Apple)
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#Functions
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)
#Keyboard 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()
#Check for a collision with the food
if head.distance(food) < 20:
#Move the food to a 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()
time.sleep(delay)
wn.mainloop()
this is what it says…
hi i have a proble when i eat firs food segment 0 instead of going to the end of head it goes to the same place where the head is
I have an error its this: for index in range(len(segments)-1, 0, -1): type error object of turtle has no Len()
here code
I'm having a little problem with getting my snake to grow bigger. I was making this snake game on my own but i ran into a phew problems so I decided to look for help. If you run the code it actually does get bigger but for some reason the snake body part changes its color to the head part. By the way i really enjoy your tutorials and I've learnt a lot so thanks! Here's the code https://pastebin.com/N6nLtdAR
1:17
Hii. Right now my game starts with both "head" and one "segment". I wonder how I fix so that the game starts with only the "head"? Here's my code (I've added the function def tran() btw but that doesn't cause the trouble I think):
import turtle
import time
import random
delay = 0.1
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("green")
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"
#food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("black")
food.penup()
food.goto(0, 0)
segments = []
#Functions
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)
#Snake eating, collision
def eat():
if head.distance(food) < 20:
# move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
#Add segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange")
new_segment.penup()
segments.append(new_segment)
def grow():
#Move the end segment first in reverse 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)
#Move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
#To the other side
def tran():
x = head.xcor()
y = head.ycor()
if x >=300:
head.setx(x*(-1))
if x <= -300:
head.setx(x*(-1))
if y >= 300:
head.sety(y*(-1))
if y <= -300:
head.sety(y*(-1))
#Keyboard binding
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
#Main game loop
while True:
wn.update()
tran()
eat()
grow()
move()
time.sleep(delay)
wn.mainloop()