Skip to content

Latest commit

 

History

33 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Platformer 2D (SFML, C++)

English version below


Gra platformowa 2D w C++17 z biblioteką SFML 2.x. Gracz przechodzi kampanię czterech poziomów, zbiera monety, skacze (triple jump), strzela pociskami poziomo, unika lub eliminuje wrogów patrolujących platformy. Są animacje sprite’ów, SFX, muzyka i parallax z innym zestawem teł na każdym poziomie (m.in. grafiki Kenney — las, pustynia, zamek, noc). Menu główne, ekran Top 10 oraz zapis wyników do scores.json (łączna liczba monet z całej kampanii i czas).


Wymagania

  • C++17
  • CMake >= 3.20
  • SFML 2.5+ (gałąź 2.x — nie 3.x)

Instalacja SFML

macOS (Homebrew)

brew install sfml

Linux (Debian/Ubuntu)

sudo apt install libsfml-dev

Windows (vcpkg) — instalacja krok po kroku

CMakeLists.txt zawiera ścieżki zakodowane na sztywno i oczekuje, że vcpkg znajduje się dokładnie w C:\game2d\vcpkg, a biblioteka SFML w C:\game2d\vcpkg\vcpkg_installed\x64-windows. Poniższe kroki tworzą dokładnie taki układ, więc projekt zbuduje się bez modyfikowania CMakeLists.txt.

1. Zainstaluj narzędzia. Pobierz i zainstaluj Visual Studio 2022 z zestawem „Desktop development with C++" (zawiera kompilator MSVC, CMake i Ninja) oraz Git for Windows.

2. Otwórz konsolę dewelopera. Z menu Start uruchom „Developer Command Prompt for VS 2022" (ma już ustawiony kompilator w PATH).

3. Zainstaluj vcpkg i SFML dokładnie w C:\game2d\vcpkg:

git clone https://github.com/microsoft/vcpkg C:\game2d\vcpkg
cd C:\game2d\vcpkg
.\bootstrap-vcpkg.bat
.\vcpkg install sfml:x64-windows

4. Utwórz dowiązanie vcpkg_installed wymagane przez CMakeLists.txt (klasyczny tryb vcpkg instaluje do installed\, a projekt szuka w vcpkg_installed\):

mklink /J C:\game2d\vcpkg\vcpkg_installed C:\game2d\vcpkg\installed

5. Zbuduj grę (z katalogu projektu):

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=C:\game2d\vcpkg\scripts\buildsystems\vcpkg.cmake
cmake --build build --config Release

CMake po zbudowaniu sam kopiuje folder assets/ oraz potrzebne biblioteki DLL SFML obok pliku .exe.

6. Uruchom grę:

cd build\Release
Platformer.exe

Budowanie (macOS / Linux)

mkdir build
cd build
cmake ..
cmake --build .

Plik wykonywalny: Platformer (Windows: Platformer.exe).
Folder assets/ jest kopiowany automatycznie do katalogu z binarką po każdym buildzie.


Uruchomienie

cd build
./Platformer        # macOS / Linux

Sterowanie

Menu główne

Klawisz / akcja Opis
/ Poruszanie po opcjach
Ruch myszy Podświetlenie opcji pod kursorem
Enter / LPM Wybór opcji

W grze

Klawisz Akcja
A / Ruch w lewo
D / Ruch w prawo
Skok (do 3× w powietrzu)
Spacja Strzał (pocisk poziomy, cooldown)

Po grze

Klawisz Akcja
R Natychmiastowy restart (Game Over)
Escape Powrót do menu (Game Over)
Enter Zatwierdź nick po wygranej → powrót do menu

Mechanika gry

  • Gra startuje w menu głównym z animowanym tłem parallax.
  • Kampania: kolejno 4 poziomy — po zebraniu wszystkich monet na bieżącym poziomie następuje przejście do następnego; po ostatnim — ekran wygranej.
  • Gracz ma 3 życia — wypadnięcie poza dolną krawędź mapy powoduje respawn i utratę życia (kolizja z wrogiem też).
  • Brak żyć → ekran GAME OVER (R = restart od poziomu 1, Escape = menu).
  • Wrogowie patrolują w poziomie; pociski je dezaktywują.
  • Monety — zebrane znikają i zwiększają licznik; wynik w scores.json uwzględnia sumę monet z całej ukończonej kampanii oraz czas.
  • Ekran YOU WIN z formularzem na nick — po zatwierdzeniu zapis do scores.json.
  • Top 10 — wyniki sortowane rosnąco po czasie (szybciej = wyżej).

Menu główne

Opcja Akcja
Start Rozpoczyna kampanię od poziomu 1
Najlepsze wyniki Ekran Top 10 z scores.json
Wyjdź Zamknięcie gry

