you deserve more subscribers, the way you explain is really awesome!
@robertlouis57214 жыл бұрын
Your videos are simply phenomenal as they offer the viewer the opportunity to learn complex systems that could improve an understanding of coding. Thank you for taking the time to make these tutorials.
@cyndinorwood78415 жыл бұрын
Just an FYI for the #Move the end segments first in reverse order. I changed the 0 to a -1 to get rid of the grey segment that was appearing after I ran the program each time. So if you see this, the way to fix it is to change this line here for index in range(len(segments)-1, 0, -1) .....change the 0 to a -1 This is an excellent tutorial Christian Thompson.
@Ajajqiqjaa3 жыл бұрын
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!!!!!!!!!!!!!
@TokyoEdTech3 жыл бұрын
Thanks - keep on codin'!
@BananaDope2 ай бұрын
Hey, I'm a little late but at 6:45 you said you will post in the comments the link of your video about loops and that we should remind you if you forgot.
@TokyoEdTech2 ай бұрын
@@BananaDope Here you go: kzbin.info/www/bejne/poeZk56hZrqrf5osi=slfzrgDQm7D0jv4V Thanks for the reminder!
@Ajajqiqjaa3 жыл бұрын
the parts are divided so incredibly insightful - different part of the games and it is explain with so much details!
@TokyoEdTech3 жыл бұрын
Thanks - I try. :)
@idkhow96035 жыл бұрын
this is the best tutorial i have seen so far. Thanks so much
@cartoonpage96964 жыл бұрын
This is the best tutorial in KZbin I ever seen..
@TokyoEdTech4 жыл бұрын
Thanks!
@SenseiEli4 жыл бұрын
Wrote all till now works perfect! Next I will build my own game! Thank you!
@thelolgamer6124 жыл бұрын
I think it is better to use: if head.distance(food) < 20: x = random.randrange(-280, 280, 20) y = random.randrange(-280, 280, 20) food.goto(x, y) this way the food spawn in the path of the snake
@TokyoEdTech4 жыл бұрын
Great tip - thanks!
@arin24424 жыл бұрын
I have a slight problem at 3:00.. When I typed in the code new_segment.speed(0), python tells me that it is an attribute error. When ever my black square touches the red circle, the game just stops and it shows me Attribute error. What should I do?
@beetal38504 жыл бұрын
this is awesome! Im thinking about adding other things to it like new fruits that are harder to eat and so on! Im actually pretty good with python.
@vaishalirawal91404 жыл бұрын
You are a very good teacher... Love from India🇮🇳🇮🇳
@TokyoEdTech4 жыл бұрын
Thank you!
@Ph34dz6 жыл бұрын
There is no good talkthrough like this in any other language, i looked up in every language to find out how it works, and there is none that explains as well as this guy. Im doing this in c++, but the thought process is same as in Python :D
@darkknight-r4gАй бұрын
the snake grow bigger everytime it moves .it is not forming a single single segment .here is the code import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("Snake game by @Aryan") wn.bgcolor("grey") 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("blue") food.penup() food.goto(0,100) segments = [] 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) 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() 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("brown") 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()
@TokyoEdTechАй бұрын
@@darkknight-r4g The new segment section needs to be indented one more level.
@leonmeister80376 жыл бұрын
Just a reminder that you wanted to link your Python 3 basics in the comments!
@KabeerSehgal016 жыл бұрын
You can explain super good! :)
@JanMarsalekWirecard5 жыл бұрын
9:33 it is not runking for me, the grey part is still in the middle, help me please
@ethanreed26725 жыл бұрын
# Simple Snake Game in Python 3 for Beginners # By @TokyoEdTech 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" # 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 + 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 segments 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) wn.mainloop() I hope this can help! Just copy and paste the parts of your code that MIGHT be broken
@mobzblizzard7975 жыл бұрын
you mentioned there was an alternative method to create the segments by using a temp variable, is that method easier than this? And if so, could u explain how to do that method?
@harikalatheeswaran92064 жыл бұрын
Amazing tutorial ! Tried pendown() for every turtle object and the patterns created were very mesmerising ! This tutorial gives me new ideas to create different games. Thanks a lot for this tutorial !
@TokyoEdTech4 жыл бұрын
Glad it helped!
4 жыл бұрын
next step add snake in neural network to teach play by it self ! please make tutoral
@00wx4 жыл бұрын
Please help, when my snake eats, the food just doesnt connect with snake
@TokyoEdTech4 жыл бұрын
There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
@manaser024 жыл бұрын
This is a clear tutorial. But my segments draw on the screen as you mentioned it does not draw on the screen 2:12 if put in the command new_segment.penup() . Please help me with this
@manaser024 жыл бұрын
@@TokyoEdTech Thanks
@rexpetrie96595 жыл бұрын
after 8:20 seconds of the video when i run my code, I cant move the snake anymore? Heres my code: import turtle import time import random delay = 0.1 #screen setup wn = turtle.Screen() wn.title("Snake") 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" #Apple apple = turtle.Turtle() apple.speed(0) apple.shape("circle") apple.color("red") apple.penup() apple.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() if head.distance(apple) < 20: x = random.randint(-290,290) y = random.randint(-290, 290) apple.goto(x,y) new_segment=turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("black") 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()
@rexpetrie96595 жыл бұрын
@@TokyoEdTech Here you go pastebin.com/hnX6Z38u
@minonakura88586 жыл бұрын
Do you mind explaining to me why the body parts don't just stack on top of the head? doesn't setting X and Y to the head.x/ycor() mean that the body parts are at the same coordinates as the head?
@genericgraphix72295 жыл бұрын
Mine doesn't stick at all
@葉孟韋-u1c5 жыл бұрын
I am confused about line 85.Knowing that while loop will repeat again and again , why dont the statement "new_segment =turtle.Turtle "defined again and again .
@葉孟韋-u1c5 жыл бұрын
@@TokyoEdTechI learn c++ before ,and find that there is a little difference between two languges.In my opinion i thought that new_segment is 'local variable' ."new_segment =turtle .Turtle( )" is included in the if statement. Once the program jump out of the if statement, if new_segment still be valid?
@reeddhimansaha27824 жыл бұрын
I followed the tutorial exactly. Whenever the snake hits the food, the segment does not get appended to the snake head. It remains there itself and food changes its color to grey. Can you please help out with that?
@bernardbeinhauer95286 жыл бұрын
Hello, I have a trouble understanding this piece of code, how it actually works : for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index-1].goto(x, y) could you please explain it in details so I get what's going on? Thanks in advance! :)
@bernardbeinhauer95286 жыл бұрын
@@TokyoEdTech Oh, got it. Thank you!
@adlifiruz8083 жыл бұрын
hmmm, same here. Do you know how it works?
@Pancakeplanet6 жыл бұрын
Hey! So how would i go about changing the snake's head/body/food with a jpeg? Would this be too complicated to a beginner or?
@LaMalo172 күн бұрын
I'm trying to move the head for 2 pixels instead of 20, but the tail is then only adding up behind the head by only 2 pixels instead of the size of the head (20 pixels). Can u tell me what should I change?
@kidskpadonou8944 жыл бұрын
Thanks for the vids, Christian! Okay now here's my problem. Whenever my worm(what i called my snake) touches his food, the program just closes. I would really appreciate if you told me why that could happen. Please make it simple.
@HELLFIRE02393 жыл бұрын
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)?
@TokyoEdTech3 жыл бұрын
It is the index of the given segment. Here's how it works. The head moves AFTER the body segments move - this is key. Let's say you have a snake and it is moving right. It has a head and two body segments: S - S - H We move from the end to the front like so (Note there is overlap so I'm showing the moving segment on top): S - H
@mohamedirsathabdulazeez36106 жыл бұрын
Hey I have a doubt.when the head take the first food, segments [0] is replacing the head.Can you explain on this.The code says to move the firs segment to where the head positioned.Iam confused
@mohamedirsathabdulazeez36106 жыл бұрын
Christian Thompson thanks
@jimmyyyy2355 жыл бұрын
@@TokyoEdTech can you tell me how to do this pls
@marshall_103892 ай бұрын
when i run it says unindent does not match any outer indentation level
@marshall_103892 ай бұрын
and its the line where it says for index in range(len(segments)- 1, 0, -1):
@TokyoEdTech2 ай бұрын
@@marshall_10389 Can you share your code so I can take a look?
@minyminnos55184 жыл бұрын
SIR I AM SO THANKFUL FOR THIS VIDEO. IT HELPS MY SCHOOL PROJECT. LOVE YA
@ofpqo39514 жыл бұрын
Hi Christian! I'm an absolute beginner in Python and I've been loving your videos. Though, I'm afraid there's a teeny weeny problem. While writing one of the codes, I got what you programmers call an unprintable characters, and despite of all the solutions I've read I still can't seem to remove it and it's holding me down. Please respond ASAP! Thanks!
@ghosttm38236 ай бұрын
I wanted to ask you how I can configure the same element that is food but it takes away your body, it does nothing for the score and you can continue playing until you play it again with your head without having a body, any idea on how to configure it, I am very new to this
@TokyoEdTech6 ай бұрын
It's not super complicated - you need to add the item; basically do the same thing you did with the food. When you collide you need to move the last segment off the screen and remove it from the body. It would be something like: segments[-1].goto(1000, 1000) del segments[-1] Hopefully that will point you in the right direction.
@rayhanali10783 жыл бұрын
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.
@TokyoEdTech3 жыл бұрын
Here’s a video on indentation errors which you may find helpful. kzbin.info/www/bejne/Y2fNhHygfZaYaa8
@rayhanali10783 жыл бұрын
@@TokyoEdTech Thank you so much
@yephoppler3 жыл бұрын
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
@TokyoEdTech3 жыл бұрын
Glad you got it sorted - keep on codin'!
@major2683 жыл бұрын
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() 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
@TokyoEdTech3 жыл бұрын
Hiya - move() Must go before the section that says "#check for collision with snake body"
@major2683 жыл бұрын
It's working thanks
@TokyoEdTech3 жыл бұрын
@@major268 You're welcome - keep on codin'!
@darksoulcreeper79544 жыл бұрын
When the snake eats the 1st food the segment is left at the centre , but after eating the 2nd food the segments joins! Why??? I checked the codes, but they were fine!
@danielgalamas20574 жыл бұрын
I NEED HELP i did all the coding correctly but when i test it my snake only eats one piece of food then when he eats his 2nd one everything just freezes
@TokyoEdTech4 жыл бұрын
If you did everything correctly, it would probably work as expected - coding is very touchy and small errors matter greatly. There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
@EthanJbleethan4 жыл бұрын
I am getting this error Line 111, in for index in range(Len(segments)-1, 0, -1): TypeError: object of type 'Turtle' has no Len() Plz help
@EthanJbleethan4 жыл бұрын
@@TokyoEdTech do I have to write all of the code in postbin or can I just write the part that I think messes up the code? And btw thank you a lot
@EthanJbleethan4 жыл бұрын
Oops, found out the error. In the hide segments part of the next video, instead of putting for segment in segments, I put for segments in segments. I am still very greatful of you giving me advice and for that I gave you a sub, like and I put notifs on. Love you
@questionmarkquestionmarkques3 жыл бұрын
you are a fantastic teacher, thanks so much for this series
@TokyoEdTech3 жыл бұрын
Thanks - keep on codin'!
@ethanreed26725 жыл бұрын
I've noticed that the food isn't always snapped to the grid because, it's on a random coordinate and not something like "25, 25" it's can end up on random coordinate like "348, 321" so, I was wondering if there was/is a way to change this? Maybe use an "if" statement? I'm not sure.
@ethanreed26725 жыл бұрын
@@TokyoEdTech Thanks!
@jedicubing47713 жыл бұрын
9:49 says "Hope you join me for "Part 4" Problem, this is part 4 XD
@TokyoEdTech3 жыл бұрын
Oops! Glad you were paying close attention!
@ThoughtfulSummit4 жыл бұрын
Please tell me, are you looking at written down code on a notebook or document or something, or do you just know all of this code from memory. Because I'm thinking of becoming a programmer, but i have a shit memory.
@pallepuaruna69704 жыл бұрын
This is the best thing I have ever seen
@kurawtw4 жыл бұрын
@TokyoEdTech , Its actually working now. it was a indentation problem. anyway, your the only youtube that actually answers comments. thanks man! keep up this miracle work.
@TokyoEdTech4 жыл бұрын
Glad you got it sorted. I try to answer as many comments as possible, but I do miss some here and there. Keep on codin'!
@ohful5 жыл бұрын
When the game starts my snake already has an extra segment and when I run into the food the new segments go to the middle. I don't know what I'm doing wrong so it would be great if you could help me.
@sivasurya41494 жыл бұрын
Could u please explain why u red that 9 in 5:34. segment has zero
@TokyoEdTech4 жыл бұрын
???
@sivasurya41494 жыл бұрын
I try to hard but to difficult to understand reverse process....
@quynhnhupham71805 жыл бұрын
why we need to move the end segment to the first segment ??? i do not understand. And Why we it is the index-1 instead of the index?
@rohaannz66064 жыл бұрын
hi, Christian, your tutorials are the best but I don't know why when I eat the food the segment appears in the middle then when i eat the second one it joins my body can you please explain
@cevinsamuel6 жыл бұрын
Mine, when the head once touch the food, the program immediately stops. Why ?
@frankiemilson15405 жыл бұрын
There is no error message for me and I did the same code in the same order as you but when my snake eats the food the the extra segment for the snake spawns in the middle of the screen, please help.
@karls49343 жыл бұрын
how do i add another food object that make the body segment loose 1 segment when consumed.
@TokyoEdTech3 жыл бұрын
Which part do you need help with? Adding another food object, or removing a segment?
@karls49343 жыл бұрын
@@TokyoEdTech removing a segment
@TokyoEdTech3 жыл бұрын
@@karls4934 Thanks - that was an important detail to have left out. Try this: # Move the last segment off the screen segments[-1].goto(1000, 1000) # Remove it from the list segments.remove(segments[-1]) That should do it - I wrote that from memory, so if it doesn't work, let me know.
@ssayorr3 жыл бұрын
@@TokyoEdTech what if he wants to do both like make a new 'posionus' food that makes you lose a segment i guess just make a new turtle and then when it collisions with head you the line you gave? i do not know the code but id guess it is like food
@im_dalton_2974 жыл бұрын
when i get one apple and i go to get the other one as soon as it hits it the programs closes
@im_dalton_2974 жыл бұрын
import os import turtle import time import random delay = 0.1 # Set up screen wn = turtle.Screen() wn.title("Snake game") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) #turns off sceen 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 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 random spot on screen 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].ycore() 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()
@TokyoEdTech4 жыл бұрын
Read the error message - the problem is obvious: Traceback (most recent call last): File "snake.py", line 189, in y = segments[index-1].ycore() AttributeError: 'Turtle' object has no attribute 'ycore' It tells you the EXACT problem and the EXACT line where the problem is.
@im_dalton_2974 жыл бұрын
@@TokyoEdTech i figured it out
@TokyoEdTech4 жыл бұрын
@@im_dalton_297 Good to hear! And, you're welcome.
@ha3la9824 жыл бұрын
The segments appear on the head once the snake eats the food...Please help
@Jiggly124 жыл бұрын
my game doesnt add the segment when i touch the food but adds it when i spawn in and this is the code (i know i havent completed it yet but im trying to work this out first.) import turtle import time import random delay = 0.07 #Screen wn = turtle.Screen() wn.title("Snake Game By Basel Waiel") wn.bgcolor("blue") 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("white") 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) #binds 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() if head.distance(food) < 20: x = random.randint (-290, 290) y = random.randint (-290, 290) food.goto(x, y) #segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") segments.append(new_segment) move() time.sleep(delay) wn.mainloop()
@TokyoEdTech4 жыл бұрын
Check your indentation.
@catsonair94326 жыл бұрын
Hey could you exsplain a little more the line, for element in range(len(segments) -1, 0, -1): and what the whole line means with the numbers at the end a little more in-depth thanks
@AbdullahKhalil-k5v3 ай бұрын
hi my snake is leaving a line behinde it after adding segments
@TokyoEdTech3 ай бұрын
@@AbdullahKhalil-k5v Can you share the code and I'll take a look?
@samimsheikh9624 жыл бұрын
Hey Chris ! I am having no issues so far.... just want to know how to reduce the size of my food by 10 pixels( you mentioed about shape pixels earlier )
@potto14885 жыл бұрын
At xcor I keep getting syntax errors?
@stnldvs5 жыл бұрын
on the line if len(segments) > 0: it says i have a syntax error and highlights the colon
@SaazGamingChannel4 жыл бұрын
Grey segment appears on top of the head segment and is leaving grey trails even when I coded the pen to be up...any fixes?? Would appreciate if you looked through the code :) Snake Game (First Ever Game!) import turtle import time import random delay = 0.125 win = turtle.Screen() win.title("Snake Game by Max Sarris") win.bgcolor("green") win.setup(width=600, height=600) win.tracer(0) #turns off 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 (points) 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 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 collision with food if head.distance(food) < 20: x = random.randint(-290,290) y = random.randint(-290,290) food.goto(x,y) # Add a new 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) win.mainloop()
@TokyoEdTech4 жыл бұрын
This section needs to be indented one more level: # Add a new segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment)
@SaazGamingChannel4 жыл бұрын
@@TokyoEdTech thanks bro, I also added a separate multiplayer scoring system for the second player...was just wondering how you can set an icon for the game
@TokyoEdTech4 жыл бұрын
@@SaazGamingChannel Cool! I think that is OS dependent. If you want to make a standalone program check out pyfreeze or py2exe, or py2app.
@logandukes59844 жыл бұрын
anyone else having a problem with the body just making more turtles as soon as you spawn in and youre essentially just drawing a line with your snake. I checked the code and I really have no idea what is wrong I have my penup on the segment turtle and I am really confused
@logandukes59844 жыл бұрын
@@TokyoEdTech finally figured it out after a long long time of staring at code. my #add segment line of code wasnt indented to only happen if the if statement is true so it was just adding segments every time it moved which resulted in basically a non stop drawing pen
@VAMSIKRISHNA-fs3ff4 жыл бұрын
@Logan May be u r missing pen up for the new segment u r creating
@TokyoEdTech4 жыл бұрын
Yeah, indentation will kill you!
@TokyoEdTech4 жыл бұрын
Glad you figured it out!
@TokyoEdTech4 жыл бұрын
Could be lots of things!
@wzards35296 жыл бұрын
Are you using sublime text? How do you set it up to run python code? I've never been able to run game programs through it.
@ryuujianselmoong67532 жыл бұрын
hi @TokyoEdtech the segmentspart is not working its just in the middle when i press play not when i collect food idk why here is the code: import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.setup(width=600, height=600) wn.bgcolor("black") wn.tracer(0) head = turtle.Turtle() head.speed(0) head.shape("square") head.color("green") head.penup() head.goto(0, 0) head.direction = "stop" food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() segments = [] def go_up(): head.direction = "up" def go_down(): head.direction = "down" def right(): head.direction = "right" def left(): head.direction = "left" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 8) if head.direction == "down": y = head.ycor() head.sety(y - 8) if head.direction == "right": x = head.xcor() head.setx(x + 8) if head.direction == "left": x = head.xcor() head.setx(x - 8) wn.listen() wn.onkeypress(go_up, "Up") wn.onkeypress(go_down, "Down") wn.onkeypress(right, "Right") wn.onkeypress(left, "Left") while True: wn.update() if head.distance(food) < 20: x = random.randint(-290, 290) y = random.randint(-290,290) food.goto(x, y) new_segments = turtle.Turtle() new_segments.speed(0) new_segments.shape("triangle") new_segments.color("green") new_segments.penup() segments.append(new_segments) move() time.sleep(delay) wn.mainloop()
@VedicGemstoneTherapy10275 жыл бұрын
The body of the snake keeps growing even when it doesn't touch the food..plz help....it just keeps on growning long and long rapidly...plz help
@blueskull55295 жыл бұрын
Same
@fieerce98034 жыл бұрын
Hi when i do this code the segment is laready there before i intersect the food.
@SquidWRLD4 жыл бұрын
Hello so it all worked fine but once the head started moving it constantly grew without stopping so i would have a long line from the beginning to the end. How can i fix this and if i need to show you the code how do i do that ( btw great video your like the only python tutorial maker who can explain stuff with out making my brain run at max capacity)
@TauseefKhan-or2yr5 жыл бұрын
no error showing, running perfectly, but i think the penup() function not working , whenever i run the segment drow all over my screen .. please help me.
@TauseefKhan-or2yr5 жыл бұрын
the gray part of the game are drown all over the screen, wherever the head moving ..
@c14aryanpatyal936 жыл бұрын
if we reduce the speed then the segments[0] takes the place of head please help me on resolving this issue
@maxbleakley32415 жыл бұрын
Hi, i don't get an error but whenever i get the food the first time it doesn't add it, but the second time i get the food it adds it on
@darshilchitranshi5924 жыл бұрын
I am getting:- on the line new_segment = turtle.Turtle() ^ TabError : incosistent use of tabs and spaces in indentation PLEASE HELP ME !!!
@TokyoEdTech4 жыл бұрын
Chill. You are mixing tabs and spaces. You can only use one or the other.
@darshilchitranshi5924 жыл бұрын
@@TokyoEdTech Thanks for the reply..... Can you please elaborate and rewrite the error... I am a beginner with very less knowledge about python...
@mayursanghvi54804 жыл бұрын
@@darshilchitranshi592 Try and align that line with the others
@darshilchitranshi5924 жыл бұрын
@@mayursanghvi5480 Thanks..
@mayursanghvi54804 жыл бұрын
@@darshilchitranshi592 you can watch his video on indentation error
@halladaas99154 жыл бұрын
thank you soo much, that was really cool. I don't understand how the body moves when we set the segments to go to the x and y coordinates of the other segments. I mean, won't all the segments and the head be on the same place??
@halladaas99154 жыл бұрын
@@TokyoEdTech oooh makes sense. So if we start all the way from the head to the tail and we set the coordinates of a segment to be that of segment + 1 rather than segment - 1, would the game work properly and function the same way?
@merolekenola21224 жыл бұрын
Hi, thanks for the great tutorial! I was wondering why do you move every single segment instead of only moving the last element to the position of the head when you wanna move the snake forward? Thanks in advance for your explanation :)
@TokyoEdTech4 жыл бұрын
Hiya - that's a great idea - I wish I had thought of it! But, I think for my students, the way I've done it easier to understand.
@tormodsolemslupphaug10433 жыл бұрын
Intiuitively one might think you could just move only the tail element up front, without doing any code looping. But after adding more than one segment, the last element goes up and stays in second place in tow with the head, thus leaving the segment before behind. This process repeats itself for every new segment added, leaving all the previous ones behind. Hope it makes sense...
@diededeur61393 жыл бұрын
is there a function to say if snake goes left the snake body has img... if snake goed right the snake body has img....?
@TokyoEdTech3 жыл бұрын
Something like this will do it - assuming you have a shape named "left.gif" in the same folder. wn.register_shape("left.gif") if head.direction == "left": head.shape("left.gif")
@ravegames10034 жыл бұрын
why does my window close every time i eat the food it works but closes out every time i eat the food
@TheMursk6 жыл бұрын
Another implementation would perhaps be creating individual segments every time the snake moves and having a counter go down for each segment until it's on the tip of the tail and dies next
@gg_lukeee20193 жыл бұрын
it always crashes instead of the snake getting longer please help
@TokyoEdTech3 жыл бұрын
What is the error message? Copy and paste it here along with the code and I'll take a look.
@timeextreme51613 жыл бұрын
@@TokyoEdTech same promblem
@timeextreme51613 жыл бұрын
import turtle import time import random delay = 0.1 # set up the screen wn = turtle.Screen() wn.title("Snake game by @AirZn") wn.bgcolor("black") 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("red") head.penup() head.goto(0,0) head.direction = "stop" # Snake food food = turtle.Turtle() food.speed (0) food.shape("circle") food.color("orange") 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": pass else: 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("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()
@nigelngai32885 жыл бұрын
Hi @Christian Thompson ! I'm really enjoying your videos! I am currently on part 4 of the series and unfortunately I ran into a little bit of a problem. When I try to run, this shows up: Traceback (most recent call last): File "snake_game1.py", line 83, in new_segment.speed(0) File "C:\AppData\Local\Programs\Python\Python37-32\lib\turtle.py", line 2167, in speed return self._speed AttributeError: 'int' object has no attribute '_speed' What should I do? My code: From line 82-86 new_segment = turtle.Turtle new_segment.speed(0) new_segment.color("grey") new_segment.penup() segments.append(new_segment) Thanks again for the videos :)
@nigelngai32885 жыл бұрын
@@TokyoEdTech OMG!!! I swear I was constantly comparing your code with mine and yet I still missed this. I am blind 🤣 !! Thanks it works now! You've just earned yourself a subscriber
@qwretyredty45495 жыл бұрын
why my body of my snake is not following the head its stay on the spot where the food eaten
@alvitocabral4 жыл бұрын
Hi Christian - the code all works fine & the snake grows loner on consumption of the food. But the speed gets progressively slower. While inthe beginning its 0.1 delay and it moves really fast, the speed of the snake moving is super slow after like 10-15 additions to the body.. Does that happen to you as well.
@TokyoEdTech4 жыл бұрын
It does slow a bit, but it shouldn't be that much.
@karismanabil4 жыл бұрын
hello, good explanation you have. But I keep have trouble here in x = random.randint(-290,290) y = random.randint(-290,290) and the errors says undefined variabel 'random' . can you help me?
@karismanabil4 жыл бұрын
oof, sorry, i just figure it out that i forgot to type import random
@karismanabil4 жыл бұрын
@@TokyoEdTech thank you. btw, how to add a border on the screen? since when i maximize my window screen the bg color is goes all of the screen and i can't see the wn.setup border. so i want to add a color at the border.
@mcreallyfunky3 жыл бұрын
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)
@TokyoEdTech3 жыл бұрын
Adding the grape is pretty trivial - just copy the apple code and rename it (but a loop would work better). Adding two segments is a bit tricky as you have to add a segment, move, then add the next segment.
@mcreallyfunky3 жыл бұрын
@@TokyoEdTech Thanks for the quick reply! The advice is greatly appreciated.
@TokyoEdTech3 жыл бұрын
No problem - keep on codin'!
@shashankk_tyagi6 жыл бұрын
thank you so much buddy! helped a lot :)
@baha45352 жыл бұрын
I have an error its this : unident does not match any outer indentation level Code: import turtle import time import random delay = 0.1 #Set up the screen wn = turtle.Screen() wn.title("Snake Game by Baha") 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 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()
@TokyoEdTech2 жыл бұрын
The error message tells you everything ... Make sure you have the same number of spaces for your indentation levels.
@biancaandreeas2 жыл бұрын
Hi ! My code is working just fine but the only problem I have is that the snake starts from the beginning with a segment, is not starting only with the head. So the snake always has a segment more than it should... But the code starts from an empty list, I don't understand why this is happening...
@TokyoEdTech2 жыл бұрын
Hi. If you share your code I can take a look.
@tylercantrell77955 жыл бұрын
I made the game smoother by making the delay 0.0075 and doing this: if head.direction == "up": y = head.ycor() head.sety(y + 1(instead of 20)) if head.direction == "down": y = head.ycor() head.sety(y - 1(instead of 20)) if head.direction == "right": x = head.xcor() head.setx(x + 1(instead of 20)) if head.direction == "left": x = head.xcor() head.setx(x - 1(instead of 20)) If I set everything back to normal delay and change the 1 to 20, it works fine, but with this code the new sections of the snake appear at the position of the head, not behind it. Any help is appreciated! Or is there a way to make it smoother without changing those values?
@tylercantrell77955 жыл бұрын
@@TokyoEdTech Thank you! I'll try it
@Anniecao04305 жыл бұрын
I know im late but my grey part is super long like the longer it moves the longer it bevomes
@mea6646186 жыл бұрын
Nice work man, keep up i tried a shorter code to move body segment and it works first i added 'head' to 'segments' list as a first item # Snake body segments = [ ] segments.append(head) then in while loop i added the following code # move the segments for i in range(len(segments)-1, 0, -1): x = segments[i-1].xcor() y = segments[i-1].ycor() segments[i].goto(x, y)
@Gladionuh3 жыл бұрын
When I eat the first apple the application crashes
@TokyoEdTech3 жыл бұрын
There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
@apurvapragya5534 жыл бұрын
I program in IDLE In it the attachment overlaps to the head everytime I run it Please give solution to it
@shivangsingh38564 жыл бұрын
Which algorithm you are using ?
@penultimania42953 жыл бұрын
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)
@TokyoEdTech3 жыл бұрын
Yeah. It takes a lot of time and experimentation. I've been coding off and on for decades and it took time to come up with this.
@mariajreijiri32163 жыл бұрын
Did you remember to put the functions video in the description ?
@TokyoEdTech3 жыл бұрын
I just did - thanks!
@Donplays_114 жыл бұрын
I'm at 3:05 pastebin.com/Nn7Afqwe Traceback (most recent call last): File "/Volumes/LUIGI/Snakeee.py", line 96, in segments.append(new_segment) NameError: name 'segments' is not defined
@TokyoEdTech4 жыл бұрын
Watch carefully. You are missing : segments = []
@Donplays_114 жыл бұрын
@@TokyoEdTech omg thank you so much! I can't believe I did not see that!
@TokyoEdTech4 жыл бұрын
Happens to the best of us!
@ariharbas67954 жыл бұрын
@@TokyoEdTech is it segments = [] append (new_segment) or segment = [append] (new_segment)
@victorbrorson66055 жыл бұрын
Great tutorial! The code works, and i get no error message. Still, when i run the program, a grey line appears between where the snake head collided with the food and the new segment. This results in many grey lines around the screen after having picked up a few pieces of food. What to do ? :))
@victorbrorson66055 жыл бұрын
@@TokyoEdTech Ahh, perfect, that solved it. Forgot parenthesis after .penup in the new segment code :))) Thx!
@gitanshkumar41135 жыл бұрын
great explaining!! But when I was trying it to add segment to head the first part is going on top of the head, please explain, i wrote the same cod as u did
@haridaspm85745 жыл бұрын
Name segment is not defining
@bronder10924 жыл бұрын
I ran into the same issue. All I did was move the "move()" function to the bottom of the code, including using (window.tracer(0) at the top of the code, including (time.sleep(0.1) you pick the number), that should fix your problem, fixed mine at least. Have a great day!