├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # Snake Game 🐍🎮 2 | A classic Snake game built with Python using Pygame. Control the snake, eat food, and grow while avoiding collisions! 3 | 4 | ## 📌 Features 5 | - Smooth gameplay with increasing difficulty 6 | - Customizable snake speed and colors 7 | - Sound effects and game-over animations 8 | - Score tracking and high-score saving 9 | 10 | ## 🔗 Project Structure 11 | /snake-game-python 12 | ├── src/ # Game source code 13 | ├── assets/ # Game assets (images, sounds) 14 | ├── README.md # Setup and game instructions 15 | ├── requirements.txt # Dependencies 16 | ├── config.json # Game settings 17 | ├── main.py # Entry point for the game 18 | 19 | ## 🚀 How to Run 20 | 21 | ### **1. Clone the repository:** 22 | ```bash 23 | git clone https://github.com/your-username/snake-game-python.git 24 | cd snake-game-python 25 | pip install -r requirements.txt 26 | python main.py 27 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | 2 | --- 3 | 4 | ### **Basic Code for `main.py`** 5 | ```python 6 | import pygame 7 | import random 8 | 9 | # Initialize Pygame 10 | pygame.init() 11 | 12 | # Game Settings 13 | WIDTH, HEIGHT = 600, 400 14 | GRID_SIZE = 20 15 | WHITE = (255, 255, 255) 16 | GREEN = (0, 255, 0) 17 | RED = (255, 0, 0) 18 | BLACK = (0, 0, 0) 19 | 20 | # Create Game Window 21 | screen = pygame.display.set_mode((WIDTH, HEIGHT)) 22 | pygame.display.set_caption("Snake Game") 23 | 24 | # Snake & Food Initialization 25 | snake = [(100, 100), (80, 100), (60, 100)] 26 | food = (random.randint(0, WIDTH // GRID_SIZE - 1) * GRID_SIZE, 27 | random.randint(0, HEIGHT // GRID_SIZE - 1) * GRID_SIZE) 28 | direction = (GRID_SIZE, 0) 29 | 30 | # Game Loop 31 | clock = pygame.time.Clock() 32 | running = True 33 | while running: 34 | screen.fill(BLACK) 35 | 36 | # Event Handling 37 | for event in pygame.event.get(): 38 | if event.type == pygame.QUIT: 39 | running = False 40 | elif event.type == pygame.KEYDOWN: 41 | if event.key == pygame.K_UP and direction != (0, GRID_SIZE): 42 | direction = (0, -GRID_SIZE) 43 | elif event.key == pygame.K_DOWN and direction != (0, -GRID_SIZE): 44 | direction = (0, GRID_SIZE) 45 | elif event.key == pygame.K_LEFT and direction != (GRID_SIZE, 0): 46 | direction = (-GRID_SIZE, 0) 47 | elif event.key == pygame.K_RIGHT and direction != (-GRID_SIZE, 0): 48 | direction = (GRID_SIZE, 0) 49 | 50 | # Move Snake 51 | new_head = (snake[0][0] + direction[0], snake[0][1] + direction[1]) 52 | if new_head in snake or new_head[0] < 0 or new_head[1] < 0 or new_head[0] >= WIDTH or new_head[1] >= HEIGHT: 53 | running = False # Game Over 54 | else: 55 | snake.insert(0, new_head) 56 | if new_head == food: 57 | food = (random.randint(0, WIDTH // GRID_SIZE - 1) * GRID_SIZE, 58 | random.randint(0, HEIGHT // GRID_SIZE - 1) * GRID_SIZE) 59 | else: 60 | snake.pop() 61 | 62 | # Draw Food 63 | pygame.draw.rect(screen, RED, (*food, GRID_SIZE, GRID_SIZE)) 64 | 65 | # Draw Snake 66 | for segment in snake: 67 | pygame.draw.rect(screen, GREEN, (*segment, GRID_SIZE, GRID_SIZE)) 68 | 69 | pygame.display.flip() 70 | clock.tick(10) # Adjust speed 71 | 72 | pygame.quit() 73 | --------------------------------------------------------------------------------