-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrickBreaker.java
More file actions
205 lines (168 loc) · 6.11 KB
/
Copy pathBrickBreaker.java
File metadata and controls
205 lines (168 loc) · 6.11 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
package brickBreacker;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;
class Gameplay extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 50;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private int ballposX = 370;
private int ballposY = 525;
private int ballXdir = -1;
private int ballYdir = -2;
private MapGenerator map;
private Image backgroundImage; // Variable to hold the background image
public Gameplay() {
map = new MapGenerator(5,10);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
// Load the background image
backgroundImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("image.jpg")); // Adjust path as necessary
}
public void paint(Graphics g) {
// Draw the background image
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
// Draw the bricks
map.draw((Graphics2D) g);
// Scores
g.setColor(Color.cyan);
g.setFont(new Font("Times New Roman", Font.BOLD, 30));
g.drawString("Score: " + score, 630, 30);
// Borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, getHeight());
g.fillRect(0, 0, getWidth(), 3);
g.fillRect(getWidth() - 3, 0, 3, getHeight());
// The paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 150, 10);
// The ball
g.setColor(Color.red);
g.fillOval(ballposX, ballposY, 25, 25);
// Winning and game over messages
if (totalBricks <= 0) {
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.red);
g.setFont(new Font("Times New Roman", Font.BOLD, 30));
g.drawString("Congrats You Won", 250, 300);
g.setFont(new Font("Times New Roman", Font.BOLD, 20));
g.drawString("Press Enter To Restart", 200, 350);
}
if (ballposY > 570) {
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.red);
g.setFont(new Font("Times New Roman", Font.BOLD, 30));
g.drawString("Game Over, score: " + score, 190, 300);
g.setFont(new Font("Times New Roman", Font.BOLD, 20));
g.drawString("Press Enter To Restart", 240, 350);
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (play) {
// Ball and paddle collision
if (new Rectangle(ballposX, ballposY, 25, 25).intersects(new Rectangle(playerX, 550, 120, 8))) {
ballYdir = -ballYdir;
}
// Ball and bricks collision
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;
Rectangle rect = new Rectangle(brickX, brickY, map.brickWidth, map.brickHeight);
Rectangle ballRect = new Rectangle(ballposX, ballposY, 25, 25);
if (ballRect.intersects(rect)) {
map.setBrickValue(0, i, j);
totalBricks--;
score += 10;
// Ball collision logic
if (ballposX + 25 <= rect.x || ballposX >= rect.x + rect.width) {
ballXdir = -ballXdir; // Change direction on horizontal collision
} else {
ballYdir = -ballYdir; // Change direction on vertical collision
}
break A;
}
}
}
}
// Move the ball
ballposX += ballXdir;
ballposY += ballYdir;
// Wall collision
if (ballposX < 0) {
ballXdir = -ballXdir;
}
if (ballposY < 0) {
ballYdir = -ballYdir;
}
if (ballposX > 740) {
ballXdir = -ballXdir;
}
}
repaint();
}
@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 = 630; // Prevent going out of bounds
} else {
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) {
playerX = 10; // Prevent going out of bounds
} else {
moveLeft();
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (!play) {
play = true;
resetGame();
repaint();
}
}
}
private void resetGame() {
ballposX = 120;
ballposY = 350;
ballXdir = -1;
ballYdir = -2;
playerX = 310;
score = 0;
totalBricks = 50; // Set total bricks for your game
map = new MapGenerator(5, 10); // Re-initialize the map
}
public void moveRight() {
play = true;
playerX += 20; // Move right
}
public void moveLeft() {
play = true;
playerX -= 20; // Move left
}
}