-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemp
373 lines (267 loc) · 8.38 KB
/
Temp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package Breakout;
import java.awt.Color;
import java.awt.Component;
import acm.graphics.*;
import acm.program.*;
public class FunctionalityTest extends GraphicsProgram{
// private static final double SIZE = 5;
// AnimatedBall redBall = new AnimatedBall(SIZE);
public void run() {
setSize(Model.getWidth(), Model.getHeight());
AnimatedBall curBall = Model.getRedBall();
curBall.setFilled(true);
curBall.setColor(Color.RED);
add(curBall, Model.getBallStart());
AnimatedPaddle curPaddle = Model.getPaddle();
curPaddle.setFilled(true);
curPaddle.setColor(Color.BLACK);
add(curPaddle, Model.getPaddleStart());
GLabel startMessage = new GLabel("Click to start Breakout!");
startMessage.setLocation(Model.getPaddleStart().getX() - 50, Model.getPaddleStart().getY() + 25);
add(startMessage);
Thread curBallThread = new Thread(curBall);
Thread curPaddleThread = new Thread(curPaddle);
waitForClick();
remove(startMessage);
curBallThread.start();
curPaddleThread.start();
while(curBallThread.isAlive() && !Model.isLost() && !Model.isWon()){
println(curBall.getLocation().toString());
if(wouldCollideWith(curBall) != null){
curBall.setDirection(calculateDirection(curBall,getElementAt(curBall.getX(),curBall.getY())));
}
}
}
private Object wouldCollideWith(AnimatedBall curBall) {
double deltaX = Math.sin(curBall.getDirection()) * AnimatedBall.getDelta();
double deltaY = Math.cos(curBall.getDirection()) * AnimatedBall.getDelta();
double redX = curBall.getX();
double redY = curBall.getY();
double size = AnimatedBall.getBallSize();
Object collisionObject = null;
collisionObject = getElementAt(redX + deltaX, redY + deltaY);
if(!(collisionObject instanceof GCanvas)){
collisionObject = getElementAt(redX + deltaX + size , redY + deltaY);
if(!(collisionObject instanceof GCanvas)){
collisionObject = getElementAt(redX + deltaX , redY + deltaY + size);
if(!(collisionObject instanceof GCanvas)){
collisionObject = getElementAt(redX + deltaX + size, redY + deltaY + size);
}else{
/*When all cllisionObjects were GCanvas*/
collisionObject = null;
}
}
}
return collisionObject;
}
private double calculateDirection(AnimatedBall redBall, Object elementAt) {
double newDirection = redBall.getDirection();
// if(redBall.getX() >= Model.getWidth() || redBall.getX() <= 0 || redBall.getY() <= 0){
// reflect(redBall);
//t }
if(elementAt != null){
if(elementAt instanceof AnimatedPaddle){
newDirection = paddleReflect(redBall, elementAt);
}else if(elementAt instanceof Brick /*|| elementAt instanceof Wall*/){
newDirection = reflect(redBall);
// }else if(elementAt instanceof Bottom){
// Model.setLost(true);
}
}else{
assert false : "No collision object handed to calculate new direction.";
}
assert newDirection == redBall.getDirection() : "No new direction set.";
return newDirection;
}
private double paddleReflect(AnimatedBall redBall, Object elementAt) {
double direction = redBall.getDirection();
assert(360 >= direction && 0 <= direction):"Invalid direction.";
double xBall = redBall.getLocation().getX();
double xPaddle = ((Component) elementAt).getLocation().getX();
double deltaDistance = xBall - (xPaddle + redBall.getWidth() / 2.0);
double deltaMax = ((Program) elementAt).getWidth() / 2.0 + redBall.getWidth();
double newDirection;
if(direction <= 180){
newDirection = 180 - direction;
}else{
newDirection = 360 - direction;
}
newDirection = newDirection - (deltaDistance/deltaMax * newDirection);
return newDirection;
}
private double reflect(AnimatedBall redBall) {
double newDirection;
double direction = redBall.getDirection();
int side = (int) (direction / 180);
switch (side) {
case 0:
newDirection = 180 - direction;
case 1:
newDirection = 360 - direction;
default:
assert (false): "Reflection can not be callucalted correctly";
newDirection = direction;
}
return newDirection;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
package Breakout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import acm.graphics.GRect;
// import acm.program.*;
public class AnimatedPaddle extends GRect implements Runnable{
/* Private instance variables */
private static double width = 50;
private static double height = 10;
/* Creates a new AnimatedRectangle of the specified size */
public AnimatedPaddle(double width, double height) {
super(width, height);
}
/* Runs when this object is started to animate the rectangle */
public void run() {
//while(Model.isRunning() == true){
while(true){
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
double x = point.getX();
this.setLocation(x - width / 2, Model.getHeight() - 100);
}
}
private void setLocation(double d, int i) {
// TODO Auto-generated method stub
}
public static double getPaddleWidth() {
return width;
}
public static double getPaddleHeight() {
return height;
}
public static void setWidth(double width) {
AnimatedPaddle.width = width;
}
public static void setHeight(double height) {
AnimatedPaddle.height = height;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
package Breakout;
import acm.graphics.GOval;
import acm.util.RandomGenerator;
public class AnimatedBall extends GOval implements Runnable{
/* Private instance variables */
private double direction = 90;
private static int size = 5;
// private double x = 150;
// private double y = 350;
static final int PAUSE_TIME = 20;
static final int CHANGE_TIME = 50;
private static double DELTA = 2;
/* Creates a new AnimatedBall of the specified size */
public AnimatedBall(double size) {
super(size,size);
}
/* Runs when this object is started to animate the ball */
public void run() {
//while(Model.isRunning() == true){
while(true){
movePolar(DELTA, direction);
pause(PAUSE_TIME);
}
}
public void setDirection(double dir){
this.direction = dir;
}
public double getDirection(){
return direction;
}
public static double getDelta() {
return DELTA;
}
public static int getBallSize() {
return size;
}
public static void setSize(int size) {
AnimatedBall.size = size;
}
// public double getY() {
// return y;
// }
//
// public void setY(double y) {
// this.y = y;
// }
//
// public double getX() {
// return x;
// }
//
// public void setX(double x) {
// this.x = x;
// }
}
///////////////////////////////////////////////////////////////////////////////////////////
package Breakout;
import acm.graphics.*;
import acm.program.*;
public class Model {
/*Activity state of Model*/
private static boolean won = false;
private static boolean lost = false;
private static boolean running = true;
/*Field specification*/
private static final int HEIGHT = 600;
private static final int WIDTH = 250;
/*Ball specification*/
private static final GPoint BALL_START = new GPoint(WIDTH/2,HEIGHT/2);
private static AnimatedBall redBall = new AnimatedBall(AnimatedBall.getBallSize());
/*Paddle specification*/
private static int paddleWidth = 50;
private final double PADDLE_HEIGHT = 5;
private static AnimatedPaddle paddle = new AnimatedPaddle(AnimatedPaddle.getPaddleWidth(), AnimatedPaddle.getPaddleHeight());
private static final GPoint PADDLE_START = new GPoint(WIDTH/2 - paddleWidth/2,HEIGHT - 100 );
public static boolean isRunning() {
return running;
}
public static void setRunning(boolean newRunStatus) {
running = newRunStatus;
}
public static GPoint getBallStart() {
return BALL_START;
}
public static int getHeight() {
return HEIGHT;
}
public static int getWidth() {
return WIDTH;
}
public static AnimatedBall getRedBall() {
return redBall;
}
public static void setRedBall(AnimatedBall redBall) {
Model.redBall = redBall;
}
public static GPoint getPaddleStart() {
return PADDLE_START;
}
public double getPADDLE_HEIGHT() {
return PADDLE_HEIGHT;
}
public static boolean isLost() {
return lost;
}
public static void setLost(boolean lost) {
Model.lost = lost;
}
public static boolean isWon() {
return won;
}
public static void setWon(boolean won) {
Model.won = won;
}
public static AnimatedPaddle getPaddle() {
return paddle;
}
}