Academy
Tap Tap (C++ 3/5)
Inputs, update, draw

Inputs, update, draw

Step 1

Before diving head first into our code, let's hold up for a moment and ask ourselves "How will I organize my code?"

Before diving head first into our code, let's hold up for a moment and ask ourselves "How will I organize my code?" Thankfully, in the world of video games, the main game loop is always the same :

    1. Gather player** inputs**.
    1. Update the game logic.
    1. Draw the game to the display.

And actually, based on the platform you are working with, there may be more steps. For example, the throttling of the execution speed of our loop (so that the game is identical, regardless of whether you are playing on a PlayStation, an Xbox, or a computer). But this is already handled by gb.update() for us, our games will always run at 25 frames per second. So in reality, there should be a 0th step : while(!gb.update());

By the way, you have already used this very method in the Pong workshop! Step 1 of the method was simply to check for button presses. The 2nd step was responsible for all of the game logic (collisions, AI). And all the drawing at the end was step 3.

This "Input / Update / Draw" method is really useful. First of all it allows to be able to more rapidly juggle between different parts of our game, as each part is clearly separated. It also decouples the inputs from the logic from the drawing (we will see the advantages of this shortly). Let's start by placing comments to divide each part:

#include 

void setup() {
    gb.begin();
}

void loop() {
    while(!gb.update());

    // INPUTS //


    // LOGIC //


    // DRAW //
    gb.display.clear();
}

I also placed other things like gb.begin();, but nothing new ;)

Next step