Recreating Pong
Published:
pong-arcade
, a simple pong game developed using Python and Pygame. Pong
Pong is a classic game that was released in 1972. The game is a two-player game where each player controls a paddle. The objective of the game is to hit the ball past the opponent's paddle. The game ends when one player reaches 10 points.
Program Design
The game is developed using Python and Pygame. Pong consists of one or two players and a ball. Therefore, the design of the program is based on three types of objects: Player
, Ball
, and Pong
.
Player
The Player
object represents a player in the game.
Click here to show its attributes:
id
: The ID number of the player.color
: The color of the player.score
: The current score of the player.winner
: The flag to indicate if the player is the winner.paddle
: The paddle of the player. (pygame.Rect)
Click here to show its methods:
__init__(self, id, color)
: Initializes the player with the given ID and color.reset_score(self)
: Resets the player's score to 0.set_paddle_pos(self)
: Sets the default position of the player's paddle.ensure_in_bound_top(self)
: Ensures that the player's paddle does not go above the top of the screen.ensure_in_bound_bot(self)
: Ensures that the player's paddle does not go below the bottom of the screen.
Ball
The Ball
object represents the ball in the game.
Click here to show its attributes:
default_pos
: The default position of the ball.rect
: The ball's position and size. (pygame.Rect)default_speed
: The default speed of the ball.x_diff
: The derivative of the ball's x-coordinate.y_diff
: The derivative of the ball's y-coordinate.top_edge_line
: The top edge of the screen.bot_edge_line
: The bottom edge of the screen.edge_flag
: The flag to indicate if the ball is at the top or bottom edge of the screen.paddle_flag
: The flag to indicate if the ball is at the paddle.
Click here to show its methods:
__init__(self, color)
: Initializes the ball with the given color.reset_position(self)
: Resets the ball's position to the default position.reset_movement(self)
: Resets the ball's movement to the default speed.reset_flags(self)
: Resets the ball's flags.reset(self)
: Resets the ball's position, movement, and flags.increase_speed(self)
: Increments the speed of the ball.get_collision_point(self, paddle)
: Returns the collision point of the ball with the paddle.trajectory(self)
: Calculates the trajectory of the ball.bounce_off_edges(self)
: Bounces the ball off the top and bottom edges of the screen.bounce_off_paddles(self, paddle1, paddle2)
: Bounces the ball off the paddles.shift_pos_horizontal(self)
: Shifts the ball's position horizontally.shift_pos_vertical(self)
: Shifts the ball's position vertically.move(self)
: Moves the ball.
Pong
The Pong
object represents the game itself.
Click here to show its attributes:
surface
: The game's display surface.clock
: The game's clock. (pygame.time.Clock)dir
: The directory of the game.file
: The file of the game.font
: The font of the game. (pygame.font.Font)font_big
: The big font of the game. (pygame.font.Font)font_small
: The small font of the game. (pygame.font.Font)font_tiny
: The tiny font of the game. (pygame.font.Font)sound_paddle
: The sound of the ball hitting a paddle. (pygame.mixer.Sound)sound_wall
: The sound of the ball hitting a wall. (pygame.mixer.Sound)sound_score
: The sound of a scoring play. (pygame.mixer.Sound)help_rect
: Represents the help menu item. (pygame.Rect)opt1_rect
: Represents the first menu item. (pygame.Rect)opt2_rect
: Represents the second menu item. (pygame.Rect)opt3_rect
: Represents the third menu item. (pygame.Rect)show_help_menu
: The flag to indicate if the help menu is shown.opt1_selected
: The flag to indicate if the first menu item is selected.opt2_selected
: The flag to indicate if the second menu item is selected.opt3_selected
: The flag to indicate if the third menu item is selected.show_menu
: The flag to indicate if the menu is shown.paused
: The flag to indicate if the game is paused.show_win_screen
: The flag to indicate if the win screen is shown.running
: The flag to indicate if the game is running.bg_color
: The background color of the game.fg_color
: The foreground color of the game.red
: The red color of the game.blue
: The blue color of the game.p1
: The firstPlayer
of the game.p2
: The secondPlayer
of the game.ball
: TheBall
for the game.
Click here to show its methods:
__init__(self, lightmode=False)
: Initializes the game.- Run
pong
with any argument to enable light mode. pong 1
set_window_properties(self)
: Sets the window properties of the game.print_info(self)
: Prints the information of the game.reset_game(self)
: Resets the game.start_new_game(self)
: Starts a new game.menu_keyboard_select(self)
: Handles the keyboard selection in the menu.menu_mouse_select(self)
: Handles the mouse selection in the menu.go_to_menu(self)
: Goes to the menu.toggle_pause(self)
: Toggles a game pause.handle_event(self, event)
: Handles a game event.is_winner(self, player)
: Determines if a player is a winner.game_in_progress(self)
: Determines if the game is in progress.give_victory(self, player)
: Gives victory to a player.check_for_winner(self)
: Checks for a winner of the game.check_for_score(self)
: Checks for a scoring play.tick(self)
: Updates the game clock.ensure_boundaries(self)
: Ensures that the paddles do not go out of bounds.ai_paddle_movement(self)
: Moves the opposing paddle in single-player mode.update_paddle_positions(self)
: Updates the paddle positions.update_ball_position(self)
: Updates the ball position.draw_background(self)
: Draws the background of the game.draw_center_line(self)
: Draws the center line of the game.draw_ball(self)
: Draws the ball of the game.draw_scores(self)
: Draws the scores of the game.draw_paddles(self)
: Draws the paddles of the game.draw_game(self)
: Draws the game.draw_title(self)
: Draws the title screen of the game in the menu.draw_options(self)
: Draws the game's options menu.draw_info(self)
: Draws the game's information.draw_menu(self)
: Draws the game's menu.draw_arrow(self)
: Draws an arrow on the screen.draw_help_arrows(self)
: Draws the arrows in the help menu.draw_help_text(self)
: Draws the text in the help menu.draw_help_menu(self)
: Draws the help menu.draw_paused_screen(self)
: Draws the paused screen.draw_win_screen(self)
: Draws the win screen.draw_frame(self)
: Draws the frame of the game.update_frame(self)
: Updates the frame of the game.
Main Script Logic
The main script logic is as follows:
Initialize the game
Start the game loop
Handle events
Update object positions
Draw the game
Update the display
What I Learned -- Python Packaging
The main thing that I learned from this project was Python packaging. I learned how to build a Python package, and I then learned how to publish this Python package to PyPI.
Packaging Python projects can be rather confusing at first. There are many tools and libraries available, and it can be difficult to decide which one to use. I found the hatchling build backend to be a good tool for building Python packages.
The reason I chose hatchling is because I wanted to keep all of my package configuration information in the pyproject.toml file. This file is easy to read and understand, and it keeps all of the configuration information in one place. Another reason I chose this build backend is because there already exists an ecosystem of helpful plugins. In this project, I used the hatch-vcs plugin to help with version control.
pyproject.toml
My pyproject.toml file may be useful to you if you are looking to package a similar Python project. PyPI offers a nice guide for those looking to learn how to package Python projects.
Click here to download a sample pyproject.toml configuration that uses the hatchling
build backend with the hatch-vcs
plugin to assist with version control.
If your pyproject.toml and project structure are correct, you can build and install your package by running the following commands:
python3 -m build
python3 -m pip install dist/*.tar.gz
Conclusion
I hope you enjoyed reading about pong-arcade
and Python packaging. If you have any questions or feedback, feel free to reach out to me.