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(); } }
private void Form1_KeyDown(object sender, KeyEventArgs e) { if (timer.Enabled && player.Lives != 0) { // move right if (e.KeyCode == Keys.Right) { player.MoveRight(); player.MoveSprite(); } // move left if (e.KeyCode == Keys.Left) { player.MoveLeft(); player.MoveSprite(); } // move up if (e.KeyCode == Keys.Up) { player.MoveUp(); player.MoveSprite(); } // move down if (e.KeyCode == Keys.Down) { player.MoveDown(); player.MoveSprite(); } // select missile if (e.KeyCode == Keys.D1) { player.weaponType = WeaponType.missle; laserImg.BorderStyle = BorderStyle.None; nukeImg.BorderStyle = BorderStyle.None; missleImg.BorderStyle = BorderStyle.Fixed3D; } // select laser if (e.KeyCode == Keys.D2) { player.weaponType = WeaponType.laser; laserImg.BorderStyle = BorderStyle.Fixed3D; nukeImg.BorderStyle = BorderStyle.None; missleImg.BorderStyle = BorderStyle.None; } // select nuke if (e.KeyCode == Keys.D3 && nuke) { player.weaponType = WeaponType.nuke; laserImg.BorderStyle = BorderStyle.None; missleImg.BorderStyle = BorderStyle.None; nukeImg.BorderStyle = BorderStyle.Fixed3D; } // shoot... if (e.KeyCode == Keys.Space) { if (player.weaponType == WeaponType.missle && missleAmmo > 0) // ...with missle { Weapon weapon = player.CreateWeapon(missleSpeed, 1); weapon.InitializeSprite(); weapon.PositionSprite(); ammo.Add(weapon); Controls.Add(weapon.Sprite); missleAmmo--; } else if (player.weaponType == WeaponType.laser && laserAmmo > 0) //...with laser { Weapon weapon = player.CreateWeapon(missleSpeed, 1); weapon.InitializeSprite(); weapon.PositionSprite(); ammo.Add(weapon); Controls.Add(weapon.Sprite); laserAmmo--; } else if (player.weaponType == WeaponType.nuke) // ...with nuke { timer.Enabled = !timer.Enabled; // stop time #region explosion animation BackgroundImage = null; BackColor = Color.White; // simulate explosion // fade out effect after explosion nukeAnimationTime.Tick += new EventHandler(fadeOut); nukeAnimationTime.Start(); #endregion // reset energy player.Energy = 0; // remove all asteroids int total = 0; for (int i = 0; i < asteroids.Count; i++) { total += asteroids[i].astScore; Controls.Remove(asteroids[i].Sprite); } asteroids.Clear(); // update score scoreLabel.Text = String.Format("SCORE : {0:D2}", player.Score += total); // auto deactivate nuke nuke = false; nukeImg.Hide(); // auto set weapon to missle after nuke player.weaponType = WeaponType.missle; laserImg.BorderStyle = BorderStyle.None; nukeImg.BorderStyle = BorderStyle.None; missleImg.BorderStyle = BorderStyle.Fixed3D; } // update ammo count after shoot missleAmmoLabel.Text = Convert.ToString(missleAmmo); laserAmmoLabel.Text = Convert.ToString(laserAmmo); } // exit program if (e.KeyCode == Keys.Escape) { bgm.Stop(); this.Close(); return; } } // pause if (e.KeyCode == Keys.P) { timer.Enabled = !timer.Enabled; } // restart if (!timer.Enabled && e.KeyCode == Keys.Enter) { //Clear screen Controls.Clear(); asteroids.Clear(); ammo.Clear(); //Replay background music winSound.Stop(); bgm.Play(); // reset player lives and position player.Lives = numberOfLives; player.Reposition(this.Width, this.Height, 1); player.InitializeSprite(); player.PositionSprite(); // reset settings numberOfLivesLabel.Text = String.Format("x {0}", player.Lives); scoreLabel.Text = String.Format("SCORE : {0:D2}", player.Score = 0); missleAmmoLabel.Text = Convert.ToString(missleAmmo = 20); laserAmmoLabel.Text = Convert.ToString(laserAmmo = 5); Control[] controls = new Control[] { numberOfLivesLabel, scoreLabel, laserAmmoLabel, missleAmmoLabel, missleAmmoLabel, nukeImg, laserImg, missleImg, liveImg, player.Sprite }; Controls.AddRange(controls); // restart time timer.Start(); } //exit and view scoreboard if (e.KeyCode == Keys.Escape) { bgm.Stop(); winSound.Stop(); Scoreboard scr = new Scoreboard(); scr.updateScoreboard(player); scr.updateFile(); ScoreBoardDisplay scrBoard = new ScoreBoardDisplay(scr); scrBoard.Show(); this.Close(); } }