Certainly! Here's an example of a simple car game written in Java:



java
import java.awt.*; import java.awt.event.*; public class CarGame extends Frame { private int x = 50; private int y = 250; public CarGame() { setSize(500, 500); setTitle("Car Game"); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP) { y -= 10; } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { y += 10; } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { x -= 10; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { x += 10; } repaint(); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } public void paint(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.BLUE); g.fillRect(x, y, 50, 30); } public static void main(String[] args) { CarGame carGame = new CarGame(); } }

This code creates a simple car game window where you can control a blue rectangle using the arrow keys. Pressing the up arrow key moves the car up, the down arrow key moves it down, the left arrow key moves it left, and the right arrow key moves it right. The car is represented by a blue rectangle.

To run this code, save it in a file named "CarGame.java" and compile and run it using a Java compiler such as JDK. Make sure you have the Java Development Kit (JDK) installed on your system.

Compile the code using the following command:

javac CarGame.java

Then run the code with the following command:

java CarGame

A window will open, and you can control the car using the arrow keys. The car will move on the screen according to the key presses.