HUD (lewy górny róg)

  • Coins: X / Y — monety na aktualnym poziomie
  • Level: N / 4
  • Lives: X
  • Time: MM:SS

Wyniki (scores.json)

Plik tworzony / dopisywany po wygranej — jeden wpis na linię:

{"name":"Jan","time":"1:23","coins":15}

Pole coins to łączna liczba monet zebranych w ukończonej kampanii. Ekran Top 10 wczytuje plik przy każdym otwarciu, sortuje po czasie i wyświetla maksymalnie 10 wyników.


Assety

Gra nie crashuje przy braku plików — działa bez dźwięku lub bez tekstury (np. wrogowie mają wtedy prosty kolorowy prostokąt zamiast sprite’u).

Tekstury

Ścieżka Opis
assets/textures/player_sheet.png Spritesheet gracza (9 kolumn × 3 wiersze, klatki 80×110 px)
assets/textures/bg_clouds.png Warstwa parallax — chmury
assets/textures/bg_mountains.png Warstwa parallax — góry
assets/textures/bg_trees.png Warstwa parallax — drzewa
assets/textures/parallax/l1_*.pngl3_*.png Zestawy tła pod poziomy 2–4 (Kenney Background Elements, m.in. pustynia / zamek / noc)
assets/textures/enemies/zombie_walk1.png, zombie_walk2.png Klatki animacji chodu wroga (Kenney Platformer Characters)

Pociski są rysowane jako mały prostokąt (bez osobnej tekstury).

Dźwięki SFX

Ścieżka Zdarzenie
assets/sounds/jump.ogg Skok gracza
assets/sounds/coin.ogg Zebranie monety
assets/sounds/death.ogg Utrata życia / respawn
assets/sounds/gameover.ogg Game over
assets/sounds/shoot.ogg Wystrzelenie pocisku (Kenney Interface Soundspluck_001)

Muzyka

Ścieżka Kiedy gra
assets/music/menu.ogg Menu główne (pętla)
assets/music/gameplay.ogg Podczas rozgrywki (pętla)
assets/music/gameover.ogg Ekran game over
assets/music/win.ogg Ekran wygranej

Pliki muzyczne w repozytorium pochodzą z paczki Kenney Music Jingles (CC0).

Czcionka

Ścieżka Opis
assets/fonts/Roboto-Regular.ttf Czcionka HUD i UI (Google Fonts, OFL)

Uwaga licencyjna (Kenney)

Wiele plików graficznych i dźwiękowych oznaczonych jako pochodzące z Kenney jest na licencji CC0 (domena publiczna). Tekstury parallax w assets/textures/parallax/ pochodzą z paczki Kenney Background Elements (Flat / PNG). Szczegóły w License.txt w paczce źródłowej.


Struktura katalogów

.
├── CMakeLists.txt
├── README.md
├── assets/
│   ├── fonts/
│   │   └── Roboto-Regular.ttf
│   ├── music/
│   │   ├── menu.ogg
│   │   ├── gameplay.ogg
│   │   ├── gameover.ogg
│   │   └── win.ogg
│   ├── sounds/
│   │   ├── jump.ogg
│   │   ├── coin.ogg
│   │   ├── death.ogg
│   │   ├── gameover.ogg
│   │   └── shoot.ogg
│   └── textures/
│       ├── player_sheet.png
│       ├── bg_clouds.png
│       ├── bg_mountains.png
│       ├── bg_trees.png
│       ├── parallax/
│       │   ├── l1_far.png … l1_near.png
│       │   ├── l2_far.png … l2_near.png
│       │   └── l3_far.png … l3_near.png
│       └── enemies/
│           ├── zombie_walk1.png
│           └── zombie_walk2.png
├── files/
│   ├── analiza_kodu.md
│   ├── spec_01_sprite_gracza.md … spec_10_wrogowie_strzal.md
│   └── …
└── src/
    ├── main.cpp
    ├── Game.h / Game.cpp
    ├── Player.h / Player.cpp
    ├── Platform.h / Platform.cpp
    ├── Coin.h / Coin.cpp
    ├── Level.h / Level.cpp
    ├── Enemy.h / Enemy.cpp
    ├── Projectile.h / Projectile.cpp
    ├── Animator.h / Animator.cpp
    ├── AudioManager.h / AudioManager.cpp
    └── ParallaxLayer.h / ParallaxLayer.cpp

Architektura klas

Klasa Odpowiedzialność
Game Pętla gry, stany (MENU/PLAYING/GAME_OVER/WINNING/SCORES), HUD, muzyka, parallax, kampania poziomów, pociski, wyniki
Player Fizyka, input, animacje, sprite, życia
Level Ładowanie platform, monet i wrogów, kolizje gracza
Platform Prostokąt kolizyjny platformy
Coin Moneta — pozycja, stan zebrania
Enemy Wróg — patrol, rysunek / animacja
Projectile Pocisk gracza — ruch poziomy, kolizje
Animator Spritesheet — zarządzanie klatkami animacji
AudioManager Ładowanie i odtwarzanie krótkich SFX
ParallaxLayer Jedna warstwa tła z efektem paralaksy

