// Handles game over when ball falls out of bounds // Clears powerups from gameboard, saves new game statistics, and shows "gameOverUI" // Returns: // Void public void initiateGameOver() { gamePlayMusic.Pause(); gameOver.Play(); shakeEffect.ShakeIt(); removeAllPowerups(); StartCoroutine(waitForWhile()); // Update 'final score' board then display game over screen finalBoard.updateText("Score: " + gameScore.ToString()); // If a new highscore, set and save to device if (gameScore > PlayerPrefs.GetInt("highScore")) { PlayerPrefs.SetInt("highScore", gameScore); } // Set and save total score to device PlayerPrefs.SetInt("totalScore", PlayerPrefs.GetInt("totalScore") + gameScore); // Set and save total game count to device PlayerPrefs.SetInt("gamesPlayed", PlayerPrefs.GetInt("gamesPlayed") + 1); return; }
// Increases the gamescore text element on board // Every tenth bounce it will call to spawn a random powerup and increase ball speed by 5% // Returns: // Void public void updateExistingScore() { // Alternate ping pong sound effects if (numberOfBounces % 2 == 0) { pongHit1.Play(); } else { pongHit2.Play(); } numberOfBounces++; // If double points powerup active, add extra point if (doublePointsActive) { gameScore += 2; } else { gameScore++; } // Start bubble effect for board bubble.bubbleEffect(); scoreBoard.updateText(gameScore.ToString()); // Instantiate a powerup every 10 bounces and increase ball speed by 5% if (numberOfBounces % 10 == 0 && numberOfBounces != 0) { spawnPowerUp(); ballScript.changeSpeed(1.05f); } return; }
// Determines which player (top or bottom) has won, and updates corresponding "Win/Lose" text elements to respective sides // Returns: // Void public void decideWinLoseSides() { if (bottomPlayerScore == scoreToWin) { topOutcome.updateText("Win"); bottomOutcome.updateText("Lose"); } else if (topPlayerScore == scoreToWin) { topOutcome.updateText("Lose"); bottomOutcome.updateText("Win"); } else { bottomOutcome.updateText("Draw"); topOutcome.updateText("Draw"); } }
// At start of scene, before gameplay has begun, update high and average scores void Start() { // Update highscore text highScoreBoard.updateText("Highscore: " + PlayerPrefs.GetInt("highScore").ToString()); // Calculate average score float averageScore = 0; if (PlayerPrefs.GetInt("gamesPlayed") > 0) { averageScore = ((float)PlayerPrefs.GetInt("totalScore") / (float)PlayerPrefs.GetInt("gamesPlayed")); averageScore = Mathf.Round(averageScore); } // Update average score text averageScoreBoard.updateText("Average Score: " + averageScore); }
// Handles game rounds every time ball fall out of bounds // Either displays game over screen (top or bottom player score reaches 3), or sets up next round // Returns: // Void public void replayOrOver() { gameOver.Play(); shakeEffect.ShakeIt(); if (lastTouched == TOP) { topPlayerScore++; topPlayerScoreBoard.updateText(topPlayerScore.ToString()); } else if (lastTouched == BOTTOM) { bottomPlayerScore++; bottomPlayerScoreBoard.updateText(bottomPlayerScore.ToString()); } // If top or bottom player has reached the score to win, initiate game over screen if (topPlayerScore == scoreToWin || bottomPlayerScore == scoreToWin) { startingGameOver = true; gamePlayMusic.Pause(); initiateGameOver(); } else { // Else, setup for next round // Drop ball again, reset powerups, ball speed, and destory all power ups ballScript.dropBall(); turnOffPowerUpBar(BOTH); ballScript.changeSpeed(1 - (multTimes / 1f)); multTimes = 0f; removeAllPowerups(); } return; }