int main(int argc, char* args[]) { SDL_Window* window = nullptr; SDL_Surface* windowSurface = nullptr; SDL_Renderer* renderer = nullptr; if (!init(&window, &windowSurface, &renderer)) { printf("Failed to initialize SDL.\n", SDL_GetError()); } else { bool quit = false; SDL_Event e; Player player; SDL_Rect winBox; addEnemy(); addEntity(player.m_pos, 375, 25, 25, 25); addEntity(winBox, 0, 750, 50, 800); //Game loop while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } if (e.type == SDL_KEYDOWN) { inputMananger(e, player.m_pos, player.m_movementSpeed); } } if (playerCollisions(enemies, player)) { resetPlayerPos(player); } enemyMovement(); gameBounds(player.m_pos); render(&renderer, player.m_pos, winBox); //Check to see whether the game is won if (checkWinCollision(winBox, player)) { quit = true; } SDL_Delay(16); //Simulate 16 fps } } close(&window, &renderer); return 0; }
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; } }