Creating a simple game (part 2)

The series starts here

Now we have a solid base for our game, lets start of fairly simply by adding our players ship…

typedef enum playerMode {
     pm_Playing = 0, pm_Explode, pm_GameOver

We are using a very simple state machine to control how we handle our player, in conjunction with a simple frame counter, to prevent too rapid transitions between modes, for example when the players explosion animation has ended it stays in the pm_Explode mode while rendering nothing just to effect a slight pause.

In player.c you can see how the different modes are handled, notice for example you can only move left and right while in pm_Playing mode, you don’t really want a steerable explosion!

Notice that its the usually the responsibility of the current mode to change modes if required, because of the way the modes are implemented, if somewhere else we decide that the player has lost a live (we detect a collision with a missile for example) all we have to do is reset the mode counter (player.frame) and change the mode to pm_Explode, we don’t need to worry about lives or if the game is over, as this is all taken care of by the players state.

You can see this in action by pressing the ENTER key, you’ll see the players ship explode and the lives indicator reduce, when the player has run out of lives the game will end…

When rendering with DrawTexturePro we do so with an offset, this means that our texture (or sprite) is rendered from its centre, meaning it rotates as you’d expect (rather than from a corner) and also means that collision is just a simple matter of a distance test.

Hopefully by studying the code in player.c you should get an idea of what is happening with the different states and hopefully appreciate how powerful this can be. Be prepared the aliens behavior is much richer and therefor the handling of the modes is just that little bit more complex, as well see next time

You can get the code for this part here.

Now you have two different versions of the code, its worth using a tool like meld which will allow you to compare not just individual source files but whole directories…

The series continues here.

Leave a Reply

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