private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { this.Close(); } //Press escape to quit if (e.KeyCode == Keys.F1) //Reload the game { ball.Top = 50; ball.Left = 50; speed_left = 4; speed_top = 4; point = 0; points.Text = "0"; timer1.Enabled = true; gameOver_label.Visible = false; //Hides the game over text label playground.BackColor = Color.White; //reassigns the background color plays++; //increments the number of games played Player turn = new Player(); //instantiate a new Player class turnCount.Text = turn.ComputeTurns(plays); //send the updated turns to ComputeTurns method turnCount.Visible = false; //Hides the turn count highestScore_label.Visible = false; //Hides the high score user_label.Visible = false; //Hides the player name user_label this.racket.Size = new System.Drawing.Size(200, 20); //reset racket size this.ball.Image = global::PongGame.Properties.Resources.ball1; //reset ball image this.pictureBox1.Visible = false; //Hides picturebox fakeButton.Visible = false; //HidesFake botton } }
private int plays; //Number of turns #endregion Fields #region Constructors public Form1() { InitializeComponent(); timer1.Enabled = true; Cursor.Hide(); //Hides the cursor plays = 1; Player turn = new Player(); turnCount.Text = turn.ComputeTurns(plays); this.FormBorderStyle = FormBorderStyle.None; //removes any border this.TopMost = true; //Brings the form to the front this.Bounds = Screen.PrimaryScreen.Bounds; //Makes it fullscreen racket.Top = playground.Bottom - (playground.Bottom / 10); //set the position of the racket gameOver_label.Left = (playground.Width / 2) - (gameOver_label.Width / 2); //Centers the label gameOver_label.Top = (playground.Height / 2) - (gameOver_label.Height / 2); gameOver_label.Visible = false; //Hides the game over highestScore_label.Visible = false; turnCount.Visible = false; //Hides the turn count }
private void timer1_Tick(object sender, EventArgs e) { racket.Left = Cursor.Position.X - (racket.Width / 2); //Set the center of the racket to the position of the cursor ball.Left += speed_left; //Move the ball ball.Top += speed_top; if (ball.Bottom >= racket.Top && ball.Bottom <= racket.Bottom && ball.Left >= racket.Left && ball.Right <= racket.Right) //racket collision { speed_top += 2; speed_left += 2; speed_top = -speed_top; //change direction point += 1; points.Text = point.ToString(); Random r = new Random(); playground.BackColor = Color.FromArgb(r.Next(155, 255), r.Next(155, 255), r.Next(155, 255)); //Get a random RGB color and set it to playground background color } if (ball.Left <= playground.Left) { speed_left = -speed_left; } if (ball.Right >= playground.Right) { speed_left = -speed_left; } if (ball.Top <= playground.Top) { speed_top = -speed_top; } if (point > 5) //Level two modifications making game more difficult { this.racket.Size = new System.Drawing.Size(150, 20); //Reduces the size of the racket } if (point > 10) //Level three modifications { this.ball.Image = global::PongGame.Properties.Resources.face1; //change the image of ball to face1 } if (point > 15) //Level 4 modifications { this.pictureBox1.Visible = true; //Display distracting gif } if (point > 20) //Level 5 modifications { this.pictureBox1.Visible = false; fakeButton.Visible = true; //Distract with fake button } if (ball.Bottom >= playground.Bottom) { timer1.Enabled = false; //ball is out -> stop the game gameOver_label.Visible = true; //Diplays game over if ball hits bottom if (point > highScore) //Compares the current point total to the current highest score { highScore = point; //Assigns the high score to the new point table if true } Player hScore = new Player(); //instantiate new Player instance hScore.PlayerHighScore = highScore; highestScore_label.Text = hScore.DisplayHighestScore(highScore); turnCount.Visible = true; //Displays the turn count on highestScore_label.Visible = true; Player user = new Player(""); user_label.Text = user.Name; user_label.Visible = true; } }
public override void OnCollision(GameObject other) { if (other is Obstacles) { Obstacles tempObstacle = other as Obstacles; if (!tempObstacle.IsMiddleLine) this.velocity.Y *= -1; } if (other is Player && !collidedWithPlayer1 && !collidedWithPlayer2) { lastHitPlayer = other as Player; lastHitPlayer.Velocity = new Vector2(0, 0); if (lastHitPlayer.IsFirstPlayer) collidedWithPlayer1 = true; else if (!lastHitPlayer.IsFirstPlayer) collidedWithPlayer2 = true; float midBall = (this.CollisionRect.Y + this.CollisionRect.Height) - (this.CollisionRect.Height / 2); float midPlayer = (lastHitPlayer.CollisionRect.Y + lastHitPlayer.CollisionRect.Height) - (lastHitPlayer.CollisionRect.Height / 2); float deltaY = midBall - midPlayer; float lastDir = this.velocity.X; this.velocity = new Vector2(lastDir * -1, (deltaY) * 10); } if (other is PickUp) { HandlePickUp(other as PickUp); } }