public boolean collision(GObject a, GObject b) { if (a.getBounds().intersects(b.getBounds())) { return true; } else { return false; } }
/** * Creates a point at the ball 4 check and verifies the presence of an object near it. Returns * null or the name of the object, if it is present */ private GObject getCollidingObject() { GObject colliding; colliding = getElementAt(ball.getX(), ball.getY()); // Creates the upper left point of check ball if (colliding != null) { Color color = colliding.getColor(); count(color); return colliding; } colliding = getElementAt( ball.getX() + BALL_RADIUS * 2, ball.getY()); // Creates the top right point of check ball if (colliding != null) { Color color = colliding.getColor(); count(color); return colliding; } colliding = getElementAt( ball.getX(), ball.getY() + BALL_RADIUS * 2); // Creates the lower left point of check ball if (colliding != null) { Color color = colliding.getColor(); count(color); return colliding; } colliding = getElementAt( ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2); // Creates a check point of the lower right on the ball if (colliding != null) { Color color = colliding.getColor(); count(color); return colliding; } return null; }
/** * Called after the body has moved to update the leg motion accordingly. This method does not use * a particularly convincing leg motion algorithm; low-budgets Saturday morning cartoons would be * proud. */ public void bodyMovedBy(double dx, double dy) { if (anchored) { graphics.move(-dx, -dy); } else { double speed = Math.hypot(dx, dy), targetX = restPosition.getX() + dx / speed * rangeOfMotion, targetY = restPosition.getY() + dy / speed * rangeOfMotion, toTargetX = targetX - graphics.getX(), toTargetY = targetY - graphics.getY(), distToTarget = Math.hypot(toTargetX, toTargetY); graphics.move(toTargetX / distToTarget * speed * 1.5, toTargetY / distToTarget * speed * 1.5); } double distention = Math.hypot(graphics.getX() - restPosition.getX(), graphics.getY() - restPosition.getY()); if (distention >= rangeOfMotion) anchored = !anchored; }
public void play() { GCanvas canvas = getGCanvas(); ball.move(); if (ball.getX() < 0 || ball.getX() + ball.getDiameter() > WIDTH) { ball.bounceX(); } if (ball.getY() < 0) { ball.bounceY(); } if (ball.getY() + ball.getDiameter() > HEIGHT) { lives--; lifeDisplay.setLabel(String.format("Lives remaining: %d", lives)); if (lives <= 0) { showMessage("DEFEAT!"); } else { ball.setLocation(WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS); } return; } for (int i = 0; i < 4; i++) { GPoint point = new GPoint( ball.getX() + (i & 1) * ball.getDiameter(), ball.getY() + ((i & 2) / 2) * ball.getDiameter()); GObject o = canvas.getElementAt(point); if (o == null) { continue; } if (o instanceof BreakoutBrick) { canvas.remove(o); bricksRemaining--; if (bricksRemaining <= 0) { showMessage("VICTORY!"); } } else if (o instanceof BreakoutPaddle) { paddleBounces++; if (paddleBounces > NPADDLE_BOUNCES) { paddleBounces = 0; ball.accelerate(1.0); } } if (point.getX() < o.getX() + o.getWidth() || point.getX() < o.getX()) { ball.bounceX(); } if (point.getY() < o.getY() + o.getHeight() || point.getY() < o.getY()) { ball.bounceY(); } return; } }
/** * Critters should instantiate legs from their createGraphics() methods. * * @param graphics The visual representation of the leg, positioned at rest. * @param rangeOfMotion How far away from its rest position the leg is allowed to move. */ public Leg(GObject graphics, double rangeOfMotion) { this.graphics = graphics; this.restPosition = graphics.getLocation(); this.rangeOfMotion = rangeOfMotion; }