-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.java
192 lines (173 loc) · 4.63 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import greenfoot.*;
/**
* A Ball is a thing that bounces of walls and paddles (or at least i should).
*
* @author The teachers
* @version 1
*/
public class Ball extends Actor
{
private static final int BALL_SIZE = 25;
private static final int BOUNCE_DEVIANCE_MAX = 25;
private static final int STARTING_ANGLE_WIDTH = 90;
private static final int DELAY_TIME = 100;
private Actor lastTouch = this;
private int speed;
private boolean hasBouncedHorizontally;
private boolean hasBouncedVertically;
private int delay;
private Color color = Color.BLACK;
/**
* Contructs the ball and sets it in motion!
*/
public Ball()
{
createImage();
init();
}
/**
* Creates and sets an image of a black ball to this actor.
*/
private void createImage()
{
GreenfootImage ballImage = new GreenfootImage(BALL_SIZE,BALL_SIZE);
ballImage.setColor(color);
ballImage.fillOval(0, 0, BALL_SIZE, BALL_SIZE);
setImage(ballImage);
}
/**
* Act - do whatever the Ball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (delay > 0)
{
delay--;
}
else
{
move(speed);
checkBounceOffWalls();
checkBounceOffPaddle();
checkRestart();
}
}
/**
* Returns true if the ball is touching one of the side walls.
*/
private boolean isTouchingSides()
{
return (getX() <= BALL_SIZE/2 || getX() >= getWorld().getWidth() - BALL_SIZE/2);
}
/**
* Returns true if the ball is touching the ceiling.
*/
private boolean isTouchingCeiling()
{
return (getY() <= BALL_SIZE/2);
}
private boolean isTouchingPaddle()
{
Paddle paddle = (Paddle)getOneIntersectingObject(Paddle.class);
if(paddle != null)
{
switchColor(paddle);
if(getLastTouch().equals(paddle))
{
return false;
}
paddle.hitByBall();
setLastTouch(paddle);
return true;
}
return false;
}
/**
* Returns true if the ball is touching the floor.
*/
private boolean isTouchingFloor()
{
return (getY() >= getWorld().getHeight() - BALL_SIZE/2);
}
/**
* Check to see if the ball should bounce off one of the walls.
* If touching one of the walls, the ball is bouncing off.
*/
private void checkBounceOffWalls()
{
if (isTouchingSides())
{
if (! hasBouncedHorizontally)
{
revertHorizontally();
}
}
else
{
hasBouncedHorizontally = false;
}
}
private void checkBounceOffPaddle()
{
if(isTouchingPaddle())
{
revertVertically();
}
}
/**
* Check to see if the ball should be restarted.
* If touching the floor the ball is restarted in initial position and speed.
*/
private void checkRestart()
{
if (isTouchingFloor() || isTouchingCeiling())
{
init();
setLocation(getWorld().getWidth() / 2, getWorld().getHeight() / 2);
}
}
/**
* Bounces the ball back from a vertical surface.
*/
private void revertHorizontally()
{
int randomness = Greenfoot.getRandomNumber(BOUNCE_DEVIANCE_MAX)- BOUNCE_DEVIANCE_MAX / 2;
setRotation((180 - getRotation()+ randomness + 360) % 360);
hasBouncedHorizontally = true;
}
/**
* Bounces the ball back from a horizontal surface.
*/
private void revertVertically()
{
int randomness = Greenfoot.getRandomNumber(BOUNCE_DEVIANCE_MAX)- BOUNCE_DEVIANCE_MAX / 2;
setRotation((360 - getRotation()+ randomness + 360) % 360);
hasBouncedVertically = true;
}
/**
* Initialize the ball settings.
*/
private void init()
{
speed = 1;
delay = DELAY_TIME;
hasBouncedHorizontally = false;
hasBouncedVertically = false;
setLastTouch(this);
setRotation(Greenfoot.getRandomNumber(STARTING_ANGLE_WIDTH)+STARTING_ANGLE_WIDTH/2);
}
public void switchColor(Paddle paddle)
{
// Implement this method so that the color of the ball changes to match
// the color of given paddle.
}
private Actor getLastTouch()
{
return lastTouch;
}
private void setLastTouch(Actor actor)
{
lastTouch = actor;
}
}