
Python Game Programming Tutorial: Snake Game Part 1
🆘 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
You are the best and one day i dream to be like you
can you run this in Geany
It's a great tutorial. I like your videos and tutorials very much
I just love ur effort and would like to say
Thanks a ton my hero!!!😃😃😃😃
should i even write the green colour script at the top
this works thx bro
i use spyder, what app is this
I tryed turning tracer on and Python has stoped responding what could be the problem
Can I get thiz code plzzzz
Green color window is opening but there is no snake y?
Good 👍.
Can you make snakes and ladders game please.
in your next video of making game try to make it on windows 7 python 3.8.6
plz i T WOULD BE REALLY HELP FULL
Sir I ran the code ….my green screen came but it vanished quickly
Hi sir, I need your help for some program. can you please support
Dude you should write a book on turtle module it would be so helpful i bet you could make some good money…. coding books most of the time are like 30 bucks (united states dollars)
hello sir, I ran the code but i didnt get the 600×600 size of the screen and instead it was a full screen and I couldnt reduce the size of screen even if I take values less than 600.
could you make a car racing game please ?
I'm not getting the title in output. What should i do?
You could use try and except to stop the "window.update() cannot be terminated" message, it looks some thing like this…
try:
window.update()
except:
break
i need the code to copy paste where is he?????????
good pl
support
did you know that when you make the setup for the screen you don't need to type width or height you just need to type in how high and wide it's going to be.
can U share the python which u are using
AttributeError: 'tuple' object has no attribute 'append'
here is the code 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 @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 = []
# 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_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
# Reset the delay
delay = 0.1
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 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)
# Shorten the delay
delay -= 0.001
# 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(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()
# Reset the score
score = 0
# Reset the delay
delay = 0.1
# Update the score display
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
time.sleep(delay)
wn.mainloop()
i am trying this code on vs code but it is not showing the output then what should i do
can you teach us how to add pause and quit button in the game
i wrote all the code down pycharm had the little tick to show everything was correct then i clicked run and chose the file i wanted to run but it still ran another file instead do you know what happened
hi i absolutely love your videos and I have followed so many tutorials now, I was just wondering if you could make a tutorial on how to make a simple clicker game (like cookie clicker)
Hi, can you do a version where the food does not spawn under the snake itself?
wn.mainloop() does not work for me. It says, "AttributeError: '_Screen' object has no attribute 'mainloop'"
do you know how to fix this?
ok, so i wanted a tutorial on a cookie clicker because I wanted to watch your channel more because it is really easy to follow, but I have to pay to watch it. why………
line 8
NameError: name 'screen' is not defined
What can I doooo ?
My code works, but the screen is only staying up for a millisecond.
What do I do?
'python is not responding' how can i fix it?thank you
my background colour is not coming green
can u plz send the code
Hi! so I was wondering what program you used to make the game. I'm pretty new at coding and I use repl.it. If we used different programs, the code would stay the same right? Thanks 😊 And by the way, your tutorials are great!
hi! is there a way i can detect what the score is, im trying to see when the score reaches certent numbers
how do i open in terminal
File "c:Users*****DesktopPyGamemain.py", line 8, in <module>
wn = turtle.screen
AttributeError: module 'turtle' has no attribute 'screen'
plz plz plz help anyone
I just completed and coded this entire tutorial series. This is one of the better Snake Game tutorials on YouTube – You did an excellent job explaining the logic behind the code in each video!
found you from free code channel. the guy who was teaching snake there was completely useless in teaching and i got the concepts of the pong so easily due to your teaching methods. thanks again for uploading these kind of videos of aspiring software engineers like us. really appreciate your time and effort in doing so.
thanks so much it was very helpfulll!
Attribute error: Module ‘Turtle’ has no attribute ‘shape’
Help please
tq so much for dis …… how to solve this error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'wn' is not defined
thanks for leading in my first python project
It says no module named turtle
Thank you for these awesome tutorials. Is there a way to display a grid where the snake is moving?
why is the green screen not showing up?