Skip to content

daxferab/42_so_long

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

49 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ So Long - 42 School Project

Welcome to So Long! ๐ŸŽ‰ This is an exciting project from Rank 2 at 42 School that introduces us to 2D game programming and graphics libraries. The goal is to create a simple 2D game where the player must collect all items and reach the exit, all while learning the fundamentals of game development and the MLX graphics library.

Build your own 2D top-down adventure game with sprites, animations, and player controls! ๐Ÿš€


๐Ÿ“‚ Project Structure

so_long/ โ”œโ”€โ”€ include/so_long.h # Header file (.h)
โ”œโ”€โ”€ src/ # Source files (.c)
โ”‚ โ”œโ”€โ”€ main.c # Main program entry point
โ”‚ โ”œโ”€โ”€ hook.c # Input handling and game logic
โ”‚ โ”œโ”€โ”€ errors.c # Error handling and validation
โ”‚ โ”œโ”€โ”€ free.c # Memory management
โ”‚ โ”œโ”€โ”€ initialize/ # Game initialization
โ”‚ โ”‚ โ”œโ”€โ”€ draw_map.c # Map rendering functions
โ”‚ โ”‚ โ”œโ”€โ”€ draw_tools.c # Drawing utilities
โ”‚ โ”‚ โ””โ”€โ”€ init_map.c # Map initialization
โ”‚ โ””โ”€โ”€ parse/ # Map parsing and validation
โ”‚ โ”œโ”€โ”€ arg_validation.c # Argument validation
โ”‚ โ”œโ”€โ”€ map_utils.c # Map utility functions
โ”‚ โ”œโ”€โ”€ map_utils2.c # Additional map utilities
โ”‚ โ”œโ”€โ”€ map_is_player_exit.c # Player/exit validation
โ”‚ โ””โ”€โ”€ map_validation.c # Map structure validation
โ”œโ”€โ”€ obj/ # Object files directory
โ”œโ”€โ”€ lib/ # External libraries
โ”‚ โ”œโ”€โ”€ libft/ # Libft library
โ”‚ โ””โ”€โ”€ mlx/ # MLX42 graphics library
โ”œโ”€โ”€ maps/ # Game maps (.ber files)
โ”‚ โ”œโ”€โ”€ small.ber # Small test map
โ”‚ โ”œโ”€โ”€ big.ber # Large test map
โ”‚ โ”œโ”€โ”€ maptry.ber # Default test map
โ”‚ โ””โ”€โ”€ error_maps/ # Invalid maps for testing
โ”œโ”€โ”€ textures/ # Game sprites and textures
โ”‚ โ”œโ”€โ”€ player.png # Player character sprite
โ”‚ โ”œโ”€โ”€ collect.png # Collectible item sprite
โ”‚ โ”œโ”€โ”€ collected.png # Collected item sprite
โ”‚ โ”œโ”€โ”€ floor.png # Floor tile sprite
โ”‚ โ”œโ”€โ”€ wall*.png # Wall tile sprites
โ”‚ โ”œโ”€โ”€ closedExit.png # Closed exit sprite
โ”‚ โ””โ”€โ”€ openExit.png # Open exit sprite
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ Makefile # Makefile to compile the project
โ””โ”€โ”€ README.md # This file


๐ŸŽฏ Game Mechanics

๐ŸŽฎ Core Gameplay

  • Objective: Collect all collectible items ('C') and reach the exit ('E')
  • Movement: Use WASD or arrow keys to move the player ('P')
  • Win condition: Collect all items, then reach the exit
  • Move counter: Track the number of moves made
  • Real-time graphics: Smooth 2D rendering with MLX42

๐Ÿ—บ๏ธ Map Elements

  • 1 - Wall (impassable terrain)
  • 0 - Floor (walkable terrain)
  • P - Player starting position (exactly one required)
  • C - Collectible items (at least one required)
  • E - Exit (exactly one required)

๐ŸŽจ Visual Features

  • Sprite-based graphics: Custom PNG textures for all elements
  • Dynamic visuals: Exit changes appearance when all items are collected
  • Tile-based rendering: Each map cell displays appropriate sprite
  • Window management: Proper window creation and event handling

๐Ÿง  Core Components

๐ŸŽฏ Game Engine

  • Initialization: Sets up MLX window, loads textures, parses map
  • Game loop: Handles input, updates game state, renders graphics
  • Input handling: Keyboard event processing for player movement
  • Rendering: Efficient sprite drawing and map visualization

๐Ÿ—บ๏ธ Map System

  • Map parsing: Reads .ber files and validates format
  • Map validation: Ensures valid game rules and structure
  • Collision detection: Prevents movement through walls
  • Path validation: Ensures map is solvable (all items and exit reachable)

๐ŸŽฎ Player System

  • Movement logic: Handles player position updates
  • Collision system: Checks for valid moves
  • Item collection: Tracks collected items and updates game state
  • Win detection: Checks for victory conditions

๐Ÿ› ๏ธ How to Use

