EasyManua.ls Logo

Arduino NG - Page 174

Arduino NG
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
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. Well 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 screens bounds. Also, it uses the
millis
function to determine the targets 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

Table of Contents

Related product manuals