Skip to content

Daniel-Ro-Santiago/Pong-Game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Pong-Game

Table of contents

Project overview

This project implements the classic Pong game in Python using Pygame, featuring local multiplayer gameplay where two players control opposite paddles. The objective of the game is to score three points against your opponent to win.

Technologies

  • Python
  • Pygame

Features

Here is what you can do:

  • Classic Pong gameplay: A Python implementation of the original Pong game.
  • Local multiplayer Two players can play on the same keyboard, controlling the left and right paddles using the following controls:

Left player:

Function Command
Move up W
Move down S

Right player:

Function Command
Move up (Up arrow)
Move down (Down arrow)
  • Score tracking: Real-time score display for both players.

Game architecture

The game architecture is structured as follows:

Game architecture

Limitations

The main limitations of this project are:

  • The game does not include an AI opponent.
  • No graphical user interface or menus.
  • No configuration options.

Process

First, I defined the window size and core game parameters, including colors, paddle dimensions, ball radius, font settings for the score display, the winning condition, and an original configuration of 60 frames per second (FPS).

Next, I created two main classes, Paddle and Ball, to define the behavior of the game elements, such as movement, velocity, collision logic, and rendering. Handling ball collisions requires special attention to the angle of impact. Collisions with the screen boundaries simply reverse the ball’s direction, while paddle collisions depend on the displacement between the center of the ball and the center of the paddle, as shown in the image below:

We can calculate the midpoint of the paddle $m$ and the displacement $d$ as follows:

$$ m = x_{1} + \frac{h}{2} $$

$$ d = m - x_{2} $$

To adjust the ball’s velocity after a collision, we need to consider a reduction factor $r$ calculated as follows:

$$ m\acute{a}x \ velocity = \frac{d}{r} $$

$$ = r = \frac{d}{m\acute{a}x \ velocity} $$

As we want to allow the ball’s speed and direction to vary depending on the impact point we need to divide the paddle into sections based on the máximum displacement of the ball from a paddle corner to the center of the paddle, which is $\frac{h}{2}$. Substituting this value, we obtain:

$$ r = \frac{\frac{h}{2}}{m\acute{a}x \ velocity} = \frac{h}{2(m\acute{a}x \ velocity)} $$

This can be represented as follows:

The number of possible bounce angles depends on the chosen $m\acute{a}x \ velocity$. After implementing the physics, I rendered all visual elements on the screen, including the paddles, the ball, the score display, and the dashed center line.

Finally, I created the main game loop and integrated all components.

What I learned

The most important thing I learned from this project is that designing collision mechanics is a complex task that requires handle of multiple conditions. However, it is possible to build simplified yet effective physics models for games and simulations.

How can it be improved

  • Create an AI opponent.
  • Add new gameplay modes and mechanics.
  • Design a title screen and improve the visuals.

Running the project

To run the project, download the Pong.py file and execute it. Make sure you have Pygame installed before running the game.

Preview

This is how the game is supposed to look:

Video_Pong.mp4

About

This project implements the classic Pong game in Python using Pygame, featuring local multiplayer with two players controlling opposite paddles.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages