Make a Snake Game in Python | Turtle | Python Project
Вставка
- Опубліковано 8 вер 2024
- How to make a snake game using python. Here we will make the classic snake game with python.
Source Code : github.com/eva...
Join our Facebook Group : / codingcom
Our Instagram : / codingwithevan
Our Facebook : / codingwithevan
------------------------------------------
Music Credit: Lakeyinspired
Soundcloud: / lakeyinspired
------------------------------------------
if somebody has trouble that the snake is not running just run it again
sorry moving not running
what do you mean the snake wont move for me. i'll restared the program 100 times
i type def but def is giving error
@@rasmusmoto7668 yeah its not working its stuck nt moving
maybe the capital letter is on
i love this channel .this channel deserves more subs
Thank you! You can share my videos with your friends. That will mean a lot ❤️
i use pycharm but when i start plying the snake does not moved
me to
are you using w a s d to move
me to
@@panva9948 I am
me too but it doesn't work :(
Bro i tried alot but snake doesn't moved from his place....now what did i do?
La même
Please help me, can someone tell me why the snake game what I made doesn't move? I had press (W A S D) but still can move😭
Same problem
same brthr snake not moving
Only put onkey donot put press
Thanks bro it was entertainment day for making snake game
Thank you so much bro......
I Made a snake game and it worked! BIG TIME.
Could somebody explain these lines, please?:
for i in range(len(segments) - 1, 0, -1):
x = segments[i - 1].xcor()
y = segments[i- 1].ycor()
segments[i].goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
Bro literally the same problem serious help required
Yeah I actually know little about python and a good amount about java, this part of for in is very confusing to me, rest I could make out myself
It is a code for segments to follow the head
It is using a for loop to iterate through all of the objects in the segments list. i is an integer, and the range goes backwards from the end of the len(gth) of the list (-1) to zero, going backwards -1 each time. IT then tells the object at index i to goto the x,y of the previous object.
finally it tells the first object in the list to go to the head objects x,y if there are any objects in the segments list.
this is a very good tutorial thanks
in order to run this program i just need to download python turtle right?
Yes.
Yep and visual studio code.
Very nice video in UA-cam thanks bro
Great tutorial 👍 its working
really how did it move? its not working for me pls help me if u can
@@farzadtafreem6414 send the code
@@farzadtafreem6414 same for me its nt working
After finishing the code and getting the interface of the game which key I have to press to start the game?
all perfet but my snake is not movig i am pressing W A S D Keywords but no change please help
im also
Oh my God BRO! , I think you are tired of this video !! Really cool !! I've just put you a "like" when I arrive in the middle of the video !! OK, I will but " subscribe "
Hello im having a problem in the line wn.update() it says exception has occurred: terminator X
Same error
thank you very much it was sucessful
My everything is perfect and my snake is also moving but when touching food it's shape is not increasing so plz any one can help me.
Put food.direction(0,0)
This is a copy of my Snake Game tutorial found here: ua-cam.com/video/BP7KMlbvtOo/v-deo.html
I don't mind you using my code - everything I do is Creative Commons Share-Alike Attribution licensed. But, I would appreciate a link back in the description. Thanks.
How does the segment part work?
Very cool💐
#imports
import turtle
import time
import random
delay = 0.1
#scores
score = 0
high_score = 0
#set up screen
wn = turtle.screen()
wn.title("snake game")
wn.bgcolor('yellow')
wn.setup(width=600, hight=600)
wn.tracer(0)
#snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto()
head.direction = "stop"
#snake food
food= turtle.Turtle()
food.speed(0)
food.shape("square")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#scoreboards
sc = turtle.Turtle()
sc.speed(0)
sc.shape("square")
sc.color("black")
sc.penup()
sc.hideturtle()
sc.goto(0,260)
sc.write("score: 0 High score: 0", align = "center", font=("ds-digital", 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")
#MainLoop
while True:
wn.update()
#check collision with border area
if head.xcor()>290 or head.xcor()290 or head.ycor()0
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
move()
#check for collision with body
for segment in segments:
if segment.distance(head)
I’m getting the turtle.terminator error with wn.update(). Is there a way to fix this?
same bro my error is same
@@allwinlion7806 yo how did you find my comment that was 3 years ago
I have done the coding perfectly and program has been ran successfully but the problem is snake is stable at one place and its not moving. So what shall I do please help me bro
According to this code you can move the snake by pressing w a s d. Check that block.
@@CodingWithEvan Hi Evan! thank you for this video : )
I have a little question, is it possible to link the opposite borders of the turtle screen in this game? So when the snake goes through one side, it comes out through the opposite side?
If yes, can you tell me😟 I've tried checking on Google but didn't find anything : (
@@CodingWithEvan it is not moving yet, with w, s, a ,d
What should I do
NO NEED TO RUN THE PROGRAMME IN VS CODE JUST PUT THE CODE IN PTHON 64-BIT THEN THE GAME WILL START AUTOMATICALLY. BY THE WAY THANKS FOR THE CODE BRO.
Thanks bro!
So the program runs, but there is some error showing, I copied your exact code. Also, for some reason, even after I copied your exact code, my snake dies even if it hasn't touched its body or the borders, it's laggy and all. Another thing that is happening is that if I die after touching the border, the snake resets to its original position, but the food item still stays at its previous spot. Someone please help.
when i run it the "snake head" does not move
You can move them by pressing W A S D
I know but it still doesnt move :(
@Aplin Plays Now you know how to move that snake? Please tell me i also have the same problem
@@sushantshrestha4095 Sorry but i havent figured it out yet
Thanks soo much i subbed and liked the video
Hello I made this game in pydriod in my android phone and the bug is that the Snake is not moving could you tell me hove to solve this ??
great work.
I now can update my program to 0.3!
Can you please tell me what should I do it says "AttributeError: 'int' object has no attribute ' speed'
ua-cam.com/video/tRVj18DTMz8/v-deo.html
Nice coding
What app did you use
I think you used pycharm, did you and i subbed
I used vscode.
@@CodingWithEvan ok thanks
@@CodingWithEvan what did you do to run it cause when i click on run it says start debbuging and run without debugging pls help me
Thank You Very Much !
Here are full code:
#imports
import turtle
import time
import random
delay = 0.1
#scores
score = 0
high_score = 0
#set up screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor('yellow')
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("square")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#scoreboards
sc = turtle.Turtle()
sc.speed(0)
sc.shape("square")
sc.color("black")
sc.penup()
sc.hideturtle()
sc.goto(0,260)
sc.write("score: 0 High score: 0", align = "center", font=("ds-digital", 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")
#MainLoop
while True:
wn.update()
#check collision with border area
if head.xcor()>290 or head.xcor()290 or head.ycor()0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
move()
#check for collision with body
for segment in segments:
if segment.distance(head)
Can somebody tell how to fix this issue:
It is showing turtle has no attribute direction in the the line head.direction
@@gagandeepsinghjass first its probably head.direction('stop')
Thank you Very Much
i have a doubt that can i want to move my snake with keys like up key and left key
as it is difficult for me to play with w,a,s,d
please reply/help
yes you can,
on the keyboard binding, where I used "w", "a", "s", "d" you can write "Up", "Down", "Left", "Right"
everything work find but one thing that I feel almost give up why can't I start to move? I follow ever single code you write and double check it and nothing wring but I just can't move how to fix it
how can i add a property which makes this snake faster as long as i keep holding the button
Bro it's working but the keys function were not working
Hi. I am trying to make a life simulator game using python and turtle. I already know how to move the character, but I am having problems with how the character should interact with the environment (like buildings or something). any suggestions or suggested resources?
Super
its not moving some body please help here is the code:
#imports
import turtle
import time
import random
delay = 0.1
#scores
score = 0
high_score = 0
#set up screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor('yellow')
wn.setup(width=600, height=600)
wn.tracer(0)
#snakes 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("square")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#scoreboards
sc = turtle.Turtle()
sc.speed(0)
sc.shape("square")
sc.color("black")
sc.penup()
sc.hideturtle()
sc.goto(0,260)
sc.write("score:0 High score:0", align = "center", font=("ds-digital", 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")
#MainLoop
while True:
wn.update()
#check collision with border area
if head.xcor()>290 or head.xcor()290 or head.ycor()0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
move()
#check for collision with body
for segment in segments:
if segment.distance(head)
The yellow screen is just visible for a second but i have written the code as it is then why it's not working?
help pls it only opens for a second and then closes
edit: it runs but i cant go left and it doesnt grow segments
The score is going up on it's own and it's making my speed go insane. Can someone be so kind as to tell me what might be wrong?
Actually, I tried creating these icons, but it didn't work for me, so, I'm sorry ...
If it works, I'll tell you very quickly without a doubt.
Yeah I’m having the same problem I don’t know how to fix it help please
bro x=segments[index-1].xcor() is showing out of range
in your video it is line no.134
what to do plz help.
whenever i try to make the snake move it does not move and whenever close the game it gives me errors please tell why it is doing this i really want to learn programming
Bro I need help with line 134 on the code the x has a squiggly red line under it and don’t know how to fix
damn its working if fun ..... but there's a problem when i try to go up , it just suddenly close n when i try to go left it's just stop :[ ....... any suggestion ??? wht shoul i do now :[
Uh can someone explain to me why the segments have to be out of range
It is not working bgcolor screen is only coming
when i try running th game in vscode the game opens for a split second and then closes. it says "TypeError: turtle.Vec2D() argument after * must be an iterable, not float" can someone help pls
Very useful and good!
Hello Evan? The snake isn't moving at all. I cross checked the code more
than three times.
There is a problem in line 84 with me please solve it
Thanks man!!!
It says there is no title for class turtle...
Snake is not moving by using your source code what happens.
how i can change window's icon??? help me please!
?
The snake doesn't move even on pressing w,a,s,d
bro you may have done it wrong it worked for me ,try looking for mistakes in direction or the keyboard binding etc..
@@arj2924 how to correct them?
thanks
bro my 49 line def is not working because of it .
what's the solution
My snake is stuck and is moving very far distances and i cannot do anything after i eat the first red dot my score remains ten and nothing works
pls help
i am very upset that the snake did not move after all this hard work, I needed 4 hours to understand and do the program :(
i am extremly upset
You can get the source code from the description. Then run the code. Also make sure turtle is installed properly.
NameError: name 'y' is not defined
please help me bro
Im trying to learn python 😇
better go with course of code with harry or codeitup on youtube
Also, when my program ends when the head hits the food
how can I add this font type my code ?
Hello Evan!!!
I run the program but snake is not moving...
Can you figure out this problem?
I have some advice that would help the viewers, can u pls run the code after every line so if u see an error, like the snake not moving, u can fix it and we can also learn from it
that would be very nice
the turtle screen in showing and disappearing. pls help
(variable).mainloop() should help
Course outcome give me
Hlo bro
My program throw's an attributeError
What can I do??
Not working
W a s d is not working
How to fix it?
name segments is not define how to solve this
Bro it is not working with W S A D can you help me
Can i know what are the extension to install for this??
How to run in mobile
Sir, please provide the module named tkinter
9:52
Bro syntax error: invalid syntax please help me bro
11:51
snake is not moving plzz help
Name app?
the snake is stable not moving
bruh you put the code in the desription and we can copy it bruh
5:41
will it still work with pycharm'
Yes
not working
Bro, what happen if I run this project in Android Mobile ?
It will not work
My snake is not moving
Dude it did not worked for me 😭😭😭
onkeypress has no attribute
Congrats! You earned a sub!
It didnt work for me :(((((((
I am STILL a KID...
does this work on all computers
the reason i am asking is because i am trying it and it is not working . i dont know what the issue is
Yes it will work. Did you installed 'turtle' properly?
@@CodingWithEvan i thought that turtle was already installed automatically
@@CodingWithEvan thanks for these responses. i will subscribe for sure
My snake don't move