-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBall.java
132 lines (118 loc) · 2.72 KB
/
Ball.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
The Ball class includes variables which identify
and describe the state of the ball, such as coordinates
radius, vertical and horizontal speed, orientation etc.
*/
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Ball
{
private int radius;
//center of ball
private int x;
private int yTop;
private int verticalSpeed;
private int horizontalSpeed;
private boolean goingDown;
private int diameter;
/**
* Constructor
*
* @param x_
* @param yTop_
* @param radius_
* @return
*/
public Ball(int x_, int yTop_, int radius_)
{
x = x_;
yTop = yTop_;
radius = radius_;
goingDown = true;
verticalSpeed = 2;
horizontalSpeed = 0;
diameter = 2*radius_;
}
/**
* Moves ball. Change orientation when side walls or top is hit
* and prevent ball from sticking in the gutter.
*/
public void move()
{
if(goingDown)
yTop += verticalSpeed;
else
yTop -= verticalSpeed;
x += horizontalSpeed;
Commons commons = new Commons();
if (( (x-radius) == 0) || (x + (radius) == commons.getWidth()))
horizontalSpeed = -horizontalSpeed;
if (x + (radius) > commons.getWidth())
{
x = commons.getWidth() - radius;
horizontalSpeed = -horizontalSpeed;
}
if ((x-radius) < 0)
{
x = radius;
horizontalSpeed = -horizontalSpeed;
}
if (yTop <= 0)
goingDown = true;
if (yTop + (2*radius) >= commons.getHeight())
goingDown = false;
}
/**
* Vertical speed setter.
*
* @param speed
*/
public void setVerticalSpeed(int speed)
{
verticalSpeed = speed;
}
/**
* Horizontal speed setter.
*
* @param speed
*/
public void setHorizontalSpeed(int speed)
{
horizontalSpeed = speed;
}
public int getRadius()
{
return radius;
}
public int getX()
{
return x;
}
public int getY()
{
return yTop;
}
public void changeVerticalDirection()
{
goingDown = !goingDown;
}
public Ellipse2D.Double ballAsEllipse()
{
Ellipse2D.Double ball
= new Ellipse2D.Double(x-radius, yTop, 2*radius, 2*radius);
return ball;
}
/**
* Determine where ball is placed
* when the game starts or the player respawns.
*/
public void reset()
{
Commons commons = new Commons();
x = commons.getWidth()/2;
yTop = commons.getHeight()/ 2;
horizontalSpeed = 0;
goingDown = true;
}
}