snake game

PHOTO EMBED

Mon Oct 14 2024 11:32:07 GMT+0000 (Coordinated Universal Time)

Saved by @Alexlotl

 
import pygame
import random
import time
speed = 15
x = 720
y = 480
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
pygame.init()
(5, 0)
window = pygame.display.set_mode((x, y))
fps = pygame.time.Clock()
sp = [100, 50]
sb = [[100,50],
      [90, 50],
      [80, 50],
     [70, 50]]
fp = [random.randrange(1, (x//10)) * 10,
      random.randrange(1, (y//10)) * 10]
fruit_spawn = True
dr = 'RIGHT'
change_to = dr
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                sp[1] -= 10
            if event.key == pygame.K_DOWN:
                sp[1] += 10
            if event.key == pygame.K_LEFT:
                sp[0] -= 10
            if event.key == pygame.K_RIGHT:
                sp[0] += 10

                
#growth happens if tail not removed and fruit is eaten 
sb.insert(0, list(sp))
if sp[0] == fp[0] and sp[1] == fp[1]:
fruit_spawn = False
#tail constantly removed and no growth if fruit not eaten
else:
snake_body.pop()
if not fruit_spawn:
fp = [random.randrange(1,(x//10)) * 10, random.randrange(1,(y//10)) * 10]
fruit_spawn = True
window.fill(black)
for pos in sb:
pygame.draw.rect(window, green,
pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(window, white, pygame.Rect(fruit_position[0], fruit_position[1], 10, 10))
pygame.display.update()
fps.tick(snake_speed)
content_copyCOPY