// Restart game variables public void Reset() { // Reset all game elements for (int i = 0; i < obstacleList.length; ++i) { if (obstacleList[i].isActive()) { obstacleList[i].setActive(false); } } nearestObstacle = obstacleList[0]; SpawnRate = 0.5f; SpawnTimer = 0.f; ScrollSpeed = 500; timer = 0.f; score = 0; Restart_button.setActive(false); Mainmenu_button.setActive(false); Tapped = false; FingerDown = false; vibrateTime = 0.f; UpdateHighscore = true; Progress_bar.setPosX(400); Win = false; // Reset everything first before we set game active back to true GameActive = true; }
// Update method to update the game play public void update(float dt, float fps) { FPS = fps; switch (GameState) { case 0: { // Only if game is not paused if (!GamePaused) { bgX -= ScrollSpeed * dt; // Speed of background scrolling // Reset once reaches 0 if (bgX < -ScreenWidth) { bgX = 0; } // Only when game is active we update the following if (GameActive) { SpawnTimer += dt; timer += dt; if (timer > 5.f) { ScrollSpeed += 100; timer = 0; } if (SpawnTimer > SpawnRate) { FetchObstacle(); SpawnTimer = 0.f; } stickman_anim.update(System.currentTimeMillis()); // Adventure mode if (GameMode == 1) { Progress_bar.setPosX(Progress_bar.getPosX() + BarSpeed * 0.015f); if (Progress_bar.getPosX() > DestinationPoint) { Win = true; GameActive = false; } } } // Feedback for game over if (GameActive == false) { if (UpdateHighscore && GameMode == 0) { Vector<Integer> highscores = appPrefs.getHighscore(); Vector<Integer> updatedscores = new Vector<Integer>(); boolean scoreentered = false; int index = 0; for (int i = 0; i < highscores.size(); ++i) { if (score > highscores.get(index) && !scoreentered) { updatedscores.addElement(score); scoreentered = true; } else { updatedscores.addElement(highscores.get(index)); ++index; } } for (int i = 0; i < updatedscores.size(); ++i) { appPrefs.setHighscore(i, updatedscores.get(i)); } UpdateHighscore = false; } // Vibration feedback vibrateTime += dt; if (vibrateTime > MaxVibrateTime) { stopVibrate(); } else { startVibrate(); } } if (nearestObstacle.isActive()) { // Detecting user tap for tapping obstacle if (nearestObstacle.getType() == Obstacle.TYPE.T_TAP && Tapped == true) { score += 10; nearestObstacle.setActive(false); DirectionVector.SetZero(); Tapped = false; } // Detecting user swipe direction for direction obstacle else if (DirectionVector.IsZero() == false && Obstacle.fromInteger(ProcessSwipe(DirectionVector)) == nearestObstacle.getType()) { score += 10; nearestObstacle.setActive(false); DirectionVector.SetZero(); } DirectionVector.SetZero(); } // Updating game elements for (int i = 0; i < obstacleList.length; ++i) { if (obstacleList[i].isActive()) { obstacleList[i].setPosX(obstacleList[i].getPosX() - ScrollSpeed * dt); // if out of screen if (obstacleList[i].getPosX() < 0) { obstacleList[i].setActive(false); } // Only if game is active we check these collisions if (GameActive == true) { // Player collision against obstacles if (CheckCollision( stickman_anim.getX(), stickman_anim.getY(), stickman_anim.getSpriteWidth(), stickman_anim.getSpriteHeight(), (int) obstacleList[i].getPosX(), (int) obstacleList[i].getPosY(), obstacleList[i].getImgWidth(), obstacleList[i].getImgHeight())) { obstacleList[i].setActive(false); GameActive = false; // Game status set to false // Enable buttons Restart_button.setActive(true); Mainmenu_button.setActive(true); } // Get nearest obstacle if (nearestObstacle.isActive() == false) { nearestObstacle = obstacleList[i]; } else if (obstacleList[i].getPosX() < nearestObstacle.getPosX()) { nearestObstacle = obstacleList[i]; } } } } } } break; } }
public void RenderGameplay(Canvas canvas) { // 2) Re-draw 2nd image after the 1st image ends if (canvas == null) { return; } canvas.drawBitmap(scaledbg, bgX, bgY, null); canvas.drawBitmap(scaledbg, bgX + ScreenWidth, bgY, null); // FPS canvas.drawText("FPS:" + FPS, 50, 75, paint); if (GameActive) { // Score // Only show score if endless mode if (GameMode == 0) { // Score canvas.drawText("Score:" + score, 800, 75, paint); } stickman_anim.draw(canvas); // Pause button canvas.drawBitmap( Pause_button.getImage(), Pause_button.getPosX(), Pause_button.getPosY(), null); // Adventure mode if (GameMode == 1) { // Adventure mode UI info canvas.drawBitmap( Progress_line.getImage(), Progress_line.getPosX(), Progress_line.getPosY(), null); canvas.drawBitmap( Progress_bar.getImage(), Progress_bar.getPosX(), Progress_bar.getPosY(), null); } } // Obstacles for (int i = 0; i < obstacleList.length; ++i) { // Only draw if active if (obstacleList[i].isActive()) { canvas.drawBitmap( obstacleList[i].getObstacle(), obstacleList[i].getPosX(), obstacleList[i].getPosY(), null); } } // Game is paused if (GamePaused) { // Paused Game canvas.drawBitmap( Pause_screen.getImage(), Pause_screen.getPosX(), Pause_screen.getPosY(), null); canvas.drawBitmap( Unpause_button.getImage(), Unpause_button.getPosX(), Unpause_button.getPosY(), null); } // Game is lost if (GameActive == false) { if (Win) { canvas.drawBitmap(Win_screen.getImage(), Win_screen.getPosX(), Win_screen.getPosY(), null); } else { canvas.drawBitmap( Gameover_screen.getImage(), Gameover_screen.getPosX(), Gameover_screen.getPosY(), null); // Only if endless we display the score if (GameMode == 0) { // Score canvas.drawText("Score:" + score, 800, 500, paint); } } canvas.drawBitmap( Restart_button.getImage(), Restart_button.getPosX(), Restart_button.getPosY(), null); canvas.drawBitmap( Mainmenu_button.getImage(), Mainmenu_button.getPosX(), Mainmenu_button.getPosY(), null); } }