-
Notifications
You must be signed in to change notification settings - Fork 2
/
Bricks.java
369 lines (305 loc) · 8.8 KB
/
Bricks.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
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
// The "PongGameX" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Bricks extends Applet implements Runnable, MouseMotionListener
{
// Place instance variables here
final int numBricksX = 10;
final int numBricksY = 10;
int xInc = 2, yInc = 1;
int xPos = 0, yPos = 0;
Brick[] [] brickWall;
int mouseX;
Paddle pad;
Ball ball;
int appletsize_x = 500;
int appletsize_y = 600;
private Image dbImage;
private Graphics dbg;
int posx_new = 180;
int posy_new = 250;
int speedy = 0;
int posx_diffi = 180;
int posy_diffi = 300;
int posx_controls = 180;
int posy_controls = 350;
int posx_credits = 180;
int posy_credits = 400;
int posx_highscores = 180;
int posy_highscores = 450;
int posx_cursor = 180;
int posy_cursor = 250;
int t = 1;
boolean gamePlaying = true, mainMenu = false;
final private int RIGHT = 10; //Identifies the RIGHT variable at 10
final private int UP = 20; //Identifies the UP variable at 20
final private int LEFT = 30; //Identifies the LEFT variable as 30
final private int DOWN = 40; //Identifies the DOWN variable as 40
final private int MIDDLE = 0; //Identifies the DOWN variable as 40
final private int UPLEFT = 25; //Identifies UPLEFT between UP and LEFT at 25
final private int UPRIGHT = 15; //Identifies UPRIGHT between UP and RIGHT at 15
final private int DOWNLEFT = 35; // Identifies DOWNLEFT between DOWN and LEFT at 35
final private int DOWNRIGHT = 45; //Identifies DOWNLEFT at 45
public void init ()
{
setBackground (Color.black);
mouseX = 300;
pad = new Paddle ();
ball = new Ball ();
addMouseMotionListener (this);
resize (appletsize_x, appletsize_y);
brickWall = new Brick [numBricksX] [numBricksY];
createBricks ();
} // init method
public void start ()
{
// define a new thread
Thread th = new Thread (this);
// start this thread
th.start ();
}
/** Update - Method, implements double buffering */
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
// draw elements in background
dbg.setColor (getForeground ());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
void createBricks ()
{
for (int x = 0 ; x < numBricksX ; x++)
{
yPos = 50;
xPos += xInc + 40;
for (int y = 0 ; y < numBricksY ; y++)
{
brickWall [x] [y] = new Brick (1, 1);
yPos += yInc + brickWall [x] [y].brickHeight ();
brickWall [x] [y].changeX (xPos);
brickWall [x] [y].changeY (yPos);
}
}
}
public void stop ()
{
//nothing right now
}
public void destroy ()
{
//nothing right now
}
public void run ()
{
while (true)
{
if (t < 5)
{
t++;
}
else
{
t = 0;
}
if (mainMenu)
{
// Ball is bounced if its x - position reaches the right border of the applet
if (posy_cursor < posy_new)
{
// Change direction of ball movement
posy_cursor += 50;
}
// Ball is bounced if its x - position reaches the left border of the applet
else if (posy_cursor > posy_highscores)
{
// Change direction of ball movement
posy_cursor -= 50;
}
}
else if (gamePlaying)
{
ball.move ();
if (ball.y_Pos () < 0)
{
ball.reflectVertically ();
}
else if ((ball.x_Pos () < 0) || (ball.x_Pos () > appletsize_x))
{
ball.reflectHorizontally ();
}
if (ball.y_Pos () > 590)
{
if ((ball.x_Pos () > pad.x () - ball.radius ()) && ((ball.x_Pos () < pad.x () + (pad.width () / 2))))
{
ball.reflectFromPaddle (LEFT);
}
else if ((ball.x_Pos () > pad.x () + ( pad.width () / 2)) && ((ball.x_Pos () < pad.x () + pad.width + ball.radius ())))
{
ball.reflectFromPaddle (RIGHT);
}
else
{
ball.startBall ();
}
}
for (int x = 0 ; x < numBricksX ; x++)
{
for (int y = 0 ; y < numBricksY ; y++)
{
if (brickWall [x] [y].notBroken ())
{
if (brickWall [x] [y].ballContactVertical (ball.x_Pos (), ball.y_Pos ()))
{
ball.reflectVertically ();
brickWall [x] [y].reduceLife ();
brickWall [x] [y].startDropping ();
}
else if (brickWall [x] [y].ballContactHorizontal (ball.x_Pos (), ball.y_Pos ()))
{
ball.reflectHorizontally ();
brickWall [x] [y].reduceLife ();
brickWall [x] [y].startDropping ();
}
}
}
}
}
if (t == 5)
{
for (int x = 0 ; x < numBricksX ; x++)
{
for (int y = 0 ; y < numBricksY ; y++)
{
brickWall [x] [y].dropPowerUp ();
if (brickWall [x] [y].paddleContact (pad.xPos, 590))
{
pad.widthIncrease ();
}
}
}
}
try
{
// Stop thread for 20 milliseconds
Thread.sleep (2);
}
catch (InterruptedException ex)
{
// do nothing
}
repaint ();
}
}
public void mouseMoved (MouseEvent e)
{ // called during motion when no buttons are down
// if (e.getY ==
if ((e.getX () < appletsize_x - pad.width () / 2) && (e.getX () > (pad.width () / 2)))
{
pad.changeX (e.getX () - (pad.width () / 2));
ball.moveWithPaddle (pad.xPos + (pad.width () / 2) - (ball.radius ()));
}
e.consume ();
repaint ();
}
public void mouseDragged (MouseEvent e)
{ // called during motion with buttons down
ball.LaunchBall ();
e.consume ();
repaint ();
}
public void drawTitleScreen (Graphics g)
{
//color the screen
g.setColor (Color.red);
g.fillRect (posx_cursor - 5, posy_cursor - 5, 150, 50);
g.fillRect (posx_cursor + 5, posy_cursor - 5, 150, 50);
// Place the body of the drawing method here
g.setColor (Color.white);
g.setFont (new Font ("Vineta BT", Font.ITALIC, 50));
g.drawString ("SUPER", 125, 80);
g.drawString ("SMASH", 120, 130);
g.drawString ("BRICKS", 115, 180);
g.setFont (new Font ("Vineta BT", Font.PLAIN, 50));
g.drawString ("SUPER", 125, 80);
g.drawString ("SMASH", 120, 130);
g.drawString ("BRICKS", 115, 180);
g.setFont (new Font ("Arial", Font.PLAIN, 20));
g.setColor (Color.white);
g.fillRect (posx_new, posy_new, 150, 40);
g.setColor (Color.black);
g.fillRect (185, 255, 140, 30);
g.setColor (Color.white);
g.setFont (new Font ("Imprint MT Shadow", Font.PLAIN, 20));
g.drawString ("New Game", 205, 275);
g.setColor (Color.white);
g.fillRect (posx_diffi, posy_diffi, 150, 40);
g.setColor (Color.black);
g.fillRect (185, 305, 140, 30);
g.setColor (Color.white);
g.drawString ("Difficulty", 215, 325);
g.setColor (Color.white);
g.fillRect (posx_controls, posy_controls, 150, 40);
g.setColor (Color.black);
g.fillRect (185, 355, 140, 30);
g.setColor (Color.white);
g.drawString ("Controls", 217, 375);
g.setColor (Color.white);
g.fillRect (posx_credits, posy_credits, 150, 40);
g.setColor (Color.black);
g.fillRect (185, 405, 140, 30);
g.setColor (Color.white);
g.drawString ("Credits", 222, 425);
g.setColor (Color.white);
g.fillRect (posx_highscores, posy_highscores, 150, 40);
g.setColor (Color.black);
g.fillRect (185, 455, 140, 30);
g.setColor (Color.white);
g.drawString ("High Scores", 205, 475);
g.setFont (new Font ("Arial", Font.BOLD, 15));
g.setColor (Color.white);
g.drawString ("Instructions:", 218, 520);
g.setFont (new Font ("Arial", Font.PLAIN, 15));
g.drawString ("Scroll to the appropriate button and hit [Enter] to continue.", 70, 535);
g.drawString ("Press [Esc] to return back to Main Menu.", 130, 550);
}
void drawBricks (Graphics g)
{
for (int x = 0 ; x < numBricksX ; x++)
{
for (int y = 0 ; y < numBricksY ; y++)
{
if (brickWall [x] [y].notBroken ())
{
g.setColor (brickWall [x] [y].colour ());
g.fillRect (brickWall [x] [y].brickX (), brickWall [x] [y].brickY (), brickWall [x] [y].brickWidth (), brickWall [x] [y].brickHeight ());
}
}
}
}
public void paint (Graphics g)
{
if (mainMenu)
{
drawTitleScreen (g);
}
else if (gamePlaying)
{
drawBricks (g);
g.setColor (Color.green);
g.fillRect (pad.x (), 590, pad.width (), 10);
g.fillOval (ball.x_Pos () - ball.radius (), ball.y_Pos () - ball.radius (), ball.radius () * 2, ball.radius () * 2);
}
} // paint method
} // PongGameX class