{"id":74,"date":"2025-09-12T13:44:33","date_gmt":"2025-09-12T13:44:33","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/l_rankins\/?p=74"},"modified":"2025-09-12T13:44:46","modified_gmt":"2025-09-12T13:44:46","slug":"blog-9-maddness-returns","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/l_rankins\/2025\/09\/12\/blog-9-maddness-returns\/","title":{"rendered":"BLOG 8: MADDNESS RETURNS"},"content":{"rendered":"\n<p>i made Snake via a video<\/p>\n\n\n\n<p><\/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 = 0.1<\/p>\n\n\n\n<p># Score<\/p>\n\n\n\n<p>score = 0<\/p>\n\n\n\n<p>high_score = 0<\/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.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 head<\/p>\n\n\n\n<p>head = turtle.Turtle()<\/p>\n\n\n\n<p>head.speed(0)<\/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># Snake food<\/p>\n\n\n\n<p>food = turtle.Turtle()<\/p>\n\n\n\n<p>food.speed(0)<\/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># Pen<\/p>\n\n\n\n<p>pen = turtle.Turtle()<\/p>\n\n\n\n<p>pen.speed(0)<\/p>\n\n\n\n<p>pen.shape(&#8220;square&#8221;)<\/p>\n\n\n\n<p>pen.color(&#8220;white&#8221;)<\/p>\n\n\n\n<p>pen.penup()<\/p>\n\n\n\n<p>pen.hideturtle()<\/p>\n\n\n\n<p>pen.goto(0, 260)<\/p>\n\n\n\n<p>pen.write(&#8220;Score: 0 &nbsp;High Score: 0&#8243;, align=&#8221;center&#8221;, font=(&#8220;Courier&#8221;, 24, &#8220;normal&#8221;))<\/p>\n\n\n\n<p># Functions<\/p>\n\n\n\n<p>def go_up():<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction != &#8220;down&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.direction = &#8220;up&#8221;<\/p>\n\n\n\n<p>def go_down():<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction != &#8220;up&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.direction = &#8220;down&#8221;<\/p>\n\n\n\n<p>def go_left():<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction != &#8220;right&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.direction = &#8220;left&#8221;<\/p>\n\n\n\n<p>def go_right():<\/p>\n\n\n\n<p>&nbsp; &nbsp; if head.direction != &#8220;left&#8221;:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; head.direction = &#8220;right&#8221;<\/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># 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; # 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; # Clear the segments list<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; segments.clear()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; # Reset the score<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; score = 0<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; # Reset the delay<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; delay = 0.1<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; pen.clear()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; pen.write(&#8220;Score: {} &nbsp;High Score: {}&#8221;.format(score, high_score), align=&#8221;center&#8221;, font=(&#8220;Courier&#8221;, 24, &#8220;normal&#8221;))<\/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(-290, 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; # 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(0)<\/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; &nbsp; &nbsp; # Shorten the delay<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; delay -= 0.001<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; # Increase the score<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; score += 10<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; if score &gt; high_score:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; high_score = score<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; pen.clear()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; pen.write(&#8220;Score: {} &nbsp;High Score: {}&#8221;.format(score, high_score), align=&#8221;center&#8221;, font=(&#8220;Courier&#8221;, 24, &#8220;normal&#8221;))<\/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 0 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() &nbsp; &nbsp;<\/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; 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; # Hide the segments<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for segment in segments:<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; segment.goto(1000, 1000)<\/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.clear()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Reset the score<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score = 0<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Reset the delay<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delay = 0.1<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Update the score display<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pen.clear()<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pen.write(&#8220;Score: {} &nbsp;High Score: {}&#8221;.format(score, high_score), align=&#8221;center&#8221;, font=(&#8220;Courier&#8221;, 24, &#8220;normal&#8221;))<\/p>\n\n\n\n<p>&nbsp; &nbsp; time.sleep(delay)<\/p>\n\n\n\n<p>wn.mainloop()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>i made Snake via a video import turtle import time import random delay = 0.1 # Score score = 0 [&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":"","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-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":"","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-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":"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":""},"mobile":{"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":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":2,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/74\/revisions\/76"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}