Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The Breakout Ball
- *
- * @author CMS_167
- */
- public class Ball {
- //*** Instance variables ***//
- private double x, y;
- private double radius;
- private double dx, dy;
- /**
- * Constructor -- intiailize a new Ball
- */
- public Ball() {
- this.x = .5;
- this.y = .10;
- this.radius = .01;
- this.dx = .008;
- this.dy = .008;
- }
- /**
- * Update -- add code to move the ball and reflect off of the sides
- *
- * Look at the BouncingBall and Pong examples
- */
- public void update() {
- if (this.x + this.radius > 1.0 || this.x + this.radius < 0.0) {
- this.dx = -this.dx;
- }
- if (this.y + this.radius > 1.0 || this.y + this.radius < 0.0) {
- this.dy = -this.dy;
- }
- this.x += this.dx;
- this.y += this.dy;
- }
- /**
- * Check if this Ball collides with the Paddle
- *
- * If so, reverse the Ball's y velocity
- *
- * Look at the examples from Secret Collect and Pong
- *
- * @return nothing, this method only updates the Ball's dy data member
- */
- public void checkPaddleCollision(Paddle paddle) {
- double right = this.x + this.radius;
- double left = this.x - this.radius;
- double top = this.y + this.radius;
- double bottom = this.y - this.radius;
- if (paddle.top() < bottom) {
- return;
- }
- if (paddle.bottom() > top) {
- return;
- }
- if (paddle.left() > right) {
- return;
- }
- if (paddle.right() < left) {
- return;
- }
- this.dy = -this.dy;
- }
- /**
- * Check if this Ball collides with the given Brick
- *
- * Similar to testing for Paddle collisions, but this method
- * returns a boolean indicating if the collision happened
- *
- * @return false if no collision, true otherwise
- */
- public boolean checkBrickCollision(Brick brick) {
- double right = this.x + this.radius;
- double left = this.x - this.radius;
- double top = this.y + this.radius;
- double bottom = this.y - this.radius;
- if (brick.top() < bottom) {
- return false;
- }
- if (brick.bottom() > top) {
- return false;
- }
- if (brick.left() > right) {
- return false;
- }
- if (brick.right() < left) {
- return false;
- }
- // Starter code returns false so the program will run
- if (brick.right() < right || brick.left() > left) {
- this.dx = -this.dx;
- return false;
- }
- this.dy = -this.dy;
- return true;
- }
- /**
- * Draw
- */
- public void draw() {
- StdDraw.filledCircle(this.x, this.y, this.radius);
- }
- }
- /**
- * A Breakout Brick
- *
- * @author CMS_167
- */
- public class Brick {
- //*** Instance variables ***//
- private double x, y;
- private double halfWidth, halfHeight;
- private int red, green, blue;
- /**
- * Constructor -- takes a position in the grid of bricks and automatically
- * calculates the correct x and y position for the new Brick's center
- *
- * DON'T MODIFY THIS METHOD.
- */
- public Brick(int row, int col) {
- // Calculate the starting position based on the row and column
- this.halfHeight = .30 / Breakout.NUM_ROWS / 2;
- this.halfWidth = 1.0 / Breakout.NUM_COLS / 2;
- this.x = col * (2 * halfWidth) + halfWidth;
- this.y = .90 - row * (2 * halfHeight) - halfHeight;
- // Random color
- this.red = (int) (Math.random() * 256);
- this.green = (int) (Math.random() * 256);
- this.blue = (int) (Math.random() * 256);
- }
- /**
- * Add new methods to calculate and return the right, left, top, and bottom
- * of this Brick
- */
- public double right() {
- return this.x + this.halfWidth;
- }
- public double left() {
- return this.x - this.halfWidth;
- }
- public double top() {
- return this.y + this.halfHeight;
- }
- public double bottom() {
- return this.y - this.halfHeight;
- }
- /**
- * Draw
- */
- public void draw() {
- StdDraw.setPenColor(this.red, this.green, this.blue);
- StdDraw.filledRectangle(this.x, this.y, this.halfWidth, this.halfHeight);
- StdDraw.setPenColor(0, 0, 0); // Set color back to black
- }
- }
- /**
- * The Breakout Paddle
- *
- * @author CMS_167
- */
- import java.awt.event.KeyEvent;
- public class Paddle {
- //*** Instance variables ***//
- private double x, y;
- private double halfWidth, halfHeight;
- private final double STEP = .02; // Distance to move on each key press
- /**
- * Construct a new Paddle
- */
- public Paddle() {
- this.x = .5;
- this.y = .01;
- this.halfWidth = .10;
- this.halfHeight = .01;
- }
- /**
- * Update -- move the Paddle
- *
- * Check for left and right arrow presses StdDraw.isKeyPressed
- */
- public void update() {
- if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT)){
- this.x -= STEP;
- }
- if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT)) {
- this.x += STEP;
- }
- }
- /**
- * Add methods to calculate and return the right, left, top, and bottom sides
- * of this Paddle
- */
- public double right() {
- return this.x + this.halfWidth;
- }
- public double left() {
- return this.x - this.halfWidth;
- }
- public double top() {
- return this.y + this.halfHeight;
- }
- public double bottom() {
- return this.y - this.halfHeight;
- }
- /**
- * Draw
- */
- public void draw() {
- StdDraw.filledRectangle(this.x, this.y, this.halfWidth, this.halfHeight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement