-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarGame.java
More file actions
228 lines (189 loc) · 7.54 KB
/
CarGame.java
File metadata and controls
228 lines (189 loc) · 7.54 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
//java program to implement car game using JFrames//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class CarGame extends JPanel implements KeyListener, ActionListener {
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int LANE_WIDTH = 100;
private static final int CAR_WIDTH = 45; // Car width
private static final int CAR_HEIGHT = 100; // Car height
private static final int VEHICLE_SPEED_INCREMENT = 5;
private Timer timer;
private int playerX = 250;
private int playerY = 400;
private int speed = 15;
private int score = 0;
private boolean gameOver = false;
private Image playerCar;
private ArrayList<Vehicle> vehicles;
private Random random;
private int roadOffset = 0; // Variable to simulate the road movement
public CarGame() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.setBackground(new Color(76, 208, 56));
this.addKeyListener(this);
this.setFocusable(true);
// Load and scale player car image to fit within the lane width (45px width)
playerCar = new ImageIcon("C:\\Users\\kavur\\Desktop\\Screenshot 2024-11-02
193729.png").getImage();
playerCar = playerCar.getScaledInstance(CAR_WIDTH, CAR_HEIGHT,
Image.SCALE_SMOOTH);
vehicles = new ArrayList<>();
random = new Random();
// Game timer
timer = new Timer(1000 / 60, this); // 60 FPS
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Scroll the road effect (move the lanes downward)
roadOffset += speed; // Increase roadOffset to scroll downward
if (roadOffset >= 100) { // Reset road offset after it has scrolled down past the screen
roadOffset = 0;
}
// Draw road
g.setColor(Color.WHITE);
g.fillRect(100, 0 + roadOffset, 300, HEIGHT); // Road moves downward with roadOffset
g.fillRect(100, 0 + roadOffset - HEIGHT, 300, HEIGHT); // Duplicate road for continuous
effect
// Draw edge markers
g.setColor(Color.YELLOW);
g.fillRect(95, 0 + roadOffset, 10, HEIGHT); // Left edge marker moves downward
g.fillRect(395, 0 + roadOffset, 10, HEIGHT); // Right edge marker moves downward
g.fillRect(95, 0 + roadOffset - HEIGHT, 10, HEIGHT); // Left edge duplicate
g.fillRect(395, 0 + roadOffset - HEIGHT, 10, HEIGHT); // Right edge duplicate
// Draw lane markers
g.setColor(Color.BLACK);
for (int i = roadOffset - 50; i < HEIGHT; i += 100) {
g.fillRect(150, i, 10, 50);
g.fillRect(250, i, 10, 50);
g.fillRect(350, i, 10, 50);
}
// Draw player's car
g.drawImage(playerCar, playerX - CAR_WIDTH / 2, playerY - CAR_HEIGHT, CAR_WIDTH,
CAR_HEIGHT, this);
// Draw vehicles
for (Vehicle vehicle : vehicles) {
g.drawImage(vehicle.getImage(), vehicle.getX() - CAR_WIDTH / 2, vehicle.getY() -
CAR_HEIGHT, CAR_WIDTH, CAR_HEIGHT, this);
}
// Display score
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.PLAIN, 16));
g.drawString("SCORE: " + score, 20, HEIGHT - 20);
// Game over screen
if (gameOver) {
g.setColor(Color.RED);
g.fillRect(0, 50, WIDTH, 100);
g.setColor(Color.WHITE);
g.drawString("GAME OVER. Play Again? (Y/N)", WIDTH / 2 - 100, HEIGHT / 2);
}
}
@Override
public void keyPressed(KeyEvent e) {
if (gameOver) return;
if (e.getKeyCode() == KeyEvent.VK_LEFT && playerX > 150) {
playerX -= 100;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT && playerX < 350) {
playerX += 100;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void actionPerformed(ActionEvent e) {
if (gameOver) return;
// Move vehicles
for (int i = vehicles.size() - 1; i >= 0; i--) {
Vehicle vehicle = vehicles.get(i);
vehicle.setY(vehicle.getY() + speed);
if (vehicle.getY() > HEIGHT) {
vehicles.remove(i);
score++;
if (score % VEHICLE_SPEED_INCREMENT == 0) {
speed++; // Increase speed as score increases
}
}
// Check for collision with player
if (vehicle.getBounds().intersects(new Rectangle(playerX - CAR_WIDTH / 2, playerY -
CAR_HEIGHT, CAR_WIDTH, CAR_HEIGHT))) {
gameOver = true;
}
}
// Add new vehicles, ensuring no more than 1 vehicle on the screen at once
if (vehicles.size() < 1) { // Ensures only 1 vehicle can be on the screen at a time
int lane = random.nextInt(3); // Randomly choose a lane (0, 1, 2)
// Load and scale the vehicle image to fit in the lane
Image vehicleImage = new ImageIcon("C:\\Users\\kavur\\Desktop\\Screenshot 2024
11-02 193740.png").getImage();
vehicleImage = vehicleImage.getScaledInstance(CAR_WIDTH, CAR_HEIGHT,
Image.SCALE_SMOOTH);
// Add new vehicle to the screen at random lane
vehicles.add(new Vehicle(vehicleImage, 150 + lane * 100, -50));
}
repaint();
}
// Vehicle class for random vehicles
private class Vehicle {
private Image image;
private int x, y;
public Vehicle(Image image, int x, int y) {
this.image = image;
this.x = x;
this.y = y;
}
public Image getImage() {
return image;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Rectangle getBounds() {
return new Rectangle(x - CAR_WIDTH / 2, y - CAR_HEIGHT, CAR_WIDTH, CAR_HEIGHT);
}
}
// Reset game after game over
private void resetGame() {
playerX = 250;
playerY = 400;
score = 0;
speed = 1;
gameOver = false;
vehicles.clear();
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Car Game");
CarGame game = new CarGame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(game);
frame.pack();
frame.setVisible(true);
// Add listener for keyboard input to restart game
game.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (game.gameOver) {
if (e.getKeyCode() == KeyEvent.VK_Y) {
game.resetGame();
} else if (e.getKeyCode() == KeyEvent.VK_N) {
System.exit(0);
}
}
}
});
}
}