|
| 1 | +import pygame |
| 2 | +import random |
| 3 | + |
| 4 | +# Initialize pygame |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +# Set the screen dimensions |
| 8 | +screen_width = 640 |
| 9 | +screen_height = 480 |
| 10 | + |
| 11 | +# Set the colors |
| 12 | +black = pygame.Color(0, 0, 0) |
| 13 | +white = pygame.Color(255, 255, 255) |
| 14 | +red = pygame.Color(255, 0, 0) |
| 15 | + |
| 16 | +# Create the game window |
| 17 | +screen = pygame.display.set_mode((screen_width, screen_height)) |
| 18 | +pygame.display.set_caption("Snake Game") |
| 19 | + |
| 20 | +# Set the game clock |
| 21 | +clock = pygame.time.Clock() |
| 22 | + |
| 23 | +# Set the snake's initial position and speed |
| 24 | +snake_position = [100, 50] |
| 25 | +snake_body = [[100, 50], [90, 50], [80, 50]] |
| 26 | +snake_speed = 10 |
| 27 | + |
| 28 | +# Set the initial food position |
| 29 | +food_position = [random.randrange(1, screen_width // 10) * 10, |
| 30 | + random.randrange(1, screen_height // 10) * 10] |
| 31 | +food_spawn = True |
| 32 | + |
| 33 | +# Set the initial game score |
| 34 | +score = 0 |
| 35 | + |
| 36 | +# Set the game over flag |
| 37 | +game_over = False |
| 38 | + |
| 39 | +# Game loop |
| 40 | +while not game_over: |
| 41 | + # Handle events |
| 42 | + for event in pygame.event.get(): |
| 43 | + if event.type == pygame.QUIT: |
| 44 | + game_over = True |
| 45 | + |
| 46 | + # Handle snake movement |
| 47 | + keys = pygame.key.get_pressed() |
| 48 | + for key in keys: |
| 49 | + if keys[pygame.K_LEFT]: |
| 50 | + snake_position[0] -= snake_speed |
| 51 | + if keys[pygame.K_RIGHT]: |
| 52 | + snake_position[0] += snake_speed |
| 53 | + if keys[pygame.K_UP]: |
| 54 | + snake_position[1] -= snake_speed |
| 55 | + if keys[pygame.K_DOWN]: |
| 56 | + snake_position[1] += snake_speed |
| 57 | + |
| 58 | + # Check for collision with the food |
| 59 | + if pygame.Rect(snake_position[0], snake_position[1], 10, 10).colliderect( |
| 60 | + pygame.Rect(food_position[0], food_position[1], 10, 10)): |
| 61 | + score += 1 |
| 62 | + food_spawn = False |
| 63 | + |
| 64 | + # Spawn new food if the previous one was eaten |
| 65 | + if not food_spawn: |
| 66 | + food_position = [random.randrange(1, screen_width // 10) * 10, |
| 67 | + random.randrange(1, screen_height // 10) * 10] |
| 68 | + food_spawn = True |
| 69 | + |
| 70 | + # Update the snake's body |
| 71 | + snake_body.insert(0, list(snake_position)) |
| 72 | + if len(snake_body) > score + 1: |
| 73 | + snake_body.pop() |
| 74 | + |
| 75 | + # Check for collision with the snake's own body |
| 76 | + if snake_position in snake_body[1:]: |
| 77 | + game_over = True |
| 78 | + |
| 79 | + # Check for collision with the screen boundaries |
| 80 | + if snake_position[0] < 0 or snake_position[0] >= screen_width or \ |
| 81 | + snake_position[1] < 0 or snake_position[1] >= screen_height: |
| 82 | + game_over = True |
| 83 | + |
| 84 | + # Set the screen background |
| 85 | + screen.fill(black) |
| 86 | + |
| 87 | + # Draw the snake |
| 88 | + for pos in snake_body: |
| 89 | + pygame.draw.rect(screen, white, pygame.Rect(pos[0], pos[1], 10, 10)) |
| 90 | + |
| 91 | + # Draw food |
| 92 | + pygame.draw.rect(screen, red, pygame.Rect( |
| 93 | + food_position[0], food_position[1], 10, 10)) |
| 94 | + |
| 95 | + # Update the screen |
| 96 | + pygame.display.flip() |
| 97 | + |
| 98 | + # Set the game speed |
| 99 | + clock.tick(20) |
| 100 | + |
| 101 | +# Quit the game |
| 102 | +pygame.quit() |
0 commit comments