private void GameForm_Paint(object sender, PaintEventArgs e) { if (gameStart) { text.Draw(e.Graphics, "Press space to start and pause" + "\n" + "Use the arrow keys to move around" + "\n" + "Mines reduce the amount of balls collected" + "\n" + "Collect 20 balls to level up", DisplayRectangle.Width / 3, DisplayRectangle.Height / 3); } text.Draw(e.Graphics, string.Format("Level: {0}", level), DisplayRectangle.Width - 200, 20); UpdateBallsCollected(e.Graphics); hippo.Draw(e.Graphics); foreach (Ball ball in balls) { ball.Draw(e.Graphics); } foreach (Mine mine in mines) { mine.Draw(e.Graphics); } if (paused) { text.Draw(e.Graphics, "PAUSED", DisplayRectangle.Width / 2, DisplayRectangle.Height / 2); } if (ballsCollected >= 20)//balls needed to level up { level++; ballsCollected = 0; maxMines += 2; AnimationTimer.Stop(); BallTimer.Stop(); text.Draw(e.Graphics, "Level Up. Press Space For The Next level", DisplayRectangle.Width / 3, DisplayRectangle.Height / 2); } }
private void BallTimer_Tick(object sender, EventArgs e) { if (top == false) { Ball.Top += speed; if (Ball.Top > 800) { top = true; } } if (top == true) { Ball.Top -= speed; if (Ball.Top < 5) { top = false; } } if (left == false) { Ball.Left += speed; if (Ball.Left == (Player2Picture.Left - Player2Picture.Width) & (Ball.Top >= Player2Picture.Top & Ball.Top <= Player2Picture.Top + Player2Picture.Height)) { left = true; } } if (left == true) { Ball.Left -= speed; if (Ball.Left == (Player1Picture.Left + Player1Picture.Width) + Player1Picture.Width & (Ball.Top >= Player1Picture.Top & Ball.Top <= Player1Picture.Top + Player1Picture.Height)) { left = false; } } if (Ball.Left > Player2Picture.Left) //om spelare två förlorar { BallTimer.Stop(); label1.ForeColor = Color.Red; label1.Text = "Player 2, you suck..."; Ball.Left = 750; playerOneScore++; PlayerOneScoreLabel.Text = "Player 1: " + playerOneScore.ToString(); } if (Ball.Left < Player1Picture.Left) //om spelare ett förlorar { BallTimer.Stop(); label1.ForeColor = Color.Red; label1.Text = "Player 1, try to hit the ball..."; Ball.Left = 750; playerTwoScore++; PlayerTwoScoreLabel.Text = "Player 2: " + playerTwoScore.ToString(); } }
/// <summary> /// Listens for keys pressed /// </summary> /// <param name="sender">key pressed object</param> /// <param name="e">Key event arguments</param> private void GameForm_KeyDown(object sender, KeyEventArgs e) { if (AnimationTimer.Enabled) { switch (e.KeyData) { case Keys.Left: { if (!KeyList.Contains(Keys.Left)) { KeyList.Add(Keys.Left); } break; } case Keys.Right: { if (!KeyList.Contains(Keys.Right)) { KeyList.Add(Keys.Right); } break; } case Keys.Up: { if (!KeyList.Contains(Keys.Up)) { KeyList.Add(Keys.Up); } break; } case Keys.Down: { if (!KeyList.Contains(Keys.Down)) { KeyList.Add(Keys.Down); } break; } } } if (e.KeyData == Keys.Space) { if (gameStart) { gameStart = false; } if (AnimationTimer.Enabled) { paused = true; AnimationTimer.Stop(); BallTimer.Stop(); Invalidate(); } else { paused = false; AnimationTimer.Start(); BallTimer.Start(); } } }