Game Structure Review

Hi, I’m quite new to Java programming; in January I started tinkering. I’m trying to organize my game code in a reasonable OOP fashion. The first stage is just adding a ship or two and having it update itself. My first go at it was not quite organized and this is my 3rd effort after some Internet reading. Eventually it will be a 2d space combat game. Before I add a bunch of physics, ship types, enemy levels, I want to make sure my organization is going in the right direction.

Any hints, critiques would be most welcome.

–Class Ship is thread and basically sets non moving ships like satellites.
–PatrolShip extends ship and takes polygon patrol points.
–Each ship will paint itself as well as take care of movement. Nothing fancy yet.
–DisplayPane is a pane that displays an off screen image.
–loopGame is the control method that basically adds and takes away the ships.

Next step is to add range checks and then weapon firing. Thinking about doing a class for them as well.

Ok here are a few specific questions:

  1. How would you set up weapons? would you thread them the same way as the ships?
  2. Regarding frame rates, in the loopGame method if I set the thread delay to zero I get flicker and my fps goes to 1500. with it set to 1 very little flicker and frame rate at 500 something. Have I set this up badly? Is it normal to get flicker at high frame rates? Any Corrections.
  3. At t his point, I don’t want to use any libraries like SWING, JOGL, LWJGL because I’m code overwhelmed as it is. However, any suggestions to abstracting my display layer better so moving over later might be easy enough?
  4. It seems like the player’s ship should also extend the Ship class as well. Do you usually set up this way?

Again thanks for taking a look. The code can be compiled and run. I hope my questions are not too inane.

package controller;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Controller extends JFrame {
    DisplayPane pane;
    PatrolShip patrol;
    Ship satallite;


   public Controller(){
        setTitle("Controller");
        setBounds(0,0, 400, 400);
        setLayout(null);  //absolute position
        setResizable(false);

        pane = new DisplayPane(getBounds().width, getBounds().height);
        add(pane);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
   }


   class DisplayPane extends JPanel{
       BufferedImage offImage;
       long startTime = System.currentTimeMillis();
       long count = 1;

       public DisplayPane(int width, int height){
           offImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           setBounds(0, 0, width, height);
       }

       @Override
       protected void paintComponent(Graphics g){            
            g.drawImage(offImage,0,0,this);            
       }

       public BufferedImage getImage(){
           return offImage;
       }

       public void setImage(BufferedImage tempImage){
           offImage = tempImage;
       }

       public void wipeImage(){
            Graphics2D big = offImage.createGraphics();
            big.setColor(Color.BLACK);
            big.fillRect(0, 0, getBounds().width, getBounds().height);
            big.setColor(Color.red);
            String fps = Integer.toString((int) (++count *1000 /(System.currentTimeMillis() - startTime)));
            big.drawString("fps: "+ fps, 20, getBounds().height-40);
            repaint();
       }

   }

   class Ship implements Runnable{
       Thread thread;
       int speedStep;
       boolean bAlive = true;
       Point point = new Point(0,0);

       public Ship(String shipName, int speed){
           thread = new Thread(this, shipName);
           speedStep = speed;
           point.x = (int) ((int) 300 * Math.random());
           point.y = (int) ((int) 300 * Math.random());
       }
       public void start(){
           thread.start();
       }
       public void kill(){
           bAlive = false;
       }

       public void drawShip(BufferedImage tempImage){
            if (!bAlive)                return;
            Graphics2D big = tempImage.createGraphics();
            int[] pXfrigate = {point.x, point.x-4, point.x-4, point.x+4, point.x+4};
            int[] pYfrigate = {point.y, point.y+7, point.y+17, point.y+17, point.y+7};
            Polygon frigateShape = new Polygon(pXfrigate, pYfrigate, 5);
            Polygon currentShip = frigateShape;
            big.setColor(Color.GRAY);
            big.fillPolygon(currentShip);
            big.setColor(Color.RED);
            big.drawPolygon(currentShip);
       }

       public void run() {
           int delay = 500;
           while (bAlive){
               try{
                   //System.out.println(thread.getName()+" x: "+point.x+" y: "+point.y);
                   Thread.currentThread().sleep(delay);
               }catch (Exception e){}
           }
           thread = null;

       }
   }

   class PatrolShip extends Ship{
       Polygon path;
       PatrolShip(String shipName, int speed, Polygon p){
            super(shipName, speed);
            path = p;
        }
        public void run(){
           int delay = 10;
           int node = 0;

           while (super.bAlive){
               int[] xArray = path.xpoints;
               int[] yArray = path.ypoints;
               int xDistance = point.x - xArray[node];
               int yDistance = point.y - yArray[node];

               if (Math.abs(xDistance) <= super.speedStep)
                   point.x = xArray[node];
               else {
                   if (xDistance > 0)
                       point.x -= super.speedStep;
                   else
                       point.x += super.speedStep;
               }

               if (Math.abs(yDistance) <= super.speedStep)
                   point.y = yArray[node];
               else {
                   if (yDistance > 0)
                       point.y -= super.speedStep;
                   else
                       point.y += super.speedStep;
               }

               //System.out.println(thread.getName()+" x: "+point.x+" y: "+point.y);

               if ((point.x == xArray[node]) && (point.y == yArray[node])){
                 //  System.out.println("Node reached:"+node);
                   node++;
               }

               if (node >= xArray.length - 1)
                   node = 0;

               try{
                    Thread.sleep(delay);
               }catch (Exception e){}
           }


        }
   }

   public void loopGame(){
        Polygon poly = new Polygon();
        poly.addPoint(50,50);
        poly.addPoint(200,50);
        poly.addPoint(150, 150);
        patrol = new PatrolShip("X2", 1, poly);
        satallite = new Ship("Dot", 0);
        satallite.start();
        patrol.start();

        while (isVisible()){
           pane.wipeImage();
            patrol.drawShip(pane.getImage());
            satallite.drawShip(pane.getImage());
          //  pane.revalidate();
           // pane.repaint();
            
            try{
                Thread.sleep(1);
            }catch (Exception e){}

 

           // satallite.kill();
        }

        System.exit(0);
   }

   public static void main(String[] args) {
        Controller control = new Controller();
        control.loopGame();
   }
}

