PC Games

Lasagne Monsters
Three Guys Apocalypse
Water Closet
Blob Wars : Attrition
The Legend of Edgar
TBFTSS: The Pandoran War
Three Guys
Blob Wars : Blob and Conquer
Blob Wars : Metal Blob Solid
Project: Starfighter
TANX Squadron

Android Games

Number Blocks
Match 3 Warriors


2D shoot 'em up
2D top-down shooter
2D platform game
Sprite atlas tutorial
Working with TTF fonts
2D adventure game
Widget tutorial
2D shoot 'em up sequel
2D run and gun
Medals (Achievements)
2D turn-based strategy game
SDL 1 tutorials (outdated)

Latest Updates

SDL2 turn-based strategy tutorial
Thu, 14th April 2022

Water Closet ported to PlayStation Vita
Tue, 4th January 2022

The Legend of Edgar 1.35
Sat, 1st January 2022

Achievements tutorial
Thu, 2nd December 2021

SDL2 Rogue tutorial
Thu, 30th September 2021

All Updates »


android (3)
battle-for-the-solar-system (9)
blob-wars (9)
brexit (1)
code (6)
edgar (7)
games (39)
lasagne-monsters (1)
making-of (5)
match3 (1)
numberblocksonline (1)
orb (2)
site (1)
tanx (4)
three-guys (3)
three-guys-apocalypse (3)
tutorials (10)
water-closet (4)


A North-South Divide

For over a hundred years, messenger Duncan has wandered the world, searching for the missing pieces of an amulet that will rid him of his curse; a curse that has burdened him with an extreme intolerance of the cold, an unnaturally long life, and the despair of watching all he knew and loved become lost to the ravages of time. But now, Duncan is close to the end of his long quest.

Click here to learn more and read an extract!

Basic Tutorials

Basic Game Tutorial #9 - Starfields


This tutorial demonstrates how to display a moving starfield on the screen.

Compile and run tutorial09. The stars on the screen will move from right to left at various speeds. Pressing Escape or closing the window will exit the program.

An in-depth look

Creating a moving starfield is very simple and there are only a few files of note for this tutorial. First, we define a structure for our starfield in structs.h:

typedef struct Star
    int x, y, speed;
} Star;
The x and y variables define the star's location on the screen and the speed defines its movement speed. Next we will look at the stars.c file.

stars.c contains 4 functions:

void resetStars()
    int i;

    for (i=0;i<MAX_STARS;i++)
        star[i].x = rand() % SCREEN_WIDTH;
        star[i].y = rand() % SCREEN_HEIGHT;
        star[i].speed = 1 + (rand() % 12);
resetStars randomly places all of the stars on the screen with a random speed. We need to do this before entering our main loop otherwise all of the stars might suddenly all appear on the right hand side of the screen which wouldlook wrong.
void doStars()
    int i;

    for (i=0;i<MAX_STARS;i++)
        star[i].x -= star[i].speed;

        if (star[i].x < 0)
            star[i].x = SCREEN_WIDTH + rand() % 20;
            star[i].y = rand() % SCREEN_HEIGHT;
            star[i].speed = 1 + (rand() % 12);
doStars loops though all of the stars and moves them across the screen from right to left by subtracting their speed from their current x position. If a star's x position becomes negative then it has moved off the screen, so we randomly set its x position to be the screen's width plus a random amount to stop it appearing immediately and also set its y and speed.
void drawStars()
    int i;
    SDL_Rect rect;

    for (i=0;i<MAX_STARS;i++)
        if (star[i].x < SCREEN_WIDTH)
            rect.x = star[i].x;
            rect.y = star[i].y;
            rect.w = 1;
            rect.h = 1;

            SDL_FillRect(screen, &rect, getStarColor(star[i].speed));
drawStars is another simple function. We loop through all the stars and, if their x position is on the screen, we draw it using SDL_FillRect. We determine its colour by calling the function getStarColor which is described below.
int getStarColor(int speed)
    SDL_Color color;

    switch (speed)
        case 1:
        case 2:
        case 3:
            color.r = 159;
            color.g = 159;
            color.b = 159;

        case 4:
        case 5:
        case 6:
            color.r = 191;
            color.g = 191;
            color.b = 191;

        case 7:
        case 8:
        case 9:
            color.r = 223;
            color.g = 223;
            color.b = 223;

            color.r = 255;
            color.g = 255;
            color.b = 255;

    return SDL_MapRGB(screen->format, color.r, color.g, color.b);
getStarColor takes a simple parameter, the speed we wish to test. We then switch this speed and set the RGB values of the SDL_Color. The lower the speed, the darker the star will be, since it's further away. Finally, we use SDL_MapRGB to get the correct value for the colour we've chosen and return it.

In main.c we animate the stars as follows:

/* Initialise the stars */


/* Loop indefinitely for messages */

while (go == 1)

    /* Update the stars */


    /* Blank the screen */

    SDL_FillRect(screen, NULL, 0);

    /* Draw the stars */


    /* Swap the buffers */


    /* Sleep briefly to stop sucking up all the CPU time */



    frameLimit = SDL_GetTicks() + 16;
First we position the stars before entering the main loop by calling resetStars. Then, in our main loop, we call doStars to update their positions and drawStars to draw them. That's really all there is to it.


Now that we can accept user input, perform collision detection, use SDL_TTF to render text to the screen and animate sprites, we can put this all together to make a small game. We will do this in the next tutorial.


Source Code

Mobile site