This Python code uses Turtle graphics to create a simple Pong game where players control paddles to bounce a ball and earn points.
Scoreboard:-
Certainly! This Python class, `ScoreBoard`, is responsible for managing and displaying the scores of both players in a Pong game using the Turtle graphics library. Here's a detailed explanation of its attributes and methods:
1. **Attributes**:
- `penup()`: Lifts the pen to prevent drawing lines while positioning the turtle.
- `ht()`: Hides the turtle cursor.
- `colour ("white")`: Sets the text colour to white.
- `l_score` and `r_score`: Variables to store the scores of the left and right players, both initialized to 0.
2. **Constructor (`__init__` method)**:
- Initializes the `ScoreBoard` object.
- Sets up the turtle's appearance and position.
- Calls the `update_scoreboard` method to display the scores initially.
3. **`update_scoreboard` method**:
- Clears the existing score display.
- Moves the turtle to specific positions to display the left and right player's scores.
- Uses the `write` method to display the scores on the screen with a specified font and alignment.
4. **`l_point` method**:
- Increments the left player's score (`l_score`) by 1 when the left player scores a point.
- Calls `update_scoreboard` to update the displayed scores.
5. **`r_point` method**:
- Increments the right player's score (`r_score`) by 1 when the right player scores a point.
- Calls `update_scoreboard` to update the displayed scores.
This `ScoreBoard` class provides functionality to display and update the scores of both players during the Pong game. It uses the Turtle graphics library to render the scores on the screen, clearing the previous scores and writing the updated ones as the game progresses.
main:-
1. **Import Necessary Modules and Initialize Game Elements**:
- The code begins by importing the required modules: `Screen`, `Paddle`, `Ball`, and `ScoreBoard`.
- It initializes the game screen (`screen`), the ball (`ball`), and the scoreboard (`scoreboard`).
2. **Set Up the Game Screen**:
- `screen. tracer(0)` turns off automatic screen updates to improve game performance.
- `screen.setup(height=600, width=800)` sets up the screen dimensions.
- `screen. bgcolor("blue")` sets the background colour to blue.
- `screen. title("Pong")` sets the title of the game window.
3. **Create Paddles and Set Up Event Listeners**:
- Two paddles, `r_paddle` and `l_paddle`, are created using the `Paddle` class at specific initial positions.
- Event listeners are set up to respond to key presses:
- "Up" and "Down" arrow keys control the right paddle (`r_paddle`) movement.
- The "w" and "s" keys control the left paddle (`l_paddle`) movement.
4. **Initialize Game Loop**:
- The variable `game_is_on` is set to `True` to initiate the game loop.
5. **Game Loop**:
- The game loop (`while game_is_on`) continuously updates the game state and screen.
6. **Update the Screen**:
- `screen. update()` is used to update the screen and show changes made during each game iteration.
7. **Move the Ball**:
- `ball. move()` updates the ball's position.
- `time. sleep(ball.move_speed)` introduces a slight delay to control the ball's speed.
8. **Ball Collision Handling**:
- If the ball hits the top or bottom walls (vertical boundaries), it bounces by reversing its vertical velocity using `ball.bounce_y()`.
9. **Paddle Collision Handling**:
- If the ball is close enough to a paddle and the conditions are met (e.g., it's the proper distance and on the correct side of the paddle), it bounces off the paddle horizontally using `ball.bounce_x()`.
10. **Scoring**:
- If the ball crosses the right boundary (`ball. xcor() > 380`), it means the left player has scored a point. The ball is reset, and the left player's score is updated using `scoreboard.l_point()`.
- If the ball crosses the left boundary (`ball. xcor() < -350`), it means the right player has scored a point. The ball is reset, and the right player's score is updated using `scoreboard.r_point()`.
11. **Exit the Game**:
- The game loop continues until the player clicks on the game screen, at which point `screen. ExoClick ()` is called to exit the game gracefully.
This code provides the structure for a basic Pong game, where players control paddles to bounce a ball, score points, and continue playing until they choose to exit. The game's speed and responsiveness are managed through the game loop and the interaction between the ball and paddles.
ball:-
This Python class, named `Ball`, represents the behaviour and attributes of a ball object in a Pong game using the Turtle graphics library. Here's a detailed explanation of its attributes and methods:
1. **Attributes**:
- `colour ("white")`: Sets the colour of the ball to white.
- `shape("circle")`: Sets the shape of the ball to a circle.
- `penup()`: Lifts the pen to prevent drawing lines while positioning the turtle.
- `move_speed`: Sets the initial movement speed of the ball.
- `x_move`: Represents the horizontal movement speed of the ball.
- `y_move`: Represents the vertical movement speed of the ball.
2. **Constructor (`__init__` method)**:
- Initializes the `Ball` object with the specified attributes.
- The ball starts with a white colour, a circular shape, and is positioned at the centre of the screen.
- It is initially set to move horizontally (right) and vertically (up) with a specified speed (`move_speed`).
3. **`move` method**:
- Updates the ball's position by moving it in both the x and y directions.
- The new position is calculated based on the current position and the values of `x_move` and `y_move`.
4. **`detect_collision` method** (currently empty):
- This method is a placeholder for detecting collisions with other objects (e.g., paddles). You can implement collision detection logic here as needed.
5. **`bounce_y` method**:
- Inverts the direction of the ball's vertical movement (`y_move *= -1`), effectively making the ball bounce off the top or bottom walls.
6. **`bounce_x` method**:
- Inverts the direction of the ball's horizontal movement (`x_move *= -1`).
- Reduces the `move_speed` by 10% to make the ball gradually faster with each bounce.
7. **`reset_position` method**:
- Resets the ball's position to the centre of the screen (`(0, 0)`).
- Resets the `move_speed` to its initial value.
- Calls `bounce_x` to start the ball moving to the right again.
This `Ball` class provides the functionality for creating and controlling the behaviour of the game ball. It handles movement, collision detection (placeholder), bouncing off walls, and resetting the ball's position when a point is scored.
paddle:-
This Python class, named `Paddle`, defines the behaviour and attributes of a paddle in a Pong game using the Turtle graphics library. Here's a detailed explanation of its attributes and methods:
1. **Attributes**:
- `penup()`: Lifts the pen to prevent drawing lines while positioning the turtle.
- `colour ("white")`: Sets the colour of the paddle to white.
- `shape("square")`: Sets the shape of the paddle to a square.
- `shape size (5, 1)`: Adjusts the size of the paddle to be 5 times wider horizontally than its height (creating a rectangular shape).
- `goto(position)`: Positions the paddle at the specified initial position.
2. **Constructor (`__init__` method)**:
- Initializes the `Paddle` object with the specified attributes.
- The paddle is created at the given `position` (specified as a tuple) and set to an initial state.
3. **`go_up` method**:
- Moves the paddle upwards by 20 units along the y-axis.
- The new y-coordinate is calculated (`new_y`) by adding 20 to the current y-coordinate, and then the paddle is moved to the new position.
4. **`go_down` method**:
- Moves the paddle downwards by 20 units along the y-axis.
- The new y-coordinate is calculated (`new_y`) by subtracting 20 from the current y-coordinate, and then the paddle is moved to the new position.
These methods (`go_up` and `go_down`) allow for the control of the paddle's movement in the vertical direction, enabling the player to move the paddle up and down within the game screen. The paddle's appearance, size, and colour are also configured in the constructor.
output;-
Submitted by Aditya Agarwal (Aditya1066)
Download packets of source code on Coders Packet
Comments