void instructions() { REG_DISPCTL = MODE4 | BG2_ENABLE; loadPalette(instructionsPal); drawBackgroundImage4(instructionsBitmap); while(!BUTTON_HELD(BUTTON_B)); playSoundB(menuSound, MENUSOUNDLEN, MENUSOUNDFREQ, 0); state = SPLASH; }
void game() { // Reset running motion in case user stops pressing arrow key player.isRunning = 0; // Running/moving speed if (player.frameCount % 4 == 0) { changeFrame(); } // Button Handling if (BUTTON_PRESSED(BUTTON_A)) { // Only jump if player is not already jumping if (canJump && checkCollision()) { canJump = 0; player.isJumping = 1; jumpFrameCounter = 30; playSoundB(jumpSound, JUMPSOUNDLEN, JUMPSOUNDFREQ, 0); jump(); } } if (BUTTON_PRESSED(BUTTON_B)) { vBlankCount = 100; prevSeason = season; playSoundB(warpSound, WARPSOUNDLEN, WARPSOUNDFREQ, 0); state = SEASONCHANGE; } if (BUTTON_PRESSED(BUTTON_START)) { REG_DISPCTL ^= BG0_ENABLE; pauseSound(); state = PAUSE; } // Change to neutral season - CHEAT MODE if (BUTTON_HELD(BUTTON_R) && BUTTON_HELD(BUTTON_L) && BUTTON_HELD(BUTTON_SELECT)) { vBlankCount = 100; prevSeason = -1; playSoundB(warpSound, WARPSOUNDLEN, WARPSOUNDFREQ, 0); state = SEASONCHANGE; } if (BUTTON_HELD(BUTTON_RIGHT)) { player.isRunning = 1; player.facing = 0; player.deltaCol = 1; if (hOff < 272 && player.col >= 120 - player.height / 2) { hOff += player.deltaCol; } else if (player.col < 240 - player.width) { player.col += player.deltaCol; } } if (BUTTON_HELD(BUTTON_LEFT)) { player.isRunning = 1; player.facing = 1; player.deltaCol = -1; if (hOff > 0 && player.col < 120 - player.width / 2) { hOff += player.deltaCol; } else if (player.col > 0) { player.col += player.deltaCol; } } // End of button handling // Jump handling if (player.isJumping) { jump(); } /* if (!canJump && hOff < 272) { player.col += player.deltaCol; } */ // Gravity and Collision if (!checkCollision() || player.isJumping) { player.isRunning = 0; // Stop running animation /*if (vOff <= 96 && player.row >= 80 - player.height / 2) { */ vOff += player.deltaRow; //} if (player.row < 160) { player.row += player.deltaRow; } else { state = LOSE; } } else { canJump = 1; player.deltaCol = 0; } // Gem collision detection - player wins current level if (checkWinCollision()) { playSoundB(winLevelSound, WINLEVELSOUNDLEN, WINLEVELSOUNDFREQ, 0); state = WINLEVEL; } // Standing still animation if (!player.isRunning) { player.currentFrame = 0; } // Running animation if (player.isRunning) { player.frameCount++; } // Seasonal BG1 Animations switch (season) { case FALL: bg1VOff--; break; case WINTER: bg1HOff--; bg1VOff--; break; } }
int main(void) { while(1) { previousSpriteState = spriteState; pollKeys(); enableSounds(); setupInterrupts(); switch(state) { case SPLASH: { REG_DISPCNTL = BG2_ENABLE | MODE3; fillPicture((u16 *)splashBitmap); if (BUTTON_PRESSED(START_BUTTON)) { state = GAME; } if (BUTTON_PRESSED(SELECT_BUTTON)) { state = INSTRUCTIONS; } break; } case INSTRUCTIONS: { fillPicture((u16 *)instructionsBitmap); if (BUTTON_PRESSED(SELECT_BUTTON)) { state = SPLASH; } break; } case GAME: { //load tiles DMA_MEMORY[3].source = backgroundTiles; DMA_MEMORY[3].destination = &CHARBLOCK[0]; DMA_MEMORY[3].control = 12000 | DMA_ON; //load map DMA_MEMORY[3].source = backgroundMap; DMA_MEMORY[3].destination = &SCREENBLOCK[27]; DMA_MEMORY[3].control = 4096 | DMA_ON; //load pallette DMA_MEMORY[3].source = backgroundPal; DMA_MEMORY[3].destination = PAL_MEM; DMA_MEMORY[3].control = 256 | DMA_ON; //load the sprites into memory DMA_MEMORY[3].source = playerTiles; DMA_MEMORY[3].destination = &CHARBLOCK[4]; DMA_MEMORY[3].control = 16384 | DMA_ON; DMA_MEMORY[3].source = playerPal; DMA_MEMORY[3].destination = SPRITE_PALETTE; DMA_MEMORY[3].control = 256 | DMA_ON; //Hide all sprites int i; for(i=0; i<128; i++) { OAM[i].attr0 |= ATTR0_HIDE; } //Set Mode 0 BG 0 and Enable sprites. then set up the sprites REG_DISPCNTL = MODE0 | BG0_ENABLE | SPRITE_ENABLE; REG_BG0CNT = BG_8BPP | BG_REG_64x64 | CBB(0) | SBB(27); REG_TM0CNT = 0; DMA[1].cnt = 0; REG_TM0D = timerintervalA; REG_TM0CNT = TIMER_ON; DMA[1].src = bgsound; DMA[1].dst = REG_FIFO_A; DMA[1].cnt = DMA_ON | START_ON_FIFO_EMPTY | DMA_32 | DMA_REPEAT | DMA_DESTINATION_FIXED; startA = vblankcountA; while(1){ shadowOAM[0].attr0 = (playerRow & MASKROW) | ATTR0_TALL | ATTR0_REGULAR | ATTR0_8BPP; shadowOAM[0].attr1 = (playerCol & MASKCOL)| ATTR1_SIZE32; shadowOAM[0].attr2 = (SPRITEOFFSET16(0,0)); shadowOAM[1].attr0 = badguyRow | ATTR0_TALL | ATTR0_REGULAR | ATTR0_8BPP; shadowOAM[1].attr1 = badguyCol | ATTR1_SIZE32; shadowOAM[1].attr2 = (SPRITEOFFSET16(0,8)); pollKeys(); bigRow = playerRow + vOff; bigCol = playerCol + hOff; switch(spriteState) { case STILL: { shadowOAM[0].attr2 = (SPRITEOFFSET16(0,0)); break; } case MOVING: { if((walkCounter%2)==1) shadowOAM[0].attr2 = (SPRITEOFFSET16(0,4)); else if((walkCounter%2==0)) { shadowOAM[0].attr2 = (SPRITEOFFSET16(0,0)); } break; } } if(BUTTON_HELD(A_BUTTON) && BUTTON_HELD(B_BUTTON)) { //load tiles DMA_MEMORY[3].source = cheatbackgroundTiles; DMA_MEMORY[3].destination = &CHARBLOCK[0]; DMA_MEMORY[3].control = 11456 | DMA_ON; //load map DMA_MEMORY[3].source = cheatbackgroundMap; DMA_MEMORY[3].destination = &SCREENBLOCK[27]; DMA_MEMORY[3].control = 4096 | DMA_ON; //load pallette DMA_MEMORY[3].source = cheatbackgroundPal; DMA_MEMORY[3].destination = PAL_MEM; DMA_MEMORY[3].control = 256 | DMA_ON; } if (BUTTON_HELD(RIGHT_BUTTON)) { spriteState=MOVING; walkCounter++; if(collisionmapBitmap[((bigRow+playerHeight)*collMapSize)+(bigCol+playerCVel+playerWidth)]==WHITE) { if(playerCol<(SCREEN_WIDTH-(playerWidth+5))){ playerCol+=playerCVel; } else if(hOff<(collMapSize-SCREEN_WIDTH)) { hOff+=playerCVel; } } } if (BUTTON_HELD(LEFT_BUTTON)) { spriteState=MOVING; walkCounter++; if(collisionmapBitmap[((bigRow+playerHeight)*collMapSize)+(bigCol+playerCVel)]==WHITE) { if(playerCol>(0)) { playerCol-=playerCVel; } else if(hOff>(SCREEN_WIDTH-collMapSize)) { hOff-=playerCVel; } } } if (BUTTON_HELD(UP_BUTTON)) { spriteState=MOVING; walkCounter++; //Check collision and move if((collisionmapBitmap[(((bigRow-playerRVel)*collMapSize)+(bigCol+playerWidth))]==WHITE)&& (collisionmapBitmap[(((bigRow-playerRVel)*collMapSize)+(bigCol))]==WHITE)) { if(playerRow>30) { playerRow-=playerRVel; } else if(vOff>0) { vOff-=playerRVel; } } } if (BUTTON_HELD(DOWN_BUTTON)) { spriteState=MOVING; walkCounter++; //Check collision and move if((collisionmapBitmap[(((bigRow+playerHeight+playerRVel)*collMapSize)+(bigCol+playerWidth))]==WHITE)&& (collisionmapBitmap[(((bigRow+playerHeight+playerRVel)*collMapSize)+(bigCol))]==WHITE)) { if(playerRow<(SCREEN_HEIGHT-(playerHeight))) { playerRow+=playerRVel; } else if(vOff<(collMapSize-SCREEN_HEIGHT)) { vOff+=playerRVel; } if((playerRow > 440) && (playerCol > 464)) { state = WIN; } } } if (BUTTON_PRESSED(START_BUTTON)) { REG_TM1CNT = 0; DMA[2].cnt = 0; REG_TM1D = timerintervalB; REG_TM1CNT = TIMER_ON; DMA[2].src = bark; DMA[2].dst = REG_FIFO_B; DMA[2].cnt = DMA_ON | START_ON_FIFO_EMPTY | DMA_32 | DMA_REPEAT | DMA_DESTINATION_FIXED; startB = vblankcountB; } if (BUTTON_PRESSED(SELECT_BUTTON)) { //waitForNoKey(); state = PAUSE; } waitForVBlank(); OAM[0] = shadowOAM[0]; OAM[1] = shadowOAM[1]; REG_BG0HOFS = hOff; REG_BG0VOFS = vOff; } } case WIN: { while(1); } case PAUSE: { if(BUTTON_PRESSED(SELECT_BUTTON)) { state=GAME; } } } } return 0; }