{"id":130,"date":"2026-03-27T14:14:41","date_gmt":"2026-03-27T14:14:41","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/l_rankins\/?p=130"},"modified":"2026-03-27T14:14:41","modified_gmt":"2026-03-27T14:14:41","slug":"message-22-snake-3-return-of-mad-jack","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/l_rankins\/2026\/03\/27\/message-22-snake-3-return-of-mad-jack\/","title":{"rendered":"MESSAGE 22: SNAKE 3: Return of Mad Jack"},"content":{"rendered":"\n<p>Today I made Snake<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame\nimport sys \nimport random\n\npygame.init()\n\nSW, SH = 800, 800\n\nBLOCK_SIZE = 50\nFONT = pygame.font.Font(\"font.ttf\", BLOCK_SIZE*2)\n\nscreen = pygame.display.set_mode((800, 800))\npygame.display.set_caption(\"Snake!\")\nclock = pygame.time.Clock()\n\nclass Snake:\n    def __init__(self):\n        self.x, self.y = BLOCK_SIZE, BLOCK_SIZE\n        self.xdir = 1\n        self.ydir = 0\n        self.head = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)\n        self.body = &#91;pygame.Rect(self.x-BLOCK_SIZE, self.y, BLOCK_SIZE, BLOCK_SIZE)]\n        self.dead = False<\/code><\/pre>\n\n\n\n<p><strong>This is the code that draws the grid<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    def update(self):\n        global apple\n        \n        for square in self.body:\n            if self.head.x == square.x and self.head.y == square.y:\n                self.dead = True\n            if self.head.x not in range(0, SW) or self.head.y not in range(0, SH):\n                self.dead = True\n        \n        if self.dead:\n            self.x, self.y = BLOCK_SIZE, BLOCK_SIZE\n            self.head = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)\n            self.body = &#91;pygame.Rect(self.x-BLOCK_SIZE, self.y, BLOCK_SIZE, BLOCK_SIZE)]\n            self.xdir = 1\n            self.ydir = 0\n            self.dead = False\n            apple = Apple()\n        \n        self.body.append(self.head)\n        for i in range(len(self.body)-1):\n            self.body&#91;i].x, self.body&#91;i].y = self.body&#91;i+1].x, self.body&#91;i+1].y\n        self.head.x += self.xdir * BLOCK_SIZE\n        self.head.y += self.ydir * BLOCK_SIZE\n        self.body.remove(self.head)<\/code><\/pre>\n\n\n\n<p><strong>This is the code for drawing the snake<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Apple:\n    def __init__(self):\n        self.x = int(random.randint(0, SW)\/BLOCK_SIZE) * BLOCK_SIZE\n        self.y = int(random.randint(0, SH)\/BLOCK_SIZE) * BLOCK_SIZE\n        self.rect = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)\n    \n    def update(self):\n        pygame.draw.rect(screen, \"orange\", self.rect)\n\ndef drawGrid():\n    for x in range(0, SW, BLOCK_SIZE):\n        for y in range(0, SH, BLOCK_SIZE):\n            rect = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)\n            pygame.draw.rect(screen, \"#3c3c3b\", rect, 1)\n\nscore = FONT.render(\"1\", True, \"white\")\nscore_rect = score.get_rect(center=(SW\/2, SH\/20))\n\ndrawGrid()\n\nsnake = Snake()\n\napple = Apple()<\/code><\/pre>\n\n\n\n<p><strong>This is the code that draws the apple<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            pygame.quit()\n            sys.exit()\n        if event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_DOWN:\n                snake.ydir = 1\n                snake.xdir = 0\n            elif event.key == pygame.K_UP:\n                snake.ydir = -1\n                snake.xdir = 0\n            elif event.key == pygame.K_RIGHT:\n                snake.ydir = 0\n                snake.xdir = 1\n            elif event.key == pygame.K_LEFT:\n                snake.ydir = 0\n                snake.xdir = -1\n\n    snake.update()\n    \n    screen.fill('black')\n    drawGrid()\n\n    apple.update()\n\n    score = FONT.render(f\"{len(snake.body) + 1}\", True, \"white\")\n\n    pygame.draw.rect(screen, \"green\", snake.head)\n\n    for square in snake.body:\n        pygame.draw.rect(screen, \"green\", square)\n\n    screen.blit(score, score_rect)\n\n    if snake.head.x == apple.x and snake.head.y == apple.y:\n        snake.body.append(pygame.Rect(square.x, square.y, BLOCK_SIZE, BLOCK_SIZE))\n        apple = Apple()\n\n    pygame.display.update()\n    clock.tick(5)\n<\/code><\/pre>\n\n\n\n<p><strong>This is the code that lets you control the snake and makes it grow<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I made Snake This is the code that draws the grid This is the code for drawing the snake [&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-130","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/130","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=130"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/posts\/130\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/l_rankins\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}