Skip to content

Hassan-Mef/Space-Shooter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Space Shooter Game (C++ OOP Project)

Space Shooter is a 2D arcade-style shooter game built with C++ and raylib. It was developed as a semester project for the Object-Oriented Programming (OOP) course at ITU. The game encapsulates major OOP principles such as inheritance, polymorphism, templates, file I/O, and design patterns like Singleton, all wrapped in an engaging and nostalgic arcade experience.


🎮 Gameplay Overview

🧠 Classic concept — Modern C++

  • Use A and D keys to move your spaceship left/right.
  • Press SPACE to shoot bullets upward and destroy incoming enemies.
  • Each enemy destroyed gives you +10 points, but if:
    • An enemy reaches the bottom → –10 points
    • An enemy hits your ship → 💀 Game Over
  • The game saves your progress automatically using a .json file in the Player_data/ folder and maintains a global high score in HighestScore.txt.

🧠 OOP Concepts Demonstrated

Concept Implemented In
Encapsulation Classes like Ship, Enemies, Bullets, etc.
Inheritance All drawable objects inherit from Game_objects
Polymorphism Virtual draw() function in base class
Templates Player<U, S> class using templates
File I/O Save/load scores via .json & .txt
Static / Singleton Console and player score logic
Operator Overloading operator<< in Player class

🧠 Code Structure (OOP in Action)

🚀 Ship, Bullets, and Enemies Inherit from Game_objects

class Game_objects {
protected:
    int x, y;
public:
    Game_objects(int x, int y);
    virtual void draw() = 0; // Polymorphic draw
};

🎯 Bullet-Enemy Collision Detection

float closestX = max(enemies[i].getX(), min(bullets[j].getX(), enemies[i].getX() + enemies[i].getWidth()));
float closestY = max(enemies[i].getY(), min(bullets[j].getY(), enemies[i].getY() + enemies[i].getHeight()));
float distanceX = bullets[j].getX() - closestX;
float distanceY = bullets[j].getY() - closestY;

if ((distanceX * distanceX) + (distanceY * distanceY) <= (bullets[j].getRadius() * bullets[j].getRadius())) {
    bullets.erase(bullets.begin() + j);
    enemies.erase(enemies.begin() + i);
    score += 10;
}

💾 Saving Player Score Using JSON

void Player<U, S>::saveToFile(U name) {
    json j;
    j[name] = getScore(name);
    ofstream outfile(name + ".json");
    outfile << setw(4) << j;
}

🎮 Level Handling Logic

float Game_Manager::level_Manager() {
    if (score > 100) return 0.5;
    else if (score < 200) return 1.0;
    return 0.0;
}

🗂️ Folder Structure

Space-Shooter/
├── .vs/
├── Fonts/ # Custom game fonts
├── Player_data/ # JSON files storing player scores
├── Sounds/ # Game background music and sound effects
├── external_lib/nlohmann/ # JSON library
├── x64/
│ └── Debug/
├── Bullets.cpp/h # Bullet logic
├── Console.cpp/h # Console class (friend of Game_Manager)
├── Enemies.cpp/h # Enemy logic and movement
├── Game_Manager.cpp/h # Main game loop and control logic
├── Game_objects.cpp/h # Abstract base class for all drawable objects
├── HighestScore.txt # Stores highest score across all players
├── Player.cpp/h # Player template class with file I/O
├── Ship.cpp/h # Player spaceship logic
├── Space-Shooter.cpp # Entry point for the game
├── Space-Shooter.sln # Visual Studio Solution file
├── Space-Shooter.vcxproj # Project configuration

🧩 Requirements

Dependency Purpose
raylib Graphics, Input, Audio
nlohmann/json JSON I/O for player scores

📦 How to Setup

This project is built with Visual Studio on Windows.

  1. Clone this repository:
    git clone https://github.com/Hassan-Mef/Space-Shooter-game.git
  2. Open the solution file Space-Shooter.sln in Visual Studio.
  3. Make sure raylib is properly set up in your include/lib directories.
  4. Build the project and run.

💾 Score Management

  • On game launch, player is prompted to enter a name.
  • If the name exists in /Player_data, their profile is loaded.
  • If not, a new profile is created using Player<string, int> with default score.
  • All scores are auto-saved after the game ends.

📝 Example:

Player_data/
└── Shaggy.json
    {
        "Shaggy": 120
    }

📸 Screenshots (Optional)


🧠 Learning Takeaways

  • Applying OOP in real-world projects.
  • Working with external libraries (raylib, JSON).
  • Understanding memory, vector management, object lifecycles.
  • Handling I/O via file streams for persistent data storage.

🧑‍💻 Author

Made with ❤️ by a Hassan-Mef from ITU as part of the university OOP coursework.


📜 License

This project is provided for academic and educational purposes. Feel free to fork, clone, or modify.


💡 Future Ideas

  • Power-ups and level scaling
  • Multiple enemy types with varying difficulty
  • Add UI screens (pause, leaderboard, etc.)
  • Gamepad and mobile support

About

A simple c++ game created using Raylib library

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages