Before we dive into the game’s code, make sure you’ve installed the TVout
library as described in Using the TVout Library, on page 133. You also have to
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
code/Tinkering/Nunchuk
directory to the
libraries
folder of your
Arduino IDE. Alternatively, you can create a folder named
Nunchuk
in your
IDE’s
libraries
folder and copy the
nunchuk.h
and
nunchuk.cpp
files to it. In both
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
contain the screen dimensions, and
CH_LEN
contains the
length of a single crosshair element. (The crosshairs consist of four elements.)
MAX_TARGET
contains the number of circles you have to shoot, and
TARGET_LIFESPAN
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;
-
report erratum • discuss
Creating Your Own Video Game • 155
www.it-ebooks.info