hits = 0;
create_target();
}
void create_target() {
target_r = random(7, 11);
target_x = random(target_r, WIDTH - target_r);
target_y = random(target_r, HEIGHT - target_r);
target_count++;
target_creation = millis();
}
The
init_game
function sets most of the global variables to constant values.
create_target
is a bit more interesting. It creates a new target at a random
position and having a random size. We’ll use it later on in the game loop
whenever we need to create a new target. Note that the function ensures that
the target always stays within the screen’s bounds. Also, it uses the
millis
function to determine the target’s creation time.
Adding the Setup and Loop Functions
Like all Arduino programs our little game needs
setup
and
loop
functions:
Tinkering/Pragduino/Pragduino.ino
void setup() {
Line 1
randomSeed(analogRead(A0));
-
tv.begin(PAL, WIDTH, HEIGHT);
-
nunchuk.initialize();
-
init_game();
5
}
-
-
void loop() {
-
check_controls();
-
switch (state) {
10
case INTRO: intro(); break;
-
case STARTING: start_game(); break;
-
case RUNNING: update_game(); break;
-
case DONE: game_over(); break;
-
}
15
tv.delay_frame(1);
-
}
-
-
void check_controls() {
-
up = down = left = right = c_button = z_button = false;
20
if (nunchuk.update())
-
{
-
if (nunchuk.joystick_x() < 70)
-
left = true;
-
if (nunchuk.joystick_x() > 150)
25
right = true;
-
report erratum • discuss
Creating Your Own Video Game • 157
www.it-ebooks.info