-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBrickBreaker.java
More file actions
297 lines (251 loc) · 8.18 KB
/
BrickBreaker.java
File metadata and controls
297 lines (251 loc) · 8.18 KB
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
/*
BrickBreaker.java
A simple Brick Breaker (Breakout) GUI game using Java Swing.
Controls:
- LEFT/RIGHT arrow keys to move the paddle
- ENTER to restart when game is over or won
How to compile & run:
1. Save this file as BrickBreaker.java
2. javac BrickBreaker.java
3. java BrickBreaker
Features:
- Ball physics (basic reflection)
- Paddle controlled by keyboard
- Brick grid with collision detection
- Score and lives
- Pause / Restart support
Author: Generated by ChatGPT for Strange (Pradyumn)
Date: 2025-10-11
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BrickBreaker extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 48;
private Timer timer;
private int delay = 8; // milliseconds
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int ballXDir = -1;
private int ballYDir = -2;
private int lives = 3;
private MapGenerator map;
public BrickBreaker() {
map = new MapGenerator(6, 8); // rows, cols
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
// drawing map
map.draw((Graphics2D) g);
// borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// scores
g.setColor(Color.white);
g.setFont(new Font("Serif", Font.BOLD, 25));
g.drawString("Score: " + score, 540, 30);
g.drawString("Lives: " + lives, 40, 30);
// the paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
// the ball
g.setColor(Color.yellow);
g.fillOval(ballposX, ballposY, 20, 20);
// game over
if (lives <= 0) {
play = false;
ballXDir = 0;
ballYDir = 0;
g.setColor(Color.red);
g.setFont(new Font("Serif", Font.BOLD, 30));
g.drawString("Game Over, Scores: " + score, 190, 300);
g.setFont(new Font("Serif", Font.BOLD, 20));
g.drawString("Press Enter to Restart", 230, 340);
}
// win condition
if (totalBricks <= 0) {
play = false;
ballXDir = 0;
ballYDir = 0;
g.setColor(Color.GREEN);
g.setFont(new Font("Serif", Font.BOLD, 30));
g.drawString("You Won! Scores: " + score, 190, 300);
g.setFont(new Font("Serif", Font.BOLD, 20));
g.drawString("Press Enter to Restart", 230, 340);
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (play) {
// ball and paddle intersection
Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
Rectangle paddleRect = new Rectangle(playerX, 550, 100, 8);
if (ballRect.intersects(paddleRect)) {
ballYDir = -ballYDir;
// tweak ball X direction based on where it hits the paddle
int hitPoint = ballposX - (playerX + 50);
ballXDir = hitPoint / 10;
if (ballXDir == 0) ballXDir = (Math.random() > 0.5) ? 1 : -1;
}
A: for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j < map.map[0].length; j++) {
if (map.map[i][j] > 0) {
int brickX = j * map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickHeight = map.brickHeight;
Rectangle brickRect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j);
totalBricks--;
score += 5;
// ball collision direction
if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) {
ballXDir = -ballXDir;
} else {
ballYDir = -ballYDir;
}
break A;
}
}
}
}
ballposX += ballXDir;
ballposY += ballYDir;
if (ballposX < 0) {
ballXDir = -ballXDir;
}
if (ballposY < 0) {
ballYDir = -ballYDir;
}
if (ballposX > 670) {
ballXDir = -ballXDir;
}
// ball falls below paddle
if (ballposY > 570) {
lives--;
if (lives > 0) {
resetBallAndPaddle();
play = false; // pause until user moves
} else {
play = false;
}
}
}
repaint();
}
private void resetBallAndPaddle() {
ballposX = 120;
ballposY = 350;
ballXDir = -1;
ballYDir = -2;
playerX = 310;
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
} else {
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) {
playerX = 10;
} else {
moveLeft();
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (!play) {
if (lives <= 0 || totalBricks <= 0) {
restartGame();
} else {
play = true;
}
}
}
}
public void moveRight() {
play = true;
playerX += 20;
}
public void moveLeft() {
play = true;
playerX -= 20;
}
private void restartGame() {
play = true;
ballposX = 120;
ballposY = 350;
ballXDir = -1;
ballYDir = -2;
playerX = 310;
score = 0;
lives = 3;
totalBricks = 48;
map = new MapGenerator(6, 8);
repaint();
}
public static void main(String[] args) {
JFrame obj = new JFrame();
BrickBreaker gamePlay = new BrickBreaker();
obj.setBounds(10, 10, 700, 600);
obj.setTitle("Brick Breaker - Strange");
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(gamePlay);
obj.setVisible(true);
}
}
class MapGenerator {
public int[][] map;
public int brickWidth;
public int brickHeight;
public MapGenerator(int row, int col) {
map = new int[row][col];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
map[i][j] = 1;
}
}
brickWidth = 540 / col;
brickHeight = 150 / row;
}
public void draw(Graphics2D g) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if (map[i][j] > 0) {
g.setColor(Color.white);
g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
// brick border
g.setStroke(new BasicStroke(3));
g.setColor(Color.black);
g.drawRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
}
}
}
}
public void setBrickValue(int value, int row, int col) {
map[row][col] = value;
}
}