# Make Your Own Sideways Scroller: Part 7 - Putting it all Together

(Simon) #1

The code described in these lessons can be found at https://github.com/filmote/Steve

## Putting It All Together

So far we have delved into the details of creating an endless runner game. We have moving ground, a moving player and random obstacles to avoid. If we do hit an obstacle it can be detected and we can save our best scores into EEPROM for posterity.

But how do we put it all together?

In the final part of Crait’s lessons, Make Your Own Arduboy Game: Part 7 - Make Pong From Scratch!, he discusses a ‘state machine’ pattern for controlling the flow of a game between introduction screens, the actual game play and game over screens. Our game uses this exact same concept with a minor change – the gameStatus variable that holds the current state is defined as an enumeration which in turn makes the code more readable.

GameStatus gameStatus = GameStatus::Introduction;

void loop() {

…

switch (gameStatus) {

case GameStatus::Introduction:
introduction();
break;

case GameStatus::PlayGame:
playGame();
break;

case GameStatus::GameOver:
gameOver();
break;

}

}

The main loop() is intentionally kept clean and all work is done by discrete functions. The introduction() function performs the single task of displaying the introduction screen and waiting for the user to start a game.

Before completing it updates the gameStatus variable with the next state – in this case GameStatus::PlayGame. On the next pass through the main loop(), control will pass to the playGame() function.

void introduction() {

EEPROM.get(EEPROM_SCORE, highScore);
arduboy.clear();

initialiseGame();

arduboy.setCursor(17, 12);
arduboy.print(F("Press A to begin"));

drawGround(false);
drawSteve();
drawScoreboard(false);
arduboy.display();

if (arduboy.pressed(A_BUTTON)) {

gameStatus = GameStatus::PlayGame;
steve.stance = Stance::Running1;

}

}

I encourage you to review the structure of the code taking note how control is passed between the functions using the gameStatus variable. Digging into these functions will reveal the snippets of code we have discussed above and they should be quite recognisable.

Finally, what could you change to make this version of the game your own? Maybe some different obstacles – you could simply replace the images or you could add new obstacles by creating images, adding the new types into the ObstacleType enumeration and changing the launchObstacle() function. Why not add a caveman with a big club?