{"id":39,"date":"2025-10-03T15:22:31","date_gmt":"2025-10-03T15:22:31","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/c_menhart\/?p=39"},"modified":"2025-10-03T15:22:31","modified_gmt":"2025-10-03T15:22:31","slug":"free-friday-10-2-2025","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/c_menhart\/2025\/10\/03\/free-friday-10-2-2025\/","title":{"rendered":"free Friday 10\/2\/2025"},"content":{"rendered":"\n<p>today i tried to finish the snake game but it&#8217;s confusing <\/p>\n\n\n\n<p>import os<\/p>\n\n\n\n<p>import turtle<\/p>\n\n\n\n<p>import time<\/p>\n\n\n\n<p>import random<\/p>\n\n\n\n<p>delay = .1<\/p>\n\n\n\n<p># Set up the screen<\/p>\n\n\n\n<p>wn = turtle.Screen()<\/p>\n\n\n\n<p>wn.title(&#8220;Snake Game by @TokyoEdTech&#8221;)<\/p>\n\n\n\n<p>wn.bgcolor(&#8220;green&#8221;)<\/p>\n\n\n\n<p>wn.setup(width=600, height=600)<\/p>\n\n\n\n<p>wn.tracer(0) # Turns off the screen updates<\/p>\n\n\n\n<p># Snake food<\/p>\n\n\n\n<p>food = turtle. Turtle()<\/p>\n\n\n\n<p>food.speed(9)<\/p>\n\n\n\n<p>food.shape(&#8220;circle&#8221;)<\/p>\n\n\n\n<p>food.color(&#8220;red&#8221;)<\/p>\n\n\n\n<p>food.penup()<\/p>\n\n\n\n<p>food.goto(0,100)<\/p>\n\n\n\n<p>segments = []<\/p>\n\n\n\n<p>#Snake head<\/p>\n\n\n\n<p>head = turtle.Turtle()<\/p>\n\n\n\n<p>head.speed(8)<\/p>\n\n\n\n<p>head.shape(&#8220;square&#8221;)<\/p>\n\n\n\n<p>head.color(&#8220;black&#8221;)<\/p>\n\n\n\n<p>head.penup()<\/p>\n\n\n\n<p>head.goto(0,0)<\/p>\n\n\n\n<p>head.direction = &#8220;stop&#8221;<\/p>\n\n\n\n<p>#Functions<\/p>\n\n\n\n<p>def move():<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction == &#8220;up&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; y = head.ycor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.sety(y + 20)<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction == &#8220;down&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; y = head.ycor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.sety(y &#8211; 20)<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction == &#8220;left&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; x = head.xcor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.setx(x &#8211; 20)<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction == &#8220;right&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; x = head.xcor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.setx(x + 20)<\/p>\n\n\n\n<p># Functions<\/p>\n\n\n\n<p>def go_up():<\/p>\n\n\n\n<p>&nbsp; &nbsp; head.direction = &#8220;up&#8221;<\/p>\n\n\n\n<p>def go_down():<\/p>\n\n\n\n<p>&nbsp; &nbsp; head.direction = &#8220;down&#8221;<\/p>\n\n\n\n<p>def go_left():<\/p>\n\n\n\n<p>&nbsp; &nbsp; head.direction = &#8220;left&#8221;<\/p>\n\n\n\n<p>def go_right():<\/p>\n\n\n\n<p>&nbsp; &nbsp; head.direction = &#8220;right&#8221;<\/p>\n\n\n\n<p>#Keyboard bindings<\/p>\n\n\n\n<p>wn.listen()<\/p>\n\n\n\n<p>wn.onkeypress(go_up, &#8220;w&#8221;)<\/p>\n\n\n\n<p>wn.onkeypress (go_down, &#8220;s&#8221;)<\/p>\n\n\n\n<p>wn.onkeypress(go_left, &#8220;a&#8221;)<\/p>\n\n\n\n<p>wn.onkeypress(go_right, &#8220;d&#8221;)<\/p>\n\n\n\n<p># Main game Loop<\/p>\n\n\n\n<p>while True:<\/p>\n\n\n\n<p>&nbsp; &nbsp; wn.update()<\/p>\n\n\n\n<p>&nbsp; &nbsp; # Check for a collision with the border<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.xcor()&gt;290 or head.xcor()&lt;-290 or head.ycor()&gt;290 or head.ycor()&lt;-290:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(1)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.goto(0,0)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.direction = &#8220;stop&#8221;<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; # Clear the segments list<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; segments = []<\/p>\n\n\n\n<p>&nbsp; &nbsp; # Check for a collision with the food<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.distance(food) &lt;20:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; # Move the food to a random spot<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; x = random.randint(-298, 290)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; y = random.randint(-290, 290)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; food.goto(x,y)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Hide the segments<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; for segment in segments:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; segment.goto(1000, 1000)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; # Add a segment<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; new_segment = turtle. Turtle()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; new_segment.speed(8)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; new_segment.shape(&#8220;square&#8221;)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; new_segment.color(&#8220;grey&#8221;)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; new_segment.penup()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; segments.append(new_segment)<\/p>\n\n\n\n<p>&nbsp; &nbsp; # Move the end segments first in reverse order<\/p>\n\n\n\n<p>&nbsp; &nbsp; for index in range(len(segments)-1, 0, -1):<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; x = segments[index-1].xcor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; y = segments [index-1].ycor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; segments [index].goto(x, y)<\/p>\n\n\n\n<p>&nbsp; &nbsp; #Move segment to where the head is<\/p>\n\n\n\n<p>&nbsp; &nbsp; if len(segments) &gt; 0:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; x = head.xcor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; y = head.ycor()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; segments[0].goto(x,y)<\/p>\n\n\n\n<p>&nbsp; &nbsp; move()<\/p>\n\n\n\n<p>&nbsp; &nbsp; #Check for head collision with the body segments<\/p>\n\n\n\n<p>&nbsp; &nbsp; for segment in segments:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; print(&#8220;distance&#8221;,segment.distance)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; if segment.distance (head) &lt; 20:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(1)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; head.goto (0,0)<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; head.direction = &#8220;stop&#8221;<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Clear the segments list<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; segments = []<\/p>\n\n\n\n<p>&nbsp; &nbsp; time.sleep(delay)<\/p>\n\n\n\n<p>wn.mainloop()<\/p>\n\n\n\n<p>that&#8217;s what i did <\/p>\n","protected":false},"excerpt":{"rendered":"<p>today i tried to finish the snake game but it&#8217;s confusing import os import turtle import time import random delay [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/posts\/39\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/c_menhart\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}