give me the snake code
- User
give me the snake code
Assistant
Here's a simple implementation of the classic Snake game in Python using the
pygamelibrary. This code includes basic functionality like movement, food generation, collision detection, and scoring.
🐍 Snake Game Code (Python + Pygame)
import pygame import random import sys # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 600, 400 CELL_SIZE = 20 FPS = 10 # Colors WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) BLACK = (0, 0, 0) # Set up the display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Snake Game") clock = pygame.time.Clock() # Font for score font = pygame.font.SysFont(None, 25) def draw_text(text, color, x, y): text_surface = font.render(text, True, color) screen.blit(text_surface, (x, y)) def main(): # Initial snake and food snake = [(100, 50), (80, 50), (60, 50)] direction = (CELL_SIZE, 0) # Start moving right food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE, random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE) score = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Handle key presses if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and direction != (0, CELL_SIZE): direction = (0, -CELL_SIZE) elif event.key == pygame.K_DOWN and direction != (0, -CELL_SIZE): direction = (0, CELL_SIZE) elif event.key == pygame.K_LEFT and direction != (CELL_SIZE, 0): direction = (-CELL_SIZE, 0) elif event.key == pygame.K_RIGHT and direction != (-CELL_SIZE, 0): direction = (CELL_SIZE, 0) # Move the snake head_x, head_y = snake[0] new_head = (head_x + direction[0], head_y + direction[1]) snake.insert(0, new_head) # Check for food collision if new_head == food: score += 1 food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE, random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE) else: snake.pop() # Remove the tail if no food is eaten # Check for wall or self collision if (new_head[0] < 0 or new_head[0] >= WIDTH or new_head[1] < 0 or new_head[1] >= HEIGHT or new_head in snake[1:]): print(f"Game Over! Final Score: {score}") pygame.quit() sys.exit() # Draw everything screen.fill(BLACK) for segment in snake: pygame.draw.rect(screen, GREEN, (*segment, CELL_SIZE, CELL_SIZE)) pygame.draw.rect(screen, RED, (*food, CELL_SIZE, CELL_SIZE)) draw_text(f"Score: {score}", WHITE, 10, 10) pygame.display.flip() clock.tick(FPS) if __name__ == "__main__": main()
🔧 How to Run the Code
-
Install Pygame (if you haven't already):
pip install pygame -
Save the code to a file, e.g.,
snake.py. -
Run the script:
python snake.py
🕹️ Controls
- Arrow Keys (↑ ↓ ← →) to move the snake.
- The game ends if the snake hits the wall or itself.
🧠 Notes
- This is a basic version. You can enhance it with features like:
- Increasing difficulty over time.
- Sound effects.
- A start/pause menu.
- High score tracking.
Let me know if you want to expand this! 🐍
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.