Getting started with C programming - Example game (with SDL) (Linux) -
Here is a very simple shooter game made with SDL in C. This is quite a big of a jump from the last example, but everything is commented, just go to the main function and read along one part at a time. Read a C tutorial alongside this if you don't understand some parts of the C code.
This page is for Linux. You can also check the Windows version.
Adding sound and images to SDL
SDL can't load file formats like PNG or play sounds well by default, to do it properly we need to add additional SDL modules:
- For images: SDL_Mixer, install with
sudo apt install libsdl2-mixer-dev
- For audio: SDL_Image, install with
sudo apt install libsdl2-image-dev
And here's the new build file:
#!/bin/sh if gcc main.c -I"/usr/include/SDL2" -lSDL2 -lSDL2_Mixer -lSDL2_Image -o exampleshooter ; then ./exampleshooter fi
Note: remember when I said you may need to define the library paths with -L
? Well you may need to do it this time or else -lSDL2_Mixer
and -lSDL2_Image
may not work. I do not have enough patience for Linux to figure it out where anything is located, so you're on your own with that.
The code
I won't put the code on this webpage since there's about 700 lines of it, instead you can download the code and assets. Just move them to your project folder (be careful about overwriting your old main.c file in case it has something important in it).
Use WASD or arrow keys to move, Space to shoot, Enter to restart, Esc to quit. The enemies will start spawning faster depending on how high your score is.
The project uses some free sounds and music from mixkit.