This project is a simple yet engaging implementation of the classic Pong game using Python and the Turtle graphics module. The game follows the traditional mechanics of Pong, where two players control paddles to hit a bouncing ball and prevent it from crossing their respective sides. The game runs in a graphical window, tracks the score, and increases difficulty dynamically as the ball's speed adjusts upon paddle hits.
The project consists of four main files:
This is the core of the game, responsible for initializing the game environment, handling player controls, and implementing the game loop.
- Sets up the game screen (800x600, black background, titled "Pong").
- Creates game objects: two paddles, the ball, and the scoreboard.
- Listens for keyboard inputs to control paddle movements.
- Manages game mechanics, such as ball movement, paddle collision detection, scoring, and game speed adjustments.
This file defines the Paddle class, which represents the player-controlled paddles.
- Each paddle is a white rectangular object positioned on either side of the screen.
- The class has methods
up()anddown()to move the paddle up and down. - Uses the Turtle graphics module to render the paddle and handle movement.
The Ball class manages the behavior of the Pong ball.
- The ball is initialized as a white circle with a random starting angle.
- The
move()method controls its forward movement. bounce_wall()changes the ball’s direction when it hits the top or bottom walls.bounce_paddle()changes the ball’s direction when it collides with a paddle and increases speed.reset_position()resets the ball to the center when a player scores, choosing a new random direction.
This file contains the Scoreboard class, responsible for displaying and updating the players' scores.
- Maintains
l_scoreandr_scorevariables for the left and right players. - Uses the
update_scoreboard()method to update the score display whenever a player scores a point. - Includes methods
l_point()andr_point()to increase scores when the ball crosses the screen boundaries.
Several design choices were made to ensure smooth gameplay:
- The ball starts with a random heading to create variation in gameplay.
- When the ball hits a paddle, its speed increases slightly, making the game progressively harder.
- Paddle collision detection is based on the ball's distance from the paddle and its X-coordinate range.
- Wall collisions result in a vertical bounce, preserving the physics of a Pong game.
- The game listens for key presses (
W/Sfor the left paddle,Up/Downfor the right paddle). onkeypress()ensures smooth movement without holding down keys continuously.
- The
whileloop continuously updates the screen and moves the ball based on its speed. screen.tracer(0)ensures smooth frame updates without excessive rendering lag.- A slight delay (
time.sleep(ball.move_speed)) keeps the game responsive while controlling speed changes dynamically.