This is a simulation attempt of an "old-style game", where a "car" (nothing more than a red rectangle) must dodge some "obstacles" (blue squares) falling from the upper edge of the frame. If an obstacle hits the car, the game stops.
The app works fine, but since this is my first project involving painting and moving objects in Swing, I would appreciate the critical opinion of some expert, as I'm sure there is plenty of space for code improvement/optimization.
What do you think about the code? What should have I done in a different/better way? What are your recommendations in terms of optimization?
public class Little_car_frame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Little_car_frame frame = new Little_car_frame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Little_car_frame() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().add(new Little_car_panel());
}
}
public class Little_car_panel extends JPanel implements KeyListener {
private Car car1 = new Car();
private Timer timer1 = new Timer(1000, new TimerListener());
private ArrayList<Obstacle> list1 = new ArrayList<Obstacle>();
private Random rand1 = new Random();
public Little_car_panel() {
setBorder(BorderFactory.createLineBorder(Color.black));
setFocusable(true);
addKeyListener(this);
timer1.start();
}
public void keyPressed(KeyEvent e) {
Integer key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
car1.move_left();
check_collision();
}
if (key == KeyEvent.VK_RIGHT) {
car1.move_right();
check_collision();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
car1.draw_car(g);
for (Obstacle obstacle : list1) {
obstacle.draw_obstacle(g);
}
repaint();
}
public void check_collision() {
Integer carX1 = car1.getxPos();
Integer carX2 = carX1 + car1.getWidth();
Integer carY1 = car1.getyPos();
Integer carY2 = carY1 + car1.getHeight();
for (Obstacle obstacle : list1) {
Integer obstacleX1 = obstacle.getxPos();
Integer obstacleX2 = obstacleX1 + obstacle.getLato();
Integer obstacleY = obstacle.getyPos() + obstacle.getLato();
if (((obstacleX1 >= carX1 && obstacleX1 <= carX2) || (obstacleX2 >= carX1 && obstacleX2 <= carX2))
&& ((obstacleY >= carY1 && obstacleY <= carY2))) {
timer1.stop();
removeKeyListener(this);
}
}
}
public class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Integer x = rand1.nextInt(420);
list1.add(new Obstacle(x));
for (Obstacle obstacle : list1) {
obstacle.move_down();
check_collision();
}
}
}
}
public class Car {
private Integer xPos = 225;
private Integer yPos = 220;
private Integer width = 20;
private Integer height = 40;
public Integer getxPos() {
return xPos;
}
public void setxPos(Integer xPos) {
this.xPos = xPos;
}
public Integer getyPos() {
return yPos;
}
public void setyPos(Integer yPos) {
this.yPos = yPos;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public void move_right() {
if (xPos < 420)
xPos = xPos + width;
}
public void move_left() {
xPos = xPos - width;
if (xPos < 0) {
xPos = 0;
}
}
public void draw_car(Graphics g) {
g.setColor(Color.RED);
g.fillRect(xPos, yPos, width, height);
}
}
public class Obstacle {
private Integer xPos;
private Integer yPos = -20;
private Integer lato = 20;
public Integer getxPos() {
return xPos;
}
public void setxPos(Integer xPos) {
this.xPos = xPos;
}
public Integer getyPos() {
return yPos;
}
public void setyPos(Integer yPos) {
this.yPos = yPos;
}
public Integer getLato() {
return lato;
}
public void setLato(Integer lato) {
this.lato = lato;
}
public Obstacle(Integer xPos) {
super();
this.xPos = xPos;
}
public void move_down() {
yPos = yPos + lato;
}
public void draw_obstacle(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(xPos, yPos, lato, lato);
}
}