-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrickGame.java
176 lines (146 loc) · 4.81 KB
/
BrickGame.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
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BrickGame extends JFrame {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int PADDLE_WIDTH = 100;
private static final int PADDLE_HEIGHT = 20;
private static final int BALL_SIZE = 20;
private static final int BRICK_WIDTH = 60;
private static final int BRICK_HEIGHT = 20;
private static final int NUM_BRICKS = 30;
private int paddleX;
private int ballX, ballY;
private int ballSpeedX, ballSpeedY;
private boolean gameRunning;
private Brick[] bricks;
private Timer timer;
public BrickGame() {
setTitle("Brick Breaker");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
paddleX = WIDTH / 2 - PADDLE_WIDTH / 2;
ballX = WIDTH / 2 - BALL_SIZE / 2;
ballY = HEIGHT / 2 - BALL_SIZE / 2;
ballSpeedX = 3;
ballSpeedY = -3;
gameRunning = true;
bricks = new Brick[NUM_BRICKS];
for (int i = 0; i < NUM_BRICKS; i++) {
int brickX = i % 10 * (BRICK_WIDTH + 2) + 2;
int brickY = i / 10 * (BRICK_HEIGHT + 2) + 50;
bricks[i] = new Brick(brickX, brickY);
}
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
handleKeyPress(e);
}
});
timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
});
}
private void handleKeyPress(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
paddleX -= 10;
} else if (key == KeyEvent.VK_RIGHT) {
paddleX += 10;
}
}
private void update() {
if (!gameRunning) {
return;
}
ballX += ballSpeedX;
ballY += ballSpeedY;
// 벽과의 충돌 감지
if (ballX <= 0 || ballX >= WIDTH - BALL_SIZE) {
ballSpeedX = -ballSpeedX;
}
if (ballY <= 0) {
ballSpeedY = -ballSpeedY;
}
// 패들과의 충돌 감지
if (ballY + BALL_SIZE >= HEIGHT - PADDLE_HEIGHT && ballX + BALL_SIZE >= paddleX && ballX <= paddleX + PADDLE_WIDTH) {
ballSpeedY = -ballSpeedY;
}
// 벽돌과의 충돌 감지
for (int i = 0; i < NUM_BRICKS; i++) {
Brick brick = bricks[i];
if (brick != null) {
int brickX = brick.getX();
int brickY = brick.getY();
if (ballY <= brickY + BRICK_HEIGHT && ballX + BALL_SIZE >= brickX && ballX <= brickX + BRICK_WIDTH) {
ballSpeedY = -ballSpeedY;
bricks[i] = null;
break; // 한 번에 하나의 벽돌만 충돌 처리
}
}
}
// 게임 오버 처리
if (ballY >= HEIGHT) {
gameRunning = false;
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
// 배경 그리기
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
// 패들 그리기
g.setColor(Color.WHITE);
g.fillRect(paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
// 공 그리기
g.setColor(Color.WHITE);
g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
// 벽돌 그리기
for (int i = 0; i < NUM_BRICKS; i++) {
Brick brick = bricks[i];
if (brick != null) {
int brickX = brick.getX();
int brickY = brick.getY();
g.setColor(Color.RED);
g.fillRect(brickX, brickY, BRICK_WIDTH, BRICK_HEIGHT);
}
}
}
public void startGame() {
setVisible(true);
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BrickGame game = new BrickGame();
game.startGame();
}
});
}
}
class Brick {
private int x;
private int y;
public Brick(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}