/// <summary> /// Function for having certain keys down for movement and shooting.From my citing above I found the /// 'async' and 'await' combination.I use it pretty frequently throughout my code to have pauses between /// different parts of the game.Found it incredibly useful. /// /// THis function sets moveLeft and moveRight to true and also accounts for GodMode to change how the /// spacebar works in that mode. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void MainGame_KeyDown(object sender, KeyEventArgs e) { stopWatch.Start(); long duration = stopWatch.ElapsedMilliseconds; if (e.KeyCode == Keys.Left) { moveLeft = true; } if (e.KeyCode == Keys.Right) { moveRight = true; } //For player shooting in default mode of the game. //If duration is >= the timestamp calculated below, it will allow. This prevents player being able to spam spacebar to shoot fast if (GodMode == false && duration >= timestamp && e.KeyCode == Keys.Space) { stud.bulletCreation(); //Creates Bullet timestamp = duration + timeBetweenShots; //Adds the duration of how long the stopwatch has been runnig to variable set above stopWatch.Stop(); } else if (GodMode == true && duration >= timestamp && e.KeyCode == Keys.Space) //Changes spacebar instead of shooting, to kill all enemies on screen { timestamp = duration + timeBetweenGod; //functions the same as above but is a slightly larger gap to prevent spams that will break the game stopWatch.Stop(); //following will be condenesd to one line after flags are made into arrays. Just doing for testing purposes... //Sets all the flags of each enemy death to true, as they will all be killed by this spacebar press. These values are normally //set in the Enemy class when they die from regular enemy bullets . flags[0] = true; flags[1] = true; flags[2] = true; flags[3] = true; flags[4] = true; flags[5] = true; flags[6] = true; flags[7] = true; flags[8] = true; flags[9] = true; flags[10] = true; flags[11] = true; score += 110; //Scoreboard adds +10 under normal circumstances so 110 is to account for 11 enemies, and the other 10 in the function mechanisms.ScoreBoard(); //Calls Scoreboard method to update scoreboard for (int i = 0; i < enemies.Length; i++) { this.Controls.Remove(enemies[i]); //Removes each of the enemies on the screen and produces an explosion foe.Explosion(); } await Task.Delay(1000); //1 second pause before starting the newRound function mechanisms.newRound(); } }
/// <summary> /// Controls collision between enemy and the player bullet using foreach loops and tags. Also plays sound effects and calls /// explosion function from this class /// </summary> public override void Collision() { foreach (Control i in maingame.Controls) { foreach (Control j in maingame.Controls) { if (i is PictureBox && i.Tag == "invader") { if (j is PictureBox && j.Tag == "bullet") { if (i.Bounds.IntersectsWith(j.Bounds)) //if the pictureboxes with those tags intersect, removes them and plays the sound file { // mechanisms = new GameFunctions(_tank, maingame, newTimer, scoreboard, newScore); enemysound.URL = "invaderkilled.wav"; enemysound.controls.play(); maingame.Controls.Remove(i); maingame.Controls.Remove(j); /*The following if's check which Picturebox was intersected. SEts its respective flag to true, increment enemydeath count, and calls * explosion function to show an explosion for their death. Also calls scoreboard to update it. * * */ if (i is PictureBox && i == MainGame.enemies[0]) { MainGame.flags[0] = true; MainGame.enemydeaths++; Explosion(); mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[1]) { MainGame.flags[1] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[2]) { MainGame.flags[2] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[3]) { MainGame.flags[3] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[4]) { MainGame.flags[4] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[5]) { MainGame.flags[5] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[6]) { MainGame.flags[6] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[7]) { MainGame.flags[7] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[8]) { MainGame.flags[8] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[9]) { MainGame.flags[9] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[10]) { MainGame.flags[10] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } if (i is PictureBox && i == MainGame.enemies[11]) { MainGame.flags[11] = true; Explosion(); MainGame.enemydeaths++; mechanisms.ScoreBoard(); } } } } } } }