/** * This method is called when the model updates and has to move the ball. The logic to the * reaction of the ball, if it intersects with other objects such as paddles and walls, is placed * here. * * @param changeNo represents the ball has done if it intersects a paddle or the corner of a * paddle etc... For definitions of what each integer represents see the model class. */ public void move(int changeNo) { /* * if statement added here because if dx and dy are originally set when * the game is paused the AI knows where the ball will go before the game * commences and will therefore move to that position */ if (dx == 0 || dy == 0) { dx = randomDirection(); dy = randomDirection(); System.out.println("dx: " + dx + "\ndy: " + dy); } // if // if the ball touches the the top wall then change y direction. if ((topLeftPoint.getY() > GameDimensions.FIELD_AMPLITUDE) || (bottomRightPoint.getY() <= GameDimensions.FIELD_AMPLITUDE * -1.0f)) dy = -dy; if (isTouchingPaddle) { if (!hasChangedDirection) { // uses changeNo to decide how to react to the intersections switch (changeNo) { case 0: dx = -1.1f * dx; dy = -1.1f * dy; break; case 1: dy = -1.05f * dy; break; case 2: dx = -1.05f * dx; default: break; } // switch hasChangedDirection = true; } // inner if isTouchingPaddle = false; } // outer if else /* This is always set to false to ensure that the first time the ball * intersects the paddle, it hasn't changed direction and is does * only once after the first point of intersection. */ hasChangedDirection = false; // updates each of the ball co-ordinates after the ball has been moved. topRightPoint.set((topRightPoint.getX() + dx), (topRightPoint.getY() + dy)); topLeftPoint.set((topLeftPoint.getX() + dx), (topLeftPoint.getY() + dy)); bottomLeftPoint.set((bottomLeftPoint.getX() + dx), (bottomLeftPoint.getY() + dy)); centrePoint.set((centrePoint.getX() + dx), (centrePoint.getY() + dy)); bottomRightPoint.set((bottomRightPoint.getX() + dx), (bottomRightPoint.getY() + dy)); // updates the ball image after it;s been moved ballImage.setFrame(topLeftPoint.getX(), topLeftPoint.getY(), diameter, diameter); } // move
/** * This utility function makes it easy to check whether the coordinates of the user's mouse click * (px,py) are on (or very close to) the ellipse defined by the bounding box with corners at * (x1,y1) and (x2,y2). * * @param px x coordinate of mouse click * @param py y coordinate of mouse click * @param x1 x point of the first coordinate of object to check for mouse click proximity * @param y1 y point of the first coordinate of object to check for mouse click proximity * @param x2 x point of the second coordinate of object to check for mouse click proximity * @param y2 y point of the second coordinate of object to check for mouse click proximity * @return true if the coordinates of the user's mouse click (px,py) are on close to the ellipse * defined by the bounding box with corners at (x1,y1) and (x2,y2). */ public static boolean clickHitEllipse(int px, int py, int x1, int y1, int x2, int y2) { int minX = (x1 < x2 ? x1 : x2); int minY = (y1 < y2 ? y1 : y2); int maxX = (x1 > x2 ? x1 : x2); int maxY = (y1 > y2 ? y1 : y2); Ellipse2D.Float innerEllipse = new Ellipse2D.Float( minX + SELECTION_DISTANCE, minY + SELECTION_DISTANCE, maxX - minX - 2 * SELECTION_DISTANCE, maxY - minY - 2 * SELECTION_DISTANCE); Ellipse2D.Float outerEllipse = new Ellipse2D.Float( minX - SELECTION_DISTANCE, minY - SELECTION_DISTANCE, maxX - minX + 2 * SELECTION_DISTANCE, maxY - minY + 2 * SELECTION_DISTANCE); return outerEllipse.contains(px, py) && !innerEllipse.contains(px, py); }
/** Moves the ball to the initial position */ public final void moveToInitialPosition() { // set the ball stationary dx = 0; dy = 0; /* sets the point references of the ball to the relevant points using the * centre point as a guide */ /* centrePoint.set( (float)GameDimensions.X_DISTANCE_TO_FIELD_START + (GameDimensions.FIELD_WIDTH / 2), (float)(GameDimensions.FIELD_HEIGHT / 2));*/ centrePoint.set(0.0f, 0.0f); topRightPoint.set((centrePoint.getX() + radius), (centrePoint.getY() + radius)); topLeftPoint.set((centrePoint.getX() - radius), (centrePoint.getY() + radius)); bottomRightPoint.set((centrePoint.getX() + radius), (centrePoint.getY() - radius)); bottomLeftPoint.set((centrePoint.getX() - radius), (centrePoint.getY() - radius)); // sets the image of the ball ballImage.setFrame(topLeftPoint.getX(), topLeftPoint.getY(), diameter, diameter); } // moveInitialPosition
private void drawCircle(Graphics2D gr, Vec2 v, float size, Color color) { circle.setFrame(v.x - size, v.y - size, size * 2, size * 2); gr.setColor(color); gr.draw(circle); }
/** sets the bullet's location by taking in two floats */ public void setPosition(float x, float y) { changedPosition.x = x; changedPosition.y = y; if (changedPosition.x > Game.width || changedPosition.y > Game.height) expired = true; super.setFrame((double) x, (double) y, (double) size, (double) size); }
protected void ellipseImpl(float x, float y, float w, float h) { ellipse.setFrame(x, y, w, h); drawShape(ellipse); }