YOU'RE A DAMN GOOD TEACHER SIR 😁. Thank u for your service!
@ridwanhoque22234 жыл бұрын
No one can teach programming better than you...you are an awesome teacher...plz contine to make more videos on python... I am a newbie...hope to learn more from you in the upcoming videos...
@richardtanner53435 жыл бұрын
I was having the same issue where the game would stop whenever I ate a piece of food. I figured it was due to the segment being within that 20 range. I changed my distance to less than 10 and it resolved for me
@jeanjoubert105 жыл бұрын
Thanks
@shashankyh33665 жыл бұрын
Thanks bro...........
@lucagentile78415 жыл бұрын
an other way to solve can be writing the first "for" in the body collision as "for segment in segments[1:]:", the first segment won't be considered anymore
@laurensvanduin6564 жыл бұрын
@@lucagentile7841 I have tried both and it didn't work, do you guys happen to have any other suggestions? when i do "for segment in segments[1:]:" i get one body segment but now it shuts down when i try to get the second piece of food.
@goofyworld54103 ай бұрын
you are one of the best python tubers you explained clearlyu and went straight to the point subbed! :D
@TokyoEdTech3 ай бұрын
@@goofyworld5410 Cheers - keep on codin'!
@beitexue24904 жыл бұрын
I get an idea from your part7 Video to hide the turtle wenn it collision happens. for s in segments: s.hideturtle() yeah, it doesn't delete the Object, but i found it a little bit eleganter to hide the Turtle than set it off the screen. I always think, it's much more important to know how to teach than the Knowledge the Teacher has. You are a very good example of that, i really appreciate it ! I have learnd you Pong Game Turorial, after this Snake Game, i am going to learn your next Space Invaders Game :)
@HRBalak3 жыл бұрын
Stay tuned ! man. People gonna love your teaching skill very soon . 👍 Attitude should != negative .
@TokyoEdTech3 жыл бұрын
I hope so thanks!
@twodays80414 жыл бұрын
was about to comment that I can no longer move and don't know why, but as I typed it went "HEY I CANT... OH MY CAPS LOCK IS ON, NVM"
@amirkia23784 жыл бұрын
@@TokyoEdTech is their a way to do it even with caps if yes could you tell us.
@DanielPlayzOfficial4 жыл бұрын
i found a way to add multiple snake food. Just copy and paste the making of the snake food and change the variable and the position and remember to make it that a new segment will be added by just duplicating the new_segment
@TokyoEdTech4 жыл бұрын
Thanks for sharing!
@kaypbalk60273 жыл бұрын
How did you do? Because I am working on my "cheat mode". I need help
@timo12945 жыл бұрын
when i touch food i die instantly . i guess it thinks i collided with the new segment but i dont know how to fix it
@akshitjain76664 жыл бұрын
Hey TokyoEdTech! hi... hope u are doing all well I am really impressed with your teaching skills and the way you used your logics in building games I am new in this coding field... I just got a doubt in that code In 'for segment in segments' line can't we use directly 'if segments.distance(head)
@TokyoEdTech4 жыл бұрын
Hey, thanks! To answer your question, you have to check each segment individually - you can't check them all at once as a group, hence for segment in segments
@akshitjain76664 жыл бұрын
@@TokyoEdTech ooh yeah... got it... Thank you!
@TokyoEdTech4 жыл бұрын
You're welcome.
@SykesBFC6 жыл бұрын
at the head.direction up/down part it keeps saying invalid syntax 4 me what is this
@DGMDevGamingAndMore4 жыл бұрын
how do i create a main menu, and different difficulty levels. i think the difficulty levels wont be a problem tho...
@TokyoEdTech4 жыл бұрын
This will help you get started - the same concept can be applied to making a menu screen. kzbin.info/www/bejne/jZyWaKZ5jZqrirs
@commeca86264 жыл бұрын
Every time I add the body_segment collusion coding, like the whole for segment in segments: if segment.distance(head) < 20: head.goto(0,0) and the rest, when I run it, the snake game is frozen. No syntax errors or indentations. Please help.
@darali5686 жыл бұрын
very helpful please keep uploading !
@alvitocabral4 жыл бұрын
If anyone is facing an issue when when you eat the food, the game resets and head gets sent back to (0,0), then this might help you out. Add in a print function as "print(segment.distance(head))" after "for segment in segments:" - When I did this it told me that the distance was 10 (instead of 20 like it's supposed to be... not sure why). So then I simply update the code to be a value
@TokyoEdTech4 жыл бұрын
Glad you got it sorted!
@coolnannybros19244 жыл бұрын
I tried this and it is not working it prints 100, 0, or 20 and i tried all of them and i still can't get it to work
@alvitocabral4 жыл бұрын
@@coolnannybros1924 Can you paste in all your code here? Maybe I can check it out
@billmcphil4 жыл бұрын
@@alvitocabral Thanks a ton man! I was really getting stumped on that
@isabell97033 жыл бұрын
I did what you showed me in the vid, but the square wont move and nothing will move.
@TokyoEdTech3 жыл бұрын
There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
@toilet_shark_24366 жыл бұрын
thank you for the very educational videos but I have a problemwhen I have these lines of code and these lines only my body doesn't appear when I get foodfor 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) segments.clearif you could help that would be great
@toilet_shark_24366 жыл бұрын
here is the code. I didn't know if you wanted the whole code or just the problematic section but the section that breaks the code is lines 116 to line 128 pastebin.com/eCX4cE8J. thanks again for helping. ps. there is no major error message
@toilet_shark_24366 жыл бұрын
thanks
@mavenzeinacinematography66435 жыл бұрын
awesome tutorials its helping me a lot !
@sakman27cap454 жыл бұрын
I have a question, it was working perfectly, but suddenly it started saying syntax error for almost everything in the mainloop :( :-( 😭
@yukselbey9135 жыл бұрын
Thanks for the Tutorials. I am wondering why we start to check for head collision with body segments after the move function. how can we change the background, add a picture, or sound? Again thanks for the tutorials.
@YoungPotassium5 жыл бұрын
Hi! I found an issue where the snake would reset when when it picked up the food it reset no matter the distance I set it to, would this be a problem with the programing its self or the computer?
@YoungPotassium5 жыл бұрын
Ok, I checked the collisions again and rewatched the video a few times to make sure I wasn't missing something but I'm still stumped. Here's what I got. pastebin.com/TzSWRTEJ (Note I set the distance to
@nadaghanem46273 жыл бұрын
Thanks. I don't know how you managed to make body collision works. I tried it but it always clears the segments without a collision. The reason is that the first segment is already attached to the body so we need to check the collision from the second segment till the end.
@TokyoEdTech3 жыл бұрын
I don't know how to explain it any better than I do in the video. If you share your code, I can take a look.
@exotic.53683 жыл бұрын
I don’t know what’s going on but when ever I copied and pasted the code my snake would not move
@TokyoEdTech3 жыл бұрын
There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
@MH-yc5pr5 жыл бұрын
My python code does not recognize the term 'distance' Is it maybe because my version is a bit outdated and if so is there another word I can use to replace the term. Otherwise everything else works awesome! Thank you!
@sriram-cj9pv5 жыл бұрын
It says that segments(the list) does not have an attribute "length".
@vadymcherniavskyi93374 жыл бұрын
I think that instead of not letting a player go in the opposite direction it's better to add this simple if else statement: def go_up(): if head.direction == "down" and len(segments) > 0: t.sleep(1) head.goto(0, 0) head.direction = "stop" for segment in segments: segment.goto(1000, 1000) segments.clear() else: head.direction = "up" so that if there are segments and you go to the opposite direction it just ends the game, because you've run into your body
@Rwko.4 жыл бұрын
This is what happens anyways if you don't add the: if head.direction != "down": head.direction = "up" look at 3:27
@kevilay4 жыл бұрын
I would like to understand how the segment clearing part being after the move doesn't cause a conflict. Because in your move() you place segment 0 ontop of the head. Then check for segments
@TokyoEdTech4 жыл бұрын
As far as I know, you move the segments, then the head, then check for collisions. Check the order again to be sure.
@johnbrowning10714 жыл бұрын
When i add a segment the snake resets because the segment goes ontop of the head, how do i fix this?
@TokyoEdTech4 жыл бұрын
Check the order of the code. Move the body. Then move the head. Then check for a collision.
@laddufn3 жыл бұрын
Hey! So i Did all the steps that you showed but when i collide with my body, it just puts me to start. it does not restart score or does not reset the body. Please Help!
@TokyoEdTech3 жыл бұрын
Sure thing. There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
@AyomideOyebode-d1t5 ай бұрын
hello i love your videos i will love it if you reply I am a huge fan i and followed you step by step And it worked
@TokyoEdTech5 ай бұрын
@@AyomideOyebode-d1t Thanks - glad to hear it! Keep on codin'!
@AyomideOyebode-d1t5 ай бұрын
@@TokyoEdTech how you are such a great KZbinr.(the best)
@divyabiju_4 жыл бұрын
what does the factorial do? "head.direction != "down":" its not working for me. it says parse error, bad input, how can I fix it?
@divyabiju_4 жыл бұрын
@@TokyoEdTech I got it to work! My error, thank you!
@divyabiju_4 жыл бұрын
@@TokyoEdTech I indented the whole section wrong, when you said, check for indentation in the video, I ignored it by accident, oops, I learned my lesson! Thank you!
@divyabiju_4 жыл бұрын
@@TokyoEdTech Thank you!
@dudebright98314 жыл бұрын
It really helped me i manag d to make a game using your tutorial
@TokyoEdTech4 жыл бұрын
Great to hear!
@programmingwithguri14544 жыл бұрын
How can I show the total score on the screen=Number of segments
@coolnannybros19244 жыл бұрын
whenever I eat the food it resets me and I can't figure out why, can anyone help?
@coolnannybros19244 жыл бұрын
@TokyoEdTech
@TokyoEdTech4 жыл бұрын
I’m happy to help, but I need to see the actual code. Please share your code via pastebin.com link so I can download and test it. To learn how, please watch this video and then get back to me: kzbin.info/www/bejne/gmekqImrqpuCabs Keep on codin’!
@TokyoEdTech4 жыл бұрын
See above.
@mysterypedia68093 жыл бұрын
how to fix: Traceback (most recent call last): File "C:\Users\admin\Desktop ew game.py", line 80, in wn.update()
@TokyoEdTech3 жыл бұрын
I'm gonna need all the code and the full error message.
@tenforward74853 жыл бұрын
my segment 0 is going straight to the head instead of going behind it this causes the 'head collision on body' to not work because the first segment is always touching the head pls help
@TokyoEdTech3 жыл бұрын
Hiya - make sure the order of the code is correct (as shown in the video). If it is, feel free to post the code here and I'll take a look.
@whitedragon3652 жыл бұрын
i had the same problem and it's becasue your move() function is not in the right position it must be after the attaching the body segments but i've not understand why yet
@firasjmii9173 жыл бұрын
so instead of dying when head hits the border i did this #when head hits the border it reappears from the other side if head.xcor()290 or head.ycor()290 : if head.xcor()>=290 or head.xcor()=290 or head.ycor()
@TokyoEdTech3 жыл бұрын
The head has moved outside the border area and is switching back and forth. Instead of -head.xcor() use the actual number such as -290.
@abubakrswanshali47994 жыл бұрын
just curious what does != do?
@TokyoEdTech4 жыл бұрын
It means "not equal".
@CR7SIU2012 Жыл бұрын
i need urgent help I'm using pycharm/pygame and the problem is it keeps kicking me out the the game when my body gets its second segment. Could u pls help? Heres my code: import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("Snake") wn.bgcolor("green") wn.setup(width = 600, height = 600) wn.tracer(0) head = turtle.Turtle() head.speed(0) head.shape("square") head.color("blue") head.penup() head.goto(0, 0) head.direction = "stop" food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0, 100) segments = [] 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) wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") while True: wn.update() if head.xcor()>290 or head.xcor()290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() 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) segments.clear() time.sleep(delay) wn.mainloop()
@CR7SIU2012 Жыл бұрын
Nvm i just had a HUUGEEE typo
@TokyoEdTech Жыл бұрын
@@CR7SIU2012 Glad you got it sorted - keep on codin'!
@RE-ig2sn4 жыл бұрын
Hi, I need help. Tried everything but couldn't find my mistake. Why is my snake body overlapping? import turtle import random import time backdrop=turtle.Screen() backdrop.setup(width=520, height=520) backdrop.bgcolor("black") backdrop.tracer(0) dely=0.005 bunit=turtle.Turtle() bunit.speed(0) bunit.color(random.randint(0,455),random.randint(0,455),random.randint(0,455)) bunit.shape('square') bunit.penup() bunit.goto(0,0) bunit.direction="stop" #snake body sb=[] food=turtle.Turtle() food.color(random.randint(0,455),random.randint(0,455),random.randint(0,455)) food.shape('square') food.penup() food.goto() def up(): bunit.direction='up' def down(): bunit.direction='down' def right(): bunit.direction='right' def left(): bunit.direction='left' def movement(): if bunit.direction=='left': x=bunit.xcor() # using y coordinites bunit.setx(x-2) if bunit.direction=='right': x=bunit.xcor() # using y coordinites bunit.setx(x+2) if bunit.direction=='down': y=bunit.ycor() # using y coordinites bunit.sety(y-2) if bunit.direction=='up': y=bunit.ycor() # using y coordinites bunit.sety(y+2) backdrop.onkey(up,"UP") backdrop.onkey(down,"DOWN") backdrop.onkey(right,"RIGHT") backdrop.onkey(left,"LEFT") f=turtle.Turtle() f.hideturtle() f.penup() points=0 backdrop.listen() while True: backdrop.update() # too see snake time.sleep(dely) movement() #food colision if bunit.distance(food)0: sb[0].goto(bunit.xcor(),bunit.ycor()) if bunit.xcor()250: print("You lost the competition") print(points) backdrop.bgcolor("red") food.goto(9999999,9999999) f.goto(-150,0) f.pendown() f.write("You Lost!", font=("Ariel",60)) if bunit.ycor()250: print("You lost the competition") backdrop.bgcolor("red") print(points) food.goto(9999999,9999999) f.goto(-150,0) f.pendown() f.write("You Lost!", font=("Ariel",60)) backdrop.mainlooop()
@TokyoEdTech4 жыл бұрын
This code doesn't run at all - which problem are you trying to fix? Which error? Which line?
@RE-ig2sn4 жыл бұрын
@@TokyoEdTech Thank you for responding. I used Trinket.io IDE , here is the link: trinket.io/python/dc31127ce9. Code is working, with no errors, but the snake body segments keep overlapping.
@TokyoEdTech4 жыл бұрын
@@RE-ig2sn It's really sensitive to the number of pixels you move. You changed it to 2. It should be 20.
@RE-ig2sn4 жыл бұрын
@@TokyoEdTech Thanks!
@alexmagno86414 жыл бұрын
wich line did you create the "segment" ?
@TokyoEdTech4 жыл бұрын
Watch the video again, or check the code in github.
@ravegames10034 жыл бұрын
if segment.distance(head) < 20: NameError: name 'segment' is not defined
@divakarbhatt19555 жыл бұрын
thanks sir it helped me a lot😊😊😊😊😊😊😊😊😊😊😊
@netoorta77414 жыл бұрын
I HAVE RESOLVED THIS ERROR: for index in range(len(lista)-1, 0, -1): TypeError: object of type 'Turtle' has no len() so it says that one object of turtle dont have len, so the for cycle cant be done, I created an i variable to change in segment: // lista = segments; cabeza = head; // for i in lista: if i.distance(cabeza) < 20: time.sleep(1) cabeza.goto(0, 0) cabeza.direction = "stop" for lista in lista: lista.goto(1000, 1000) lista = [] hope it helps
@0108ben4 жыл бұрын
please help me it keeps coming up with 'Turtle' is not an itterable object import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("snake by Ben") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) #turtle head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop" # FOOOD food = turtle.Turtle() food.speed(0) food.shape("triangle") food.color("red") food.penup() food.goto(0,100) body = [] #head move 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) #keybinds wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") #game loop while True: wn.update() #border death if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290 : time.sleep(1) head.goto(0, 0) head.direction = "stop" #hide segments after death for body in body: body.goto(1000,1000) #clear the segments body = [] #food controls if head.distance(food) < 20: x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x,y) #body parts new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("black") new_segment.penup() body.append(new_segment) #move body for index in range(len(body)-1, 0, -1): x = body[index-1].xcor() y = body[index-1].ycor() body[index].goto(x, y) #move body to head if len(body) > 0: x = head.xcor() y = head.ycor() body[0].goto(x, y) #check for head collision with body for body in body: if body.distance(head) < 20: time.sleep(1) head.goto(0, 0) head.Direction = "stop" # hide segments after death for body in body: body.goto(1000, 1000) # clear the segments body = [] move() time.sleep(delay) wn.mainloop()
@TokyoEdTech4 жыл бұрын
You have for body in body. Doesn't make sense.
@0108ben4 жыл бұрын
i have changed the code to look like yours but now whenever i pick up the second piece of food it just resets import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("snake by Ben") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) #turtle head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop" # FOOOD food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) segments = [] #head move 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) #keybinds wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") #game loop while True: wn.update() #border death if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290 : time.sleep(1) head.goto(0, 0) head.direction = "stop" #hide segments after death for segment in segments: segment.goto(1000,1000) #clear the segments segments = [] #food controls if head.distance(food) < 20: x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x,y) #body parts new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("black") new_segment.penup() segments.append(new_segment) #move body 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 body to head if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) #check for head collision with body for segment in segments: if segment.distance(head) < 8: time.sleep(1) head.goto(0,0) head.direction = "stop" for segment in segments: segment.goto(1000,1000) segments = [] move() time.sleep(delay)
@rayzin93454 жыл бұрын
sir there is a problem i keep getting this syntax error can you tell the problem import turtle import time import random delay = 0.1 #Set up the screen wn = turtle.Screen() wn.title("Snake game by @zineddine8061") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) #Snake head head = turtle.Turtle() head.speed(0) head.shape("circle") head.color("red") head.penup() head.goto(0,0) head.direction ="stop" #mouse food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("grey") 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 border if head.xcor()>290 or head.xcor()290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() #Check for head collision with the body segments for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0,0) head direction = "stop" time.sleep(delay) wn.mainloop()
@TokyoEdTech4 жыл бұрын
The error tells you EXACTLY where the problem is: head direction = "stop" Look at EVERY OTHER reference to head something...what is this line missing that they others have?
@agasthhyasharma10214 жыл бұрын
@@TokyoEdTech Bro ur a lit guy uv responded to litterally everyone
@TokyoEdTech4 жыл бұрын
@@agasthhyasharma1021 Thanks, I try, but don't always succeed. I figure I respond to 80-90%...and it's tiring!
@r.e.n_0811 ай бұрын
How do I make the snake move a bit faster?
@TokyoEdTech11 ай бұрын
Reduce the delay time.
@ejthedhampir5075 жыл бұрын
Hey Christian Thompson, Love your vids ^^ I seem to be having an error. if newsegment1.distance(head2) < 20: Exception has occurred: NameError name 'newsegment1' is not defined Why is this happening?
@ejthedhampir5075 жыл бұрын
Edit: I also seem to be having a problem where whenever i touch food i die. I'm in a bit of a pickle >.< I'll send you the pastebin (Also, my version is a variant, where two snakes fight. It behaves the same, just with 2 snakes instead of 1.): pastebin.com/VFrbUCrH
@ejthedhampir5075 жыл бұрын
@@TokyoEdTech Awesome. Thank you so much ^v^
@pinkorange092 жыл бұрын
HI. my code isn't working. The segments dissapear as soon as i eat the food. My code is: import turtle import time import random delay = 0.1 # Set up the screen wn = turtle.Screen() wn.title("Snake by Snake Game") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) # Turns off the screen updates # Snake heading 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, "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 a collision with the border if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(0.5) head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() # Check for a collision with the food if head.distance(food) < 20: # Move food to random spot x = random.randint(-280, 280) y = random.randint(-280, 280) 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 rerse order for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x, y) # Move segment 0 to where the head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() # Check for head collision with the body segments for segment in segments: if segment.distance(head) < 20: time.sleep(0.5) head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() time.sleep(delay) wn.mainloop()
@TokyoEdTech2 жыл бұрын
Hiya - this part of the program is not indented enough - it needs to be inside the if statement. # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear()
@pinkorange092 жыл бұрын
@@TokyoEdTech When I do that the new bit of body just stays still. it doesn't stay with the head. The new code is: 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 heading 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, "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 a collision with the border if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(0.5) head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() # Check for a collision with the food if head.distance(food) < 20: # Move food to random spot x = random.randint(-280, 280) y = random.randint(-280, 280) 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 rerse order for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x, y) # Move segment 0 to where the head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() # Check for head collision with the body segments for segment in segments: if segment.distance(head) < 20: time.sleep(0.5) head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() time.sleep(delay) wn.mainloop()
@TokyoEdTech2 жыл бұрын
You also need to indent segments.clear()
@pinkorange092 жыл бұрын
@@TokyoEdTech The food still doesn't stay with the head. The new code is: import turtle import time import random delay = 0.1 # Set up the screen wn = turtle.Screen() wn.title("Snake by Jonathan Gersch") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) # Turns off the screen updates # Snake heading 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, "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 a collision with the border if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(0.5) head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() # Check for a collision with the food if head.distance(food) < 20: # Move food to random spot x = random.randint(-280, 280) y = random.randint(-280, 280) 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 rerse order for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x, y) # Move segment 0 to where the head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() # Check for head collision with the body segments for segment in segments: if segment.distance(head) < 20: time.sleep(0.5) head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() time.sleep(delay) wn.mainloop()
@TokyoEdTech2 жыл бұрын
@@pinkorange09 Your indentation is still wrong for segments.clear()
@namankasliwal17254 жыл бұрын
hey could you help me out, i made it such that if it touches the walls, it comes from the other side. howevery whenever i add something related to it restarting it sends this error File "snakegame.py", line 95, in for index in range(len(bod1)-1, 0, -1): TypeError: object of type 'Turtle' has no len() my code is here: import turtle import time import random # set up the screen window = turtle.Screen() window.title("snake game") window.bgcolor('black') window.setup(width=600 , height=600) window.tracer(0) #turns off screen updates # snake head head = turtle.Turtle() head.speed(1) #how fast the animation takes place head.shape('square') head.color('white') head.penup()# so that it doesnt draw a line head.goto(0,0) #where it starts head.direction = 'stop' #snake food food = turtle.Turtle() food.speed(0) food.shape('circle') food.color('red') food.penup() food.goto(0,100) food.direction = 'stop' #food eaten foodamt = 0 #snake body bod1 = [] #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) elif head.direction == 'down': y = head.ycor() head.sety(y - 20) elif head.direction == 'left': x = head.xcor() head.setx(x - 20) elif head.direction == 'right': x = head.xcor() head.setx(x + 20) # keyboard bindings window.listen() window.onkeypress( go_up, 'Up' ) window.onkeypress( go_down, 'Down' ) window.onkeypress( go_left, 'Left' ) window.onkeypress( go_right, 'Right' ) # main game loop while True: window.update() if head.distance(food) < 20: #move the food in rand place x = random.randint(-290,290) y = random.randint(-290,290) food.goto(x,y) foodamt += 1 boday = turtle.Turtle() boday.speed(0) boday.shape('square') boday.color('grey') boday.penup() bod1.append(boday) for index in range(len(bod1)-1, 0, -1): x = bod1[index-1].xcor() y = bod1[index-1].ycor() bod1[index].goto(x, y) if len(bod1) > 0: x = head.xcor() y = head.ycor() bod1[0].goto(x,y) if head.xcor() > 290: x = head.xcor()*-1 y = head.ycor() head.goto(x,y) elif head.ycor() > 290: x = head.xcor() y = head.ycor()*-1 head.goto(x,y) elif head.xcor() < -290: x = head.xcor()*-1 y = head.ycor() head.goto(x,y) elif head.ycor() < -290: x = head.xcor() y = head.ycor()*-1 head.goto(x,y) ''' if head.distance(bod1) < 20: time.sleep(1) head.goto(0,0) for bod1 in bod1: bod1.goto(1000,1000''' move() # head collisions for bod1 in bod1: if bod1.distance(head) < 20: time.sleep(1) head.goto(0,0) head.direction = 'stop' bod1.goto() for bod1 in bod1: bod1.goto(1000,1000) bod1.clear() time.sleep(0.1) window.mainloop()
@Crazysvg4 жыл бұрын
i did that and here is my code for the wall thing #Check for collision if head.xcor() > 299: head.goto(-299, 0) if head.xcor() < -299: head.goto(299, 0) if head.ycor() > 299: head.goto(0, -299) if head.ycor() < -299: head.goto(0, 299)
@thech0sen14 жыл бұрын
pls help idk what i did wrong but when I eat, my snake dies and game restarts.this is the 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("blue") head.penup() head.goto(0, 0) head.direction = "stop" # Snake food food = turtle.Turtle() food.speed(0) food.shape("square") food.color("red") food.penup() food.goto(0, 100) segments = [] # Functions def go_up(): if head.direction != "down": head.direction = "up" def go_down(): if head.direction != "up": head.direction = "down" def go_right(): if head.direction != "left": head.direction = "right" def go_left(): if head.direction != "right": 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 == "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() #Wall collision if head.xcor()>290 or head.xcor()290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) #Check for head collision with body for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0, 0) head.direction = "stop" #Bye segments lol for segment in segments: segment.goto(1000, 1000) segments.clear() move() time.sleep(delay) wn.mainloop() Thank you!
@TokyoEdTech4 жыл бұрын
Hiya - move() has to come before the head collision with body: move() #Check for head collision with body
@thech0sen14 жыл бұрын
@@TokyoEdTech Thx
@TokyoEdTech4 жыл бұрын
@@thech0sen1 You're welcome.
@saunaknandi18145 жыл бұрын
sir it will be better if u try to explain for some extra time while using using each func.
@craxycrazy83614 жыл бұрын
Hi, so I am trying to make the snake game however, I came across this error: code: for index in range(len(segments)-1, 0, -1): error: line 115, in for index in range(len(segments)-1, 0, -1): TypeError: object of type 'Turtle' has no len() I've been trying to solve this for a long time and i would appreciate if someone could help me. Thank You.
@TokyoEdTech4 жыл бұрын
Hiya. I'll need to see your code, otherwise I'd just be guessing. Paste it here and I'll take a look.
@craxycrazy83614 жыл бұрын
@@TokyoEdTech Hi again, here is my code: wn = turtle.Screen() wn.title("Snake Game") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) #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("square") food.color("red") food.penup() food.goto(0,-100) segments = [] 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) wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") while True: wn.update() if head.xcor()>290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(1) head.goto(0,0) head.direction = "stop" segments.clear() 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("grey") 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() (I haven't fully completed the course yet this is just what I did so far.) Thank You.
@TokyoEdTech4 жыл бұрын
@@craxycrazy8361 I am not getting the same error - it is working fine.
@craxycrazy83614 жыл бұрын
@@TokyoEdTech Oh ok. Well thanks anyways!
@TokyoEdTech4 жыл бұрын
You are welcome. Keep on codin!
@cal8474 жыл бұрын
head.direction('stop') is not working
@cal8474 жыл бұрын
@@TokyoEdTech I fixed it. I thought it wasn't stopping because I pressed a key as it was sleeping for 1 second. Thanks!
@lukelandry53095 жыл бұрын
Help when I die by hitting my own Body my body stays there but my head gets reset
@Rwko.4 жыл бұрын
same, did you find a solution?
@ankitchauhan36984 жыл бұрын
I need help i have done exactly what u told but after eating food two times my snak dies and though I have defined key pressing by w,s,a,d for up,down,left,right respectively but my snake is moving by pressing d instead of a. Here I have given my code please help import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("Snake Game by Ankit Chauhan") wn.bgcolor("light blue") wn.setup(width=600, height=600) wn.tracer(0) #to turn of the screen # Snake head head = turtle.Turtle() head.speed(0) head.shape("circle") head.color("green") head.penup() head.goto(0,0) head.direction = "stop" #Snake Food food = turtle.Turtle() food.speed(0) food.shape("square") food.color("red") food.penup() food.goto(0,100) segments = [] # 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_right, "d") wn.onkeypress(go_left, "a") # Main game loop while True: wn.update() # Check for collision with the border if head.xcor()>290 or head.xcor()290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() # Check for collision of head with body segments for segment in segments: if segment.distance(head) > 20: time.sleep(1) head.goto(0,0) head.direction = "stop" #for hiding the segments for segment in segments: segment.goto(1000,1000) #clear the list segments.clear() time.sleep(delay) wn.mainloop()
@TokyoEdTech4 жыл бұрын
"i have done exactly what u told " If you had, then it would work fine. First, moving left you need to SUBTRACT 20, not add 20. The opposite is for right. Also, you have: if segment.distance(head) > 20: It should be: if segment.distance(head) < 20:
@retrogamer73825 жыл бұрын
I did this move() # Check for head collision with the body segments for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0,0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() time.sleep(delay) wndw.mainloop() Running] python -u "c:\Users\user\Documents\please.py" File "c:\Users\user\Documents\please.py", line 115 move() ^ IndentationError: unindent does not match any outer indentation level [Done] exited with code=1 in 0.147 seconds why did it happen
@justsomefakeguywithoutamou55383 жыл бұрын
at this part the body isnt shown pls help: import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("Snake Game by me") 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 = [] #funktions 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 border if head.xcor()290 or head.ycor()>290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() # Check for head collision with the body segments for segment in segments: if segment.distance(head) < 20: time.sleep (1) head.goto(0,0) head.direction = "stop" # hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() time.sleep(delay) wn.mainloop() this is my code
@TokyoEdTech3 жыл бұрын
Hiya - You need to indent this section further: # hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear()
@dumbscientists577 Жыл бұрын
What does it mean attricbuteerror: 'list' object has no attribute 'distnace" # import packages 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) # 290 or head.xcor()290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) #Check for head collision with the body segments for segment in segments: if segments.distance(head) < 20: time.sleep(1) head.goto(0,0) head.direction = "stop" # hide the segments for segment in segments: segment.goto(1000, 1000) #Clear the segments list segments.clear() move() time.sleep(delay) wn.mainloop()
@TokyoEdTech Жыл бұрын
if segments.distance needs to be changed to if segment.distance
@dumbscientists577 Жыл бұрын
@@TokyoEdTech thank you I appreciate it
@TokyoEdTech Жыл бұрын
@@dumbscientists577 You're welcome - keep on codin'!
@connorbellfeuille57903 жыл бұрын
My code is fine but why do we have to write != instead of = ?
@TokyoEdTech3 жыл бұрын
!= means not equal to
@connorbellfeuille57903 жыл бұрын
@@TokyoEdTech ok i will be using that in the future!
@TokyoEdTech3 жыл бұрын
@@connorbellfeuille5790 It's very useful!
@ace.s_world2 жыл бұрын
my snake isn't moving what do I do
@TokyoEdTech2 жыл бұрын
If you share your code, I can take a look.
@rakeshreddy13654 жыл бұрын
amazing tutorial
@TokyoEdTech4 жыл бұрын
Thanks!
@dudebright98314 жыл бұрын
thank you
@TokyoEdTech4 жыл бұрын
You're welcome.
@thefatkid1211 ай бұрын
why does my snake dont grow it just resets when it hits the food here is my code import turtle import time import random delay = 0.2 kak = turtle.Screen() kak.title("Snake Game By Juan The One") kak.bgcolor("lightgreen") kak.tracer(0) kak.setup(width=600,height=600) #Head head = turtle.Turtle() head.shape("square") head.color("orange") head.speed(0) head.penup() head.goto(0,0) head.direction = "up" #food food = turtle.Turtle() food.shape("circle") food.color("red") food.speed(0) food.penup() food.goto(0,150) segments = [] #function 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 + 15) if head.direction =="down": y = head.ycor() head.sety( y - 15) if head.direction =="left": x = head.xcor() head.setx( x - 15) if head.direction =="right": x = head.xcor() head.setx( x + 15) kak.listen() kak.onkeypress( go_up,"w") kak.onkeypress( go_down,"s") kak.onkeypress( go_left,"a") kak.onkeypress( go_right,"d") #main loop while True: kak.update() if head.xcor() > int(270) or head.xcor() < int(-270) or head.ycor() > int(270) or head.ycor() < int(-270): time.sleep(0.7) head.goto(0,0) head.direction = "stop" for segment in segments: segment.goto(2000,2000) segments.clear() if head.distance(food) < int(34): x = random.randint(-285,285) y = random.randint(-285,285) food.goto(x,y) new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("purple") 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) > int(0): x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() for segment in segments: if segment.distance(head) < int(20): time.sleep(1) head.goto(0,0) head.direction = "stop" for segment in segments: segment.goto(2000,2000) segments.clear() time.sleep(delay) kak.mainloop()
@henryjiang-z5tАй бұрын
You made a mistake
@gabrielpinto58735 жыл бұрын
hi, I was wondering if you could help me figure out an error, when I touch the food, I instantly die, and the game resets. plz help!
@Tonys-Gaming-Room45333 жыл бұрын
hey I have this weird error can you help me? error: line 104, in if segment in segments: NameError: name 'segment' is not defined code: import turtle import time import random delay = 0.1 win = turtle.Screen() win.title("Snake by Antonio Gutierrez") win.bgcolor("green") win.setup(width= 600, height= 600) win.tracer(0) food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0, 100) segments = [] head = turtle.Turtle() head.speed(0) head.shape("square") head.color("blue") head.penup() head.goto(0, 0) head.direction = "stop" 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) win.listen() win.onkeypress(go_up, "Up") win.onkeypress(go_down, "Down") win.onkeypress(go_left, "Left") win.onkeypress(go_right, "Right") while True: win.update() if head.xcor()>290 or head.xcor()290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) if segment in segments:
@Tonys-Gaming-Room45333 жыл бұрын
hey didn't see your help video before srry bout that but I am putting the pastein link here: pastebin.com/BaYjVHqE#
@TokyoEdTech3 жыл бұрын
@@Tonys-Gaming-Room4533 Hiya. The error message tells you EXACTLY where the problem is: File "Snake Game code.py", line 104, in if segment in segments: NameError: name 'segment' is not defined If you rewatch the video, you'll realize that this line should be: for segment in segments: It's the little things that get you!
@Tonys-Gaming-Room45333 жыл бұрын
@@TokyoEdTech oh ok thanks for the help I liked and subscribe btw these are good tutorials
@Prostylin2 жыл бұрын
My version is 3.10.2 is that good enough
@TokyoEdTech2 жыл бұрын
Yep.
@aaronmullins52393 жыл бұрын
i dont get it why isnt it working
@TokyoEdTech3 жыл бұрын
It's pretty obvious that I can't help you if you don't share the code. I'm happy to take a look if you want to paste it here.
@qiver_47654 жыл бұрын
I also have the issue were when I touch the food, the game shuts off. Here is my link to the code pastebin.com/2WavMVsC Thank you SOO much for your amazing vids!
@qiver_47654 жыл бұрын
@@TokyoEdTech Thanks so much! Made the changes and now it works perfectly!
@alishavandi36133 жыл бұрын
Hello, your videos are amazing! I just have a problem with my code, but I get no errors. The problem is that when the snake touches the food, I fail and the game restarts. But this didn't happen until I coded the head and body segment collision part. I don't know what's wrong, here is my code: import turtle import time import random # Delayes the movement of objects so it is not super fast 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('orange') 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) # List of the snake body segments segments = [] # Snake head directions def go_up(): head.direction = 'up' def go_down(): head.direction = 'down' def go_right(): head.direction = 'right' def go_left(): head.direction = 'left' # Snake head movement of each direction def move(): if head.direction == 'up': y = head.ycor() head.sety(y + 15) if head.direction == 'down': y = head.ycor() head.sety(y - 15) if head.direction == 'left': x = head.xcor() head.setx(x - 15) if head.direction == 'right': x= head.xcor() head.setx(x + 15) # Keyboard bindings wn.listen() wn.onkeypress(go_up, 'Up') wn.onkeypress(go_down, 'Down') wn.onkeypress(go_right, 'Right') wn.onkeypress(go_left, 'Left') # Main gameloop while True: # Updates the window wn.update() # Check for border collisions if head.xcor() > 280 or head.xcor() < -280 or head.ycor() > 280 or head.ycor() < -280: time.sleep(1) head.goto(0,0) head.direction = 'stop' # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() # Snake head and food collision if head.distance(food) < 20: # Moves the food into a random spot on the window x = random.randint(-280, 280) y = random.randint(-280, 280) food.goto(x, y) # Add snake body segments new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape('square') new_segment.color('yellow') new_segment.penup() segments.append(new_segment) # Attach the segments to the snake head 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) # Calls the move function to make the snake move move() # Check for head collisions with the body for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0,0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear the segments list segments.clear() # Delays the animation speed of the turtles time.sleep(delay) wn.mainloop()
@TokyoEdTech3 жыл бұрын
You changed the movement distance to 15. That was not a good idea. The movement distance determines the collision distance. REF: head.sety(y - 15)
@alishavandi36133 жыл бұрын
@@TokyoEdTech thank you! I also added a message when the player loses that says something like “Ouch! Hit the border”, and then the game restarts.
@TokyoEdTech3 жыл бұрын
@@alishavandi3613 Awesome - that's how you get better...add your own ideas and experiment!
@madhurisalgude37493 жыл бұрын
How to solve error... 'object of type Turtle has no len ()'.. Please reply soon
@TokyoEdTech3 жыл бұрын
There is a link in the description labeled “NEED HELP?” Check that out and get back to me. Keep on codin’!
@madhurisalgude37493 жыл бұрын
@@TokyoEdTech ok I'll check.. Thank you ☺️
@madhurisalgude37493 жыл бұрын
@@TokyoEdTech Traceback (most recent call last): File "C:\Users\ARUN SALGUDE\PycharmProjects\snkgme\gma.py", line 129, in for index in range(len(segments)-1,0,-1): TypeError: object of type 'Turtle' has no len()
@madhurisalgude37493 жыл бұрын
Sir please reply as soon as you can... I'm stucked for 4 days.. In this.. And I'm beginner learner.. So don't have much idea about solving errors
@TokyoEdTech3 жыл бұрын
Did you check the link I wrote above?
@sideswifter75412 жыл бұрын
hello I really love your videos and this vid was useful but when ever my snake touches the edge my code exits out here is my code
@TokyoEdTech2 жыл бұрын
You didn't share your code.
@caelim35245 жыл бұрын
I'm using idle and everything else is working fine but now that I added the code to reset the game when you collide with your body when I pick up food it doesn't add a segment to the body here is the paste bin pastebin.com/uy43eyQR
@caelim35245 жыл бұрын
Thank you so much I love this tutorial I’m just getting into python and these videos really help
@0108ben4 жыл бұрын
i have now got this but when i pick up the second fruit it resets my game import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("snake by Ben") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) #turtle head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop" # FOOOD food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) segments = [] #head move 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) #keybinds wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") #game loop while True: wn.update() #border death if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290 : time.sleep(1) head.goto(0, 0) head.direction = "stop" #hide segments after death for segment in segments: segment.goto(1000,1000) #clear the segments segments = [] #food controls if head.distance(food) < 20: x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x,y) #body parts new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("black") new_segment.penup() segments.append(new_segment) #move body 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 body to head if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) #check for head collision with body for segment in segments: if segment.distance(head) > 8: time.sleep(1) head.goto(0,0) head.direction = "stop" for segment in segments: segment.goto(1000,1000) segments = [] move() time.sleep(delay) wn.mainloop()
@TokyoEdTech4 жыл бұрын
Watch carefully - the following section needs to be rearranged: if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() #check for head collision with body for segment in segments: if segment.distance(head) < 8: time.sleep(1) head.goto(0,0) head.direction = "stop" for segment in segments: segment.goto(1000,1000) segments = []
@0108ben4 жыл бұрын
@@TokyoEdTech thanks :D
@0108ben4 жыл бұрын
@@TokyoEdTech YAY it works :D thanks for your help :D
@BogotaBolo4 жыл бұрын
Every time when i run into myself my game chrash.... heres my code: import random import turtle import time delay = 0.1 #Screen win = turtle.Screen() win.title("Nicolas") win.bgcolor("green") win.setup(width = 800, height = 800) win.tracer(0) #growing segments =[] #SnakeHead 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) food.direction = "stop" #Movement 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" #Key_pressed 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) #Input win.listen() win.onkeypress(go_up, "w") win.onkeypress(go_down, "s") win.onkeypress(go_left, "a") win.onkeypress(go_right, "d") #Loop while True: win.update() if head.xcor() > 390 or head.xcor() < -390 or head.ycor() > 390 or head.ycor() < -390: time.sleep(1) head.goto(0,0) head.direction = "stop" for segment in segments: segment.goto(1000, 1000) segments.clear() #Eat if head.distance(food) < 20: x = random.randint(-390, 390) y= random.randint(-390, 390) food.goto(x, y) #NewBodyPart new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("blue") 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() 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) segments.clear() time.sleep(delay) win.mainloop()
@TokyoEdTech4 жыл бұрын
You should be a bit more precise - what is the exact error message? In this case it is: File "snake_help.py", line 249, in head.direction("stop") TypeError: 'str' object is not callable direction is a string, not a function so you should change it to: head.direction = "stop"
@agasthhyasharma10214 жыл бұрын
Karens be like: HE COUGHED HE HAS COVID - 19. lmao
@TokyoEdTech4 жыл бұрын
I recorded this one two years ago so hopefully no COVID! lol
@SykesBFC6 жыл бұрын
the structure is the same and everything
@sirisgoated41734 жыл бұрын
Hi, it says name error: name segment is not defined. here is my code import turtle import time import random delay = 0.1 # Set up the screen wn = turtle.Screen() wn.title("Snake game by Saif Aldaour") wn.bgcolor("black") wn.setup(width=600, height=600) wn.tracer(0) # Snake head head = turtle.Turtle() head.speed(0) head.shape("circle") head.color("green") 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 border if head.xcor()>290 or head.xcor()290 or head.ycor() 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() # Check for collision with the body segments if segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0,0) head.direction("stop") # Hide the segments for segment in segments: segment.goto(1000,1000) # Clear the segments list segment.clear() time.sleep(delay) wn.mainloop()
@sirisgoated41734 жыл бұрын
Never mind. i found where i went wrong
@TokyoEdTech4 жыл бұрын
Nice!
@TokyoEdTech4 жыл бұрын
What was the problem?
@sirisgoated41734 жыл бұрын
TokyoEdTech just like you mentioned. I uncontinued code
@0108ben4 жыл бұрын
@@sirisgoated4173 how did you fix that? i have the same problem now and cant work out how to fix it
@theprogrammer19034 жыл бұрын
please help when the snake touches the food it does what must be done when it touches it's body # Snake game # Game made by @the programmer import turtle import random import time # Creating the screen Screen = turtle.Screen() Screen.setup(width=600, height=600) Screen.bgcolor("black") Screen.title("Snake Game") Screen.tracer(0) # Creating the Snake head Snake = turtle.Turtle() Snake.shape("circle") Snake.speed(0) Snake.color("light green") Snake.penup() Snake.goto(0, 0) Snake.direction = "stop" # Creating the snake food Food = turtle.Turtle() Food.shape("circle") Food.speed(0) Food.color("red") Food.penup() Food.goto(0, 100) # Functions def move_up(): if Snake.direction != "down": Snake.direction = "up" def move_down(): if Snake.direction != "up": Snake.direction = "down" def move_right(): if Snake.direction != "left": Snake.direction = "right" def move_left(): if Snake.direction != "rigth": Snake.direction = "left" def move(): if Snake.direction == "up": y = Snake.ycor() Snake.sety(y + 15) if Snake.direction == "down": y = Snake.ycor() Snake.sety(y - 15) if Snake.direction == "right": x = Snake.xcor() Snake.setx(x + 15) if Snake.direction == "left": x = Snake.xcor() Snake.setx(x - 15) # Keyboard Binding Screen.listen() Screen.onkeypress(move_up, "w") Screen.onkeypress(move_down, "s") Screen.onkeypress(move_right, "d") Screen.onkeypress(move_left, "a") # Variables delay = 0.1 Snake_body = [] # Main game loop Game_is_running = True while Game_is_running: Screen.update() if Snake.xcor()>290 or Snake.xcor()< -290 or Snake.ycor()>290 or Snake.ycor()< -290: time.sleep(1) Snake.goto(0,0) Snake.direction = "stop" for body in Snake_body: body.goto(1000, 1000) Snake_body.clear() if Snake.distance(Food)< 20: # Move the food to a random distance Xcor_random = random.randint(-270, 270) Ycor_random = random.randint(-270, 270) Food.goto(Xcor_random, Ycor_random ) # Add a body to the snake New_Snake_body = turtle.Turtle() New_Snake_body.shape("circle") New_Snake_body.speed(0) New_Snake_body.color("dark green") New_Snake_body.penup() Snake_body.append(New_Snake_body) for index in range(len(Snake_body)-1, 0, -1): x = Snake_body[index-1].xcor() y = Snake_body[index-1].ycor() Snake_body[index].goto(x, y) if len(Snake_body) > 0: x = Snake.xcor() y = Snake.ycor() Snake_body[0].goto(x, y) move() for body in Snake_body: if body.distance(Snake) < 20: time.sleep(1) Snake.goto(0, 0) for body in Snake_body: body.goto(1000, 1000) Snake_body.clear() time.sleep(delay) Screen.mainloop()
@TokyoEdTech4 жыл бұрын
Sorry, could you try explaining that again? I'm not sure what you mean.
@theprogrammer19034 жыл бұрын
@@TokyoEdTech when the snake touches the food the game rests but when touching it's body nothing happens
@Prostylin2 жыл бұрын
1:02 he has Covid - 19, wait covid existed in 2018
@TokyoEdTech2 жыл бұрын
Wow - I must be patient zero!
@alwinvillero44044 жыл бұрын
the snakes dead body is showing up lol import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("Snaek") wn.bgcolor("black") wn.setup(width=600, height=600) wn.tracer(0) # head head = turtle.Turtle() head.speed(0) head.shape("square") head.color('Green') head.penup() head.goto(0, 0) head.direction = "up" # Food food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x,y) segments = [] def go_up(): if head.direction != "down": 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() move() for segment in segments: if segment.distance(head) < 20: time.sleep(0.1) head.goto(0, 0) head.direction = "stop" # Hide the body # cha cha real s m o o t h for segment in segments: segment.goto(1000, 1000) # clear the segments list segments.clear() # the snakes dead body is showing up i dont know why lol # Hide the body # cha cha real s m o o t h for segment in segments: segment.goto(1000, 1000) # clear the segments list segments.clear() # Check for snake collision w/ border if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(0.5) head.goto(0, 0) head.direction = "stop" # Hide the body # cha cha real s m o o t h for segment in segments: segment.goto(1000, 1000) # clear the segments list segments.clear() # Hide the body # cha cha real s m o o t h for segment in segments: segment.goto(1000, 1000) # clear the segments list segments.clear() # Snake collision w/ food if head.distance(food) < 20: x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x,y) # add a segment new_segment = turtle.Turtle() new_segment.shape("square") new_segment.color("green") new_segment.penup() new_segment.speed(0) segments.append(new_segment) # move back 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 front near head if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) time.sleep(delay) wn.mainloop()
@TokyoEdTech4 жыл бұрын
This line is indented too far: # clear the segments list segments.clear()
@alwinvillero44044 жыл бұрын
@@TokyoEdTech thx
@notdalaz4 жыл бұрын
It says in the for segment in segments part that Variable 'segment' already declared in 'for' loop or 'with' statement above import turtle import time import random delay = 0.1 wn = turtle.Screen() wn.title("pog game") wn.bgcolor("blue") wn.setup(width=600, height=600) 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() food.goto(0, 100) segments = [] 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) wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") while True: wn.update() if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(1) head.goto(0, 0) head.direction = "stop" 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("green") 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() for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) segments.clear() time.sleep(delay) wn.mainloop()
@TokyoEdTech4 жыл бұрын
It works fine on my computer - I'm guessing it is just a warning that you can ignore.
@notdalaz4 жыл бұрын
TokyoEdTech I’ll try another version of pycharm because I’m still getting errors in my game and it might be because my version is outdated. But that’s only what I can think what it is.
@TokyoEdTech4 жыл бұрын
Pycharm tries to be "helpful" by pointing these things out. Good luck!
@notdalaz4 жыл бұрын
TokyoEdTech thank you!
@notdalaz4 жыл бұрын
iori1391 yes, but the script was correct when @TokyoEdTech tried it. So my best bet is that I’m not on the right version. Also I looked at the video for about 30-35 minutes and found there was no improper indents or anything.
@andreiarseni3622 жыл бұрын
why on the top of my snakes's head is a segment and it has the same coordinates like the snake head,and everytime my snake eats it goes at 0,0. code: # Snake game # Snake Head 20x20 # Snake parts 20x20 # Screen 600x600 import turtle import time import random import logging logger=logging.getLogger(__name__) logger.setLevel(logging.INFO) formatter=logging.Formatter('%(message)s:%(asctime)s') file_handler=logging.FileHandler('register.log') file_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.info("Jucatorul a intrat in joc in acest timp in jocul SNAKE") # Screen setup wn=turtle.Screen() wn.setup(600,600) wn.bgcolor("green") wn.tracer(0) # Snake Head head=turtle.Turtle() head.color("black") head.shape("square") head.penup() head.speed(0) head.setposition(0,0) head_direction="stop" # Snake Food food=turtle.Turtle() food.color("red") food.shape("circle") food.shapesize(1.2,1.2,1) food.penup() x_f=random.randint(-290,290) y_f=random.randint(-290,290) food.setposition(x_f,y_f) food.speed(0) # Snake parts parts=[] colors=["red","yellow","blue","orange","lightgreen"] # Border Line pen=turtle.Turtle() pen.penup() pen.color("black") pen.setposition(-298,298) pen.width(6) pen.pendown() for line in range(4): pen.forward(588) pen.right(90) pen.hideturtle() # Variables time_pause=0.1 distance=20 counter = 1 # Functions def down(): global head_direction if head_direction!="up": head_direction="down" def up(): global head_direction if head_direction != "down": head_direction="up" def right(): global head_direction if head_direction != "left": head_direction="right" def left(): global head_direction if head_direction != "right": head_direction="left" # Keyboard turtle.listen() turtle.onkeypress(down,"Down") turtle.onkeypress(up,"Up") turtle.onkeypress(right,"Right") turtle.onkeypress(left,"Left") while True: wn.update() if (head.xcor() > 290) or (head.xcor() < -290) or (head.ycor() < -290) or (head.ycor() > 290): head.setposition(0,0) for part in parts: part.hideturtle() part.setposition(350,350) parts.clear() head_direction="stop" if head_direction=="stop": head.setposition(0,0) if head_direction=="up": y=head.ycor() y+=distance head.sety(y) time.sleep(time_pause) if head_direction=="down": y=head.ycor() y-=distance head.sety(y) time.sleep(time_pause) if head_direction=="right": x=head.xcor() x+=distance head.setx(x) time.sleep(time_pause) if head_direction=="left": x=head.xcor() x-=distance head.setx(x) time.sleep(time_pause) if head.distance(food) < 20: x=random.randint(-290,290) y=random.randint(-290,290) food.setposition(x,y) part=turtle.Turtle() part.color("lightgreen") part.penup() part.shape("square") part.speed(0) parts.append(part) for index in range(len(parts)-1,0,-1): x=parts[index-1].xcor() y=parts[index-1].ycor() parts[index].goto(x,y) if len(parts)>0: x=head.xcor() y=head.ycor() parts[0].goto(x,y) for part in parts: if part.distance(head) < 20: time.sleep(1) head.goto(0,0) head_direction="stop" for part in parts: part.goto(1000,1000) parts.clear() turtle.mainloop() my code is a bit different.