It was a good idea to check your direction first, because this

[quote]Class Ship is thread
[/quote]
is a such an enormous and easily made mistake that it makes me shiver to think of the problems it will cause later on.

On the face of it, it seems an attractive option - every object is a thread, and they all merrily update themselves at the same rate and everything is peachy. However, the execution of threads is controlled by the OS and is subject to such a dizzying array of factors beyond your control (OS version, processor type, system load, processor temp, etc) that to most people it is essentially random. The one thing you can be certain of is that the threads will not be advanced at the same rate. In addition, bugs arising due to threading are extraordinarily difficult to reproduce, and hence fix, even when running on the same machine. Trying to reproduce a non-trivial multithreading bug without access to the machine that it occurred on? I can think of few worse fates.

The only situations in which you should use threads are:

  • To maintain a responsive GUI in the face of heavy computation
  • To maintain throughput in the face of blocking operations - networking, file I/O etc
  • Parallelisable tasks on multicore processors

and even in these situations, have a good think about whether it’s absolutely necessary. If you can get away with having a slightly unresponsive GUI or not fully utilising all processing cores, I would recommend steering clear of multithreading.

So, now that I’ve (hopefully) instilled a paralysing fear of multithreading, what to do instead?

Have one thread that controls the execution of your game (search the forum for “game loop” examples), and have each of your game objects have update() and draw() methods that are called once per frame by the game loop. Bingo - guaranteed stable updates and no threading nightmares.

Thanks Bleb. After adding a few ships I was noticing some hiccups in the display so yeah I I think you nailed it. Back to the drawing board.