Uploaded by Umar

2D Platformer Game Project Report: C++ & OOP

Project
Report
2D Platformer Game using OOP in Raylib
"Leap into a world of obstacles, dodge danger, and rise above—every
jump takes you closer to victory!"
Course :
Object Oriented Programming in C++
Instructors:
Sir Sadiq Amin & Sir Zain ul Hasan
Members:
Ali Shayan
Qalam : 481438
Muhammad Umer Khawaja
Qalam : 468867
Date :
28 December, 2024
Software :
Ratlib
Language :
C++
Project Details
Table of Contents
1.
Background
2.
Introduction
3.
Objectives
4.
Program Flowchart
5.
Source Code
6.
Major Classes & Functions
7.
Game Snippets
8.
9.
Future Improvements
Conclusion
Background
2D Platformer Game project draws inspiration from classic platformer games that have
been a staple of the gaming industry for decades, such as Super Mario Bros. and Sonic the
Hedgehog. These games are known for their engaging mechanics, simple controls, and
challenging levels that require precision and strategy. The development of this project
seeks to explore these foundational game design principles while leveraging modern
programming techniques like Object-Oriented Programming (OOP) for code organization
and scalability. By utilizing C++ and graphical libraries, this project aims to create a game
that is not only functional but also emphasizes smooth gameplay mechanics, responsive
controls, and an enjoyable user experience. This serves as a practical introduction to game
development and programming concepts for both aspiring developers and gaming
enthusiasts.
Introduction
2D Platformer Game is a project designed to showcase fundamental game development
concepts using C++ and Object-Oriented Programming (OOP). Players control a character
navigating platforms, avoiding enemies, and overcoming obstacles to earn points.
The game implements key mechanics such as jumping, movement, and collision detection,
offering a dynamic and engaging experience. Developed with a focus on simplicity
and smooth gameplay, this project highlights the practical application of programming
principles in creating interactive entertainment.
Objective and Scope

Develop a 2D platformer game using C++ and
Object-Oriented Programming (OOP).

Implement
core
mechanics
like
player
movement, jumping, and collision detection.

Create interactive levels
difficulty and obstacles.

Design basic enemy AI with movement patterns
and behaviors.

Ensure smooth gameplay with responsive input
handling and optimized performance.
with
increasing
Project Flowchart
1. Game Initialization
o
o
o
Set up game window
Load assets (images, sounds)
Initialize player, enemies, platforms, and variables (e.g., score, level)
2. Main Game Loop
o
o
o
o
Process Input:
 Capture and process user inputs (keyboard or mouse)
Update Game State:
 Move player and enemies
 Handle collisions
 Apply gravity and jumping mechanics
Render the Scene:
 Draw the player, enemies, platforms, and background
Check for End Conditions:
 Check if the player has won or lost the level
