if (nunchuk.joystick_y() > 150)
-
up = true;
-
if (nunchuk.joystick_y() < 70)
-
down = true;
30
c_button = nunchuk.c_button();
-
z_button = nunchuk.z_button();
-
}
-
}
-
setup
initializes the random number generator using some noise from analog
pin A0. Then it initializes the screen and the Nunchuk, and finally it calls
init_game
to set all global variables to reasonable values.
The
loop
function calls
check_controls
to read the current state of the Nunchuk.
Then it checks the game’s current state in a
switch
statement and delegates
its work to the function responsible for handling the current state.
In line 16,
loop
calls a function of the TVout library you haven’t seen before.
delay_frame
waits for the beginning of the next vertical blanking interval—that
is, for the moment when the TV set’s electron beam wanders from the bottom
of the screen back to the top. We only want to wait for the beginning of the
next frame, so we pass 1 to
delay_frame
. This is necessary to prevent flickering,
because it ensures that the creation of the game’s graphics in memory stays
in sync with the actual output on the screen.
Handling the Different Game States
Handling the game’s different states is vital, so we define separate functions
for dealing with each game state:
Tinkering/Pragduino/Pragduino.ino
void intro() {
Line 1
tv.select_font(font8x8);
-
tv.printPGM(28, 20, PSTR("Pragduino"));
-
tv.select_font(font6x8);
-
tv.printPGM(16, 40, PSTR("A Pragmatic Game"));
5
tv.select_font(font4x6);
-
tv.printPGM(18, 74, PSTR("Press Z-Button to Start"));
-
if (z_button) {
-
state = STARTING;
-
z_button = false;
10
delay(200);
-
}
-
}
-
-
void start_game() {
15
tv.clear_screen();
-
tv.select_font(font8x8);
-
tv.printPGM(40, 44, PSTR("READY?"));
-
Chapter 9. Tinkering with the Wii Nunchuk • 158
report erratum • discuss
www.it-ebooks.info