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
function to determine the target s creation time.
Adding the Setup and Loop Functions
Like all Arduino programs our little game needs
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:
-
case
STARTING: start_game();
-
case
RUNNING:
-
case
DONE:
-
}
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;
-
intro();
break;
break;
update_game(); break;
game_over();
break;
www.it-ebooks.info
Creating Your Own Video Game
setup
and
loop
functions:
157
millis
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?