Hardware Reference
In-Depth Information
The resetPlay() function of part 6 ensures that the game is set back to the beginning and ready for a new
attempt. The cubeMove array is first zeroed and loaded with the initial state. The Gameduino then needs the sprite
buffer cleared, because sprites with a higher number than that currently produced from the cubeMove pattern will
remain on the screen. A loop is used to step through all 256 possible sprites and tell the Gameduino to draw blank
sprites off the screen.
Listing 11-3. Stack It's Sketch, Part 6 of 7
void resetPlay () {
for (int i = 0 ; i < 17 ; i ++) {
cubeMove [i] = 0x00000000;
}
cubeMove[16] = initPattern;
cubeMove[17] = 0x00ffffff;
for (int i = 0 ; i < 256 ; i ++) {
GD.sprite(i,450,450,0,0,0);
}
level = 0;
} // end resetPlay
As with Stop it, the final function is the loop (shown in part 7) sets the play into motion for the game. Other than
the names of the functions that are called, this function is nearly identical to the one used in Stop it. To account for the
increase in levels and the gimme level, the initial delay has been increased to 120 ms, leaving 18 ms for the player to
react at the final level. Because of the increased complexity and the display speeds included with the Gameduino,
the program spends a bit more time with the interrupt off.
Listing 11-3. Stack It's Sketch, Part 7 of 7
void loop() {
detachInterrupt(0);
if (button) {
checkWin();
}
RowShift();
displaySprites();
attachInterrupt(0, buttonInterrupt, LOW);
delay (120 - ( level * 6));
} // end loop
Verifying the Code
At this point, the code for Stack it is ready for a trial run. Configure the hardware as per Figure 11-3 (shown earlier in
the chapter), and load the sketch onto the Arduino. The game should start immediately after the upload is finished
and display four sprites in a row sweeping from side to side above a full row of sprites at the bottom. Once the
button is pressed, the current level will stop and move to the next level. Check to see if the game-loss functionality is
working by failing to match up the sprites. The game should fully reset when the last sprite is lost. Stack it does not
have a convenient developer mode like Stop it has; the final levels have to be reached naturally or the delay has to be
increased to check the reset to the beginning from the final win.
 
Search WWH ::




Custom Search