-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallGame.java
More file actions
211 lines (176 loc) · 6.83 KB
/
Copy pathBallGame.java
File metadata and controls
211 lines (176 loc) · 6.83 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class DodgeGame extends JPanel implements ActionListener {
// Game Settings
private final int WIDTH = 600;
private final int HEIGHT = 600;
private final int PLAYER_SIZE = 30;
private final int OBSTACLE_SIZE = 40;
// Player State
private int playerX = 285;
private int playerY = 500;
private int playerSpeed = 8;
// Movement Flags (for smooth control)
private boolean up = false;
private boolean down = false;
private boolean left = false;
private boolean right = false;
// Game Logic
private Timer timer;
private ArrayList<Rectangle> obstacles; // List to hold many obstacles
private Random random;
private int score = 0;
private int obstacleSpeed = 5; // Initial speed of obstacles
private boolean isGameOver = false;
public DodgeGame() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.setBackground(Color.BLACK);
this.setFocusable(true);
obstacles = new ArrayList<>();
random = new Random();
// Timer runs every 20ms (approx 50 frames per second)
timer = new Timer(20, this);
timer.start();
// Key Listener for Smooth Movement
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) left = true;
if (key == KeyEvent.VK_RIGHT) right = true;
if (key == KeyEvent.VK_UP) up = true;
if (key == KeyEvent.VK_DOWN) down = true;
// Restart game if Enter is pressed after game over
if (isGameOver && key == KeyEvent.VK_ENTER) {
resetGame();
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) left = false;
if (key == KeyEvent.VK_RIGHT) right = false;
if (key == KeyEvent.VK_UP) up = false;
if (key == KeyEvent.VK_DOWN) down = false;
}
});
}
// Ensures the panel captures key inputs immediately
@Override
public void addNotify() {
super.addNotify();
requestFocusInWindow();
}
private void resetGame() {
playerX = 285;
playerY = 500;
obstacles.clear();
score = 0;
obstacleSpeed = 5;
isGameOver = false;
timer.start();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Anti-aliasing for smoother shapes
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (isGameOver) {
drawGameOver(g);
} else {
// Draw Player (Green Square)
g.setColor(Color.GREEN);
g.fillRect(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
// Draw Player Border
g.setColor(Color.WHITE);
g.drawRect(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
// Draw Obstacles (Red Squares)
g.setColor(Color.RED);
for (Rectangle r : obstacles) {
g.fillRect(r.x, r.y, r.width, r.height);
}
// Draw Score
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 18));
g.drawString("Score: " + score, 10, 25);
g.drawString("Speed: " + obstacleSpeed, 10, 45);
}
}
private void drawGameOver(Graphics g) {
String msg = "Game Over!";
String scoreMsg = "Final Score: " + score;
String restartMsg = "Press ENTER to Restart";
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 40));
// Center the text
FontMetrics fm = g.getFontMetrics();
g.drawString(msg, (WIDTH - fm.stringWidth(msg)) / 2, HEIGHT / 2 - 50);
g.setFont(new Font("Arial", Font.BOLD, 25));
fm = g.getFontMetrics();
g.drawString(scoreMsg, (WIDTH - fm.stringWidth(scoreMsg)) / 2, HEIGHT / 2);
g.setFont(new Font("Arial", Font.PLAIN, 18));
fm = g.getFontMetrics();
g.drawString(restartMsg, (WIDTH - fm.stringWidth(restartMsg)) / 2, HEIGHT / 2 + 50);
}
@Override
public void actionPerformed(ActionEvent e) {
if (isGameOver) return;
// 1. Move Player
if (left) playerX -= playerSpeed;
if (right) playerX += playerSpeed;
if (up) playerY -= playerSpeed;
if (down) playerY += playerSpeed;
// Keep player inside screen bounds
if (playerX < 0) playerX = 0;
if (playerX > WIDTH - PLAYER_SIZE) playerX = WIDTH - PLAYER_SIZE;
if (playerY < 0) playerY = 0;
if (playerY > HEIGHT - PLAYER_SIZE) playerY = HEIGHT - PLAYER_SIZE;
// 2. Add New Obstacles
// Chance to spawn obstacle increases slightly as game goes on, but capped
if (random.nextInt(100) < 5) {
int randX = random.nextInt(WIDTH - OBSTACLE_SIZE);
obstacles.add(new Rectangle(randX, -OBSTACLE_SIZE, OBSTACLE_SIZE, OBSTACLE_SIZE));
}
// 3. Move Obstacles & Check Collisions
Rectangle playerRect = new Rectangle(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
// We use a loop to manage obstacles
for (int i = 0; i < obstacles.size(); i++) {
Rectangle obs = obstacles.get(i);
obs.y += obstacleSpeed; // Move obstacle down
// Check Collision
if (playerRect.intersects(obs)) {
isGameOver = true;
timer.stop();
}
// Remove obstacle if it goes off screen
if (obs.y > HEIGHT) {
obstacles.remove(i);
i--; // adjust index since we removed an item
score++;
// Increase difficulty every 10 points
if (score % 10 == 0) {
obstacleSpeed++;
}
}
}
repaint(); // Update screen
}
public static void main(String[] args) {
JFrame frame = new JFrame("4-Way Dodge Survival");
DodgeGame game = new DodgeGame();
frame.add(game);
frame.pack(); // Adjusts window to fit the panel size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
}