Licencja

Repozytorium łączy kod własny z zasobami z zewnątrz, więc licencja nie jest jednolita:

Część Licencja
Kod źródłowy (src/, CMakeLists.txt) MIT — Copyright (c) 2026 Wojciech Wypych (Iakirmon)
Tekstury, dźwięki i muzyka z paczek Kenney CC0 1.0 — domena publiczna
assets/fonts/Roboto-Regular.ttf (Roboto 3.015) SIL Open Font License 1.1 — assets/fonts/OFL.txt
SFML zlib/png — zależność zewnętrzna, nie jest częścią repozytorium

MIT obejmuje kod, nie zasoby. Czcionka Roboto pozostaje na OFL 1.1, a jej tekst licencji jest dołączony do repozytorium, bo OFL wymaga tego przy redystrybucji.


English version

A 2D platformer in C++17 using SFML 2.x. The player plays a four-level campaign, collects coins, performs a triple jump, shoots horizontal projectiles, and avoids or defeats patrolling enemies on platforms. Sprite animations, SFX, music, and per-level parallax (forest, desert, castle, night — Kenney assets) are included. Main menu, Top 10 leaderboard, and JSON score file (scores.json — total coins over the full run and time).

Requirements

  • C++17
  • CMake >= 3.20
  • SFML 2.5+ (branch 2.x — not 3.x)

Installing SFML

macOS (Homebrew)

brew install sfml

Linux (Debian/Ubuntu)

sudo apt install libsfml-dev

Windows (vcpkg) — step by step

CMakeLists.txt has hardcoded paths and expects vcpkg at exactly C:\game2d\vcpkg, with SFML in C:\game2d\vcpkg\vcpkg_installed\x64-windows. The steps below create exactly that layout, so the project builds without editing CMakeLists.txt.

1. Install the tools. Install Visual Studio 2022 with the "Desktop development with C++" workload (provides the MSVC compiler, CMake and Ninja) and Git for Windows.

2. Open the developer console. From the Start menu launch "Developer Command Prompt for VS 2022" (the compiler is already on PATH).

3. Install vcpkg and SFML exactly in C:\game2d\vcpkg:

git clone https://github.com/microsoft/vcpkg C:\game2d\vcpkg
cd C:\game2d\vcpkg
.\bootstrap-vcpkg.bat
.\vcpkg install sfml:x64-windows

4. Create the vcpkg_installed junction required by CMakeLists.txt (classic vcpkg installs into installed\, but the project looks in vcpkg_installed\):

mklink /J C:\game2d\vcpkg\vcpkg_installed C:\game2d\vcpkg\installed

5. Build the game (from the project directory):

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=C:\game2d\vcpkg\scripts\buildsystems\vcpkg.cmake
cmake --build build --config Release

After building, CMake automatically copies the assets/ folder and the required SFML DLLs next to the .exe.

6. Run the game:

cd build\Release
Platformer.exe

Building (macOS / Linux)

mkdir build
cd build
cmake ..
cmake --build .

Executable: Platformer (Windows: Platformer.exe).
The assets/ folder is copied automatically to the binary directory after each build.

Running

cd build
./Platformer        # macOS / Linux

Controls

Main menu

Key / action Description
/ Navigate menu options
Mouse move Highlight option under cursor
Enter / LMB Select option

In-game

Key Action
A / Move left
D / Move right
Jump (up to 3× in the air)
Space Shoot (horizontal projectile, cooldown)

After game

Key Action
R Instant restart (Game Over)
Escape Return to menu (Game Over)
Enter Confirm name after winning → back to menu

Gameplay

  • The game starts at the main menu with an animated parallax background.
  • Campaign: 4 levels in sequence — collecting all coins on the current level advances to the next; after the last level, the win screen appears.
  • The player has 3 lives — falling off the bottom of the map costs a life and respawns (enemy contact also hurts).
  • No lives left → GAME OVER (R = restart from level 1, Escape = menu).
  • Enemies patrol horizontally; projectiles deactivate them on hit.
  • Coins — collected coins disappear and increase the counter; the entry in scores.json stores the total coins collected over the completed campaign and the time.
  • YOU WIN screen with a name field — confirm to append to scores.json.
  • Top 10 — scores sorted ascending by time (faster = higher rank).

Main menu

Option Action
Start Start the campaign from level 1
Best scores Top 10 screen from scores.json
Exit Close the game

HUD (top-left corner)

  • Coins: X / Y — coins on the current level
  • Level: N / 4
  • Lives: X
  • Time: MM:SS

Scores (scores.json)

