Skip to content

Repository files navigation

This project has been created as part of the 42 curriculum by hbani-at.

So Long - A 2D Game Project

Description

So Long is a 2D game project developed as part of the 42 curriculum. The objective is to create a simple yet engaging game where the player must navigate a map, collect all items, and reach an exit to complete each level.

Project Goals

  • Build a functional 2D game using the MLX (Mini LibX) graphics library
  • Implement proper map parsing and validation with error handling
  • Develop game mechanics including player movement and collision detection
  • Practice structured C programming with modular design
  • Create an interactive graphical user interface using MLX

Key Features

  • Map-based Navigation: Load and render custom maps from .ber files
  • Player Movement: Control the player character using WASD
  • Collectibles: Gather all items scattered across the map
  • Exit: Reach the exit after collecting all items
  • Map Validation: Comprehensive validation to ensure maps are correctly formatted and solvable
  • Flood Fill Algorithm: Verify that all collectibles and the exit are reachable
  • Error Handling: Clear error messages for various failure scenarios
  • Graphical Rendering: XPM-based sprite rendering for player, collectibles, walls, ground, and exit

Instructions

Requirements

1. GCC Compiler

What it is: The GNU C Compiler, used to compile C source code into executable programs.

Installation:

  • Debian/Ubuntu:

    sudo apt-get update
    sudo apt-get install gcc
  • Fedora/RHEL/CentOS:

    sudo dnf install gcc
  • macOS (if needed):

    xcode-select --install

Verify installation:

gcc --version

2. Linux Environment with X11 Libraries

What it is: X11 (X Window System) libraries are required for graphical display on Linux systems. MLX depends on these libraries to create windows and handle graphics.

Installation:

  • Debian/Ubuntu:

    sudo apt-get update
    sudo apt-get install libxext-dev libx11-dev libximg-dev
  • Fedora/RHEL/CentOS:

    sudo dnf install libXext-devel libX11-devel libXrender-devel
  • Arch Linux:

    sudo pacman -S libx11 libxext

Verify installation:

pkg-config --cflags --libs x11 xext

3. Make Build Tool

What it is: A tool for automating the compilation process using Makefiles.

Installation:

  • Debian/Ubuntu:

    sudo apt-get install make
  • Fedora/RHEL/CentOS:

    sudo dnf install make
  • macOS:

    xcode-select --install

Verify installation:

make --version

4. MLX Library (Mini LibX)

What it is: A lightweight graphics library created by 42 school for educational purposes.

Status: ✅ Included in the project - The MLX library is already present in the mlx/ directory and will be compiled as part of the build process.

Installation (if you need to clone it separately):

If the mlx/ directory is missing or you want to install MLX separately, clone it from the official repository:

git clone https://github.com/42paris/minilibx-linux.git mlx

This will clone the MLX repository into a directory named mlx. The Makefile in this project is configured to look for MLX in this location.

Verify installation:

ls -la mlx/

You should see the MLX source files and configuration scripts in the directory.

Compilation

Navigate to the project directory and compile the project:

make

This will:

  1. Compile the libft library (custom C library)
  2. Compile the get_next_line module
  3. Link all components together to create the so_long executable

To clean up object files:

make clean

To remove all compiled files and executables:

make fclean

To rebuild from scratch:

make re

Execution

Run the game with a valid map file:

./so_long maps/easy.ber

The executable requires exactly one argument: the path to a .ber map file.

Map Format

Maps must be in .ber format with the following requirements:

  • Rectangular: All rows must have the same length
  • Surrounded by Walls: The entire perimeter must be walls (1)
  • Required Elements:
    • Exactly one player (P)
    • At least one collectible (C)
    • Exactly one exit (E)
    • Ground tiles (0)
    • Wall tiles (1)
  • Valid Characters: Only the above characters are allowed
  • Solvability: All collectibles and the exit must be reachable from the player's starting position

Example Map

111111111111
100P00000001
10000C00C001
1000000000E1
111111111111

Controls

  • WASD: Move the player
  • ESC or Close Button: Exit the game
  • Move counter displayed in terminal showing number of moves taken

Resources

Project Structure

so_long/
├── main.c                    # Entry point and main game loop initialization
├── game.c                    # Game initialization and cleanup
├── map_loader.c              # Map file reading and parsing
├── map_validation.c          # Map format and structure validation
├── map_path_validation.c     # Flood fill algorithm for path validation
├── images.c                  # XPM image loading and initialization
├── rendering.c               # Game rendering loop
├── movement.c                # Player movement and input handling
├── error.c                   # Error handling and messaging
├── utils.c                   # Utility functions
├── so_long.h                 # Main header with structures and function declarations
├── libft/                    # Custom C library (includes list operations, string manipulation)
├── get_next_line/            # Buffer-based line reading utility
├── mlx/                      # Mini LibX graphics library
├── textures/                 # XPM sprite files
├── maps/                     # Sample and test map files
└── Makefile                  # Build configuration

Key Functions

  • game_init(): Initializes game state and loads resources
  • map_load(): Reads and parses the map file
  • map_validate(): Validates map structure and format
  • map_validate_path(): Uses flood fill to verify all elements are reachable
  • render_map(): Main rendering function called in the game loop
  • player_move(): Handles keyboard input and player movement
  • init_images(): Loads XPM sprites for all game elements

Author: hbani-at
Project Type: 42 Curriculum - Graphics/Game Development
Language: C
Graphics Library: MLX (Mini LibX)

About

so_long is a small 2D game developed in C using the MiniLibX graphics library. The goal of the game is to navigate a player through a maze-like map, collect all items, and reach the exit while avoiding obstacles. This project emphasizes understanding file parsing, dynamic memory management, event handling, and basic graphics rendering in C.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages