-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreakoutGame.java
More file actions
229 lines (196 loc) · 7.71 KB
/
Copy pathBreakoutGame.java
File metadata and controls
229 lines (196 loc) · 7.71 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BreakoutGame extends JPanel implements KeyListener, ActionListener {
private static final int WIDTH = 900;
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_DIAMETER = 20;
private static final int BRICK_WIDTH = 70;
private static final int BRICK_HEIGHT = 20;
private static final int PADDLE_SPEED = 15;
private static final int BALL_SPEED = 10;
private static final int NUM_BRICKS = 105;
private static final int BRICK_ROWS = 6;
private static final int BRICK_COLS = 9;
private int paddleX = WIDTH / 2 - PADDLE_WIDTH / 2;
private int ballX = WIDTH / 2 - BALL_DIAMETER / 2;
private int ballY = HEIGHT - PADDLE_HEIGHT - BALL_DIAMETER;
private int ballDeltaX = BALL_SPEED;
private int ballDeltaY = -BALL_SPEED;
private int score = 0;
private boolean gameOver = false;
private boolean gameStarted = false;
private String playerName = "";
private boolean[] keysPressed = new boolean[KeyEvent.KEY_LAST];
private Rectangle paddle = new Rectangle(paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
private Rectangle ball = new Rectangle(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);
private Rectangle[] bricks = new Rectangle[NUM_BRICKS];
private JButton restartButton;
private JButton quitButton;
private JButton startButton;
private JLabel scoreLabel;
private JTextField nameTextField;
public BreakoutGame() {
addKeyListener(this);
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
initializeBricks();
startButton = new JButton("Start");
startButton.addActionListener(this);
add(startButton);
restartButton = new JButton("Restart");
restartButton.addActionListener(this);
add(restartButton);
quitButton = new JButton("Quit");
quitButton.addActionListener(this);
add(quitButton);
scoreLabel = new JLabel("Your Score: " + score);
scoreLabel.setFont(new Font("Sourcecodepro", Font.BOLD, 20));
scoreLabel.setForeground(Color.WHITE);
scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(scoreLabel);
nameTextField = new JTextField("Enter your name");
nameTextField.addActionListener(this);
add(nameTextField);
}
private void initializeBricks() {
int gap = 5; // Set the gap between bricks
int currentX = 0;
int currentY = 50;
for (int i = 0; i < NUM_BRICKS; i++) {
bricks[i] = new Rectangle(currentX, currentY, BRICK_WIDTH, BRICK_HEIGHT);
currentX += BRICK_WIDTH + gap; // Add the gap to the x-coordinate
if (currentX >= WIDTH) {
currentX = 0;
currentY += BRICK_HEIGHT + gap; // Add the gap to the y-coordinate
}
}
}
private void update() {
if (!gameOver && gameStarted) {
movePaddle();
moveBall();
checkCollisions();
checkGameOver();
}
}
private void movePaddle() {
if (keysPressed[KeyEvent.VK_LEFT] && paddleX > 0) {
paddleX -= PADDLE_SPEED;
}
if (keysPressed[KeyEvent.VK_RIGHT] && paddleX < WIDTH - PADDLE_WIDTH) {
paddleX += PADDLE_SPEED;
}
paddle.setLocation(paddleX, HEIGHT - PADDLE_HEIGHT);
}
private void moveBall() {
ball.x += ballDeltaX;
ball.y += ballDeltaY;
if (ball.x <= 0 || ball.x >= WIDTH - BALL_DIAMETER) {
ballDeltaX *= -1;
}
if (ball.y <= 0) {
ballDeltaY *= -1;
}
}
private void checkCollisions() {
if (paddle.intersects(ball)) {
ballDeltaY *= -1;
}
for (Rectangle brick : bricks) {
if (brick != null && brick.intersects(ball)) {
brick.setLocation(0, 0);
score += 5;
ballDeltaY *= -1;
}
}
scoreLabel.setText("Your Score: " + score);
}
private void checkGameOver() {
if (ball.y >= HEIGHT - BALL_DIAMETER) {
gameOver = true;
restartButton.setEnabled(true); // Enable restart button when the game is over
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(48, 0, 74));
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(new Color(184, 51, 255));
g.fillRect(paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
g.setColor(new Color(184, 51, 255)); // Change ball color to blue
g.fillOval(ball.x, ball.y, BALL_DIAMETER, BALL_DIAMETER);
g.setColor(Color.RED); // Change brick outline color to red
for (Rectangle brick : bricks) {
if (brick != null) {
g.setColor(new Color(219, 153, 255)); // Change brick inside color to purple
g.fillRect(brick.x, brick.y, BRICK_WIDTH, BRICK_HEIGHT);
g.setColor(new Color(219, 153, 255)); // Resetting color to draw outline
g.drawRect(brick.x, brick.y, BRICK_WIDTH, BRICK_HEIGHT);
}
}
if (gameOver) {
g.setColor(Color.WHITE);
g.setFont(new Font("Sourcecodepro", Font.BOLD, 30));
g.drawString("Game Over! " + playerName + "'s Score: " + score, WIDTH / 2 - 150, HEIGHT / 2);
}
g.setColor(Color.WHITE);
g.drawString("Player: " + playerName, 10, 20);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == restartButton) {
restartGame();
} else if (e.getSource() == quitButton) {
System.exit(0);
} else if (e.getSource() == nameTextField) {
playerName = nameTextField.getText();
nameTextField.setEditable(false);
nameTextField.setFocusable(false);
} else if (e.getSource() == startButton) {
startGame();
}
}
public void startGame() {
gameStarted = true;
startButton.setEnabled(false); // Disable start button when the game starts
requestFocus(); // Focus on the game panel
}
public void restartGame() {
score = 0;
gameOver = false;
ball.x = paddleX + PADDLE_WIDTH / 2 - BALL_DIAMETER / 2; // Reset ball position onto the paddle
ball.y = HEIGHT - PADDLE_HEIGHT - BALL_DIAMETER;
restartButton.setEnabled(false); // Disable restart button when game restarts
initializeBricks();
requestFocusInWindow(); // Request focus on the game panel after restarting
}
@Override
public void keyPressed(KeyEvent e) {
keysPressed[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keysPressed[e.getKeyCode()] = false;
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Breakout Game");
BreakoutGame game = new BreakoutGame();
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(1000 / 60, e -> {
game.update();
game.repaint();
});
timer.start();
}
}