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.
@idkhow96035 жыл бұрын
this is the best tutorial i have seen so far. Thanks so much
@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.
@SenseiEli4 жыл бұрын
Wrote all till now works perfect! Next I will build my own game! Thank you!
@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. :)
@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.
@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'!
@KabeerSehgal016 жыл бұрын
You can explain super good! :)
@cartoonpage96964 жыл бұрын
This is the best tutorial in KZbin I ever seen..
@TokyoEdTech4 жыл бұрын
Thanks!
@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!
@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!
@vaishalirawal91404 жыл бұрын
You are a very good teacher... Love from India🇮🇳🇮🇳
@TokyoEdTech4 жыл бұрын
Thank you!
@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’!
@darkknight-r4g2 ай бұрын
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()
@TokyoEdTech2 ай бұрын
@@darkknight-r4g The new segment section needs to be indented one more level.
@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
@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!
@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?
@LaMalo175 күн бұрын
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?
@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?
@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?
@minyminnos55184 жыл бұрын
SIR I AM SO THANKFUL FOR THIS VIDEO. IT HELPS MY SCHOOL PROJECT. LOVE YA
@葉孟韋-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?
@leonmeister80376 жыл бұрын
Just a reminder that you wanted to link your Python 3 basics in the comments!
@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?
@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
@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?
@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.
@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?
@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.
@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.
@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
@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
@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'!
@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.
@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
@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’!
@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
4 жыл бұрын
next step add snake in neural network to teach play by it self ! please make tutoral
@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!
@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()
@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
@qwretyredty45495 жыл бұрын
why my body of my snake is not following the head its stay on the spot where the food eaten
@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'!
@questionmarkquestionmarkques3 жыл бұрын
you are a fantastic teacher, thanks so much for this series
@TokyoEdTech3 жыл бұрын
Thanks - keep on codin'!
@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?
@ha3la9824 жыл бұрын
The segments appear on the head once the snake eats the food...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
@ravegames10034 жыл бұрын
why does my window close every time i eat the food it works but closes out every time i eat the food
@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.
@stnldvs5 жыл бұрын
on the line if len(segments) > 0: it says i have a syntax error and highlights the colon
@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.
@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.
@fieerce98034 жыл бұрын
Hi when i do this code the segment is laready there before i intersect the 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.
@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!
@pallepuaruna69704 жыл бұрын
This is the best thing I have ever seen
@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 ..
@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'!
@apurvapragya5534 жыл бұрын
I program in IDLE In it the attachment overlaps to the head everytime I run it Please give solution to it
@potto14885 жыл бұрын
At xcor I keep getting syntax errors?
@shashankk_tyagi6 жыл бұрын
thank you so much buddy! helped a lot :)
@cevinsamuel6 жыл бұрын
Mine, when the head once touch the food, the program immediately stops. Why ?
@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
@akashdinga25374 жыл бұрын
My segment is not attaching to the head I have verified my code with ur code it's same but still it's not getting attached plzz help me
@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
@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!
@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
@oster40835 жыл бұрын
i wached the video and i cant report an error message because there isint one. it is like penup is on but its not. the gray line is made constantly and i cant find the error but python dose not think its an error because it runs but it runs wrong i know im late but please please help this is my first coding project and it would be epic if it worked thanks nevermind you need to put 8 spaces in front of each line in the # New Segments place to anyone having this problem
@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
@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
@c14aryanpatyal936 жыл бұрын
if we reduce the speed then the segments[0] takes the place of head please help me on resolving this issue
@cynthiazxm4 жыл бұрын
for me, when I type: for index in range(len(segments)-1,0 -1): x = segments[index-1].xcor() y = segments[index-1].ycor segments[index].goto(x, y) it says "TypeError: unsupported operand type(s) for +: 'method' and 'float'" Can you please help?
@TokyoEdTech4 жыл бұрын
You are missing the parentheses after ycor
@amirkia23784 жыл бұрын
I barely get this part but it works it just does. Thanks. for index in range(len(Bodies)-1, 0, -1): x = Bodies[index-1].xcor() y = Bodies[index-1].ycor() Bodies[index].goto(x, y) if len(Bodies) > 0: x = Head.xcor() y = Head.ycor() Bodies[0].goto(x, y)
@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")
@Anniecao04305 жыл бұрын
I know im late but my grey part is super long like the longer it moves the longer it bevomes
@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.
@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.
@tomaslofgren85344 жыл бұрын
I wrote: if len(segments) > 0: and then the : was a synatx error, can someone please help me
@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
@mcreallyfunky4 жыл бұрын
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)
@TokyoEdTech4 жыл бұрын
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.
@mcreallyfunky4 жыл бұрын
@@TokyoEdTech Thanks for the quick reply! The advice is greatly appreciated.
@TokyoEdTech4 жыл бұрын
No problem - keep on codin'!
@NeoVTM4 жыл бұрын
i have some thought about making the head is a triangle,but when i tried,the edge's wont change as the direction i use to move the head,is there anythings that i should add or change to make things work?,or it just cant be done like that?
@TokyoEdTech4 жыл бұрын
Sure you need to also do: head.setheading(x) x should be: 0 for right 90 for up 180 for left 270 for down
@esson99715 жыл бұрын
when i add segments.append(new_segment) , and i test it, but whenever the snake head touch the apple, it says Traceback (most recent call last): File "C:\Users\RD\Desktop\snake game.py", line 107, in segments.append(new_segment) AttributeError: 'tuple' object has no attribute 'append'
@Bubble_Kayl3614 күн бұрын
it appears but wont follow (do you need the whole code or is this enough?) #add 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 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 head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x , y) move() time.sleep(delay) wn.mainloop()
@TokyoEdTech14 күн бұрын
@Bubble_Kayl36 Hi. Yes, could you provide all the code so I can test it?
@Bubble_Kayl3610 күн бұрын
@@TokyoEdTech #snake import turtle import time import random delay = 0.1 #set up screen wn = turtle.Screen() wn.title("Snake Game by Bubble") wn.bgcolor("green") 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.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) #body 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) #key 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 collision with food if head.distance(food) < 20: #move food to random spot X = random.randint(-290,290) Y = random.randint(-290,290) food.goto(X , Y) #add 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 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 head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() time.sleep(delay) wn.mainloop() sorry for the wait, youtube didn't give me a notification
@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....
@warvan52276 жыл бұрын
i got an error message can someone help me? for index in range(len(segments)) - 1, 0, -1: TypeError: 'list' object cannot be interpreted as an integer
@warvan52276 жыл бұрын
@@TokyoEdTech I put one parenthesis in the wrong spot *facepalm*
@anekhapallamreddy90895 жыл бұрын
Hi! This video is amazing, but I have a problem. When my head collides with the food, it will add a segment that follows it. However, if I try to turn, the head will turn and keep going, but the body segment will stop. I did my code a bit differently than you, but I still don't know why there is a problem. I was wondering if you could help me. def grow_snake(): global pointer_snake global pointer_apple global segments if pointer_snake.distance(pointer_apple)
@anekhapallamreddy90895 жыл бұрын
Also, my list is segments = [pointer_snake] pointer_snake is my head, and pointer_apple is my food
@anekhapallamreddy90895 жыл бұрын
@@TokyoEdTech import turtle import random right = 0 left = 0 up = 0 down = 0 def draw_square(x,y,length): global pointer_snake pointer_snake.speed(0) pointer_snake.hideturtle pointer_snake.up() pointer_snake.goto(x,y) pointer_snake.begin_fill() pointer_snake.down() pointer_snake.goto(x+length, y) pointer_snake.goto(x+length, y+length) pointer_snake.goto(x, y+length) pointer_snake.goto(x, y) pointer_snake.up() pointer_snake.end_fill() def draw_apple(): apple_x = random.randrange(1,11) apple_y = random.randrange(1,11) i = apple_x - 0.5 j = apple_y - 0.5 pointer_apple.goto(i,j) def grow_snake(): global pointer_snake global pointer_apple global segments if pointer_snake.distance(pointer_apple)0: # segments[0].goto(pointer_snake.xcor(),pointer_snake.ycor()) screen.ontimer(grow_snake,200) def snake_move_right(): global right,left,up,down right = 1 left = 0 up = 0 down = 0 while right == 1: while 0
@anekhapallamreddy90895 жыл бұрын
@@TokyoEdTech pastebin.com/DkvVQvj3 sorry! I didn't know :3
@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?
@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)
@danielrodrigo16344 жыл бұрын
my squares don't add to the back of the snake, it just stays in one position. here's my code: import turtle import time import random delay = 0.1 #set up the screen wn = turtle.Screen() wn.title("Snake Game") 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("yellow") 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.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 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) food.goto(x, y) #add a segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("red") 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)
@TokyoEdTech4 жыл бұрын
You have an indentation error. Try unindenting this section: #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)
@danielrodrigo16344 жыл бұрын
@@TokyoEdTech that worked! thanks so much
@Tottiten4 жыл бұрын
if you run the program you can see the the snake doesn't get longer when it eats the food. Thanks import turtle import time import random delay = 0.1 # Screen wn = turtle.Screen() wn.title("Snake Game") wn.bgcolor("gold") wn.setup(width=600, height=600) wn.tracer(0) # no screen updates # Snake head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop" segments = [] # Food food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) # Funtions 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) # Keys/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() # Food collision 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") new_segment.penup() segments.append(new_segment) # End segments 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 head if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() time.sleep(delay) wn.mainloop()
@Tottiten4 жыл бұрын
@@TokyoEdTech pastebin.com/BvfA4KX2
@Tottiten4 жыл бұрын
@@TokyoEdTech thanks 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.
@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 )
@mateo87234 жыл бұрын
for some reason the tail keeps going on so pretty much ive made a drawing game can you tell e whats wrong? import turtle import time import random delay = 0.1 # Set up the screen wn = turtle.Screen() wn.title("Snake boi") 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 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) # 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" #keyborad 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 collison 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 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)
@TokyoEdTech4 жыл бұрын
Check your indentation - you only want to grow the tail if you've eaten the food.
@mateo87234 жыл бұрын
ok thanks
@TokyoEdTech4 жыл бұрын
@@mateo8723 You're welcome.
@ex1ct62 жыл бұрын
hi can you help me, when i start the game my snake body is not finishing and just goes after head import turtle import time import random delay = 0.1 pitto = turtle.Screen() pitto.title("Snake Game") pitto.bgcolor("lime") pitto.setup(width=800, height=800) pitto.tracer(0) head = turtle.Turtle() head.speed(0) head.shape("circle") head.color("green") head.penup() head.goto(0,0) head.direction = "stop" food = turtle.Turtle() food.speed(0) food.shape("triangle") food.color("red") food.penup() food.goto(0,120) segments = [] 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) def go_up(): head.direction = "up" def go_down(): head.direction = "down" def go_left(): head.direction = "left" def go_right(): head.direction = "right" pitto.listen() pitto.onkeypress(go_up, "w") pitto.onkeypress(go_down, "s") pitto.onkeypress(go_left, "a") pitto.onkeypress(go_right, "d") while True: pitto.update() new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("forestgreen") 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) if head.distance(food) < 20: x = random.randint(-290,290) y = random.randint(-290,290) food.goto(x, y) move() time.sleep(delay) pitto.mainloop()
@TokyoEdTech2 жыл бұрын
Your code is out of order and indented incorrectly: import turtle import time import random delay = 0.1 pitto = turtle.Screen() pitto.title("Snake Game") pitto.bgcolor("lime") pitto.setup(width=800, height=800) pitto.tracer(0) head = turtle.Turtle() head.speed(0) head.shape("circle") head.color("green") head.penup() head.goto(0,0) head.direction = "stop" food = turtle.Turtle() food.speed(0) food.shape("triangle") food.color("red") food.penup() food.goto(0,120) segments = [] 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) def go_up(): head.direction = "up" def go_down(): head.direction = "down" def go_left(): head.direction = "left" def go_right(): head.direction = "right" pitto.listen() pitto.onkeypress(go_up, "w") pitto.onkeypress(go_down, "s") pitto.onkeypress(go_left, "a") pitto.onkeypress(go_right, "d") while True: pitto.update() 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) 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("forestgreen") new_segment.penup() segments.append(new_segment) move() time.sleep(delay) pitto.mainloop()
@albinh75 жыл бұрын
after eating the first food the first body part spawns in the middle then when I eat another it syncs up with the head?
@albinh75 жыл бұрын
@@TokyoEdTech Tried to see what the problem was but didnt find it, so i copied the link in the description, deleted the code that I didnt have in my first code and continued following your tutorial
@albinh75 жыл бұрын
@@TokyoEdTech And thanks for the awesome tutorial!
@shivangsingh38564 жыл бұрын
Which algorithm you are using ?
@prathamaggarwal78665 жыл бұрын
Best of your efforts bro
@WaffleEnjoyer2 жыл бұрын
hi i have a problem where whenever the snake touches food the game crashes. Do you think you could help me? heres the code: #snake game import turtle import time import random delay = 0.1 # set up the screen wn = turtle.Screen() wn.title("Snake Game") 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("square") food.color("red") food.penup() food.goto(0,100) food.direction = "stop" segnents = [] # 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 position x = random.randint(-300, 300) y = random.randint(-300, 300) 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()
@WaffleEnjoyer2 жыл бұрын
it also says that "segments" is not defined here is the code: #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)
@TokyoEdTech2 жыл бұрын
Earlier in the program you should have a line like: segments = [] Check your spelling.