private void GameControl_Loaded(object sender, RoutedEventArgs e) { gameLogic = ((MainMenuWindow)Application.Current.MainWindow).Logic; gameDisplay = new SpaceInvaderDisplay(this.gameLogic.Model); Window win = Window.GetWindow(this); InvalidateVisual(); if (win != null) { win.KeyDown += Win_KeyDown; win.KeyUp += Win_KeyUp; ufoTimer = new DispatcherTimer(); ufoTimer.Interval = TimeSpan.FromMilliseconds(this.gameLogic.UfoTimerTick); ufoTimer.Tick += UfoTimer_Tick; ufoTimer.Start(); sidewayUfoTimer = new DispatcherTimer(); sidewayUfoTimer.Interval = TimeSpan.FromMilliseconds(88); sidewayUfoTimer.Tick += SideWayUFOTimer_Tick; sidewayUfoTimer.Start(); projectileTimer = new DispatcherTimer(); projectileTimer.Interval = TimeSpan.FromMilliseconds(1); projectileTimer.Tick += ProjectileTimer_Tick; projectileTimer.Start(); } }
private void Win_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Space && this.canShoot) { this.canShoot = false; this.gameLogic.PlayerShoot(); } else if (e.Key == Key.Left) { this.isMovingRight = false; } else if (e.Key == Key.Right) { this.isMovingRight = true; } else if (e.Key == Key.Escape) { this.gameLogic.GameStateSwitch(GameState.Paused); PauseWindow window = new PauseWindow(this.gameLogic); window.ShowDialog(); this.gameDisplay = new SpaceInvaderDisplay(this.gameLogic.Model); this.isMovingRight = null; this.gameLogic.GameStateSwitch(GameState.Running); InvalidateVisual(); } else if (this.gameLogic.Model.GameState == GameState.Finished && e.Key == Key.Enter) { Window.GetWindow(this).Close(); } }