The
draw_crosshairs
function actually draws the crosshairs on the screen. Instead
of using a bitmap to draw the crosshairs, we use two new TVout methods.
draw_row
outputs a horizontal line, and we use it to output the two horizontal
lines of the crosshairs. Similarly, we use
draw_column
to draw the two vertical
lines. We leave the pixel at the crossing point empty to make the crosshairs
look a bit nicer.
You might wonder why we didn’t use a bitmap. The problem with bitmaps is
that they don’t look nice when they overlap. Even if the bitmap looks like
crosshairs, it still is a square. If you move a crosshairs bitmap over a circle,
the bitmap will hide a big part of the circle’s pixels.
To complete the game’s source code, we need two functions for managing the
targets:
Tinkering/Pragduino/Pragduino.ino
bool target_hit() {
if (z_button)
return (target_x - chx) * (target_x - chx) +
(target_y - chy) * (target_y - chy) < target_r * target_r;
return false;
}
void check_target() {
if (target_hit()) {
hits++;
create_target();
}
int remaining_time = millis() - target_creation;
if (remaining_time >= TARGET_LIFESPAN) {
create_target();
}
int w = map(TARGET_LIFESPAN - remaining_time, 0, TARGET_LIFESPAN, 0, WIDTH);
tv.draw_rect(0, 0, w, 3, WHITE, WHITE);
}
target_hit
checks whether the player has hit the current target. This can only
happen if the player presses the Z button. If this is the case, we use a simple
distance calculation to see whether the crosshairs are in the circle.
To manage the current target’s lifecycle, we use
check_target
. If the target was
hit, we increment the
hits
counter and create the next target. After that, we
calculate the time the current target will stay on the screen unless it gets hit.
If this time is greater than the target’s lifespan, we create a new target. At the
end of the function, we turn the remaining time into a status bar at the top
of the screen.
report erratum • discuss
Creating Your Own Video Game • 161
www.it-ebooks.info