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(); }
private void gameTimer_Tick(object sender, EventArgs e) { #region PowerUp // power ups fall for (int i = 0; i < powers.Count(); i++) { powers[i].y += 5; if (powers[i].y > this.Height - 30) { powers.RemoveAt(i); } } foreach (PowerUps p in powers) { paddle.PowerUpCollision(p); } #endregion DeclanMethod(); // Move the paddle if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } CalemMethod(); //redraw the screen Refresh(); }
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(); }
//Nit: Can you make the ball fall a little farther before resetting the ball, something doesn't feel right when it falls //Nit: Can you make the ball go in whatever the player last moved //Note Form1 has a soundplayer, you can access it with Form1.SoundPlayer private void gameTimer_Tick(object sender, EventArgs e) { angleLable.Text = angleposition.ToString(); // Move the paddle if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } if (start) { // Move ball foreach (Ball b in ballList) { anglechange(); // Move ball b.Move(); // Check for collision with top and side walls b.WallCollision(this); // Check for ball hitting bottom of screen and if there is only one ball if (b.BottomCollision(this, paddle) && ballList.Count == 1) { // decrease player 1 lives player1Lives--; // move the ball and paddle back start = false; // reset ball angle angleposition = 3; // reset paddle x and y paddle.x = paddleX; paddle.y = paddleY; ballList[0].x = ((paddle.x - ballSize) + (paddle.width / 2)); ballList[0].y = paddle.y - 40; ballList[0].Yangle *= -1; // reset x and y speeds ballList[0].xSpeed = xSpeed; ballList[0].ySpeed = ySpeed; if (player1Lives <= 0) { start = false; if (player2Lives == 0) { gameTimer.Enabled = false; OnEnd(); } // TODO: Why is this here? gameTimer.Enabled = false; OnEnd(); } } else if (b.BottomCollision(this, paddle)) { // add the ball to the remove list removeBalls.Add(b); } // Check for collision of ball with paddle, (incl. paddle movement) b.PaddleCollision(paddle, leftArrowDown, rightArrowDown); } // remove any balls that need to be removed foreach (Ball b in removeBalls) { ballList.Remove(b); } // Check if ball has collided with any blocks foreach (Ball ba in ballList) { foreach (Block b in currentlevel) { if (ba.BlockCollision(b)) { currentlevel.Remove(b); if (currentlevel.Count == 0) { score += b.score; } // powerups random if (random.Next(1, 11) <= 2) { // 20 % chance // TODO: powerups } if (blocks.Count == 0) { gameTimer.Enabled = false; if (currentlevelnum == levelList.Count()) { OnEnd(); } else { currentlevelnum++; } } break; } } } } else { // center the ball over the paddle ballList[0].x = paddle.x + (paddle.width / 2) - (ballList[0].size / 2); ballList[0].y = paddle.y - 21; } //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; } }
//Nit: Can you make the ball fall a little farther before resetting the ball, something doesn't feel right when it falls //Nit: Can you make the ball go in whatever the player last moved //Note Form1 has a soundplayer, you can access it with Form1.SoundPlayer private void gameTimer_Tick(object sender, EventArgs e) { // angleLable.Text = angleposition.ToString(); // Move the paddle if (leftArrowDown && paddle.x > 0) { paddle.Move("left"); } if (rightArrowDown && paddle.x < (this.Width - paddle.width)) { paddle.Move("right"); } if (start) { foreach (Ball b in ballList) { anglechange(); // Move ball b.Move(); // Check for collision with top and side walls b.WallCollision(this); // Check for ball hitting bottom of screen and if there is only one ball if (b.BottomCollision(this, paddle) && ballList.Count == 1) { // decrease player 1 lives player1Lives--; // move the ball and paddle back start = false; // reset ball angle angleposition = 3; anglechange(); // reset paddle x and y //paddle.x = paddleX; //paddle.y = paddleY; ballList[0].x = ((paddle.x - ballSize) + (paddle.width / 2)); ballList[0].y = paddle.y - 40; ballList[0].Yangle *= -1; // reset x and y speeds ballList[0].xSpeed = 6; ballList[0].ySpeed = 6; if (player1Lives < 1) { start = false; if (player2Lives < 0) { gameTimer.Enabled = false; scores(); } gameTimer.Enabled = false; scores(); } } else if (b.BottomCollision(this, paddle)) { //Remove ball that hit bottom from list ballList.Remove(b); break; } // Check for collision of ball with paddle, (incl. paddle movement) b.PaddleCollision(paddle, leftArrowDown, rightArrowDown); if (currentlevel.Count == 0) { gameTimer.Enabled = false; OnEnd(); } } // remove any balls that need to be removed foreach (Ball b in removeBalls) { ballList.Remove(b); } } //Testing Purposes: Score Tracker...1st Tracker...Adding the number to scores foreach (Block b in currentlevel) { if (ballList[0].ScoreTracker(b)) { score++; } } foreach (Ball ball in ballList) { for (int i = 0; i < currentlevel.Count(); i++) { Block b = currentlevel[i]; Rectangle ls = leftside[i]; Rectangle rs = rightside[i]; Rectangle curball = new Rectangle(Convert.ToInt32(ball.x), Convert.ToInt32(ball.y), ball.size, ball.size); if (curball.IntersectsWith(upLeft[i]) || curball.IntersectsWith(upRight[i]) || curball.IntersectsWith(bottomLeft[i]) || curball.IntersectsWith(bottomRight[i])) { ball.Yangle *= -1; } if (ball.BlockCollision(b)) { if (ball.side_collision(ls) || ball.side_collision(rs)) { ball.xSpeed *= -1; } leftside.RemoveAt(i); rightside.RemoveAt(i); } } } // Check if ball has collided with any currentlevel foreach (Ball ba in ballList) { ba.Move(); foreach (Block b in currentlevel) { if (ba.BlockCollision(b)) { b.hp--; if (b.hp < 1) { currentlevel.Remove(b); } score += 100; //Powerup Chance Random randPower = new Random(); randomPowerupChance = randPower.Next(1, 21); if (randomPowerupChance == 1) { Powerups p = new Powerups(b.x, b.y, 5, "3"); powerup.Add(p); activated = false; } if (randomPowerupChance == 2) { Powerups p = new Powerups(b.x, b.y, 5, "L"); powerup.Add(p); activated = false; } if (randomPowerupChance == 3) { Powerups p = new Powerups(b.x, b.y, 5, "l"); powerup.Add(p); activated = false; } if (randomPowerupChance == 4) { Powerups p = new Powerups(b.x, b.y, 5, "BS"); powerup.Add(p); activated = false; } if (randomPowerupChance == 5) { Powerups p = new Powerups(b.x, b.y, 5, "bs"); powerup.Add(p); activated = false; } if (currentlevel.Count < 1) { if (currentlevelnum == levelList.Count()) { OnEnd(); } } break; } } if (!start) { //center the ball over the paddle ballList[0].x = paddle.x + (paddle.width / 2) - (ballList[0].size / 2); ballList[0].y = paddle.y - 21; //clear the powerups from the screen powerup.Clear(); p1 = new Point(Convert.ToInt16(ballList[0].x + (ballList[0].size / 2)), Convert.ToInt16(ballList[0].y)); switch (angleposition) { // right case 1: p2 = new Point(Convert.ToInt16(ballList[0].x) + 89, Convert.ToInt16(ballList[0].y) - 44); break; case 2: p2 = new Point(Convert.ToInt16(ballList[0].x) + 70, Convert.ToInt16(ballList[0].y) - 70); break; case 3: p2 = new Point(Convert.ToInt16(ballList[0].x) + 44, Convert.ToInt16(ballList[0].y) - 89); break; case 4: p2 = new Point(Convert.ToInt16(ballList[0].x) - 44, Convert.ToInt16(ballList[0].y) - 89); break; case 5: p2 = new Point(Convert.ToInt16(ballList[0].x) - 70, Convert.ToInt16(ballList[0].y) - 70); break; case 6: p2 = new Point(Convert.ToInt16(ballList[0].x) - 89, Convert.ToInt16(ballList[0].y) - 44); break; // left default: break; } } else { if (currentlevel.Count == 0) { if (currentlevelnum == 9) { //OnEnd(); //testing WinScreen ws = new WinScreen(); Form form = this.FindForm(); form.Controls.Add(ws); form.Controls.Remove(this); ws.Location = new Point((form.Width - ws.Width) / 2, (form.Height - ws.Height) / 2); } else { currentlevelnum++; player1Lives++; levelLoad(); start = false; ballList[0].x = paddle.x + (paddle.width / 2) - (ballList[0].size / 2); ballList[0].y = paddle.y - 21; } } } //Move powerups down foreach (Powerups p in powerup) { p.powerupMove(); } //Check for collision of powerups foreach (Powerups p in powerup) { if (p.PowerupCollision() == true && activated == false) { if (p.type == "3") { Random randGen = new Random(); int x, y; x = randGen.Next(1, 301); y = randGen.Next(1, 301); //activate powerup Ball b2 = new Ball(x, y, xSpeed, ySpeed, ballSize, 1, -1); ballList.Add(b2); Ball b3 = new Ball(y, x, xSpeed, ySpeed, ballSize, 1, -1); ballList.Add(b3); break; } else if (p.type == "L") { paddle.width += 25; } else if (p.type == "l") { paddle.width -= 25; } else if (p.type == "BS") { ySpeed -= 2; foreach (Ball b in ballList) { b.ySpeed = ySpeed; } } else if (p.type == "bs") { ySpeed += 2; foreach (Ball b in ballList) { b.ySpeed = ySpeed; } } activated = true; powerup.Remove(p); break; } } //redraw the screen Refresh(); } }
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(); }