Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Game Structure Review  (Read 2514 times)
0 Members and 1 Guest are viewing this topic.
Offline Darrin

Junior Member


Projects: 1



« Posted 2009-03-06 03:04:57 »

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.   

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  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
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();
   }
}

Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2009-03-06 11:00:35 »

It was a good idea to check your direction first, because this
Quote
Class Ship is thread
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.
Offline Darrin

Junior Member


Projects: 1



« Reply #2 - Posted 2009-03-06 18:20:07 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (37 views)
2014-07-18 06:55:21

Zero Volt (35 views)
2014-07-17 23:47:54

danieldean (28 views)
2014-07-17 23:41:23

MustardPeter (31 views)
2014-07-16 23:30:00

Cero (46 views)
2014-07-16 00:42:17

Riven (47 views)
2014-07-14 18:02:53

OpenGLShaders (36 views)
2014-07-14 16:23:47

Riven (36 views)
2014-07-14 11:51:35

quew8 (32 views)
2014-07-13 13:57:52

SHC (69 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!