// METHODS // UPDATE & DRAW public override void Update(GameTime gameTime) { presentKey = Keyboard.GetState(); base.Update(gameTime); if (presentKey.IsKeyDown(Keys.Escape) && pastKey != presentKey && started) { paused = !paused; } if (!paused) { if (started) { pauseButton.Update(gameTime); if (pauseButton.Clicked) { paused = true; } player.Update(gameTime, true); foreach (Pipe pipe in pipes) { pipe.Update(gameTime); } for (int i = 0; i < 3; i++) { if ((!player.Intersect(pipes[i].Hole) && player.XPosition + Bird.Width / 2 > pipes[i].XpositionUp && player.XPosition - Bird.Width / 2 < pipes[i].XpositionUp + pipes[i].WidthUp)) { died = true; RessourcesManager.hurt.Play(); player.Flap(false); } else if (player.XPosition >= pipes[i].Hole.X + pipes[i].Hole.Width && player.XPosition <= pipes[i].Hole.X + pipes[i].Hole.Width + 1 && addScore) { totalScore++; addScore = false; RessourcesManager.pipePassed.Play(); } else { addScore = true; } } if (player.YPosition + Bird.Height / 2 >= Game1.screenHeight - ground.Height || died) { died = true; started = false; pauseBackground = true; RessourcesManager.hurt.Play(); player.Flap(false); } else if (player.YPosition - Bird.Height / 2 < 0) { player.YPosition = 0 + Bird.Height / 2; } } else if (died) { // ################ DIED ############## if (player.YPosition - Bird.Height / 2 > Game1.screenHeight + Bird.Height) { if (!over) { gameOver = new MenuGameOver(); over = true; } gameOver.Update(gameTime); } else { player.Update(gameTime, false); if (!writed) { if (totalScore > highScore) { if (totalScore < 10) { txtScore[1] = "high score=" + "00" + totalScore; } else if (totalScore < 100) { txtScore[1] = "high score=" + "0" + totalScore; } else { txtScore[1] = "high score=" + totalScore; } System.IO.File.WriteAllLines(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Flappy.txt", txtScore); highScore = totalScore; newScore = true; } writed = true; } } } else { menuButton.Update(gameTime); if (menuButton.Clicked) { GameMain.ChangeMenu = "main"; } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { started = true; } player = new Bird(); addScore = true; pipes = new Pipe[3]; for (int i = 0; i < 3; i++) { pipes[i] = new Pipe(i * 267); } } } else { menuButton.Update(gameTime); if (menuButton.Clicked) { GameMain.ChangeMenu = "main"; } resumeButton.Update(gameTime); if (resumeButton.Clicked) { paused = false; } } pastKey = presentKey; }
// METHODS // UPDATE & DRAW public override void Update(GameTime gameTime) { presentKey = Keyboard.GetState(); base.Update(gameTime); if (presentKey.IsKeyDown(Keys.Escape) && pastKey != presentKey && started) paused = !paused; if (!paused) { if (started) { pauseButton.Update(gameTime); if (pauseButton.Clicked) paused = true; player.Update(gameTime, true); foreach (Pipe pipe in pipes) { pipe.Update(gameTime); } for (int i = 0; i < 3; i++) { if ((!player.Intersect(pipes[i].Hole) && player.XPosition + Bird.Width / 2 > pipes[i].XpositionUp && player.XPosition - Bird.Width / 2 < pipes[i].XpositionUp + pipes[i].WidthUp)) { died = true; RessourcesManager.hurt.Play(); player.Flap(false); } else if (player.XPosition >= pipes[i].Hole.X + pipes[i].Hole.Width && player.XPosition <= pipes[i].Hole.X + pipes[i].Hole.Width + 1 && addScore) { totalScore++; addScore = false; RessourcesManager.pipePassed.Play(); } else addScore = true; } if (player.YPosition + Bird.Height / 2 >= Game1.screenHeight - ground.Height || died) { died = true; started = false; pauseBackground = true; RessourcesManager.hurt.Play(); player.Flap(false); } else if (player.YPosition - Bird.Height / 2 < 0) { player.YPosition = 0 + Bird.Height / 2; } } else if (died) { // ################ DIED ############## if (player.YPosition - Bird.Height / 2 > Game1.screenHeight + Bird.Height) { if (!over) { gameOver = new MenuGameOver(); over = true; } gameOver.Update(gameTime); } else { player.Update(gameTime, false); if (!writed) { if (totalScore > highScore) { if (totalScore < 10) { txtScore[1] = "high score=" + "00" + totalScore; } else if (totalScore < 100) { txtScore[1] = "high score=" + "0" + totalScore; } else { txtScore[1] = "high score=" + totalScore; } System.IO.File.WriteAllLines(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Flappy.txt", txtScore); highScore = totalScore; newScore = true; } writed = true; } } } else { menuButton.Update(gameTime); if (menuButton.Clicked) { GameMain.ChangeMenu = "main"; } if (Keyboard.GetState().IsKeyDown(Keys.Space)) started = true; player = new Bird(); addScore = true; pipes = new Pipe[3]; for (int i = 0; i < 3; i++) { pipes[i] = new Pipe(i * 267); } } } else { menuButton.Update(gameTime); if (menuButton.Clicked) { GameMain.ChangeMenu = "main"; } resumeButton.Update(gameTime); if (resumeButton.Clicked) { paused = false; } } pastKey = presentKey; }