private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Space: if (FlappyBird.Margin.Top < 370) { AddHeight = 10; if (pipeAnimationTimer == null) { InitializeGame(); } if (pipeAnimationTimer.IsEnabled == true) { if (BackgroundSound.IsEnabled == true) { BackgroundSound.Stop(); } BackgroundSound.Source = new Uri(@"Resources\Camera_Shutter.wav", UriKind.Relative); BackgroundSound.Play(); } } break; case Key.N: new MainWindow().Show(); this.Close(); break; } }
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (FlappyBird.Margin.Top < 370) { AddHeight = 10; } if (pipeAnimationTimer == null) { InitializeGame(); } if (pipeAnimationTimer.IsEnabled == true) { if (BackgroundSound.IsEnabled == true) { BackgroundSound.Stop(); } BackgroundSound.Source = new Uri(@"Resources\Camera_Shutter.wav", UriKind.Relative); BackgroundSound.Play(); } }
void pipeAnimationTimer_Tick(object sender, EventArgs e) { var flappyBirdMargin = FlappyBird.Margin; if (AddHeight > 0) { AddHeight--; flappyBirdMargin.Top += 4; } else { flappyBirdMargin.Top -= 3; } if (flappyBirdMargin.Top <= 0) { Result.Content = "Game Over!!" + "\n\nScore : " + score.ToString("000"); Result.Visibility = System.Windows.Visibility.Visible; pipeAnimationTimer.Stop(); if (BackgroundSound.IsEnabled == true) { BackgroundSound.Stop(); } BackgroundSound.Source = new Uri(@"Resources\hit.mp3", UriKind.Relative); BackgroundSound.Play(); } FlappyBird.Margin = flappyBirdMargin; for (int i = 0; i < 3; i++) { var pipeMargin = pipe[i].Margin; pipeMargin.Left = (pipeMargin.Left + 2.5) % 525; pipe[i].Margin = pipeMargin; pipeMargin.Top = hole[i].Margin.Top; if (pipeMargin.Left == 0) { pipe[i].Visibility = System.Windows.Visibility.Visible; pipeMargin.Top = randomHole.Next(70, 240); } if (score > 5) { if (holeDown[i] == true) { if (pipeMargin.Top > 160) { pipeMargin.Top -= 1; } else { holeDown[i] = false; } } else { if (pipeMargin.Top < 240) { pipeMargin.Top += 1; } else { holeDown[i] = true; } } } hole[i].Margin = pipeMargin; if (pipe[i].Visibility == System.Windows.Visibility.Visible) { if (pipeMargin.Left < FlappyBird.Margin.Left + 30 && pipeMargin.Left + 55 > FlappyBird.Margin.Left && !(pipeMargin.Top <= FlappyBird.Margin.Top + 2 && pipeMargin.Top + 80 >= FlappyBird.Margin.Top + 30)) { Result.Content = "Game Over!!" + "\n\nScore : " + score.ToString("000"); Result.Visibility = System.Windows.Visibility.Visible; pipeAnimationTimer.Stop(); if (BackgroundSound.IsEnabled == true) { BackgroundSound.Stop(); } BackgroundSound.Source = new Uri(@"Resources\hit.mp3", UriKind.Relative); BackgroundSound.Play(); } else { if (pipeMargin.Left == 205) { Score.Content = "Score : " + (++score).ToString("000"); } } } } }