// ticks every 10ms private void timer1_Tick(object sender, EventArgs e) { // erase display g.Clear(Control.DefaultBackColor); // if all bricks are destroyed if (GameOver()) { timer1.Enabled = false; MessageBox.Show("Game Over"); Application.Exit(); } // handle collisions with border BallBounces(); // ball collison with bricks BrickCollisions(); // update position ball.Move(); // draw shapes DrawShapes(); pictureBox1.Image = canvas; }
private void gameTimer_Tick(object sender, EventArgs e) { // Move the paddle if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } // Moves ball ball.Move(); // Check for collision with top and side walls ball.WallCollision(this); // Check for collision of ball with paddle, (incl. paddle movement) ball.PaddleCollision(paddle, leftArrowDown, rightArrowDown); // Check if ball has collided with any blocks foreach (Block b in blocks) { if (ball.BlockCollision(b)) { blocks.Remove(b); if (blocks.Count == 0) { gameTimer.Enabled = false; OnEnd(); } break; } } // Check for ball hitting bottom of screen if (ball.BottomCollision(this)) { lives--; // Moves the ball back to origin ball.x = ((paddle.x - (ball.size / 2)) + (paddle.width / 2)); ball.y = (this.Height - paddle.height) - 85; if (lives == 0) { gameTimer.Enabled = false; OnEnd(); } } //redraw the screen Refresh(); }
public void CalemMethod() { // Move ball ball.Move(); #region collisions // Check for collision with top and side walls ball.WallCollision(this); // Check for ball hitting bottom of screen if (ball.BottomCollision(this)) { playerLives--; lifeLabel.Text = playerLives + ""; // display updated life count //Move paddle to middle paddle.x = (this.Width / 2 - paddle.width); // Moves the ball back to origin ball.x = ((paddle.x - (ball.size / 2)) + (paddle.width / 2)); ball.y = (this.Height - paddle.height) - 85; if (playerLives == 0) { gameTimer.Enabled = false; OnEnd(); } } // Check for collision of ball with paddle, (incl. paddle movement) ball.PaddleCollision(paddle, leftArrowDown, rightArrowDown); // Check if ball has collided with any blocks foreach (Block b in blocks) { if (ball.BlockCollision(b)) { blocks.Remove(b); powerDec = randJord.Next(1, 100); if (powerDec > 1 && powerDec < 100) { JordanMethod(); } if (blocks.Count == 0) { gameTimer.Enabled = false; OnEnd(); } break; } } #endregion }
private void gameTimer_Tick(object sender, EventArgs e) { //swaps controls when shrooms is active for p1 if (isShroomsControls) { if (leftArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } if (rightArrowDown && paddle.x > 0) { paddle.Move("left"); } } if (isShroomsControls)//for p2 { if (aKeyDown && paddle2.x < (this.Width - paddle2.width)) { paddle2.Move("right"); } if (dKeyDown && paddle.x > 0) { paddle2.Move("left"); } } // Move the paddle for p1 if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } //move the paddle for p2 if (aKeyDown && paddle2.x > 0) { paddle2.Move("left"); } if (dKeyDown && paddle2.x < (this.Width - paddle2.width)) { paddle2.Move("right"); } #region Stefan and Jacks PowerUps if (MagnetTimer > 0 && isMagnet == true) { MagnetTimer--; } else if (MagnetTimer <= 0 && isMagnet == true) { isMagnet = false; } if (longPaddle == true) { longPaddleTimer++; if (longPaddleTimer >= 14 && paddle.width > 80) { longPaddleTimer = 0; paddle.x++; paddle.width -= 2; } else if (paddle.width <= 80 && longPaddleTimer >= 14) { longPaddleTimer = 0; longPaddle = false; } } else if (longPaddle == true && paddle.width != 80) { paddle.width = 80; } if (isFloor == true && floorTimer <= 0) { isFloor = false; } else if (isFloor == true && floorTimer > 0) { floorTimer--; foreach (Ball ba in balls) { if (ba.PaddleCollision(floorPaddle, false, false, 100) == 0) { floorTimer = 0; } } CollidePowerUps(floorPaddle); } if (strongBallTimer > 0 && isStrongball == true) { strongBallTimer--; } else if (strongBallTimer <= 0 && isStrongball == true) { isStrongball = false; ballBrush.Color = Color.White; } if (shroomsTimer > 0 && isShrooms == true) { ballBrush.Color = Color.FromArgb(randomNum.Next(0, 255), randomNum.Next(0, 255), randomNum.Next(0, 255)); paddleBrush.Color = Color.FromArgb(randomNum.Next(0, 255), randomNum.Next(0, 255), randomNum.Next(0, 255)); if (shroomsControlTimer >= 80 && isShroomsControls == false) { isShroomsControls = true; } shroomsControlTimer++; shroomsTimer--; } else if (shroomsTimer <= 0 && isShrooms == true) { isShrooms = isShroomsControls = false; paddleBrush.Color = ballBrush.Color = Color.White; } if (blindfoldTimer > 0 && isBlindfold == true) { blindfoldTimer--; } else if (blindfoldTimer <= 0 && isBlindfold == true) { isBlindfold = false; } // Moves powerups MovePowerups(powerUps); // Check for collision with powerups and paddle CollidePowerUps(paddle); CollidePowerUps(paddle2); #endregion // Moves ball ball.Move(); // Check for collision with top and side walls ball.WallCollision(this); // Check for collision of ball with paddle, (incl. paddle movement) ticksSinceHit = ball.PaddleCollision(paddle, leftArrowDown, rightArrowDown, ticksSinceHit); ticksSinceHit = ball.PaddleCollision(paddle2, aKeyDown, dKeyDown, ticksSinceHit); // Check if ball has collided with any of player 1 blocks foreach (Block b in blocks1p) { if (ball.BlockCollision(b)) { b.hp--; if (b.hp == 0) { blocks1p.Remove(b); } //reduce life of player p1HP--; Random n = new Random(); if (n.Next(0, 1) == 0) { PowerUp p = new PowerUp(b.x, b.y, 20, -3, n.Next(0, 7)); powerUps.Add(p); } hpLabelp1.Text = "Player 1 HP: " + p1HP.ToString(); if (blocks1p.Count == 0 && p1HP <= 0)//if player 1 runs out of blocks { gameTimer.Enabled = false; player1Won = false; player2Won = true; OnEnd(); } break; } } // Check if ball has collided with any of player 2 blocks foreach (Block b in blocks2p) { if (ball.BlockCollision(b)) { b.hp--; if (b.hp == 0) { blocks2p.Remove(b); } p2HP--; Random n = new Random(); if (n.Next(0, 1) == 0) { PowerUp p = new PowerUp(b.x, b.y, 20, 3, n.Next(0, 7)); powerUps.Add(p); } hpLabelp2.Text = "Player 2 HP: " + p2HP.ToString(); if (blocks2p.Count == 0 && p2HP <= 0) { gameTimer.Enabled = false; player2Won = false; player1Won = true; OnEnd(); } break; } } // Check for ball hitting bottom of screen if (ball.BottomCollision(this)) { // Moves the ball back to origin ball.ySpeed *= -1; } //redraw the screen Refresh(); }
private void gameTimer_Tick(object sender, EventArgs e) { BreannaPowerUp(); // Move the paddle if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); if (ballStart == false) { ball.MoveWithPaddle("left", paddle); } } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); if (ballStart == false) { ball.MoveWithPaddle("right", paddle); } } // Move ball if (ballStart) { ball.Move(); } // Check for collision with top and side walls ball.WallCollision(this); // Check for ball hitting bottom of screen if (ball.BottomCollision(this)) { lives--; if (paddle.width != 80) { paddle.width = 80; } if (ball.xSpeed != 6 && ball.ySpeed != 6) { ball.xSpeed = 6; ball.ySpeed = 6; } // Moves the ball back to origin paddle.x = (this.Width / 2); ball.x = ((paddle.x - (ball.size / 2)) + (paddle.width / 2)); ball.y = (this.Height - paddle.height) - 85; Refresh(); Thread.Sleep(1500); if (lives == 0) { gameTimer.Enabled = false; OnEnd(); } } // Check for collision of ball with paddle, (incl. paddle movement) ball.PaddleCollision(paddle); // Check if ball has collided with any blocks foreach (Block b in blocks) { if (ball.BlockCollision(b)) { b.hp--; score++; if (b.hp == 0) { blocks.Remove(b); } if (blocks.Count == 0) { gameTimer.Enabled = false; Win(); } break; } } //redraw the screen Refresh(); if (gameStart) { Thread.Sleep(1500); gameStart = false; } }
private void gameTimer_Tick(object sender, EventArgs e) { livesLabel.Text = "Lives: " + lives; scoreLabel.Text = "Score: " + score; IanMethod(); #region Move the paddle if (paddle.width > 80 && counter % 5 == 0) { paddle.width--; } // Move the paddle if (leftArrowDown && paddle.x > 0 && paddleMove == true) { paddle.Move("left"); if (ball.xSpeed == 0 && ball.ySpeed == 0) { paddleMove = false; //ball.x = paddle.x + paddle.width / 2 - 10; } } if (rightArrowDown && paddle.x < (this.Width - paddle.width) && paddleMove == true) { paddle.Move("right"); if (ball.xSpeed == 0 && ball.ySpeed == 0) { paddleMove = false; // ball.x = paddle.x + paddle.width / 2 - 10; } } if (ball.xSpeed == 0 && ball.ySpeed == 00 && spaceDown == true && leftArrowDown == true) { ball.xSpeed = -6; ball.ySpeed = 6; } else if (ball.xSpeed == 0 && ball.ySpeed == 00 && spaceDown == true) { ball.xSpeed = 6; ball.ySpeed = 6; } #endregion #region Move ball ball.Move(); if (powerupBall.Count() >= 1) { foreach (Ball p in powerupBall) { p.Move(); } } #endregion #region Check for collision with top and side walls IanMethod(); // Check for collision with top and side walls ball.WallCollision(this); if (powerupBall.Count() >= 1) { powerupBall[0].WallCollision(this); } #endregion #region Check for ball hitting bottom of screen if (ball.BottomCollision(this) && powerupBall.Count() < 1) { lives--; livesLabel.Text = ""; livesLabel.Text = "Lives: " + lives; ball.xSpeed = 0; ball.ySpeed = 0; paddle.x = this.Width / 2 - paddle.width / 2; // Moves the ball back to origin ball.x = ((paddle.x - (ball.size / 2)) + (paddle.width / 2)); ball.y = (this.Height - paddle.height) - 85; paddle.width = 80; if (powerupBall.Count >= 1) { powerupBall.RemoveAt(0); } paddle.x = this.Width / 2 - paddle.width / 2; #endregion #region check if game over if (lives == 0) { scores.Add(score + ""); scores.Sort(); gameTimer.Enabled = false; OnEnd(); } #endregion } else if (powerupBall.Count() >= 1 && powerupBall[0].BottomCollision(this) && ball.y > this.Height) { lives--; livesLabel.Text = ""; livesLabel.Text = "Lives: " + lives; ball.xSpeed = 0; ball.ySpeed = 0; paddle.x = this.Width / 2 - paddle.width / 2; // Moves the ball back to origin ball.x = ((paddle.x - (ball.size / 2)) + (paddle.width / 2)); ball.y = (this.Height - paddle.height) - 85; paddle.width = 80; if (powerupBall.Count >= 1) { powerupBall.RemoveAt(0); } paddle.x = this.Width / 2 - paddle.width / 2; if (lives == 0) { gameTimer.Enabled = false; OnEnd(); } } int index = Powerup.FindIndex(b => b.y > this.Height); if (powerupBall.Count() >= 1 && index >= 0) { powerupBall.RemoveAt(index); } #region Check for collision of ball with paddle, (incl. paddle movement) ball.PaddleCollision(paddle, leftArrowDown, rightArrowDown); if (powerupBall.Count() >= 1) { powerupBall[0].PaddleCollision(paddle, leftArrowDown, rightArrowDown); } #endregion #region Check if ball has collided with any blocks foreach (Block b in blocks) { if (ball.BlockCollision(b)) { Rectangle blockRec = new Rectangle(b.x, b.y, 50, 25); Rectangle ballRec = new Rectangle(ball.x, ball.y, ball.size, ball.size); if (ballRec.IntersectsWith(blockRec)) { if (ball.x <= b.x + 50 && ball.x >= b.x) { ball.ySpeed = ball.ySpeed * -1; ball.y = prevY; ball.x = prevX; } else if (ball.y >= b.y - ball.size && ball.y <= b.y + 25) { ball.xSpeed = ball.xSpeed * -1; ball.y = prevY; ball.x = prevX; } SoundPlayer brickPlayer = new SoundPlayer(Properties.Resources.breakSound); brickPlayer.Play(); b.hp--; score += 100; if (b.hp == 0) { blocks.Remove(b); } if (blocks.Count == 0 && level == 5) { gameTimer.Enabled = false; OnWin(); } else if (blocks.Count == 0) { level++; OnStart(); } break; } } if (powerupBall.Count() >= 1 && powerupBall[0].BlockCollision(b)) { Rectangle blockRec = new Rectangle(b.x, b.y, 50, 25); Rectangle powerballRec = new Rectangle(powerupBall[0].x, powerupBall[0].y, powerupBall[0].size, powerupBall[0].size); if (powerballRec.IntersectsWith(blockRec)) { if (powerupBall[0].x <= b.x + 50 && powerupBall[0].x >= b.x) { powerupBall[0].ySpeed = powerupBall[0].ySpeed * -1; powerupBall[0].y = powerprevY; powerupBall[0].x = powerprevX; } else if (powerupBall[0].y >= b.y - powerupBall[0].size && powerupBall[0].y <= b.y + 25) { powerupBall[0].xSpeed = powerupBall[0].xSpeed * -1; powerupBall[0].y = powerprevY; powerupBall[0].x = powerprevX; } SoundPlayer brickPlayer = new SoundPlayer(Properties.Resources.breakSound); brickPlayer.Play(); b.hp--; score += 100; if (b.hp == 0) { blocks.Remove(b); } if (blocks.Count == 0 && level == 5) { gameTimer.Enabled = false; OnWin(); } else if (blocks.Count == 0) { level++; OnStart(); } break; } } } #endregion pauseScreenEnabled(); counter++; prevX = ball.x; prevY = ball.y; if (powerupBall.Count() >= 1) { powerprevX = powerupBall[0].x; powerprevY = powerupBall[0].y; } //redraw the screen Refresh(); }
private void gameTimer_Tick(object sender, EventArgs e) { // Move the paddle for p1 if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } //move the paddle for p2 if (aKeyDown && paddle2.x > 0) { paddle2.Move("left"); } if (dKeyDown && paddle2.x < (this.Width - paddle2.width)) { paddle2.Move("right"); } // Moves ball ball.Move(); // Check for collision with top and side walls ball.WallCollision(this); // Check for collision of ball with paddle, (incl. paddle movement) ticksSinceHit = ball.PaddleCollision(paddle, leftArrowDown, rightArrowDown, ticksSinceHit); ticksSinceHit = ball.PaddleCollision(paddle2, aKeyDown, dKeyDown, ticksSinceHit); // Check if ball has collided with any of player 1 blocks foreach (Block b in blocks1p) { if (ball.BlockCollision(b)) { b.hp--; if (b.hp == 0) { blocks1p.Remove(b); } //reduce life of player p1HP--; hpLabelp1.Text = "HP: " + p1HP.ToString(); if (blocks1p.Count == 0 && p1HP >= 0) { gameTimer.Enabled = false; //TODO say who won OnEnd(); } break; } } // Check if ball has collided with any of player 2 blocks foreach (Block b in blocks2p) { if (ball.BlockCollision(b)) { b.hp--; if (b.hp == 0) { blocks2p.Remove(b); } p2HP--; hpLabelp2.Text = "HP: " + p2HP.ToString(); if (blocks2p.Count == 0 && p2HP >= 0) { gameTimer.Enabled = false; OnEnd(); } break; } } // Check for ball hitting bottom of screen if (ball.BottomCollision(this)) { // Moves the ball back to origin ball.ySpeed *= -1; } //redraw the screen Refresh(); }
private void gameTimer_Tick(object sender, EventArgs e) { //pause the game if (gamePaused == true) { gameTimer.Enabled = false; } // Move the paddle if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } if (escDown == true) { gamePaused = !gamePaused; } // Move ball ball.Move(); //Move powerups foreach (PowerUp p in powers) { p.Move(); } // Check for collision with top and side walls ball.WallCollision(this); // Check for ball hitting bottom of screen foreach (Ball b in ballList) { if (ballList.Count() < 1) { if (b.BottomCollision(this)) { ballList.Remove(b); } } if (ballList.Count() == 1) { if (b.BottomCollision(this)) { lives--; // Moves the ball back to origin b.x = ((paddle.x - (ball.size / 2)) + (paddle.width / 2)); b.y = (this.Height - paddle.height) - 85; b.xSpeed = 6; b.ySpeed = 6; b.size = 20; Refresh(); if (lives == 0) { gameTimer.Enabled = false; OnEnd(); } Thread.Sleep(2000); } } } if (ballList.Count() == 0) { lives--; // Moves the ball back to origin int ballX = ((paddle.x - (ball.size / 2)) + (paddle.width / 2)); int ballY = (this.Height - paddle.height) - 85; int xSpeed = 6; int ySpeed = 6; int ballSize = 20; ball = new Ball(ballX, ballY, xSpeed, ySpeed, ballSize); ballList.Add(ball); if (lives == 0) { gameTimer.Enabled = false; OnEnd(); } } // Check for collision of ball with paddle, (incl. paddle movement) ball.PaddleCollision(paddle, leftArrowDown, rightArrowDown); // Check if ball has collided with any blocks foreach (Block b in blocks) { if (ball.BlockCollision(b)) { --b.hp; //blocks.Remove(b); int blockX = b.x; int blockY = b.y + b.height; int blockSize = b.width; if (b.hp == 0) { blocks.Remove(b); score = score + 100 * scoreMult; double d = score / 500; double scoreint = Math.Round(d); if (scoreint > lastPower) { lastPower = scoreint; NumberGen(); int powertype = powerValue; PowerUp power = new PowerUp(blockSize / 2 + blockX, blockY, powerupSpeed, 15, powertype); powers.Add(power); } } if (blocks.Count == 0) { currentLevel++; NewLevel(); } break; } } //redraw the screen Refresh(); }