-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.java
More file actions
342 lines (291 loc) · 11.6 KB
/
GameEngine.java
File metadata and controls
342 lines (291 loc) · 11.6 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
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
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Graphics2D;
import java.awt.Font;
import javax.sound.sampled.*;
import java.io.ByteArrayInputStream;
public class GameEngine extends JPanel implements KeyListener, ActionListener {
public enum GameState {
TITLE_SCREEN,
PLAYING,
GAME_OVER,
LEVEL_COMPLETE,
VICTORY
}
private GameState currentState = GameState.TITLE_SCREEN;
private int playerScore = 0;
private int currentLevel = 1;
private int maxLevel = 3;
private Timer gameTimer;
private int timerDelay = 8;
private int paddleX = 310;
// Ball
private int ballX = 120;
private int ballY = 350;
private int ballXVelocity = -2;
private int ballYVelocity = -3;
private BrickField brickField;
private int remainingBricks;
public GameEngine() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
gameTimer = new Timer(timerDelay, this);
gameTimer.start();
}
// Sound
private void playBrickHitSound() {
try {
// brick hit
byte[] buf = new byte[1000];
float frequency = 800.0f; // Higher frequency for brick hit
for (int i = 0; i < buf.length; i++) {
double angle = i / (44100.0 / frequency) * 2.0 * Math.PI;
buf[i] = (byte) (Math.sin(angle) * 127.0 * Math.exp(-3.0 * i / buf.length));
}
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
AudioFormat af = new AudioFormat(44100, 8, 1, true, false);
AudioInputStream ais = new AudioInputStream(bais, af, buf.length);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
} catch (Exception e) {
// Sound failed, continue silently
}
}
private void playPaddleHitSound() {
try {
// paddle hit
byte[] buf = new byte[800];
float frequency = 400.0f; // Lower frequency for paddle hit
for (int i = 0; i < buf.length; i++) {
double angle = i / (44100.0 / frequency) * 2.0 * Math.PI;
buf[i] = (byte) (Math.sin(angle) * 127.0 * Math.exp(-2.0 * i / buf.length));
}
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
AudioFormat af = new AudioFormat(44100, 8, 1, true, false);
AudioInputStream ais = new AudioInputStream(bais, af, buf.length);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
} catch (Exception e) {
// Sound failed, continue silently
}
}
private void startGame() {
currentState = GameState.PLAYING;
playerScore = 0;
currentLevel = 1;
startLevel();
}
private void startLevel() {
brickField = new BrickField(3 + currentLevel/2, 7);
remainingBricks = countBricks();
// Reset ball
ballX = 120;
ballY = 350;
ballXVelocity = -2 - currentLevel/3;
ballYVelocity = -3 - currentLevel/4;
paddleX = 310;
}
private int countBricks() {
int count = 0;
for (int i = 0; i < brickField.brickGrid.length; i++) {
for (int j = 0; j < brickField.brickGrid[0].length; j++) {
if (brickField.brickGrid[i][j] > 0) count++;
}
}
return count;
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// Background gradient
Color bgTop = new Color(15, 15, 25);
Color bgBottom = new Color(25, 25, 35);
g2d.setPaint(new java.awt.GradientPaint(0, 0, bgTop, 0, 592, bgBottom));
g2d.fillRect(0, 0, 692, 592);
switch (currentState) {
case TITLE_SCREEN:
drawTitleScreen(g);
break;
case PLAYING:
drawGameplay(g, g2d);
break;
case LEVEL_COMPLETE:
drawLevelComplete(g);
break;
case VICTORY:
drawVictory(g);
break;
case GAME_OVER:
drawGameOver(g);
break;
}
g.dispose();
}
private void drawTitleScreen(Graphics g) {
// Title
g.setColor(new Color(139, 0, 0));
g.setFont(new Font("Papyrus", Font.BOLD, 48));
g.drawString("BRICK BUSTER", 180, 200);
// Menu options
g.setColor(new Color(255, 206, 100));
g.setFont(new Font("Papyrus", Font.PLAIN, 24));
g.drawString("Press ENTER to Play", 250, 300);
g.drawString("Press ESC to Exit", 270, 340);
}
private void drawGameplay(Graphics g, Graphics2D g2d) {
// Draw bricks
brickField.draw(g2d);
// Borders
g.setColor(new Color(100, 20, 20));
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
// Score and level
g.setColor(new Color(255, 200, 100));
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Score: " + playerScore, 20, 30);
g.drawString("Level: " + currentLevel + "/3", 20, 55); // Show progress out of 3
// Paddle
Color paddleTop = new Color(180, 60, 60);
Color paddleBottom = new Color(120, 30, 30);
g2d.setPaint(new java.awt.GradientPaint(paddleX, 550, paddleTop, paddleX, 558, paddleBottom));
g2d.fillRect(paddleX, 550, 100, 8);
g.setColor(new Color(220, 100, 100));
g.fillRect(paddleX, 550, 100, 2);
// Enhanced ball with glow effect
g.setColor(new Color(255, 100, 100, 80)); // Glow
g.fillOval(ballX - 5, ballY - 5, 30, 30);
g.setColor(new Color(255, 150, 150)); // Main ball
g.fillOval(ballX, ballY, 20, 20);
g.setColor(new Color(255, 200, 200)); // Highlight
g.fillOval(ballX + 3, ballY + 3, 8, 8);
}
private void drawLevelComplete(Graphics g) {
drawGameplay(g, (Graphics2D) g);
g.setColor(new Color(100, 255, 100));
g.setFont(new Font("Papyrus", Font.BOLD, 36));
g.drawString("LEVEL COMPLETE!", 200, 280);
g.setFont(new Font("Papyrus", Font.PLAIN, 18));
g.drawString("Press ENTER for next level", 240, 320);
}
private void drawVictory(Graphics g) {
g.setColor(new Color(255, 215, 0));
g.setFont(new Font("Papyrus", Font.BOLD, 48));
g.drawString("YOU WON!! :)", 200, 280);
g.setFont(new Font("Papyrus", Font.PLAIN, 24));
g.drawString("Final Score: " + playerScore, 240, 320);
g.setFont(new Font("Papyrus", Font.PLAIN, 18));
g.drawString("All 3 levels completed!", 250, 350);
g.drawString("Press ENTER to restart", 250, 380);
}
private void drawGameOver(Graphics g) {
g.setColor(new Color(220, 50, 50));
g.setFont(new Font("Papyrus", Font.BOLD, 48));
g.drawString("YOU LOST", 240, 280);
g.setFont(new Font("Papyrus", Font.PLAIN, 18));
g.drawString("Press ENTER to restart", 250, 360);
}
@Override
public void actionPerformed(ActionEvent e) {
if (currentState == GameState.PLAYING) {
// Ball movement
ballX += ballXVelocity;
ballY += ballYVelocity;
// Wall collisions
if (ballX < 0 || ballX > 670) {
ballXVelocity = -ballXVelocity;
}
if (ballY < 0) {
ballYVelocity = -ballYVelocity;
}
// Paddle collision with sound
if (new Rectangle(ballX, ballY, 20, 20).intersects(new Rectangle(paddleX, 550, 100, 8))) {
ballYVelocity = -Math.abs(ballYVelocity);
playPaddleHitSound(); // Play sound when ball hits paddle
}
// Brick collision
checkBrickCollisions();
// Check win/lose conditions
if (ballY > 570) {
currentState = GameState.GAME_OVER;
} else if (remainingBricks <= 0) {
if (currentLevel >= maxLevel) {
currentState = GameState.VICTORY;
} else {
currentState = GameState.LEVEL_COMPLETE;
}
}
}
repaint();
}
private void checkBrickCollisions() {
outerLoop: for (int i = 0; i < brickField.brickGrid.length; i++) {
for (int j = 0; j < brickField.brickGrid[0].length; j++) {
if (brickField.brickGrid[i][j] > 0) {
int brickX = j * brickField.brickWidth + 80;
int brickY = i * brickField.brickHeight + 50;
Rectangle brickRect = new Rectangle(brickX, brickY, brickField.brickWidth, brickField.brickHeight);
Rectangle ballRect = new Rectangle(ballX, ballY, 20, 20);
if (ballRect.intersects(brickRect)) {
brickField.setBrickValue(0, i, j);
remainingBricks--;
playerScore += 10;
playBrickHitSound(); // Play sound when ball hits brick
// Ball direction change
if (ballX + 19 <= brickRect.x || ballX + 1 >= brickRect.x + brickRect.width) {
ballXVelocity = -ballXVelocity;
} else {
ballYVelocity = -ballYVelocity;
}
break outerLoop;
}
}
}
}
}
@Override
public void keyPressed(KeyEvent e) {
switch (currentState) {
case TITLE_SCREEN:
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
startGame();
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
break;
case PLAYING:
if (e.getKeyCode() == KeyEvent.VK_RIGHT && paddleX < 600) {
paddleX += 20;
} else if (e.getKeyCode() == KeyEvent.VK_LEFT && paddleX > 10) {
paddleX -= 20;
}
break;
case LEVEL_COMPLETE:
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
currentLevel++;
startLevel();
currentState = GameState.PLAYING;
}
break;
case GAME_OVER:
case VICTORY:
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
startGame();
}
break;
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}