Created or appended after a win — one JSON object per line:

{"name":"Jan","time":"1:23","coins":15}

The coins field is the total coins collected in the finished campaign. The Top 10 screen loads the file on every open, sorts by time, and shows at most 10 entries.

Assets

The game does not crash on missing files — it can run without sound or without a given texture (e.g. enemies fall back to a simple colored rectangle if sprites fail to load).

Textures

Path Description
assets/textures/player_sheet.png Player spritesheet (9 cols × 3 rows, 80×110 px frames)
assets/textures/bg_clouds.png Parallax layer — clouds
assets/textures/bg_mountains.png Parallax layer — mountains
assets/textures/bg_trees.png Parallax layer — trees
assets/textures/parallax/l1_*.pngl3_*.png Background sets for levels 2–4 (Kenney Background Elements)
assets/textures/enemies/zombie_walk1.png, zombie_walk2.png Enemy walk animation frames (Kenney Platformer Characters)

Projectiles are drawn as a small rectangle (no dedicated texture).

Sound effects

Path Event
assets/sounds/jump.ogg Player jump
assets/sounds/coin.ogg Coin collected
assets/sounds/death.ogg Life lost / respawn
assets/sounds/gameover.ogg Game over
assets/sounds/shoot.ogg Projectile fired (Kenney Interface Soundspluck_001)

Music

Path When it plays
assets/music/menu.ogg Main menu (loop)
assets/music/gameplay.ogg During gameplay (loop)
assets/music/gameover.ogg Game over screen
assets/music/win.ogg Win screen

Music files in the repo come from the Kenney Music Jingles pack (CC0).

Font

Path Description
assets/fonts/Roboto-Regular.ttf HUD and UI font (Google Fonts, OFL)

Third-party note (Kenney)

Many assets credited to Kenney are CC0 (public domain). Parallax files under assets/textures/parallax/ come from Kenney Background Elements. See each pack’s License.txt for details.

Directory structure

.
├── CMakeLists.txt
├── README.md
├── assets/
│   ├── fonts/
│   │   └── Roboto-Regular.ttf
│   ├── music/
│   │   ├── menu.ogg
│   │   ├── gameplay.ogg
│   │   ├── gameover.ogg
│   │   └── win.ogg
│   ├── sounds/
│   │   ├── jump.ogg
│   │   ├── coin.ogg
│   │   ├── death.ogg
│   │   ├── gameover.ogg
│   │   └── shoot.ogg
│   └── textures/
│       ├── player_sheet.png
│       ├── bg_clouds.png
│       ├── bg_mountains.png
│       ├── bg_trees.png
│       ├── parallax/
│       │   ├── l1_far.png … l1_near.png
│       │   ├── l2_far.png … l2_near.png
│       │   └── l3_far.png … l3_near.png
│       └── enemies/
│           ├── zombie_walk1.png
│           └── zombie_walk2.png
├── files/
│   ├── analiza_kodu.md
│   ├── spec_01_sprite_gracza.md … spec_10_wrogowie_strzal.md
│   └── …
└── src/
    ├── main.cpp
    ├── Game.h / Game.cpp
    ├── Player.h / Player.cpp
    ├── Platform.h / Platform.cpp
    ├── Coin.h / Coin.cpp
    ├── Level.h / Level.cpp
    ├── Enemy.h / Enemy.cpp
    ├── Projectile.h / Projectile.cpp
    ├── Animator.h / Animator.cpp
    ├── AudioManager.h / AudioManager.cpp
    └── ParallaxLayer.h / ParallaxLayer.cpp

Class architecture

Class Responsibility
Game Game loop, states (MENU/PLAYING/GAME_OVER/WINNING/SCORES), HUD, music, parallax, level campaign, projectiles, scores
Player Physics, input, animations, sprite, lives
Level Loading platforms, coins, enemies; player collisions
Platform Platform collision rectangle
Coin Coin — position and collected state
Enemy Enemy — patrol, drawing / animation
Projectile Player projectile — horizontal motion, collisions
Animator Spritesheet — animation frame management
AudioManager Loading and playing short SFX
ParallaxLayer Single background layer with parallax effect

License

The repository mixes original code with third-party assets, so the licensing is not uniform:

Part License
Source code (src/, CMakeLists.txt) MIT — Copyright (c) 2026 Wojciech Wypych (Iakirmon)
Textures, sounds and music from Kenney packs CC0 1.0 — public domain
assets/fonts/Roboto-Regular.ttf (Roboto 3.015) SIL Open Font License 1.1 — assets/fonts/OFL.txt
SFML zlib/png — external dependency, not part of this repository

MIT covers the code, not the assets. Roboto stays under OFL 1.1 and its license text is bundled with the repository, because OFL requires it on redistribution.

About

2D graphics experiments with SFML (C++)

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages