HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Basic game with moving objects in Swing

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
objectswithgamemovingswingbasic

Problem

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 list1 = new ArrayList();
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_collis

Solution

-
The Classes Car and Obstacle have the same methods and properties. This is a good reason to create a common super class with them and move xPos, yPos as well as the related method to super class.

-
Methods draw_obstacle() and draw_car() do one logic into one place, I mean paintComponent(). The best practice is constructing special interface like IRenderable and define method paint(Graphics g) and implement it in the Car and Obstacle. This step allows your to manipulate with visible objects as one thing.

interface IRenderable {
    void render(Graphics g);
}

// class Position as result of point 1
class Car extends Position implement IRenderable {

    void render(Graphics g) {
       // place body of draw_car() 
    }
}

class Obstacle extends Position implement IRenderable {

    void render(Graphics g) {
       // place body of draw_obstacle() 
    }
}

public void paintComponent(Graphics g) {
    // renderable list with car and obstacles
    for (IRenderable render : renderableList) {
        render.paint(g);
    }
    repaint();

}

Code Snippets

interface IRenderable {
    void render(Graphics g);
}

// class Position as result of point 1
class Car extends Position implement IRenderable {

    void render(Graphics g) {
       // place body of draw_car() 
    }
}

class Obstacle extends Position implement IRenderable {

    void render(Graphics g) {
       // place body of draw_obstacle() 
    }
}

public void paintComponent(Graphics g) {
    // renderable list with car and obstacles
    for (IRenderable render : renderableList) {
        render.paint(g);
    }
    repaint();

}

Context

StackExchange Code Review Q#102005, answer score: 4

Revisions (0)

No revisions yet.