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:
- How would you set up weapons? would you thread them the same way as the ships?
- 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.
- 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?
- 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();
}
}