
Python Projects | Create Snake Game Step By Step In Python For Beginners
LEARN HOW TO MAKE A SIMPLE SNAKE GAME STEP BY STEP IN PYTHON. THIS WILL BE A BEGINNER LEVEL PROJECT.
* If you would like to leave a tip you can do so below, thanks *
PayPal:
Source Code:
⌚Chapters
0:00 Introduction
0:09 Snake Game Demo
0:54 Importing Python Libraries – Starting To Code
1:43 Creating User Interface
2:55 Creating The Border(Danger Zone For the game)
5:09 Creating Scoring System
5:24 Creating Snake
6:40 Creating Food
7:54 Creating Final Score
9:18 Defining Snake Movements
12:59 Binding Keyboard Shortcuts
14:24 Creating Main Loop / Functionalities
25:08 Facing / Solving Errors (Important)
27:36 Snake Game Output
Need any help? Join my Discord:
📜Book I Follow For Learning Python
==============================
📚Python Crash Course:
📒Learning Python (For Starting):
📓Python Automating Programming:
📚Python Programming For Beginners:
📒Python for Data Analysis:
⭐️ Merch ⭐️
👕 Merchandise: tech2-etc.creator-spring.com/
If you enjoyed this video, then please please 👍LIKE and SUBSCRIBE this channel & press the bell icon for future videos.
Thank you.
=============
SUPPORT ME 💪
=============
🎀 Donate Me By PayPal:
🎁 Support me on Patreon for exclusive episodes, discord and more!
☕ Buy Me A Coffee:
===================
Recommended Videos
===================
✅ Create Fully Professional Responsive Animated Website Using HTML & CSS …
✅ Create Netflix Responsive Landing Page Clone With Html and CSS
✅ Ultra Responsive Portfolio Website from Scratch Using Bootstrap Html CSS
==========================
Recommended Course (Playlist)
==========================
✅ Complete Bootstrap Designs:
✅ Web Development Tutorials:
✅ Responsive Web Design Tutorials:
✅ Python Projects:
===============================
Connect with me & get daily updates👇
===============================
Discord:
Instagram:
FB (Personal):
FB Page:
FB Group:
——————————-Track————————–
Music provided by : NoCopyrightSounds
Watch: …
Free Download / Stream:
#pythonprojects #snakegame #pygame #python #projects
man why when i start the game the sanke dosent move
I got an IindentationError: unident doesnot match any outer indentation level at line 125
Jririd
bad event type or keysym error generated
What should I do?
How do start a game
it says to me File "C:UsersUserAppDataLocalJetBrainsPyCharm Community Edition 2022.2.2jbrbinsnakegamemain.py", line 102, in <module>
screen.onkeypress(snake_go_up, "up")
File i dont know what mistake i did can you help me?
I am working on this for a whole day and my back is hurting alot. My all elements are okay but the snake doesn't move. if u have any message media give it. I will show u my work and see what have i done. Please this is my request I did this for school work
I love your content, very informative, my friend ari likes your content too, please keep it up!!!! I wish to see more of your videos!
I did everything also everythings the same as yours but mine wont work
old_fruit [ ] is showing a problem on pycharm
i gor error line 138
'>' not supprted between instance of 'method' and 'int'
Hi bro.. I executed this code in pycharm.. But.. At last it is showing the error that is last line turtle. Terminator ()
IDK what did I done wrong but after my snake eat the first fruit . The second food is literally invincible from getting eaten . My snake just run through the food . Can you help me ?
i request someone to paste the whole code in the comment sec!! it would be helpful.
it is not moving wat i should do
Python Projects | Create Snake Game Step By Step In Python For Beginners
mine is showing this error:
Traceback (most recent call last):
File "/Users/pranshmaurya/PycharmProjects/Snake/main.py", line 141, in <module>
screen.clear()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/turtle.py", line 1028, in clear
self.onclick(None, btn)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/turtle.py", line 1365, in onclick
self._onscreenclick(fun, btn, add)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/turtle.py", line 669, in _onscreenclick
self.cv.unbind("<Button-%s>" % num)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/turtle.py", line 422, in unbind
self._canvas.unbind(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1426, in unbind
self.tk.call('bind', self._w, sequence, '')
_tkinter.TclError: can't invoke "bind" command: application has been destroyed
Process finished with exit code 1
I have an error when running on powershell. it says that speed should have an argument
here's my code
#import packages
import turtle
import random
import time
#creating screen
screen = turtle.Screen()
screen.title("SNAKE GAME")
screen.setup(width=700,height=700)
screen.tracer(0)
screen.bgcolor("#1d1d1d")
#creating border
turtle.speed(5)
turtle.pensize(4)
turtle.penup()
turtle.goto(-310, 250)
turtle.pendown()
turtle.color("red")
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.penup()
turtle.hideturtle()
#score
score = 0;
delay = 0.1
#snake
snake = turtle.Turtle
turtle.speed(speed=)
snake.shape("square")
snake.color("green")
snake.penup()
snake.goto(0, 0)
snake.direction = 'stop'
#food
fruit = turtle.Turtle
fruit.speed()
fruit.shape()
fruit.color()
fruit.penup()
fruit.goto()
old_fruit = []
#scoring
scoring = turtle.Turtle
scoring.speed(0)
scoring.color("white")
scoring.penup()
scoring.hideturtle()
scoring.goto(0, 300)
scoring.write("Score: ", align="center", font=("Courier", 24, "bold"))
#define how to move
def snake_go_up():
if snake.direction != "down":
snake.direction = "up"
def snake_go_down():
if snake.direction != "up":
snake.direction = "down"
def snake_go_left():
if snake.direction != "right":
snake.direction = "left"
def snake_go_right():
if snake.direction != "left":
snake.direction = "right"
def snake_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":
y = snake.ycor()
snake.sety(y – 20)
if snake.direction == "right":
x = snake.xcor()
snake.setx(x + 20)
#keyboard binding
screen.listen()
screen.onkeypress(snake_go_up, "Up")
screen.onkeypress(snake_go_down, "Down")
screen.onkeypress(snake_go_left, "Left")
screen.onkeypress(snake_go_right, "Right")
#main loop
while True:
screen.update()
# snake and fruit collision
if snake.distance(fruit) <20:
x = random.randint(-290, 270)
y = random.randint(-240, 240)
fruit.goto(x, y)
scoring.clear()
score += 1
scoring.write("Score: {}".format(score), align="center", font=("Courier", 24, "bold"))
delay -= 0.001
#creating new foods
new_fruit = turtle.Turtle()
new_fruit.speed(0)
new_fruit.shape("square")
new_fruit.color("red")
new_fruit.penup()
old_fruit.append(new_fruit)
#adding ball to snake
for index in range(len(old_fruit) -1, 0, -1):
a = old_fruit[index -1].xcor()
b = old_fruit[index -1].ycor()
old_fruit[index].goto(a, b)
if len(old_fruit) > 0:
a = snake.xcor()
b = snake.ycor()
old_fruit[0].goto(a, b)
snake_move()
#snake and border collision
if snake.xcor () > 280 or snake.xcor() < 300 or snake.xcor() < -248:
time.sleep(1)
screen.clear()
screen.bgcolor("turquoise")
scoring.goto(0, 0)
scoring.write("GAME OVERn Your score is {}".format(score), align="center", font=("Courier", 30, "bold"))
#snake collision
for food in old_fruit:
if food.distance(snake) < 20:
time.sleep(1)
screen.clear()
screen.bgcolor("turquoise")
scoring.goto(0, 0)
scoring.write("GAME OVERn Your score is {}".format(score), align="center", font=("Courier", 30, "bold"))
time.sleep(delay)
turtle.Terminator
Code link bro
bro it tells me a lot of errors and stuff and i copied you from the start to the end
but still i cant even start the game and it doesnt show me the game😩
Hi bro. I typed the same but my snake isn't moving instead showing error in turtle.Terminator() line. Please reply asap.
it works! tysm
when i run the code my screen is freezed and snake does not move please help asap!
Hi, great lecture, I did everything step by step, but having error here # snake & fruit colision , if snake.distance(fruit) < 20:
why
line 11, in <module> screen.bgcolor("#ididid")
Hello! I've compared my code and the source code and it's the same but the snake still doesn't move 🙁
Bro, I also wrote the same code, but it's not working
So, I completed it and it’s working but the problem here is my snake is not moving 😂 how do I set the controls?
It saying direction is in turtle attribute has no direction
I did everything but I kept getting invalid direction
The game just crashes when i open up it
Source code
i know why there is a bug because it doesn't work in pycharm it only works in python
Snake isn't moving
lots of error … please make a video for beginners
AttributeError: module 'turtle' has no attribute 'screen'. Did you mean: 'Screen'. WHAT SHOULD I DO TO CORRECT THIS ERROR?
hi tech 2 etc can you put the link that you used to create the snake game in the comment box when you see it thanks.
Hi, i am having an issue than when i score a point i suddenly lose. I did the same code like you.
#importing libraries
import turtle
import random
import time
#creating turtle screen
screen = turtle.Screen()
screen.title('SNAKE GAME')
screen.setup(width = 700, height = 700)
screen.tracer(0)
turtle.bgcolor('green')
#creating a border for our game
turtle.speed(5)
turtle.pensize(4)
turtle.penup()
turtle.goto(-310,250)
turtle.pendown()
turtle.color('black')
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.penup()
turtle.hideturtle()
#score
score = 0
delay = 0.1
#snake
snake = turtle.Turtle()
snake.speed(0)
snake.shape('square')
snake.color("black")
snake.penup()
snake.goto(0,0)
snake.direction = 'stop'
#food
fruit = turtle.Turtle()
fruit.speed(0)
fruit.shape('circle')
fruit.color('red')
fruit.penup()
fruit.goto(30,30)
old_fruit=[]
#scoring
scoring = turtle.Turtle()
scoring.speed(0)
scoring.color("black")
scoring.penup()
scoring.hideturtle()
scoring.goto(0,300)
scoring.write("Score :",align="center",font=("Courier",24,"bold"))
#######define how to move
def snake_go_up():
if snake.direction != "down":
snake.direction = "up"
def snake_go_down():
if snake.direction != "up":
snake.direction = "down"
def snake_go_left():
if snake.direction != "right":
snake.direction = "left"
def snake_go_right():
if snake.direction != "left":
snake.direction = "right"
def snake_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)
# Keyboard bindings
screen.listen()
screen.onkeypress(snake_go_up, "Up")
screen.onkeypress(snake_go_down, "Down")
screen.onkeypress(snake_go_left, "Left")
screen.onkeypress(snake_go_right, "Right")
#main loop
while True:
screen.update()
#snake and fruit coliisions
if snake.distance(fruit)< 20:
x = random.randint(-290,270)
y = random.randint(-240,240)
fruit.goto(x,y)
scoring.clear()
score+=1
scoring.write("Score:{}".format(score),align="center",font=("Courier",24,"bold"))
delay-=0.001
# creating new_ball
new_fruit = turtle.Turtle()
new_fruit.speed(0)
new_fruit.shape('square')
new_fruit.color('red')
new_fruit.penup()
old_fruit.append(new_fruit)
#adding ball to snake
for index in range(len(old_fruit)-1,0,-1):
a = old_fruit[index-1].xcor()
b = old_fruit[index-1].ycor()
old_fruit[index].goto(a,b)
if len(old_fruit)>0:
a= snake.xcor()
b = snake.ycor()
old_fruit[0].goto(a,b)
snake_move()
#snake and border collision
if snake.xcor()>280 or snake.xcor()< -300 or snake.ycor()>240 or snake.ycor()<-240:
time.sleep(1)
screen.clear()
screen.bgcolor('turquoise')
scoring.goto(0,0)
scoring.write(" GAME OVER n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))
# snake collision
for food in old_fruit:
if food.distance(snake) < 20:
time.sleep(1)
screen.clear()
screen.bgcolor('turquoise')
scoring.goto(0,0)
scoring.write(" GAME OVER n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))
time.sleep(delay)
turtle.Terminator()
code game
It shows error on .direction while designing snake how can i fix it ?
Output screen is not able to hold waht to do
your code is error,
But how to run your code?
error in line 125
unindent does not match any outer in dentation level (<string>,line 125)
after game over to restart the game again we have to run the code again instead of that can u define any function of it and by clicking on the restart button we can start the game again
and how can we save the highscores every time it should show the highscore and update it
for index in range(len(old_fruit),-1,0,-1):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: range expected at most 3 arguments, got 4
Bro please Help Me
Nice one all work as it should🎉
Bro I can't get this
line 140, in <module>
screen.clear()
so i went through the whole thing and copied it exactly but the score system doesnt work and my snake wouldnt grow. Any idea on how to fix this?? (I just started actual coding after moving from scratch so idk what im doing)