-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRacingCarGame.java
More file actions
52 lines (45 loc) · 1.49 KB
/
RacingCarGame.java
File metadata and controls
52 lines (45 loc) · 1.49 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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RacingCarGame extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private int carX = 150;
private int carY = 300;
public RacingCarGame() {
JFrame frame = new JFrame("Racing Car Game");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
frame.addKeyListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.RED);
g.fillRect(carX, carY, 50, 50);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT && carX > 0) {
carX -= 10;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT && carX < 350) {
carX += 10;
} else if (e.getKeyCode() == KeyEvent.VK_UP && carY > 0) {
carY -= 10;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN && carY < 350) {
carY += 10;
}
repaint();
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
new RacingCarGame();
}
}