/** * 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
/** 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); }