forked from singhmayank980/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrickBreakerGame.java
293 lines (250 loc) · 7.53 KB
/
BrickBreakerGame.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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BrickBreaker extends JPanel implements KeyListener,
ActionListener, Runnable {
// movement keys..
static boolean right = true;
static boolean left = false;
// ..............
// variables declaration for ball.................................
int ballx = 160;
int bally = 218;
// variables declaration for ball.................................
// ===============================================================
// variables declaration for bat..................................
int batx = 160;
int baty = 245;
// variables declaration for bat..................................
// ===============================================================
// variables declaration for brick...............................
int brickx = 70;
int bricky = 50;
int brickBreadth = 30;
int brickHeight = 20;
// variables declaration for brick...............................
// ===============================================================
// declaring ball, paddle,bricks
Rectangle Ball = new Rectangle(ballx, bally, 5, 5);
Rectangle Bat = new Rectangle(batx, baty, 40, 5);
// Rectangle Brick;// = new Rectangle(brickx, bricky, 30, 10);
Rectangle[] Brick = new Rectangle[12];
//reverses......==>
int movex = -1;
int movey = -1;
boolean ballFallDown = false;
boolean bricksOver = false;
int count = 0;
String status;
BrickBreaker() {
}
public static void main(String[] args) {
JFrame frame = new JFrame();
BrickBreaker game = new BrickBreaker();
JButton button = new JButton("restart");
frame.setSize(350, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.add(button, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
button.addActionListener(game);
game.addKeyListener(game);
game.setFocusable(true);
Thread t = new Thread(game);
t.start();
}
// declaring ball, paddle,bricks
public void paint(Graphics g) {
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, 350, 450);
g.setColor(Color.blue);
g.fillOval(Ball.x, Ball.y, Ball.width, Ball.height);
g.setColor(Color.green);
g.fill3DRect(Bat.x, Bat.y, Bat.width, Bat.height, true);
g.setColor(Color.GRAY);
g.fillRect(0, 251, 450, 200);
g.setColor(Color.red);
g.drawRect(0, 0, 343, 250);
for (int i = 0; i < Brick.length; i++) {
if (Brick[i] != null) {
g.fill3DRect(Brick[i].x, Brick[i].y, Brick[i].width,
Brick[i].height, true);
}
}
if (ballFallDown == true || bricksOver == true) {
Font f = new Font("Arial", Font.BOLD, 20);
g.setFont(f);
g.drawString(status, 70, 120);
ballFallDown = false;
bricksOver = false;
}
}
// /...Game Loop...................
// /////////////////// When ball strikes borders......... it
public void run() {
// //////////// =====Creating bricks for the game===>.....
createBricks();
// ===========BRICKS created for the game new ready to use===
// ====================================================
// == ball reverses when touches the brick=======
//ballFallDown == false && bricksOver == false
while (true) {
// if(gameOver == true){return;}
for (int i = 0; i < Brick.length; i++) {
if (Brick[i] != null) {
if (Brick[i].intersects(Ball)) {
Brick[i] = null;
// movex = -movex;
movey = -movey;
count++;
}// end of 2nd if..
}// end of 1st if..
}// end of for loop..
// /////////// =================================
if (count == Brick.length) {// check if ball hits all bricks
bricksOver = true;
status = "YOU WON THE GAME";
repaint();
}
// /////////// =================================
repaint();
Ball.x += movex;
Ball.y += movey;
if (left == true) {
Bat.x -= 3;
right = false;
}
if (right == true) {
Bat.x += 3;
left = false;
}
if (Bat.x <= 4) {
Bat.x = 4;
} else if (Bat.x >= 298) {
Bat.x = 298;
}
// /===== Ball reverses when strikes the bat
if (Ball.intersects(Bat)) {
movey = -movey;
// if(Ball.y + Ball.width >=Bat.y)
}
// //=====================================
// ....ball reverses when touches left and right boundary
if (Ball.x <= 0 || Ball.x + Ball.height >= 343) {
movex = -movex;
}// if ends here
if (Ball.y <= 0) {// ////////////////|| bally + Ball.height >= 250
movey = -movey;
}// if ends here.....
if (Ball.y >= 250) {// when ball falls below bat game is over...
ballFallDown = true;
status = "YOU LOST THE GAME";
repaint();
// System.out.print("game");
}
try {
Thread.sleep(10);
} catch (Exception ex) {
}// try catch ends here
}// while loop ends here
}
// loop ends here
// ///////..... HANDLING KEY EVENTS................//
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left = true;
// System.out.print("left");
}
if (keyCode == KeyEvent.VK_RIGHT) {
right = true;
// System.out.print("right");
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left = false;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("restart")) {
this.restart();
}
}
public void restart() {
requestFocus(true);
initializeVariables();
createBricks();
repaint();
}
public void initializeVariables(){
// ..............
// variables declaration for ball.................................
ballx = 160;
bally = 218;
// variables declaration for ball.................................
// ===============================================================
// variables declaration for bat..................................
batx = 160;
baty = 245;
// variables declaration for bat..................................
// ===============================================================
// variables declaration for brick...............................
brickx = 70;
bricky = 50;
// variables declaration for brick...............................
// ===============================================================
// declaring ball, paddle,bricks
Ball = new Rectangle(ballx, bally, 5, 5);
Bat = new Rectangle(batx, baty, 40, 5);
// Rectangle Brick;// = new Rectangle(brickx, bricky, 30, 10);
Brick = new Rectangle[12];
movex = -1;
movey = -1;
ballFallDown = false;
bricksOver = false;
count = 0;
status = null;
}
public void createBricks(){
// //////////// =====Creating bricks for the game===>.....
/*
* creating bricks again because this for loop is out of while loop in
* run method
*/
for (int i = 0; i < Brick.length; i++) {
Brick[i] = new Rectangle(brickx, bricky, brickBreadth, brickHeight);
if (i == 5) {
brickx = 70;
bricky = (bricky + brickHeight + 2);
}
if (i == 9) {
brickx = 100;
bricky = (bricky + brickHeight + 2);
}
brickx += (brickBreadth+1);
}
}
}