Raylib Game Tutorial : Space Invaders using VSCode
Raylib is a fairly new game framework around. While other game engines are aiming to be as simple as possible, Raylib goes the most raw way, coding in C with no fancy interface, no visual helpers, no auto-debugging etc. I’ve checked it with VSCode, I thought why not write a tutorial about it.
Here’s the Game we are going to make.
Setting up Stuffs
First download Raylib.
Download the VSCode Editor.
Download the VSCode project template folder from here.
Running an Example
Make a copy of the VSCode project folder and rename it SpaceInvaders or something. Open VSCode and choose file – open workspace, and select main.code-workspace inside the folder. Now use Ctrl+Shift+P and type Build, choose Build Task,
Splitting the Game
We are going to split the game into several functions for ease of organizing things. In C, function follows a declaration and a definition later. like this.
Setting up Player
Player is setup using struct. Here we do the movements also.
Main Game Loop
Our main game loop will look like this
Adding Bullets
Initializing Bullets
Now in the update logic, we check if player press Space to shoot, and update the bullet movements.
Setting up the enemies
Here we setup enemies. Only enemies alive are drawn.
Check Collision of Bullet with Enemy
Check if the rectangle of the bullet cross with the enemy rectangle area. And make it inactive to not draw on the frame next time. inside if(shoot[i].active) code add this code.
Adding Sounds
Download the sound files from Google Drive. Copy those two files into the game folder. Add the below code and play the game.
Full Tutorial Source Code – Here
Code Previews created with the awesome Carbon Code Editor. Let me know your comments and thoughts.
is this called “procedural programming?”
Yes. I think so.
btw, when setting up your enemies, why did you use
typedef struct Enemy
{
Rectangle rect;
Vector2 speed;
bool active;
Color color;
} Enemy;
and not just
struct Enemy
{
Rectangle rect;
Vector2 speed;
bool active;
Color color;
};
I think it’s used for forward declaration … I don’t think you can define variables later … Please check and let me know