Snake Code
Snake Code:

&amp;#x200B;

&amp;#x200B;

import time  


import pygame  
from enum import Enum  
import random  


class Direction(Enum):  
UP = 1  
 DOWN = 2  
 RIGHT = 3  
 LEFT = 4  
window\_width = 720  
window\_height = 720  
pygame.init()  
pygame.display.set\_caption("Snake Game 1")  
window = pygame.display.set\_mode((window\_width, window\_height))  


refresh\_controller = pygame.time.Clock()  


snake\_position = \[250, 250\]  
snake\_body = \[\[250, 250\],  
 \[240, 240\],  
 \[230, 250\]\]  


food\_position = \[250, 250\]  


scale = 20  
global score  
score = 0  
global speed  
speed = 1  
def handle\_keys(direction):  
new\_direction = direction  
 for event in \[e for e in pygame.event.get() if e.type == pygame.KEYDOWN\]:  
 if event.key == pygame.K\_UP and direction != Direction.DOWN:  
new\_direction = Direction.UP  
 if event.key == pygame.K\_DOWN and direction != Direction.UP:  
new\_direction = Direction.DOWN  
 if event.key == pygame.K\_RIGHT and direction != Direction.LEFT:  
new\_direction = Direction.RIGHT  
 if event.key == pygame.K\_LEFT and direction != Direction.RIGHT:  
new\_direction = Direction.LEFT  
 return new\_direction  


def move\_snake(direction):  


if direction == Direction.UP:  
snake\_position\[1\] -= scale  
 if direction == Direction.DOWN:  
snake\_position\[1\] += scale  
 if direction == Direction.LEFT:  
snake\_position\[0\] -= scale  
 if direction == Direction.RIGHT:  
snake\_position\[0\] += scale  
snake\_body.insert(0, list(snake\_position))  


def generate\_new\_food():  
food\_position\[0\] = random.randint(5, ((window\_height - 2) // scale)) \* scale  
food\_position\[1\] = random.randint(5, ((window\_height - 2) // scale)) \* scale  


def get\_food():  
 global score  
 global speed  
 if abs(snake\_position\[0\] - food\_position\[0\]) &lt; 20 and abs(snake\_position\[1\] - food\_position\[1\]) &lt; 20:  
score += 1  
 speed += 2  
 generate\_new\_food()  
 else:  
snake\_body.pop()  


def paint\_hud():  
font = pygame.font.SysFont("Arial", scale\*2)  
render = font.render(f"Score: {score}", True, pygame.Color(255, 255, 255))  
rect = render.get\_rect()  
window.blit(render, rect)  
pygame.display.flip()  


def repaint():  
window.fill(pygame.Color(5, 0, 5))  
 for body in snake\_body:  
pygame.draw.circle(window, pygame.Color(0, 255, 0), (body\[0\], body\[1\]), scale/2)  
pygame.draw.rect(window, pygame.Color(255, 0, 0), pygame.Rect(food\_position\[0\]-scale/2, food\_position\[1\]-scale/2, scale, scale/2))  


def game\_over\_message():  
font = pygame.font.SysFont('Arial', scale\*3)  
render = font.render(f"Score: {score}", True, pygame.Color(255,255,255))  
rect = render.get\_rect()  
rect.midtop = (window\_width / 2, window\_height / 2)  
window.blit(render, rect)  
pygame.display.flip()  
time.sleep(2.5)  
pygame.quit()  
 exit(0)  


def game\_over():  
 if snake\_position\[0\] &lt; 0 or snake\_position\[0\] &gt; window\_width - 10:  
game\_over\_message()  
 if snake\_position\[0\] &lt; 0 or snake\_position\[1\] &gt; window\_height - 10:  
game\_over\_message()  
 for blob in snake\_body\[1:\]:  
 if snake\_position\[0\] == blob\[0\] and snake\_position\[1\] == blob\[1\]:  
game\_over\_message()  


def game\_loop():  
direction = Direction.RIGHT  
 while True:  
direction = handle\_keys(direction)  
move\_snake(direction)  
get\_food()  
repaint()  
game\_over()  
paint\_hud()  
pygame.display.update()  
refresh\_controller.tick(speed)  


if \_\_name\_\_ == "\_\_main\_\_":  
game\_loop()