|
| 1 | +import pygame |
| 2 | +import random |
| 3 | + |
| 4 | +# Initialize Pygame |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +# Set up display |
| 8 | +width, height = 800, 600 |
| 9 | +screen = pygame.display.set_mode((width, height)) |
| 10 | +pygame.display.set_caption("Snake Game") |
| 11 | + |
| 12 | +# Colors |
| 13 | +white = (255, 255, 255) |
| 14 | +green = (0, 255, 0) |
| 15 | +red = (255, 0, 0) |
| 16 | + |
| 17 | +# Clock to control the game's frame rate |
| 18 | +clock = pygame.time.Clock() |
| 19 | + |
| 20 | +# Snake class |
| 21 | +class Snake: |
| 22 | + def __init__(self): |
| 23 | + self.body = [(random.randint(0, (width - 20) // 10) * 10, random.randint(0, (height - 20) // 10) * 10)] |
| 24 | + self.direction = random.choice(['UP', 'DOWN', 'LEFT', 'RIGHT']) |
| 25 | + |
| 26 | + def move(self): |
| 27 | + x, y = self.body[0] |
| 28 | + if self.direction == 'UP': |
| 29 | + self.body = [(x, y - 10)] + self.body[:-1] |
| 30 | + elif self.direction == 'DOWN': |
| 31 | + self.body = [(x, y + 10)] + self.body[:-1] |
| 32 | + elif self.direction == 'LEFT': |
| 33 | + self.body = [(x - 10, y)] + self.body[:-1] |
| 34 | + elif self.direction == 'RIGHT': |
| 35 | + self.body = [(x + 10, y)] + self.body[:-1] |
| 36 | + |
| 37 | + def grow(self): |
| 38 | + x, y = self.body[-1] |
| 39 | + if self.direction == 'UP': |
| 40 | + self.body.append((x, y - 10)) |
| 41 | + elif self.direction == 'DOWN': |
| 42 | + self.body.append((x, y + 10)) |
| 43 | + elif self.direction == 'LEFT': |
| 44 | + self.body.append((x - 10, y)) |
| 45 | + elif self.direction == 'RIGHT': |
| 46 | + self.body.append((x + 10, y)) |
| 47 | + |
| 48 | + def check_collision(self): |
| 49 | + return len(self.body) > 1 and self.body[0] in self.body[1:] |
| 50 | + |
| 51 | + def get_head_position(self): |
| 52 | + return self.body[0] |
| 53 | + |
| 54 | + def get_body(self): |
| 55 | + return self.body |
| 56 | + |
| 57 | +# Food class |
| 58 | +class Food: |
| 59 | + def __init__(self): |
| 60 | + self.position = (random.randint(0, (width - 20) // 10) * 10, random.randint(0, (height - 20) // 10) * 10) |
| 61 | + self.is_food_on_screen = True |
| 62 | + |
| 63 | + def spawn_food(self): |
| 64 | + if not self.is_food_on_screen: |
| 65 | + self.position = (random.randint(0, (width - 20) // 10) * 10, random.randint(0, (height - 20) // 10) * 10) |
| 66 | + self.is_food_on_screen = True |
| 67 | + return self.position |
| 68 | + |
| 69 | + def set_food_on_screen(self, choice): |
| 70 | + self.is_food_on_screen = choice |
| 71 | + |
| 72 | +def draw_objects(screen, snake, food): |
| 73 | + screen.fill(white) |
| 74 | + |
| 75 | + for pos in snake.get_body(): |
| 76 | + pygame.draw.rect(screen, green, pygame.Rect(pos[0], pos[1], 10, 10)) |
| 77 | + |
| 78 | + pygame.draw.rect(screen, red, pygame.Rect(food.position[0], food.position[1], 10, 10)) |
| 79 | + |
| 80 | + pygame.display.flip() |
| 81 | + |
| 82 | +def main(): |
| 83 | + snake = Snake() |
| 84 | + food = Food() |
| 85 | + |
| 86 | + while True: |
| 87 | + for event in pygame.event.get(): |
| 88 | + if event.type == pygame.QUIT: |
| 89 | + pygame.quit() |
| 90 | + quit() |
| 91 | + |
| 92 | + # Control the snake's direction |
| 93 | + keys = pygame.key.get_pressed() |
| 94 | + if (keys[pygame.K_UP] or keys[pygame.K_w]) and not snake.direction == 'DOWN': |
| 95 | + snake.direction = 'UP' |
| 96 | + elif (keys[pygame.K_DOWN] or keys[pygame.K_s]) and not snake.direction == 'UP': |
| 97 | + snake.direction = 'DOWN' |
| 98 | + elif (keys[pygame.K_LEFT] or keys[pygame.K_a]) and not snake.direction == 'RIGHT': |
| 99 | + snake.direction = 'LEFT' |
| 100 | + elif (keys[pygame.K_RIGHT] or keys[pygame.K_d]) and not snake.direction == 'LEFT': |
| 101 | + snake.direction = 'RIGHT' |
| 102 | + |
| 103 | + snake.move() |
| 104 | + |
| 105 | + if snake.check_collision(): |
| 106 | + break |
| 107 | + |
| 108 | + if snake.get_head_position() == food.position: |
| 109 | + snake.grow() |
| 110 | + food.set_food_on_screen(False) |
| 111 | + |
| 112 | + food_position = food.spawn_food() |
| 113 | + draw_objects(screen, snake, food) |
| 114 | + |
| 115 | + clock.tick(10) |
| 116 | + |
| 117 | + pygame.quit() |
| 118 | + quit() |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments