protected override void update() { Paddle paddle = paddleSet.get(0); Ball ball = ballSet.get(0); bool playerWonGame = areAllBlocksRemoved(); if (playerWonGame) { //EXAMPLE 1 asks if playerWonGame == true gameWin(); //and then calls the gameWin() function } if (!playerWonGame) { if (keyboard.isKeyTapped(Keys.Space) || (MouseDevice.MouseOnScreen() && MouseDevice.IsLeftTapped())) { if (ball.Visible) { ball.spawn(paddle); } } //Editors note: we revert sections of future labs to their starting point where it //doesn't affect the future labs to block newer labs providing all of the answers //to previous labs, and the below unduly nested if statement is one such example if (MouseDevice.MouseOnScreen()) { //is a MouseDevice connected to the computer? if (MouseDevice.IsLeftTapped()) { //is the MouseDevice's left-button pressed? if (ball.Visible == false) { //is there no ball on the screen currently ball.spawn(paddle); } } } if (keyboard.isKeyDown(Keys.Left)) { //SOLN paddle.moveLeft(); //MouseDevice.setX() //solution to bug 1001 } if (keyboard.isKeyDown(Keys.Right)) { //SOLN paddle.moveRight(); //MouseDevice.setX() //solution to bug 1001 // MouseDevice.setWorld(new World()); } if (MouseDevice.MouseOnScreen()) { //SOLN paddle.CenterX = MouseDevice.getMouseX(); paddle.clampAtWorldBounds(); } if (ball != null && ball.Visible) { BoundCollidedStatus status = ball.collideWorldBound(); switch (status) { case BoundCollidedStatus.CollidedTop: { ball.reflectTop(); ball.playBounceSound(); break; } case BoundCollidedStatus.CollideBottom: { ball.Visible = false; ball.playDieSound(); lifeSet.remove(); if (lifeSet.getCount() < 1) { gameLost(); } break; } case BoundCollidedStatus.CollidedLeft: { ball.reflectLeft(); ball.playBounceSound(); break; } case BoundCollidedStatus.CollidedRight: { ball.reflectRight(); ball.playBounceSound(); break; } // Catch the case where the ball is inside the // world and not hitting any bounds. A warning is // given if all cases are not handled. default: break; } if (paddle.Collided(ball)) { paddle.reflect(ball); //method pushes this ball out a bit //BUG: paddle.reflect(ball) and ball.reflect(paddle) do different things //issue: using paddle.reflect(ball) pushes out in the y where ball.reflect paddle doesnt. //solutionToBug: have both methods do the algorithm below (pushout and then call superclass?) //instead of only one. //solution: have the pushout amount be relative to the inverse of the current //direction of motion, so if the ball was coming down from the top you move it -5 //in the y (or up) and if the ball is coming from the right to the left, you move //it +5 in the x (where 5 is the speed) ball.playBounceSound(); } //ROB/KELVIN; consider undoing the import so the students know PaddleStates live inside Paddles? //ROB/KELVIN Q2: unite Ball and Paddle Types/States respectively, to reduce logic to: // if(ball.getState() != paddle.getState() ) ball.setState(paddle.getState()); if (paddle.getState() == Paddle.PaddleState.NORMAL) { ball.setType(Ball.BallType.NORMAL); } if (paddle.getState() == Paddle.PaddleState.FIRE) { ball.setType(Ball.BallType.FIRE); //soln } else if (paddle.getState() == Paddle.PaddleState.ICE) { ball.setType(Ball.BallType.ICE); //soln } //logic reduction here to see ball and paddle states are synchronized /** ball.setState(paddle.getState()); REDUCE to this! */ if (blockSet.isBallCollidingWithABlock(ball)) { handleBlockBallCollision(ball, paddle); } } } }
protected override void update() { Paddle paddle = paddleSet.get(0); Ball ball = ballSet.get(0); if (blockSet.allBlocksAreDead()) { gameWin(); } else { // Spawn ball if (keyboard.isKeyTapped(Keys.Space) || (MouseDevice.MouseOnScreen() && MouseDevice.IsLeftTapped())) { if (ball.Visible == false) { ball.spawn(paddle); } } if (keyboard.isKeyDown(Keys.Left)) { paddle.moveLeft(); } if (keyboard.isKeyDown(Keys.Right)) { paddle.moveRight(); } if (MouseDevice.MouseOnScreen()) { paddle.CenterX = MouseDevice.getMouseX(); paddle.clampAtWorldBounds(); } if (ball != null && ball.Visible) { ballWorldCollision(ball); // Paddle and ball collision if (paddle.Collided(ball)) { paddle.reflect(ball); ball.playBounceSound(); } // Checks to see ball and paddle states are synchronized. if (ball.isBurning()) { if (paddle.getState() == Paddle.PaddleState.NORMAL) { ball.setType(Ball.BallType.NORMAL); } else if (paddle.getState() == Paddle.PaddleState.ICE) { ball.setType(Ball.BallType.ICE); } } if (ball.isFrozen()) { if (paddle.getState() == Paddle.PaddleState.NORMAL) { ball.setType(Ball.BallType.NORMAL); } else if (paddle.getState() == Paddle.PaddleState.FIRE) { ball.setType(Ball.BallType.FIRE); } } if (blockSet.isBallCollidingWithABlock(ball)) { // Demonstrate 'while' loops: // Walk outwards, removing blocks as we go, // until we reach a wall or missing block Block block = blockSet.getCollidedBlock(ball); block.reflect(ball); //if (block.getType() == Block.BlockType.NORMAL) { int row = block.getRow(); int col = block.getColumn(); //blockSet.remove(block); block.revealPower(); int nextCol = col; while (nextCol + 1 < blockSet.getNumColumns() && block != null && block.getType() == Block.BlockType.NORMAL) { if (block.getType() == Block.BlockType.NORMAL) { blockSet.remove(block); } //block.revealPower(); nextCol++; block = blockSet.getBlockAt(row, nextCol); } nextCol = col - 1; while (nextCol >= 0 && block != null && block.getType() == Block.BlockType.NORMAL) { if (block.getType() == Block.BlockType.NORMAL) { // blockSet.remove(block); block.revealPower(); } nextCol--; block = blockSet.getBlockAt(row, nextCol); } // } } } } }