
Python Game Programming Tutorial: Snake Game Part 7
🆘 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.
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
https://pastebin.com/rXxapdys
pls help me
Could you help?
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.colour("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0" , align="center" , font=("Courier", 24, "normal"))
Nothing comes up on the screen except black and there's an error that says:
traceback (most recent call last):
File "/Users/mishkamir/Documents/SnakeGameonPython.py", line 37, in <module>
pen.colour("white")
AttributeError: 'Turtle' object has no attribute 'colour'
thank you so much !
Woo Hoo, finally finished this! It was an amazing tutorial, thanks so much for sharing! You should do more like these 😀 Your my fav youtuber when I want to code! Keep up the good work 🙂
I know my problem is indentation but don't know how to fix it
import turtle
import time
import random
delay = 0.1
# Score
score = 0
high_score = 0
# Set up the screen
wn= turtle.Screen()
wn.title("Snake Game By Adam")
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 = []
# Pen
pen = turtle.Turtle
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup
pen.pen.hideturtle(0, 260)
pen.write("Score: 0 High Score: 0", align="center", font= ("Courier", 24, "normal")
# 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 + 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 gane loop
while True:
wn.update()
# Check for a 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)
# Clear the segments list
segments.clear()
# reset the score
score = 0
# Update the score display
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center"), ont= ("Courier", 24, "normal"))
# 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)
# Increase the score
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center"), ont= ("Courier", 24, "normal"))
# 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()
# Check for head collision with the body segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
# Hide the segments
for segment in segments:
segment.goto(1000, 1000)
# Clear the segments list
segments.clear()
time.sleep(delay)
wn.mainloop()
Thanks for making this series, I was able to learn quite a bit!
I need help-
https://pastebin.com/2b0wXn8v
It shows me the same error that you saw. How do I fix this?
Hello, I wanted to ask, if you know, how to save the high score in the computer and use it when ever I play the game again (it show the high score made from last time or so)
ay just want to say, thanks a lot! 😀
On the #pen code it comes up pen.write("Score: 0 High Score: 0, align="centre", font=("Courier", 24, "normal"))
TypeError: write() missing 1 required positional argument: 'arg'
Can anyone help with this
pastbin.com/UEF2uYu4
Hello sir i did exactly same wht you did… And everything is perfectly running.. No errors.. Just one error is coming.. When snake eats 2dt food then it gets closed automatically.. And shows error as.. 'object of type Turtle has no len()'… What should i do
how i can change window's icon? help please
Please help me- my problem is this- File "Snake-game.py", line 149
for index in range(len(segments)-1, 0, -1): IndentationError: unindent does not match any outer indentation level
lot of thanks for you teacher! I wish you a great age!
This is the great tutorial! 👏👏It's helped me a lot. Thank you sir
AAAAH THANK U THANK U SO MUCH!!!!
Thank you!
Hello, I am trying various cheat modes in the game. When I press the c key, I want the snake not to die when it hits the edges or itself. How can I write this in the if loop?
5:15
The problem: Cannot assign to function call pen.write("Score: {} High Score: {}".format(score, high_score), align="center"), font=("Courier", 24, "normal"))
my code: import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
wn = turtle.Screen()
wn.title("Snake game google assaignment")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
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=("Courier", 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"
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(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
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"
for segment in segments:
segment.goto(1000,1000)
segments.clear()
if head.distance(food) < 20:
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)
score += 10
if score > high_score:
high_score = score
pen.write("Score: {} High Score: {}".format(score, high_score), align="center"), font=("Courier", 24, "normal"))
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"
for segment in segments:
segment.goto(1000,1000)
segments.clear()
time.sleep(delay)
wn.mainloop()
Wouldent it be easier to make the score equal to the amount of segments?
it says my score += 10 is not defined
Thank you bro finally i made my first game in python.🤗🤗
Just subbed thanks for tutorials helps with gsce
Yo can u help me. I tried to create a wall for all the levels after the 3rdlevel but it after 3 levels the wall just generate in that level and keeep on generate
Can u just give me the code to add walls pls.
THX 4 READING THIS
I did it! 🙂
Hey guys , whoever in the comment section , let’s share this to our friends who are in the same field and make this legend happy .. his skills are so easy to understand… deserves much more Attention
Hey can you help me with my code? I am at 5:43 and the error says "expected integer but got 24.
My code:
import turtle
import time
import random
segments = []
delay = 0.1
score = 0
high_score = 0
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0)
#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=("Courier", 24, "normal"))
#Direction Functions
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)
#Defining Directions
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"
#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)
#Bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
#Main loop
while True:
wn.update()
#Check for a 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"
for new_segment in segments:
new_segment.goto(1000,1000)
segments.clear()
if head.distance(food) < 25:
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
score +=10
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("blue")
new_segment.penup()
segments.append(new_segment)
if score > high_score:
high_score = score
pen.write("Score: {}, High Score: {}".format(score, high_score), align="center", font=("Courier, 24, normal")),
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()
#Boby Collision
for new_segment in segments:
if new_segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
for new_segment in segments:
new_segment.goto(1000,1000)
segments.clear()
time.sleep(delay)
wn.mainloop()
*my organization isnt very good*
Hi me again the score is not showing
Here is my code
import turtle
import time
import random
from turtle import Turtle
delay = 0.1
# set up the screen
wn = turtle.Screen()
wn.title("snake game by Menuka")
wn.bgcolor("#363636")
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("cyan")
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=("Courier", 24, "normal"))
# 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 + 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_right, "d")
wn.onkeypress(go_left, "a")
# Main game loop
while True:
wn.update()
# check for A collision with the border
if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
time.sleep(2)
head.goto(0,0)
head.ddirection = "stop"
# Hide the segments
for segment in segments:
segment.goto(1000,1000)
# clear the segments
segments = []
# 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 end segments first in reverse
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()
# check for head collision with the body segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
# Hide the segments
for segment in segments:
segment.goto(1000, 1000)
# clear the segments list
segments = []
time.sleep(delay)
wn .mainloop()
Hello dear sir,
How to avoid food on the score board
Hello sir, I have completed most of the programming here, but I have a problem now, that is, my score and highest score will not be reset. If you are free, please help me.
import turtle
import time
import random
delay = 0.1
# Score
score = 0
high_score = 0
# Screen Set Up
win = turtle.Screen()
win.title("Snake Game by @Nicky")
win.bgcolor("green")
win.setup(width=600, height=600)
win.tracer(0)
# Snake Head
head = turtle.Turtle()
head.speed(0)
head.shape("circle")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Food(Apple)
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("yellow")
food.penup()
food.goto(0, 100)
segments = []
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("Yellow")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score:0", align="center", font=("Courier", 24, "normal"))
# Functions(moves)
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 + 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
win.listen()
win.onkeypress(go_up, "w")
win.onkeypress(go_down, "s")
win.onkeypress(go_left, "a")
win.onkeypress(go_right, "d")
# Main Game Loop
while True:
win.update()
# Check For a 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"
# 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 segments
new_segments = turtle.Turtle()
new_segments.speed(0)
new_segments.shape("circle")
new_segments.color("grey")
new_segments.penup()
segments.append(new_segments)
# Increase The 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 In Reverse Oder
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()
# Check For Head Collision With Body Segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
# Hide The Segments
for segment in segments:
segment.goto(1000, 1000)
# Clear The Segment List
segments.clear()
# Reset Score
Score = 0
time.sleep(delay)
win.mainloop()
man cant believe you answer most questions 3 years after you uploaded the video,respect bro
hi, um i have a question. Why does my snake not have like one and then the other parts when it eats the food, it goes an it is like a snail
Hi,
I started making a snake game in python and looked at your tutorial for help in some key points. I have a problem that though the game works as it is supposed to, the score doesn't update. I mean when it collides with itself, the score remains as it is instead of resetting to 0. I have checked several times, but to no use.
Note: i haven't completely followed your tutorial, because i have tried to see how much i have learned, so some things might be different. Instead of the snake dying when it hits the wall, i made it such that it appears at the other side, like in the real game and i also made movements using a different method
Thank you so much for this Tut! I've also found your voice pretty relaxing/ soothing which makes it easier for me to follow the codes. Btw, I suppose you're living in Tokyo aren't you. It's a great city that I would love to visit.
i cant my Score 🙁 I've checked the code several times on pen and it doesn't show up
Awesome! Loved it! What's the next best thing to do after this? Especially to get some income, even if it's a low key thing/project
hi i really like your tutorials and i followed them all but i had a question, you see the game here that i coded adds the score up even when i am not touching the food can you see whats the problem please, the code:
import turtle
import time
import random
delay = 0.1
# score
Score = 0
High_score = 0
# Screen Setup
wn = turtle.Screen()
wn.title('snake game by malaz')
wn.setup(width=600, height=600)
wn.bgcolor('yellow')
wn.tracer(0) #stops screen updates
# the head
head = turtle.Turtle()
head.speed(0)
head.shape('square')
head.color('blue')
head.penup()
head.goto(0,0)
head.direction = 'stop'
# the 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=("courier", 24, "normal"))
# 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 stop():
head.direction = 'stop'
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_down, 's')
wn.onkeypress(go_right, 'd')
wn.onkeypress(go_left, 'a')
wn.onkeypress(stop, 'space')
# Main gameloop
while True:
wn.update()
# check for a 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)
# clear the segments list
segments = []
# check for a collision with the food
if head.distance(food) < 20:
# Move the food to a 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)
# Increase the 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 in reverse order first
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()
# Check for collision with the segments
for segment in segments:
if new_segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
# hide the segments
for new_segment in segments:
new_segment.goto(1000, 1000)
# clear the segments list
segments = []
# reset the score
Score = 0
pen.clear()
pen.write("Score: {} High Score: {}".format(Score, High_score), align="center", font=("courier", 24, "normal"))
time.sleep(delay)
wn.mainloop()
A quick question. How would I increase the number of segments you grow after you eat one apple?
* patiently waiting for him to finish playing his game off screen*
Thank you so much for doing these tutorials. You are an amazing teacher.
AAH I FINISHED THE GAME ! HOW DO I ADD INTERESTING BACKGROUND AND SOUND EFFECTS OR DETAILED-LOOKING SNAKEs?
Pen is not defined.🤷🏼♂️ Wdoido?
Heyo! Just finished the tutorial and just wanted to say that you are an excellent teacher. Also I was just wondering what font that is! 🙂
Hey… I tried this, and the screen seemed to show; but when I press w, a, s or d, the snake doesn't move. Please help. Thank you. (there is no error, bye the way: it just doesn't move)
import turtle
import time
import random
delay = 0.1
# Score
score = 0
high_score = 0
# Set up the screen
wn = turtle.Screen()
wn.title("Snake – By Yatharth")
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 = "up"
# 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("black")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 Highscore: 0", align="center", font=("Courier", 24, "normal"))
# 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 + 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.ycor()
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 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)
# Clear the segments list
segments.clear()
# Reset the score
score = 0
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
# Check for a collision with the food
if head.distance(food) < 20:
# Move food to random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
# 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)
# Increase the 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 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(segment) > 0:
x = head.xcor()
y = head.ycor()
wsegments[0].goto(x,y)
move()
# Check for head collisions with the body segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
time.sleep(delay)
wn.mainloop()
Hey bud thanks I finished it.
It works!
this code actually works
thanks
thank you so frickin much!!!!!
When I die my snake just speeds up so much when i respawn. How do i stop that. Heres my code:
#Snake
#By Cat_CodingTech
import turtle
import time
import random
delay = 0.1
#Score
score = 0
high_score = 0
wn = turtle.Screen()
wn.bgcolor("green")
wn.title("Snake by Cat_CodingTech")
wn.setup(width=600, height=600)
wn.tracer(0) #Turns off screen updates
#Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.setposition(0,0)
head.direction = "stop"
#Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.setposition(x, y)
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=("Courier", 25, "normal"))
#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 + 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.onkey(go_up, "Up")
wn.onkey(go_down, "Down")
wn.onkey(go_left, "Left")
wn.onkey(go_right, "Right")
#Main game loop
while True:
wn.update()
#Check for collision with border
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
#Reset delay
delay = 0.01
time.sleep(1)
head.setposition(0, 0)
head.direction = "stop"
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
pen.clear()
pen.write("Score: 0 High Score: {}".format(high_score), align="center", font=("Courier", 24, "normal"))
score = 0
#hide the segments
for segment in segments:
segment.setposition(1000, 1000)
#clear segments list
segments.clear()
#check for collision with food
if head.distance(food) < 20:
#move food to an random position
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.setposition(x, y)
#add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("gray")
new_segment.penup()
segments.append(new_segment)
#Shorten delay
delay -= 0.01
#Increase the score
score += 1
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 end segments first in reverse over
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].setposition(x, y)
#move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].setposition(x, y)
move()
#check for head collision with body
for segment in segments:
if segment.distance(head) < 20:
#Reset delay
delay = 0.01
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
pen.clear()
pen.write("Score: 0 High Score: {}".format(high_score), align="center", font=("Courier", 24, "normal"))
score = 0
#hide the segments
for segment in segments:
segment.setposition(1000, 1000)
#clear segments list
segments.clear()
time.sleep(delay)
wn.mainloop()
How to add more food at the sameme time