This is a classic Snake Game implemented using Python and the Pygame library. The objective of the game is to control a snake, move it around the game window, eat food to grow longer, and avoid colliding with the boundaries or the snake's own body.
These instructions will help you get the game up and running on your local machine.
- Python 3.x
- Pygame library
-
Clone the repository: https://github.com/ParsaZa79/CW-Snake-Game.git
-
Navigate to the project directory:
cd snake-game- Install the required dependencies:
pip install -r requirements.txt- Run the game:
python snake_game.py- Use the arrow keys to control the snake's movement:
- Up Arrow: Move the snake upwards
- Down Arrow: Move the snake downwards
- Left Arrow: Move the snake to the left
- Right Arrow: Move the snake to the right
- Eat the food (red squares) to grow longer and increase your score.
- Avoid colliding with the boundaries of the game window or the snake's own body.
- The game ends if the snake collides with the boundaries or itself.
- Press 'Q' to quit the game or 'C' to play again after the game ends.
- Responsive snake movement controlled by arrow keys.
- Randomly generated food positions.
- Snake growth and score tracking.
- Collision detection with boundaries and self-collision.
- End game screen with options to quit or play again.
The code is structured into the following main components:
Snakeclass: Represents the snake and handles its movement, growth, and collision detection.Foodclass: Represents the food and handles its random position generation.game_loopfunction: Implements the main game loop, handling events, updating game state, and rendering.messagefunction: Displays game over messages on the screen.
You can customize various aspects of the game by modifying the following variables in the code:
window_widthandwindow_height: Adjust the size of the game window.snake_block_size: Change the size of each block that makes up the snake and food.snake_speed: Modify the speed of the snake's movement.- Colors (
white,black,red,green): Change the colors used for the snake, food, and background.
Feel free to experiment and make the game your own!
Contributions are welcome! If you find any bugs or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License.