Project develop for 42Cursus
So long is a small 2D game project, help the little hero collect all coins and escape safety while avoiding the slimes!!
This is a graphic design project that will help you to improve skills in the following
areas: window management
, event handling
, colours
, textures
, and so forth
.
You can see MinilibX's Documentation here.
Unix logic
Applicative
Rigor
Algorithms & AI
Imperative programming
Graphics
To play the game, you can execute the executable so_long
passing the map you want to play as a parameter, as an example:
./so_long assets/maps/map_bonus.ber
Use WASD
or the Arrows Keys
to move.
You move one tile at a time. Enemies move after you do, and you die when you move into their tile. After picking up all Coins, the exits open and you may leave to see how many moves you’ve used.
You can close the game with the ESC
or Q
button.
The game can play any map you want as long as it follow some specific rules:
- The map has to be a
.ber
file. - It can only contain some of the following characters:
Character | Object |
---|---|
1 | Wall. |
0 | Empty space. |
C | Collectable. |
E | Exit. |
P | Player starting position |
V | Enemy starting position. |
- The map must be a rectangle surrounded by walls
1
. - It must have at least one exit
E
and one collectableC
. And only one playerP
.
See some examples in the ./assests/maps
folder of this project.
In so_long.h
and so_long_bonus.h
is where we do the includes, defines and create some structs,the t_game *game
argument is the main struct that contains other struct, to facilitate the code, like:
t_vars
: Used for init mlx;s_map
: Contains variables of map;s_img
ands_position
: The img and its position.
In the main(int argc, **char argv)
of the so_long.c
file, first of all, we need to check the user input with the function verify_argc(int argc)
, then, we can read the map file passed as an argument after initializing some variables.
The game works because of mlx_hook()
is triggered by an event looping by the mlx_loop(void *mlx_ptr)
.
In this file, read_map.c
, we read the map with the function read_map(char *argv, t_game *game)
and with get_next_line(int fd)
. About that, it needs to check if the game is playable, those functions are in the verify_error.c
verifying if the game is surrounded by walls, if the map is not a square, if the chars are the expected and their quantities. If something fails or are wrong, the program ends by exit_error(char *s)
in exit_free.c
file.
After the map was correctly read we init the game, initing the mlx library, a window, and our images.
To render the game, render_game(t_game *game)
we loop the map for each character, we put the image to window passing its position. For the bonus part, the exit is open after there is no collectible, and the enemy looks at the player and bounces after two moves, and the player follows according to the pressed key.
Feel free to clone this project. If something keeps confusing, contact me, I'll be happy to help, good luck!!.