import pygame
import random
# Initialize pygame
pygame.init()
# Set up the display
screen = pygame.display.set_mode((800, 600))
# Set the title of the window
pygame.display.set_caption("Snake Game")
# Set up the clock
clock = pygame.time.Clock()
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Set up the game clock
clock.tick(60)
# Set up the game variables
snake_length = 1
snake_positions = [(100, 100), (120, 100), (140, 100)]
direction = random.choice([UP, DOWN, LEFT, RIGHT])
head = snake_positions[0]
# Set up the game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
direction = UP
if event.key == pygame.K_DOWN:
direction = DOWN
if event.key == pygame.K_LEFT:
direction = LEFT
if event.key == pygame.K_RIGHT:
direction = RIGHT
# Move the snake
if direction == UP:
snake_positions.insert(0, head[1], head[0], None)
elif direction == DOWN:
snake_positions.insert(0, head[1], head[0], None)
elif direction == LEFT:
snake_positions.insert(1, head[0], head[1], None)
elif direction ==