To transfer the string into flash memory, we use the
PSTR
macro. It ensures
that the strings we output using
printPGM
will be copied to flash memory when
the program gets compiled.
Writing the Game Loop
The most central function of our game is
update_game
. It implements the actual
game loop and first clears the screen by calling
clear_screen
. Then it uses
draw_circle
to draw the current target.
move_crosshairs
calculates the new position
of the crosshairs depending on the player’s movement.
draw_crosshairs
outputs
the crosshairs to the screen.
check_target
determines the state of the current target—in other words, it checks
whether the user has hit the target, whether the target has been on the screen
for too long, or whether nothing special has happened. If all targets have been
shown already, the game is over.
To control the crosshairs, we use the following helper functions:
Tinkering/Pragduino/Pragduino.ino
void move_crosshairs() {
if (left) chx -= chvx;
if (right) chx += chvx;
if (up) chy -= chvy;
if (down) chy += chvy;
if (chx <= CH_LEN)
chx = CH_LEN + 1;
if (chx >= WIDTH - CH_LEN)
chx = WIDTH - CH_LEN - 1;
if (chy <= CH_LEN)
chy = CH_LEN + 1;
if (chy >= HEIGHT - CH_LEN)
chy = HEIGHT - CH_LEN - 1;
}
void draw_crosshairs() {
tv.draw_row(chy, chx - CH_LEN, chx - 1, WHITE);
tv.draw_row(chy, chx + 1, chx + CH_LEN, WHITE);
tv.draw_column(chx, chy - CH_LEN, chy - 1, WHITE);
tv.draw_column(chx, chy + 1, chy + CH_LEN, WHITE);
}
move_crosshairs
checks all global variables related to the current Nunchuk state.
It updates the position of the crosshairs depending on the variable values.
Then it ensures that the crosshairs stay within the screen’s bounds.
Chapter 9. Tinkering with the Wii Nunchuk • 160
report erratum • discuss
www.it-ebooks.info