forked from Nikunj-Gupta/Brick-Breaker-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrickBreaker.java
executable file
·214 lines (187 loc) · 4.71 KB
/
BrickBreaker.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
//package code;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyListener;
import java.awt.BorderLayout;
import java.awt.Rectangle;
public class BrickBreaker extends JPanel implements KeyListener,ActionListener, Runnable {
int velocity_x = -1;
int velocity_y = -1;
boolean below_paddle_level = false;
boolean bricks_finished = false;
int count = 0;
String status;
private static final long serialVersionUID = 1L;
static boolean left_key = false;
static boolean right_key = false;
public int ball_x = 160;
public int ball_y = 218;
int paddle_x = 160;
int paddle_y = 245;
int brick_x = 70;
int brick_y = 50;
Rectangle Ball = new Rectangle(ball_x, ball_y, 5, 5);
Rectangle Bat = new Rectangle(paddle_x, paddle_y, 40, 20);
Rectangle[] Brick = new Rectangle[12];
BrickBreaker()
{
}
public static void main(String[] args) {
JFrame frame = new JFrame();
BrickBreaker game = new BrickBreaker();
JButton button = new JButton("Start/Restart");
JButton nextlevel = new JButton("Next Level");
frame.setSize(350, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.add(button , BorderLayout.SOUTH);
frame.add(nextlevel , BorderLayout.NORTH);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
button.addActionListener(game);
nextlevel.addActionListener(game);
game.addKeyListener(game);
game.setFocusable(true);
Thread t = new Thread(game);
t.start();
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 350, 450);
g.setColor(Color.blue);
g.fillOval(Ball.x, Ball.y, Ball.width, Ball.height);
g.setColor(Color.WHITE);
g.fill3DRect(Bat.x, Bat.y, Bat.width, Bat.height, true);
g.setColor(Color.GRAY);
g.fillRect(0, 251, 450, 200);
g.setColor(Color.GREEN);
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 (below_paddle_level == true || bricks_finished == true) {
Font f = new Font("Arial", Font.BOLD, 20);
g.setFont(f);
g.drawString(status, 70, 120);
below_paddle_level = false;
bricks_finished = false;
}
}
public void run() {
for (int i = 0; i < Brick.length; i++) {
Brick[i] = new Rectangle(brick_x, brick_y, 30, 10);
if (i == 5) {
brick_x = 70;
brick_y = 62;
}
if (i == 9) {
brick_x = 100;
brick_y = 74;
}
brick_x += 31;
}
while (true) {
for (int i = 0; i < Brick.length; i++) {
if (Brick[i] != null) {
if (Brick[i].intersects(Ball)) {
Brick[i] = null;
//velocity_x = -velocity_x;
velocity_y = -velocity_y;
count++;
}
}
}
if (count == Brick.length) {
bricks_finished = true;
status = "YOU WON THE GAME";
repaint();
}
repaint();
Ball.x += velocity_x;
Ball.y += velocity_y;
if (left_key == true) {
Bat.x -= 3;
right_key = false;
}
if (right_key == true) {
Bat.x += 3;
left_key = false;
}
if (Bat.x <= 4) {
Bat.x = 4;
} else if (Bat.x >= 298) {
Bat.x = 298;
}
if (Ball.intersects(Bat)) {
velocity_y = -velocity_y;
}
if (Ball.x <= 0 || Ball.x + Ball.height >= 343) {
velocity_x = -velocity_x;
}
if (Ball.y <= 0) {
velocity_y = -velocity_y;
}
if (Ball.y >= 250) {
below_paddle_level = true;
status = "YOU LOST THE GAME";
repaint();
}
try {
Thread.sleep(10);
}
catch (Exception ex) {
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("Start/Restart")) {
levels.restart(this);
}
if(str.equals("Next Level"))
{
try {
Thread.sleep(100);
}
catch (Exception ex)
{
}
levels.nextlevel(this);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left_key = true;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right_key = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left_key = false;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right_key = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
}