https://github.com/alexandrefrt/pong-cpp

A Pong game prgrammed in C++ with SFML and Visual Studio 2022.

https://github.com/alexandrefrt/pong-cpp

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (5.3%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

A Pong game prgrammed in C++ with SFML and Visual Studio 2022.

Basic Info
  • Host: GitHub
  • Owner: AlexandreFrt
  • License: mit
  • Language: C++
  • Default Branch: main
  • Homepage:
  • Size: 15.2 MB
Statistics
  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created almost 2 years ago · Last pushed almost 2 years ago
Metadata Files
Readme License

README.md

Image of the game

Image of the game

How to play ?

Controls

Left racket :\ Move up : Z\ Move down : S

Right racket :\ Move up : \ Move down :

Toggle pause : Escape

How it works ?

[!IMPORTANT] This project was made with Visual Studio 2022. You can find all he settings in the main.h file.\ Therefore, you can change all the settings.


For instance, if you want to change the speed of the rackets, you need to change these values.

cpp const float racketLSpeed{ 7.f }; const float racketRSpeed{ 7.f };

[!NOTE] racketL : Left racket\ racketR : Right racket

Collision calculation

I used the Axis-Aligned Bounding Box (AABB) algorithm to calculate the intersections between the balls and the rackets or borders in 2D.

The boolean function : $f(A,B) = (A{\text{minX}} \leq B{\text{maxX}} \land A{\text{maxX}} \geq B{\text{minX}}) \land (A{\text{minY}} \leq B{\text{maxY}} \land A{\text{maxY}} \geq B{\text{minY}})$

Collision calculation\ Collision calculation

```cpp // Collision with left racket if (ballMinX <= racketLMaxX && ballMaxX >= racketLMinX && ballMinY <= racketLMaxY && ballMaxY >= racketLMinY) { const float racketMiddleY = racketLMinY + racketLHeight / 2.0f;

if (ballMaxY >= racketMiddleY)
{
    collisionObject = BottomRacketL;
}
else
{
    collisionObject = TopRacketL;
}

} // Collision with right racket else if (ballMinX <= racketRMaxX && ballMaxX >= racketRMinX && ballMinY <= racketRMaxY && ballMaxY >= racketRMinY) { const float racketMiddleY = racketRMinY + racketRHeight / 2.0f;

if (ballMaxY >= racketMiddleY)
{
    collisionObject = BottomRacketR;
}
else
{
    collisionObject = TopRacketR;
}

} ```


[!NOTE] If that interests you, you can read the article below.\ https://developer.mozilla.org/en-US/docs/Games/Techniques/3Dcollisiondetection

Main settings

Window settings

cpp // Window properties const string GAME_TITLE{ "Pong" }; const unsigned int WINDOW_WIDTH{ 800 }; const unsigned int WINDOW_HEIGHT{ 600 }; const unsigned int FRAME_LIMIT{ 60 };

[!WARNING]
The frame limit influences game speed.

Game settings

cpp // Game properties const Color PLAYER_L_COLOR{ Color::Blue }; const Color PLAYER_R_COLOR{ Color::Red }; const Color BALL_COLOR{ Color::White }; const Player DEFAULT_PLAYER = PlayerRight; const unsigned int MAX_SCORE{ 10 }; const float RACKET_L_SPEED{ 7.f }; const float RACKET_R_SPEED{ 7.f }; const float DEFAULT_BALL_SPEED{ 7.f }; // With each collision with a racket, the speed of the ball increases by this value const float BALL_SPEED_INCREASE_VALUE{ 0.15f };

Volume

cpp // Sound properties (Volume) // Racket collision sound const float RACKET_SOUND_VOLUME{ 10.f }; // Wall collision sound const float WALL_SOUND_VOLUME{ 10.f };

[!NOTE] The volume is clamped between 0 and 100. There is no danger in changing it.

Racket properties

```cpp // Left racket properties const unsigned int RACKETLWIDTH{ 16 }; const unsigned int RACKETLHEIGHT{ 80 }; const unsigned int DEFAULTRACKETLPOSX{ 32 }; const unsigned int DEFAULTRACKETLPOSY{ (WINDOWHEIGHT / 2) - (RACKETLHEIGHT / 2) }; // Minimum and maximum location of the racket const unsigned int RACKETLMINPOSY{ 0 }; const unsigned int RACKETLMAXPOSY{ WINDOWHEIGHT - RACKETLHEIGHT };

// Right racket properties const unsigned int RACKETRWIDTH{ 16 }; const unsigned int RACKETRHEIGHT{ 80 }; const unsigned int DEFAULTRACKETRPOSX{ WINDOWWIDTH - 32 - RACKETRWIDTH }; const unsigned int DEFAULTRACKETRPOSY{ (WINDOWHEIGHT / 2) - (RACKETRHEIGHT / 2) }; // Minimum and maximum location of the racket const unsigned int RACKETRMINPOSY{ 0 }; const unsigned int RACKETRMAXPOSY{ WINDOWHEIGHT - RACKETR_HEIGHT }; ```

Ball properties

cpp // Ball properties const float BALL_RADIUS{ 12.f }; // Default location of the ball const unsigned int DEFAULT_BALL_POS_X{ static_cast<unsigned int>(WINDOW_WIDTH / 2.f - BALL_RADIUS / 2.f) }; const unsigned int DEFAULT_BALL_POS_Y{ static_cast<unsigned int>(WINDOW_HEIGHT / 2.f - BALL_RADIUS) };

Owner

  • Name: Alexandre
  • Login: AlexandreFrt
  • Kind: user

Junior Developper

GitHub Events

Total
Last Year