if
(z_button) {
-
init_game();
20
state = RUNNING;
-
}
-
}
-
-
void
game_over() {
25
tv.clear_screen();
-
tv.select_font(font8x8);
-
tv.printPGM(28, 38,
-
int
x = (WIDTH - 7 * 8) / 2;
-
if
(hits > 9)
30
x = (WIDTH - 8 * 8) / 2;
-
tv.printPGM(x, 50,
-
tv.print(x + 6 * 8, 50, hits);
-
if
(z_button) {
-
state = STARTING;
35
z_button = false;
-
delay(200);
-
}
-
}
-
40
void
update_game() {
-
tv.clear_screen();
-
tv.draw_circle(target_x, target_y, target_r, WHITE);
-
move_crosshairs();
-
draw_crosshairs();
45
check_target();
-
if
(target_count == MAX_TARGET + 1) {
-
state = DONE;
-
z_button = false;
-
delay(200);
50
}
-
}
-
The
intro
,
start_game
, and
message to the screen, then they wait for a Z button press. If the Z button
was pressed, they move to the next state. Before they move to the next state,
they set
z_button
to false and wait for 200 milliseconds. This is necessary to
debounce the Z button.
All three functions use yet another TVout method. Look at line 3, for example.
Here we use TVout s
but it reads the string to be output from the Arduino s flash memory and not
from its precious SRAM. For applications that display a lot of constant mes-
sages, this can save a lot of memory.
PSTR("Game
Over"));
PSTR("Hits:
"));
game_over
functions are very similar. They print a
method. It works like the regular
printPGM
www.it-ebooks.info
Creating Your Own Video Game
method,
print
159
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?