import pygame import random
pygame.init()
width, height = 640, 480 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Snake Game")
BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0)
snake_size = 20 food_size = 20
snake_x = width // 2 snake_y = height // 2 snake_dx = 0 snake_dy = 0
food_x = random.randint(0, (width - food_size) // food_size) * food_size food_y = random.randint(0, (height - food_size) // food_size) * food_size
clock = pygame.time.Clock()
running = True while running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: snake_dy = -snake_size snake_dx = 0 elif event.key == pygame.K_DOWN: snake_dy = snake_size snake_dx = 0 elif event.key == pygame.K_LEFT: snake_dx = -snake_size snake_dy = 0 elif event.key == pygame.K_RIGHT: snake_dx = snake_size snake_dy = 0
# Update snake position
snake_x += snake_dx
snake_y += snake_dy
# Check collision with boundaries
if snake_x < 0 or snake_x >= width or snake_y < 0 or snake_y >= height:
running = False
# Check collision with food
if snake_x == food_x and snake_y == food_y:
# Generate new food position
food_x = random.randint(0, (width - food_size) // food_size) * food_size
food_y = random.randint(0, (height - food_size) // food_size) * food_size
# Clear the screen
screen.fill(BLACK)
# Draw the snake and food
pygame.draw.rect(screen, GREEN, (snake_x, snake_y, snake_size, snake_size))
pygame.draw.rect(screen, RED, (food_x, food_y, food_size, food_size))
# Update the display
pygame.display.flip()
# Set the game speed
clock.tick(10)
pygame.quit()