Hardware Reference
In-Depth Information
Listing 11-3. Stack It's Sketch, Part 2 of 7
void RowShift() {
if (cubeMove[16-level] & 0x00000001){
RightLeft = false;
}
if (cubeMove[16-level] & 0x00800000){
RightLeft = true;
}
if (!RightLeft ){
cubeMove[16-level] = cubeMove[16-level] << 1;
}
if (RightLeft){
cubeMove[16-level] = cubeMove[16-level] >> 1;
}
} // end row shift
The function that is responsible for displaying the game to the player is the same in functionality as the display
function for Stop it, but is executed differently. The use of the Gameduino allows for dynamic positioning of game
elements within the 400×300 pixel viewing area, and there are no registers to directly manipulate. Part 3 is the function
for displaying all the sprites to the screen. The cubeMove array is used to hold the patterns that need to be displayed.
Every time the displaySprites() function is called, it will display all the current values of cubeMove ; A value of 1
equals a sprite, and a sprite will be displayed according to position within the array. The array is two dimensional
there is a vertical and a horizontal component; the array position is the vertical and the individual bits within the
variable makes up the horizontal position. Stepping through the array is done with one for loop, while a nested for
loop shifts though the bits of the variable. When there is a 1 in the variable, a sprite is displayed, and when there is a
0, the loop continues to the next step. The position of the sprite on the screen is determined by what step each for
loop is at, the sprite is 16×16 pixels. The step count of the for loops is multiplied by 16 so the sprites will be place side
by side on the screen. A counter that is incremented when a 1 is found to keep track of the number of sprites being
displayed and is used to create a dynamic sprite count for the Gameduino.
Listing 11-3. Stack It's Sketch, Part 3 of 7
void displaySprites() {
int spriteNum = 0; // start sprite count at 0
for (int y = 0 ; y < 18 ; y ++ ) { // loop though the array for y positon
for (int x = 0 ; x < 24 ; x ++) { // loop though the variable for x positon
if ((cubeMove[y] >> x) & 0x00000001) { // check current variable position for a 1
GD.sprite(spriteNum, (x* 16)+7, (y*16)+7 ,0, 8 , 0);
spriteNum++;
} // end if
} // end for loop x
} // end for loop y
} // end displaySprites
The buttonInterrupt() and WinState() functions implement part 4. buttonInterrupt() is called when the
player attempts to win the current level and move on to the next. The interrupt is activated in the same fashion as
in Stop it. buttonInterrupt() waits in a loop while the button is depressed and calls the WinState() function to
determine if the player has won or not. The check for a win condition has been moved to a separate function to allow
for possible other functions to check for win conditions outside of the player's control. A win state is true if there is
at least 1 bit in common between the current level and the prior level. The first level of the game is compared against
the foundation bits in the cubeMove array. The first level is always a gimme, and allows the player to decide where the
stack starts. If there are no common bits, the win state is false and the game is reset.
 
Search WWH ::




Custom Search