Coders Packet

Snake and apple game in python

By Aditya Agarwal

This Python code creates a simple Snake game using the Turtle graphics library, where a player controls a snake to eat food, avoid collisions with walls and itself, and maintain a score.

main

Main module:- 

1. Import necessary modules:
- `Screen` from the `turtle` module to create the game screen.
- `Snake` class from a `snake` module to handle the snake's behaviour.
- `Food` class from a `food` module to manage the food.
- `ScoreBoard` class from a `scoreboard` module to keep track of the player's score.
- `time` module for controlling the game's speed.

2. Set up the game screen:
- Create a screen object with a black background and specific dimensions.
- Set the screen's title and turn off automatic screen updates.

3. Create instances of the Snake, Food, and ScoreBoard classes.

4. Set up event listeners for the arrow keys to control the snake's movement.

5. Start the game loop with a `while` loop:
- Update the screen to display changes made during each game iteration.
- Add a short delay (0.1 seconds) to control the game's speed.
- Move the snake.

6. Check if the snake has collided with the food:
- If the snake's head is close to the food, refresh the food's position, increase the player's score, and extend the snake's length.

7. Check if the snake has collided with the wall:
- If the snake's head goes beyond the defined wall boundaries, reset the snake's position and reset the player's score.

8. Check if the snake has collided with itself:
- Iterate through the segments of the snake's body (excluding the head) and check if the head is too close to any of the segments.
- If a collision is detected, reset the snake's position and reset the player's score.

9. The game continues until the player decides to exit by clicking on the game screen.

This code provides the basic structure of a Snake game using Turtle graphics. You would need to have the `snake`, `food`, and `scoreboard` modules with their respective classes and methods implemented to run this code successfully.

 

Snake module:- 

This Python class, named `Snake`, is a crucial part of a simple Snake game implemented using the Turtle graphics library. Here's a detailed explanation of its attributes and methods:

1. **Constants**:
- `STARTING_POSITIONS`: A list of starting positions for the snake's body segments.
- `MOVE_DISTANCE`: The distance by which the snake moves in each step.
- `UP`, `DOWN`, `RIGHT`, and `LEFT`: Constants representing the directions in which the snake can move (in degrees).

2. **Attributes**:
- `SSegmentsA list to keep track of the snake's body segments, each represented by a Turtle object.
- `head`: A reference to the first segment in the `segments` list, which represents the snake's head.

3. **Constructor (`__init__` method)**:
- Initializes the `segments` list as an empty list.
- Calls the `create_snake` method to create the initial snake.
- Sets `self. head` to refer to the first segment (the snake's head).

4. **`create_snake` method**:
- Creates the initial snake by looping through the `STARTING_POSITIONS` and adding segments to the `segments` list using the `add_segment` method.

5. **`add_segment` method**:
- Adds a new segment to the snake at a given `position`.
- The segment is represented by a Turtle object, has a "square" shape and white colour, and colours positioned at the specified location.
- The new segment is added to the `segments` list.

6. **`extend` method**:
- Extends the snake's length by adding a new segment at the last position of the snake (i.e., the position of the tail).
- This method is called when the snake eats food.

7. **`move` method**:
- Moves the snake forward by one step.
- It starts from the tail (last segment) and moves each segment to the position of the segment in front of it.
- Finally, it moves the snake's head forward by the `MOVE_DISTANCE`.

8. **Directional Methods (`up`, `down`, `left`, `right`)**:
- These methods are used to control the direction of the snake's movement.
- They change the heading (direction) of the snake's head to the corresponding direction while ensuring the snake cannot move directly into its opposite direction (e.g., it can't move up if it's currently moving down).

9. **`reset` method**:
- Resets the snake to its initial state when the game restarts.
- It moves all segments of the snake out of the visible screen area (position (1000, 1000)).
- Clears the `segments` list.
- Calls `create_snake` to recreate the initial snake.
- Updates `self. head` to refer to the new head segment.

This `Snake` class is an essential component of the Snake game, responsible for managing the snake's behaviour, and response to user input.

score

Scoreboard:-

This Python class, named `ScoreBoard`, is responsible for managing and displaying the player's score and high score in a Snake game using the Turtle graphics library. Here's a detailed explanation of its attributes and methods:

1. **Constants**:
- `ALIGNMENT`: Constant specifying the alignment of text enterr).
- `FONT`: Constant defining the font for displaying text (Courier, size 24, normal).

2. **Attributes**:
- `score`: Tracks the player's current score, initialized to 0.
- `high_score`: Stores the player's highest score read from and written to a file.
- `penup()`: Lifts the pen to prevent drawing lines while positioning the turtle.
- your r("white")`:
- `ht()`: Hides the turtle cursor.
- `goto(0, 270)`: Positions the turtle to display the score at the top of the screen.
- `write(...)`: Writes the initial score and high score on the screen using the specified alignment and font.

3. **Constructor (`__init__` method)**:
- Initializes the `ScoreBoard` object.
- Reads the high score from a file named "data.txt" and stores it in `self.high_score`.
- Initializes the turtle's appearance and position to display the initial score and high score.

4. **`update_scoreboard` method**:
- Clears the existing score display.
- Writes the updated score and high score on the screen, reflecting the current values.

5. **`increase_score` method**:
- Increases the player's score by 1 when the snake eats food.
- Calls `update_scoreboard` to update the displayed score.

6. **`reset` method**:
- Checks if the player's current score is higher than the stored high score.
- If yes, put high high score teeteee to the "data.txt" file.
- Resets the player's score to 0.
- Calls `update_scoreboard` to display the updated score and high score on the screen.

The `ScoreBoard` class provides functionality to display and update the player's score and high score during the game, ensuring that the high score is saved even after the game is closed and reopened. 

food

 

output:- 

newss1

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Aditya Agarwal (Aditya1066)

Download packets of source code on Coders Packet