A Feature-Rich 2D Platformer Game Built with SDL in C
Features • Installation • Gameplay • Controls • Architecture • Contributing
- Overview
- Features
- Screenshots
- Installation
- Gameplay
- Controls
- Game Modes
- Project Structure
- Architecture
- Technical Details
- Configuration
- Assets
- Development
- Troubleshooting
- Contributing
- License
- Credits
This is a comprehensive 2D platformer game developed in C using the SDL (Simple DirectMedia Layer) library. The game features a sophisticated game engine with multiple game states, physics-based movement, AI enemies, puzzle challenges, and mini-games. It demonstrates advanced C programming techniques including modular architecture, state machines, sprite animation, collision detection, and audio integration.
- 🎯 State Machine Architecture - Robust game state management system
- 🏃 Physics-Based Movement - Realistic player movement with acceleration, friction, and jumping mechanics
- 🤖 AI Enemy System - Intelligent enemy AI with following behavior
- 🧩 Puzzle Mini-Game - Quiz-based puzzles with scoring system
- 🎲 Connect Four Game - Fully playable Connect Four mini-game
- 👥 Multiplayer Support - Split-screen two-player mode
- 🗺️ Minimap System - Real-time minimap with player tracking
- 🎨 Sprite Animation - Multi-directional character sprites with smooth animations
- 🎵 Audio System - Background music and sound effects
- ⚙️ Settings Menu - Customizable game settings including fullscreen mode
-
Advanced Player Movement System
- Velocity-based movement with acceleration
- Friction and deceleration
- Jump mechanics with arc-based physics
- Multi-directional sprite animations (idle, walking, jumping)
-
Enemy AI
- State-based AI (waiting, following)
- Player tracking and pursuit
- Collision detection
-
Game States
- Main Menu
- Settings Menu
- Active Gameplay
- Puzzle Mode
- Mini-Game Mode
- Pause State
- Game Over
-
Quiz Puzzle System
- Timed questions with multiple-choice answers
- Score tracking (correct: +20, wrong: -20)
- 16-second time limit per puzzle
- Visual feedback
-
Connect Four
- Classic Connect Four gameplay
- AI opponent
- 6x7 game board
- Win condition detection
-
Graphics
- Parallax scrolling backgrounds
- Cloud animations
- Sprite-based character animations (12 frames per direction)
- Split-screen rendering for multiplayer
- Minimap with real-time player tracking
-
Audio
- Background music system
- Sound effects (click sounds, etc.)
- SDL_mixer integration
- 60 FPS frame rate targeting
- 1280x639 screen resolution (customizable)
- Modular architecture with clear separation of concerns
- Memory management with proper initialization and cleanup
- Collision detection system
- Camera system with viewport management
- Score persistence system
Add your game screenshots here
[Main Menu] [Gameplay] [Puzzle Mode] [Minimap]
Before building the game, ensure you have the following dependencies installed:
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libsdl1.2-dev
sudo apt-get install libsdl-image1.2-dev
sudo apt-get install libsdl-ttf2.0-dev
sudo apt-get install libsdl-mixer1.2-devsudo dnf install gcc make
sudo dnf install SDL-devel
sudo dnf install SDL_image-devel
sudo dnf install SDL_ttf-devel
sudo dnf install SDL_mixer-develsudo pacman -S base-devel
sudo pacman -S sdl sdl_image sdl_ttf sdl_mixer-
Clone the repository
git clone https://github.com/aliammari1/C-GAME-PROJECT.git cd C-GAME-PROJECT -
Build the game
make clean all
-
Run the game
make run # or directly ./game
-
Clean build artifacts:
make clean
-
Build only (without running):
make all
-
Debug build: The makefile includes
-gflag for debugging. Use with gdb:gdb ./game
The game is a side-scrolling platformer where you control a character navigating through a world filled with enemies and challenges. Your objective is to:
- Navigate the Environment - Use arrow keys to move and jump
- Avoid or Defeat Enemies - Enemy AI will track and follow you
- Complete Puzzles - Answer quiz questions within the time limit
- Play Mini-Games - Challenge yourself with Connect Four
- Score Points - Track your progress with the scoring system
- Use the Minimap - Navigate using the real-time minimap display
Main Menu → Select Mode → Gameplay → Puzzles/Mini-Games → Game Over → Menu
↓
Settings ← → Fullscreen Toggle
↓
Quit
| Key | Action |
|---|---|
↑ / ↓ |
Navigate menu options |
Enter |
Select menu item |
Mouse |
Click buttons |
| Key | Action |
|---|---|
→ |
Move right |
← |
Move left |
↑ |
Jump |
P |
Pause/Unpause |
ESC |
Return to menu |
| Key | Action |
|---|---|
F |
Toggle fullscreen |
ESC |
Return to menu |
| Key | Action |
|---|---|
1 / 2 / 3 |
Select answer |
Mouse |
Click answer |
| Mouse | Action |
|---|---|
Click Column |
Drop token |
Navigate through the world, complete challenges, and achieve the highest score.
Split-screen multiplayer experience with independent camera systems.
Answer timed quiz questions:
- Correct Answer: +20 points
- Wrong Answer: -20 points
- Time Limit: 16 seconds per question
Classic strategy game against AI opponent:
- 6 rows × 7 columns board
- First to connect 4 wins
- AI with intelligent move selection
C-GAME-PROJECT/
│
├── 📄 main.c # Entry point and initialization
├── 📄 core.h # Core header with all includes
├── 📄 config.h # Game configuration constants
├── 📄 types.h # Type definitions and structures
│
├── 🎮 Game Engine
│ ├── game_loop.c # Main game loop
│ ├── game_state.c # State machine implementation
│ └── game_state.h # State definitions
│
├── 👤 Player System
│ ├── player.c # Player logic and physics
│ └── player.h # Player declarations
│
├── 👾 Enemy System
│ ├── enemy.c # Enemy AI and behavior
│ └── enemy.h # Enemy declarations
│
├── 🎨 UI & Rendering
│ ├── ui.c # UI rendering functions
│ └── ui.h # UI declarations
│
├── 🌍 World
│ ├── world.c # World, minimap, scoring
│ └── world.h # World declarations
│
├── 🧩 Puzzle System
│ ├── puzzle.c # Quiz puzzle logic
│ ├── puzzle.h # Puzzle declarations
│ └── puzzle_game.c # Puzzle game implementation
│
├── 🎲 Mini-Game
│ ├── minigame.c # Connect Four logic
│ ├── minigame.h # Mini-game declarations
│ ├── minigame_utils.c # Mini-game utilities
│ └── menu_keyboard.c # Menu input handling
│
├── ⏱️ Timer System
│ ├── timer.c # Timer implementation
│ └── timer.h # Timer declarations
│
├── 🛠️ Utilities
│ └── utils.c # Utility functions
│
├── 📦 Assets
│ ├── images/ # Sprite sheets and graphics
│ ├── music/ # Background music
│ ├── song/ # Sound effects
│ ├── logo/ # Logo assets
│ ├── *.ttf # Font files
│ └── *.png, *.jpg, *.bmp # Image files
│
└── 📋 makefile # Build configuration
The game uses a robust state machine pattern for managing game flow:
┌─────────────┐
│ STATE_MENU │ ◄──────────────────┐
└──────┬──────┘ │
│ │
├─► STATE_SETTINGS │
│ │
├─► STATE_GAMEPLAY ────────►│
│ │ │
│ ├─► STATE_PAUSE │
│ │ │
│ ├─► STATE_PUZZLE ─┤
│ │ │
│ └─► STATE_MINIGAME│
│ │
└─► STATE_QUIT │
│
STATE_GAME_OVER ────────────┘
The codebase follows a modular architecture with clear separation of concerns:
-
Core Systems
- Game Engine (state management, main loop)
- Input handling
- Rendering pipeline
-
Game Logic
- Player system (physics, animation)
- Enemy system (AI, collision)
- World system (background, camera, minimap)
-
Mini-Games
- Puzzle system (questions, scoring, timer)
- Connect Four (board logic, AI)
-
Utilities
- Timer system
- UI components
- Asset management
The central structure managing the entire game:
typedef struct {
GameState currentState; // Current state
GameState previousState; // For state transitions
MenuSelection menuSelection; // Menu navigation
int numPlayers; // 1 or 2 players
int playerScore; // Current score
int isRunning; // Game running flag
Player player; // Player 1
Player player2; // Player 2
Enemy enemy; // Enemy entity
Background background; // World and camera
Minimap minimap; // Minimap display
GameTimer gameTimer; // Timer system
// ... more fields
} GameEngine;Physics-based player with sprite animation:
typedef struct {
SDL_Rect position; // Screen position
SDL_Rect relativePosition; // Jump arc position
SDL_Surface *sprites[6][12]; // 6 directions × 12 frames
float velocityX; // Horizontal velocity
PlayerDirection direction; // Current direction
int currentFrame; // Animation frame
int isJumping; // Jump state
} Player;- Rendering: SDL 1.2 software rendering
- Sprites: Multi-frame sprite sheets with 12 frames per direction
- Animations: Frame-based animation system at 60 FPS
- Camera: Viewport-based camera system with smooth scrolling
- Collision: Pixel-perfect collision detection using color masks
-
Movement: Velocity-based with acceleration/deceleration
- Base velocity: 5 units
- Max velocity: 10 units
- Acceleration: 0.01 units per frame
- Friction: 0.001 units per frame
-
Jumping: Arc-based jump system
- Jump duration: 20 frames
- Parabolic trajectory
- Music: Background music using SDL_mixer
- Sound Effects: Click sounds and effects
- Audio Format: 44100 Hz, stereo, 1024 byte chunks
- Target FPS: 60
- Frame Time: ~16.67ms per frame
- Resolution: 1280×639 pixels (16:10 aspect ratio)
- Game Area: 1280×419 pixels (UI takes bottom portion)
All game constants can be modified in config.h:
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 639
#define SCREEN_BPP 32
#define TARGET_FPS 60#define PLAYER_BASE_VELOCITY 5
#define PLAYER_MAX_VELOCITY 10
#define PLAYER_ACCELERATION 0.01f
#define PLAYER_FRICTION 0.001f
#define PLAYER_JUMP_FRAMES 20#define PUZZLE_TIME_LIMIT 16000 // milliseconds
#define PUZZLE_SCORE_CORRECT 20
#define PUZZLE_SCORE_WRONG -20#define MINIMAP_POSITION_X 1000
#define MINIMAP_POSITION_Y 100
#define TIMER_X 50
#define TIMER_Y 50The game requires the following assets in the assets/ directory:
angelina.TTF- Default UI fontTrajan Pro.ttf- Large title fontarial.ttf- Alternative fontalger.TTF- Additional font
- Backgrounds:
back0.jpg,back1.jpg,back2.jpg,bg.png,fond.png - UI Elements:
play0.png,play1.png,settings0.png,settings1.png,quit0.png,quit1.png - Game Elements:
cloud.png,mini_backgg.png,skulltest.png - Tokens:
rouge.png,jaune.png(Connect Four) - Sprites: Player and enemy sprite sheets in
images/subdirectories
- Music: Background music files in
music/andsong/ - SFX:
videoplayback.wav,Mouse_Click.wav,audio.wav,effet.wav
- Team and game logos in
logo/directory
assets/
├── images/
│ ├── 0/ ... 5/ # Sprite sheets organized by direction
├── music/ # Background music tracks
├── song/ # Sound effects
├── logo/ # Logo files
└── [root files] # Fonts, UI elements, backgrounds
CFLAGS := -g -Wall -Wextra -Wno-unused-parameter-g: Debug symbols for gdb-Wall -Wextra: Enable all warnings-Wno-unused-parameter: Suppress unused parameter warnings
LIBS := -lSDL -lSDL_ttf -lSDL_image -lSDL_mixer-
New Game State:
- Add state to
GameStateenum ingame_state.h - Implement state logic in
game_state.c - Add rendering in
GameEngine_Render()
- Add state to
-
New Character/Enemy:
- Define structure in
types.h - Implement init/update/render functions
- Add to game engine structure
- Define structure in
-
New Mini-Game:
- Create new
.cand.hfiles - Add state to
GameStateenum - Implement game logic and integrate into main loop
- Create new
Using GDB:
make clean all
gdb ./game
(gdb) run
(gdb) break main
(gdb) continueCommon Debug Points:
main.c:33- Game engine creationgame_loop.c:30- Game loop startgame_state.c- State transitionsplayer.c- Player physics
SDL initialization failed: [error]
Solution: Ensure SDL libraries are properly installed
sudo apt-get install --reinstall libsdl1.2-devFailed to load: assets/[filename]
Solution: Verify assets are in the correct directory structure
undefined reference to `SDL_*`
Solution: Check that SDL libraries are linked correctly in makefile
Audio initialization failed
Solution: Ensure SDL_mixer is installed and audio device is available
sudo apt-get install libsdl-mixer1.2-devTTF initialization failed
Solution: Verify SDL_ttf is installed and font files exist
sudo apt-get install libsdl-ttf2.0-dev- Low FPS: Reduce sprite resolution or disable cloud animations
- Memory Leaks: Ensure proper cleanup in
GameEngine_Destroy() - Stuttering: Check for blocking I/O operations
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch
git checkout -b feature/amazing-feature
- Commit your changes
git commit -m "Add amazing feature" - Push to the branch
git push origin feature/amazing-feature
- Open a Pull Request
- Follow the existing code style
- Comment your code appropriately
- Test your changes thoroughly
- Update documentation if needed
- Keep commits atomic and well-described
- 🎮 New mini-games or puzzles
- 🎨 Additional sprites and animations
- 🤖 Enhanced enemy AI
- 🎵 More sound effects and music
- 🌍 New levels or world designs
- 🐛 Bug fixes and optimizations
- 📚 Documentation improvements
- 🧪 Unit tests
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2025 Ali Ammari
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Ali Ammari - Project Lead & Developer
- GitHub: @aliammari1
- SDL 1.2 - Simple DirectMedia Layer
- SDL_image - Image loading library
- SDL_ttf - TrueType font rendering
- SDL_mixer - Audio mixing library
- GCC - GNU Compiler Collection
- SDL Development Team for the amazing multimedia library
- All contributors and testers
- Open source community
Ali Ammari
- GitHub: @aliammari1
- Repository: C-GAME-PROJECT
Current Version: 1.0.0
Status: Active Development
Last Updated: December 2025
- Level editor
- Save/Load game state
- Achievement system
- Networked multiplayer
- Mobile port (SDL2 migration)
- Steam integration
- Modding support