//Setting Gmae Values private void SetParameters() { backgroundPlayer.Play(); winnerLabel.Visible = false; switch (SelectionScreen.player1PowerUp) { case 1: reset2 = true; resetPlayer2 = 8; player1Speed = 4; break; case 2: player1Speed = 8; reset2 = false; break; } switch (SelectionScreen.player2PowerUp) { case 1: reset1 = true; resetPlayer1 = 8; player2Speed = 4; break; case 2: player2Speed = 8; reset1 = false; break; } timerX = this.Width / 2 - 5; timerY = this.Height; //add box for (int i = 0; i <= 10; i++) { Bullets newBulletLeft = new Bullets(randGen.Next(0, this.Width), randGen.Next(0, this.Height - 100), "Left"); bulletList.Add(newBulletLeft); Bullets newBulletRight = new Bullets(randGen.Next(0, this.Width), randGen.Next(0, this.Height - 100), "Right"); bulletList.Add(newBulletRight); } }
//GameTimer Tick Method private void gameTimer_Tick(object sender, EventArgs e) { //Collision Code #region Collision foreach (Bullets x in bulletList) { if (Player1.Collision(x.bulletX, x.bulletY)) { gameTimer.Enabled = false; winnerLabel.Text = "You scored " + player1Points + " points"; Refresh(); Thread.Sleep(4000); Form f = this.FindForm(); f.Controls.Remove(this); // Create an instance of the SecondScreen MainScreen ms = new MainScreen(); // Add the User Control to the Form f.Controls.Add(ms); } } #endregion //Adding Bullets Code #region Adding Bullets if (bulletCounter == 8) { Bullets newBulletLeft = new Bullets(0 - 10, randGen.Next(0, this.Height - 100), "Left"); bulletList.Add(newBulletLeft); Bullets newBulletRight = new Bullets(this.Width, randGen.Next(0, this.Height - 100), "Right"); bulletList.Add(newBulletRight); bulletCounter = 0; } bulletCounter++; #endregion //Updating Player position #region Moving Players if (wDown == true) { Player1.PlayerMoveUpDown(player1Speed, "Up"); } if (sDown == true && Player1.Y + shipHeight <= this.Height) { Player1.PlayerMoveUpDown(player1Speed, "Down"); } if (aDown == true && Player1.X >= 0) { Player1.PlayerMoveLeftRight(player1Speed, "Left"); } if (dDown == true && Player1.X + shipWidth <= this.Width) { Player1.PlayerMoveLeftRight(player1Speed, "Right"); } #endregion //Updating Bullet Position #region Moving Bullets foreach (Bullets x in bulletList) { x.BulletsMove(); } #endregion //Removing Bullets #region Removing Bullets if (bulletList[0].bulletX >= this.Width || bulletList[0].bulletX <= 0) { bulletList.RemoveAt(0); } #endregion //Score Code #region Score if (Player1.Y + shipHeight <= 0) { Player1.PlayerScore(); player1Points++; playerScore.Text = "" + player1Points; } #endregion //Calling the Paint Method Refresh(); }
//GameTimer Tick Method private void gameTimer_Tick(object sender, EventArgs e) { //Collision Code #region Collisions foreach (Bullets x in bulletList) { Player1.Collision(x.bulletX, x.bulletY); Player2.Collision(x.bulletX, x.bulletY); } #endregion //Adding Bullets Code #region Adding Bullets if (bulletCounter == 8) { Bullets newBulletLeft = new Bullets(0 - 10, randGen.Next(0, this.Height - 100), "Left"); bulletList.Add(newBulletLeft); Bullets newBulletRight = new Bullets(this.Width, randGen.Next(0, this.Height - 100), "Right"); bulletList.Add(newBulletRight); bulletCounter = 0; } bulletCounter++; #endregion //Powerups Code #region PowerUp if (resetPlayer1 >= 0 && nDown == true) { Player1.Reset(); resetPlayer1--; } if (resetPlayer2 >= 0 && vDown == true) { Player2.Reset(); resetPlayer2--; } #endregion //Updating Player position #region Moving Players if (wDown == true) { Player2.PlayerMoveUpDown(player2Speed, "Up"); } if (sDown == true && Player2.Y + shipHeight <= this.Height) { Player2.PlayerMoveUpDown(player2Speed, "Down"); } if (aDown == true && Player2.X >= 0) { Player2.PlayerMoveLeftRight(player2Speed, "Left"); } if (dDown == true && Player2.X + shipWidth <= this.Width) { Player2.PlayerMoveLeftRight(player2Speed, "Right"); } if (upArrowDown == true) { Player1.PlayerMoveUpDown(player1Speed, "Up"); } if (downArrowDown == true && Player1.Y + shipHeight <= this.Height) { Player1.PlayerMoveUpDown(player1Speed, "Down"); } if (leftArrowDown == true && Player1.X >= 0) { Player1.PlayerMoveLeftRight(player1Speed, "Left"); } if (rightArrowDown == true && Player1.X + shipWidth <= this.Width) { Player1.PlayerMoveLeftRight(player1Speed, "Right"); } #endregion //Updating Bullet Position #region Moving Bullets foreach (Bullets x in bulletList) { x.BulletsMove(); } #endregion //Updating Timer #region Timer Update if (timerCounter == 50) { timerY -= 10; timerCounter = 0; } timerCounter++; #endregion //Game Over Code #region Timer Finish if (timerY == -5) { gameTimer.Stop(); //if (player1Points >= player2Points) //{ // winnerHighScore = player1Points; //} //else //{ // winnerHighScore = player2Points; //} //if (MainScreen.highScoreList2Player.Contains(winnerHighScore)) //{ } //else //{ // MainScreen.highScoreList2Player.Add(winnerHighScore); //} //MainScreen.highScoreList2Player.Add(); winnerLabel.Visible = true; if (player1Points > player2Points) { winner = "Player 1 is the Winner!"; } else if (player2Points > player1Points) { winner = "Player 2 is the Winner!"; } else { winner = "It's a Tie!"; } winnerLabel.Text = winner; Refresh(); Thread.Sleep(4000); Form f = this.FindForm(); f.Controls.Remove(this); // Create an instance of the SecondScreen MainScreen ms = new MainScreen(); // Add the User Control to the Form f.Controls.Add(ms); } #endregion //Removing Bullets #region Removing Bullets if (bulletList[0].bulletX >= this.Width || bulletList[0].bulletX <= 0) { bulletList.RemoveAt(0); } #endregion //Score Code #region Score if (Player1.Y + shipHeight <= 0) { Player1.PlayerScore(); player1Points++; player1Score.Text = "" + player1Points; } if (Player2.Y + shipHeight <= 0) { Player2.PlayerScore(); player2Points++; player2Score.Text = "" + player2Points; } #endregion //Background sound #region Background Sound soundCounter++; if (soundCounter >= 382) { backgroundPlayer.Play(); } if (afterCollision == true) { afterCollisionCounter++; if (afterCollisionCounter == 50) { backgroundPlayer.Play(); afterCollisionCounter = 0; afterCollision = false; } } #endregion //Calling the Paint Method Refresh(); }