-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
79 lines (56 loc) · 1.99 KB
/
Ball.java
File metadata and controls
79 lines (56 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Nii Oye Kpakpo
*
*Handles the user interface for selecting or creating player profiles.
*/
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class Ball extends Circle {
// Ball properties
private int speed; // Speed of the ball
private double direction;
// Constructor: initialize the ball
public Ball(int paWidth, int paHeight, double dir) {
super(paWidth / 2, paHeight / 2, 10); // Set ball in the center of the play area, with a radius of 7
setFill(Color.SEAGREEN); // Set color of the ball
this.setSpeed(3);
this.setDirection(dir); // Set the initial direction in degrees
}
// Move the ball based on its speed and direction
public void move() {
double radians = Math.toRadians(getDirection()); // Convert the direction to radians for JavaFX trigonometry
double dx = getSpeed() * Math.cos(radians); // Change in x direction
double dy = getSpeed() * Math.sin(radians); // Change in y direction
// Update the ball's position
setCenterX(getCenterX() + dx);
setCenterY(getCenterY() + dy);
}
// Get the top edge of the ball
public int getTopEdge() {
return (int) (getCenterY() - getRadius());
}
// Get the bottom edge of the ball
public int getBottomEdge() {
return (int) (getCenterY() + getRadius());
}
// Get the left edge of the ball
public int getLeftEdge() {
return (int) (getCenterX() - getRadius());
}
// Get the right edge of the ball
public int getRightEdge() {
return (int) (getCenterX() + getRadius());
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public double getDirection() {
return direction;
}
public void setDirection(double direction) {
this.direction = direction;
}
}