private void Form1_KeyDown(object sender, KeyEventArgs e) { // move --> if (timer.Enabled && e.KeyCode == Keys.Right) { player.MoveRight(); } // move <-- if (timer.Enabled && e.KeyCode == Keys.Left) // && e.Modifiers == Keys.Control { player.MoveLeft(); } // shoot if (timer.Enabled && e.KeyCode == Keys.Space) { Missle missle = player.CreateMissle(missleSize, missleSpeed, 1); missle.InitializeSprite(); missle.SetSpriteLocation(); missles.Add(missle); Controls.Add(missle.Sprite); } // pause if (e.KeyCode == Keys.P) { timer.Enabled = !timer.Enabled; } // restart if (!timer.Enabled && e.KeyCode == Keys.Enter) { Controls.Clear(); asteroids.Clear(); missles.Clear(); player.Lives = numberOfLives; player.Reposition(this.Width, this.Height, 1); player.PositionSprite(); numberOfLivesLabel.Text = String.Format("Number of lives = {0}", player.Lives); scoreLabel.Text = String.Format("Score = {0:D2}", player.Score = 0); Controls.Add(numberOfLivesLabel); Controls.Add(scoreLabel); Controls.Add(player.Sprite); timer.Start(); } //exit if (timer.Enabled && e.KeyCode == Keys.Escape) { this.Close(); } if (!timer.Enabled && e.KeyCode == Keys.E) { Scoreboard scr = new Scoreboard(); scr.updateScoreboard(player); scr.updateFile(); ScoreBoardDisplay scrBoard = new ScoreBoardDisplay(scr); scrBoard.Show(); this.Close(); } }
// Controls private void Form1_KeyDown(object sender, KeyEventArgs e) { // move Player 1--> if (timer.Enabled && e.KeyCode == Keys.Right && Player1.Lives != 0) { Player1.MoveRight(); } // move Player 1<-- if (timer.Enabled && e.KeyCode == Keys.Left && Player1.Lives != 0) // && e.Modifiers == Keys.Control { Player1.MoveLeft(); } // shoot Player 1 if (timer.Enabled && e.KeyCode == Keys.Space && Player1.Lives != 0) { Missle missle = Player1.CreateMissle(missleSize, missleSpeed, 1); missle.InitializeSprite(); missle.SetSpriteLocation(); missles.Add(missle); Controls.Add(missle.Sprite); } // move Player 2--> if (timer.Enabled && e.KeyCode == Keys.D && Player2.Lives != 0) { Player2.MoveRight(); } // move Player 2<-- if (timer.Enabled && e.KeyCode == Keys.A && Player2.Lives != 0) // && e.Modifiers == Keys.Control { Player2.MoveLeft(); } // shoot Player 2 if (timer.Enabled && e.KeyCode == Keys.F && Player2.Lives != 0) { MissleVs2 missle2 = Player2.CreateMissle2(missleSize, missleSpeed); missles2.Add(missle2); Controls.Add(missle2.Sprite); } // pause if (e.KeyCode == Keys.P) { timer.Enabled = !timer.Enabled; } // exit if (e.KeyCode == Keys.Escape) { this.Close(); } // restart if (!timer.Enabled && e.KeyCode == Keys.Enter) { // clear everything on screen Controls.Clear(); //asteroids.Clear(); missles.Clear(); // retrieve new number of lives Player1.Lives = numberOfLives; Player2.Lives = numberOfLives; // reset Player position Player1.Reposition(this.Width, this.Height, 1); Player2.Reposition2(this.Width, this.Height, 2); // update number of lives display numberOfLivesLabel.Text = String.Format("Lives = {0}", Player1.Lives); numberOfLivesLabel1.Text = String.Format("Lives = {0}", Player2.Lives); // re-display Controls.Add(numberOfLivesLabel); // number of lives for Player 1 Controls.Add(numberOfLivesLabel1); // number of lives for Player 2 Controls.Add(Player1.Sprite); // Player 1 Controls.Add(Player2.Sprite); // Player 2 timer.Start(); // restart timer } }