๐Ÿ“ฆ Compilation

To compile the game, run:

make

This will:

  1. Compile libft library
  2. Build MLX42 graphics library
  3. Create the so_long executable

๐Ÿงฉ Usage

Launch the game with a map file:

./so_long [map_file.ber]

๐Ÿ’ก Usage Examples

Basic Gameplay

# Play with the default test map
make run
# or
./so_long maps/maptry.ber

# Play with a small map
./so_long maps/small.ber

# Play with a larger map
./so_long maps/big.ber

Controls

  • W or โ†‘ - Move up
  • A or โ† - Move left
  • S or โ†“ - Move down
  • D or โ†’ - Move right
  • ESC - Exit game
  • Red X - Close window

๐Ÿ—บ๏ธ Map Format (.ber files)

Map Rules

  1. Rectangular shape: Maps must be rectangular
  2. Wall borders: Maps must be surrounded by walls ('1')
  3. Required elements:
    • Exactly one player ('P')
    • Exactly one exit ('E')
    • At least one collectible ('C')
  4. Valid path: Player must be able to reach all collectibles and the exit
  5. Valid characters: Only '0', '1', 'P', 'C', 'E' allowed

Example Map

1111111111111
10010000000C1
1000011111001
1P0011E000001
1111111111111

โœ… Project Requirements

  • โœ… Use MLX42 graphics library
  • โœ… Create a 2D game with sprite graphics
  • โœ… Implement player movement with WASD/arrow keys
  • โœ… Display move counter in terminal
  • โœ… Handle window events (close button, ESC key)
  • โœ… Parse and validate .ber map files
  • โœ… Ensure maps are solvable (valid path exists)
  • โœ… Implement collectible items and exit mechanics
  • โœ… No memory leaks
  • โœ… Error handling for invalid maps/arguments

๐ŸŽจ Graphics and Textures

Sprite System

The game uses PNG textures for all visual elements:

  • Player sprite: Animated character representation
  • Wall sprites: Different wall variations (top, bottom, left, right, center)
  • Floor sprite: Walkable ground texture
  • Collectible sprites: Items to collect (with collected state)
  • Exit sprites: Closed and open exit states

Visual Design

  • Tile-based rendering: Each map cell is 64x64 pixels
  • Consistent art style: Cohesive visual theme
  • State-based graphics: Visual feedback for game state changes

๐Ÿšจ Error Handling

The game handles various error conditions:

  • Invalid arguments: Wrong number of arguments
  • File errors: Map file not found or unreadable
  • Map validation: Invalid map structure or characters
  • Path validation: Unreachable collectibles or exit
  • Graphics errors: MLX initialization or texture loading failures
  • Memory allocation: Out of memory conditions

All errors display descriptive messages and exit gracefully.


๐Ÿงช Testing & Validation

Map Testing

# Test with valid maps
./so_long maps/small.ber
./so_long maps/big.ber

# Test with invalid maps (should show errors)
./so_long maps/error_maps/no_exit.ber
./so_long maps/error_maps/no_player.ber
./so_long maps/error_maps/no_collectibles.ber

Gameplay Testing

  • Movement: Test all direction keys
  • Collection: Verify all items can be collected
  • Win condition: Ensure game ends when all items collected and exit reached
  • Move counter: Verify move count is accurate
  • Window events: Test ESC key and close button

๐Ÿ’ก Key Learning Objectives

  • Graphics Programming: Introduction to 2D graphics and rendering
  • Event-Driven Programming: Handling user input and window events
  • Game Development: Basic game loop and state management
  • Memory Management: Proper allocation and cleanup of graphics resources
  • File Parsing: Reading and validating external file formats
  • Algorithm Design: Pathfinding and map validation algorithms
  • Library Integration: Working with external graphics libraries

๐Ÿš€ Advanced Features

Performance Optimizations

  • Efficient rendering: Only redraw changed elements
  • Texture caching: Load textures once at startup
  • Optimized collision detection: Fast boundary checking

Extensibility

The codebase is designed for easy extension:

  • Modular structure: Separate parsing, rendering, and game logic
  • Configurable graphics: Easy to swap textures
  • Scalable map system: Support for different map sizes

๐Ÿ” Technical Details

MLX42 Integration

  • Modern graphics: Uses MLX42 for hardware-accelerated rendering
  • Cross-platform: Compatible with modern systems
  • Event handling: Robust input and window management

Memory Management

  • Proper cleanup: All allocated memory is freed
  • Error recovery: Graceful handling of allocation failures
  • Resource management: Textures and graphics resources properly managed

๐ŸŽฏ Game Development Tips

  1. Start simple: Begin with basic movement and rendering
  2. Test incrementally: Validate each feature as you build
  3. Handle errors: Robust error checking prevents crashes
  4. Design for extension: Modular code allows easy feature addition
  5. Optimize last: Focus on functionality before performance

Made with โค๏ธ at 42 Madrid

About

๐ŸŽ“ Project 3.2 - Making a mini-game using CodamMLX

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published