Before we dive into the game s code, make sure you ve installed the TVout
library as described in
make the code of your Nunchuk library available. We haven t turned it into
a complete library in this chapter, but the book s code archive contains an
enhanced version. Download the book s code from the book s website and
unzip it. Copy the
Arduino IDE. Alternatively, you can create a folder named
IDE s
libraries
folder and copy the
cases you have to restart the IDE.
That s all the preparation you need to implement the Pragduino game, so let s
get started.
Setting the Stage for the Game
Most games need to handle a lot of global state, and Pragduino is no exception,
so its code starts with a list of constant and variable definitions:
Tinkering/Pragduino/Pragduino.ino
#include <Wire.h>
#include <TVout.h>
#include <fontALL.h>
#include "nunchuk.h"
const int
WIDTH = 128;
const int
HEIGHT = 96;
const int
CH_LEN = 8;
const int
MAX_TARGET = 10;
const int
TARGET_LIFESPAN = 1500;
As usual, we include all header files we need and define a few constants.
WIDTH
and
HEIGHT
length of a single crosshair element. (The crosshairs consist of four elements.)
MAX_TARGET
contains the number of circles you have to shoot, and
contains a circle s lifespan measured in milliseconds.
Next we define several global variables:
Tinkering/Pragduino/Pragduino.ino
TVout tv;
Line 1
Nunchuk nunchuk;
-
-
boolean up, down, left, right, c_button, z_button;
-
int
chx, chy;
5
int
chvx, chvy;
-
int
target_x, target_y, target_r;
-
unsigned int
target_count;
-
unsigned int
hits;
-
Using the TVout Library, on page
code/Tinkering/Nunchuk
directory to the
nunchuk.h
contain the screen dimensions, and
www.it-ebooks.info
Creating Your Own Video Game
133. You also have to
libraries
folder of your
Nunchuk
and
nunchuk.cpp
files to it. In both
CH_LEN
contains the
TARGET_LIFESPAN
155
in your
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?