3. Game Over or Level Transition
o Display game over screen or transition to next level
o Option to restart or quit
This flowchart captures the high-level steps involved in the game logic. I can create a visual
flowchart for you if you'd like!
Source Code
The code is placed in a text box. You can view it by scrolling down in the box.
#include "raylib.h"
#include <string>
const int screenWidth = 800;
const int screenHeight = 600;
const float Player_speed = 7.0f;
const float gravity = 0.5f;
const float jumpStrength = -10.0f;
const float bulletRadius = 5.0f;
const float bulletspeed = 10.0f;
const float birdSpeed = 7.0f;
const int numCollectibles_set1 = 14;
const int Platform_set1 = 20;
const int numCollectibles_set2 = 30;
const int Platform_set2 = 31;
const int maxBullets = 10;
const int birdSize = 64;
const int num_bird1 = 14;
const int num_bird2 = 29;
// Forward declarations
class Player;
class Platform;
class Enemies;
class Collectible;
class Bullet;
// Collectible Class
class Collectible {
public:
Rectangle rect;
bool collected;
Collectible() : collected(false) {} // Default constructor
Collectible(Rectangle r) : rect(r), collected(false) {} // Constructor
void draw() {
if (!collected) {
DrawRectangleRec(rect, GOLD);
}
}
};
// Bullet Class
class Bullet {
public:
Vector2 position;
bool fire;
Bullet() : fire(false) {} // Default constructor
void update(float bulletspeed) {
if (fire) {
position.x += bulletspeed;
}
}
void draw() {
if (fire) {
DrawCircleV(position, bulletRadius, YELLOW);
}
Major Classes & Functions
1. Collectible Class
The Collectible class represents items in the game that the player can collect, such as coins or
power-ups.
 Attributes:
o Rectangle rect: Defines the position and size of the collectible item.
o bool collected: Tracks whether the collectible has been picked up by the player.
 Functions:
o draw(): This function draws the collectible on the screen if it hasn't been collected
yet.
2. Bullet Class
The Bullet class manages the bullets fired by the player.
 Attributes:
o Vector2 position: Holds the current position of the bullet on the screen.
o bool fire: Indicates whether the bullet is active (fired) or not.
 Functions:
o update(float bulletspeed): Updates the bullet’s position by moving it horizontally
based on the specified speed.
o draw(): Draws the bullet on the screen if it is fired.
3. Platform Class
The Platform class defines the platforms on which the player can walk or jump.
 Attributes:
o Rectangle rect: Represents the position and size of the platform.
 Functions:
o draw(Texture2D groundTexture, ...): Draws the platform based on the current level
and the platform's size, using different textures for different platform types (e.g.,
ground, large, floating).
4. Player Class
The Player class represents the player character in the game, handling movement, jumping,
collision detection, and interaction with other game elements like collectibles and bullets.
 Attributes:
o Rectangle rect: The player’s position and size.
o Vector2 velocity: Tracks the player's velocity in both the x and y directions.
o bool onGround: Indicates if the player is standing on the ground or in the air.
o Rectangle player_run: Holds the sprite sheet coordinates for the running animation.
 Functions:
o run_right(), run_left(): These functions control the player’s movement to the right or
left.
o jump(): Makes the player jump by applying a vertical velocity when the spacebar is
pressed.
o player_gravity(): Applies gravity to the player, pulling them down if they are in the
o
o
o
o
o
air.
collect_coins(): Checks for collisions with collectibles and updates the score.
live(): Handles the player's life and resets position if the player falls below a certain
point.
firing(): Fires a bullet when the player presses the "Z" key, using an array of bullets.
draw_lives(): Draws heart icons to represent the player's remaining lives.
draw(): Renders the player sprite on the screen.
5. Enemies Class
The Enemies class defines the enemy entities in the game, including their movement and
behavior.
 Attributes:
o Rectangle rect: Position and size of the enemy.
o bool isAlive: Indicates if the enemy is still alive.
o Rectangle enemey_run: The sprite sheet coordinates for enemy animation.
o int b: A counter to handle the enemy's animation cycle.
 Functions:
o update(Player& player): Updates the enemy's position and animation based on the
player's proximity. The enemy moves towards the player if within a certain range.
o draw(): Draws the enemy on the screen if the enemy is alive.
Summary of Interaction:
 The Player moves left or right, jumps, and interacts with Collectibles (such as coins),
which increase the score when collected.
 The Player can also fire Bullets by pressing a key, and the Bullets move across the
screen.
 Platforms are drawn depending on the level, and the Player can land on these platforms.
 Enemies chase the Player if they are within a certain distance and animate accordingly.
 The Player can lose lives by falling off the screen or colliding with enemies
Game
Snippets
Main Menu
Level 1
Win & Lose Screen
LEVEL 2
Future Improvements
Challenges:

Collision Detection: Resolved clipping issues by improving algorithms.

Player Movement: Implemented a gravity system for smoother jumps.
Improvements:

Add more levels and obstacles.

Implement advanced enemy AI.

Introduce power-ups and sound effects.
Conclusion
This project demonstrated the application of C++ and OOP principles to create an
interactive and
functional 2D platformer game. The experience highlighted
key areas of game development and provided a strong foundation for future projects.
Related documents