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.

The Classic Space Invaders in action.

Setting up Stuffs

First download Raylib.
Download the VSCode Editor.
Download the VSCode project template folder from here.
Refer this wiki page for how to do this.

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, choose debug build or release build and press enter. If everything went fine, I hope, you might get a game.exe file inside the folder. Open it, god willing, you will get a small window. If not, get ready to fix things before moving on.

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.

4 Responses

  1. a_dumb_person says:

    is this called “procedural programming?”

  2. someone_existing says:

    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;
    };

Leave a Reply

Your email address will not be published. Required fields are marked *