nishpystoned
nishpystoned

Reputation: 11

java space ship game

here is the entire code for the classes Ship,Asteroids,BaseShapeClass. Ship Class inherits from the BaseShapeClass for its shape. Asteroid class is the main source code which declares the Graphics2D object,AffineTransform(for identity creation),declares double image buffer... Code for BaseShapeClass..

package baseshapeclass;
import java.awt.Shape;


public class BaseShapeClass {
    private Shape shape;
    private double x, y;
    private double velX, velY;
    private double moveAngle, faceAngle;
    private boolean alive;

    //accessors and mutators
    public Shape getShape(){return shape;}
    public void setShape(Shape shape){ this.shape = shape; }

    public double getX() { return x; }
    public void setX(double x) { this.x = x; }
    public void incX(double ix) { this.x += ix; }

    public double getY() { return y; }
    public void setY(double y) { this.y = y; }
    public void incY(double iy) { this.y += iy; }

    public double getVelX() { return velX; }
    public void setVelX(double velX) { this.velX = velX; }
    public void incVelX(double ivX) { this.velX += ivX; }

    public double getVelY() { return velY; }
    public void setVelY(double velY) { this.velY = velY; }
    public void incVelY(double ivY) { this.velY += ivY; }
    //MoveAngle refers to the objects angular movement
    public double getMoveAngle() { return moveAngle; }
    public void setMoveAngle(double mAngle) { this.moveAngle = mAngle; }
    public void incMoveAngle(double imAngle) { this.moveAngle += imAngle; }
    //FaceAngle refers to the objects face/heads angular movement
    public double getFaceAngle() { return faceAngle; }
    public void setFaceAngle(double fAngle) { this.faceAngle = fAngle; }
    public void incFaceAngle(double ifAngle) { this.faceAngle += ifAngle; }

    public boolean isAlive() { return alive; }
    public void setAlive(boolean alive) { this.alive = alive; }

    //default constructor everything will be set to original state
    //when update is called everything will start to move
    BaseShapeClass(){
        setShape(null);
        setAlive(false);
        //all of them are set to '0' representing their initial position,
        //which will be called during the update() Event of the graphics objects
        setX(0.0);
        setY(0.0);
        setVelX(0.0);
        setVelY(0.0);
        setMoveAngle(0.0);
        setFaceAngle(0.0);
    }
}

Code for Ship class...

package baseshapeclass;
import java.awt.Rectangle;
import java.awt.Polygon;
public class Ship extends BaseShapeClass {
    //ships shape along the x and y cordinates
    private final int[] shipx = {-6,3,0,3,6,0};
    private final int[] shipy = {6,7,7,7,6,-7};

    public Rectangle getBounds(){
        Rectangle r = new Rectangle((int)getX()-6, (int)getY()-6, 12, 12);
        return r;
    }
    Ship(){
        setShape(new Polygon(shipx, shipy, shipx.length));
        setAlive(true);
    }
}

Code for Asteroid(Main source code)...

package baseshapeclass;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public abstract class Asteroid extends Applet implements Runnable, KeyListener {
    BufferedImage backbuffer;
    Graphics2D g2d;
    Ship ship = new Ship();
    boolean showBounds= true;
    AffineTransform identity = new AffineTransform();

    @Override public void init(){
        backbuffer = new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
        g2d = backbuffer.createGraphics();

        ship.setX(320);
        ship.setY(240);

        addKeyListener(this);
    }
    @Override public void update(Graphics g){
        g2d.setTransform(identity);
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, getSize().width, getSize().height);
        g2d.setColor(Color.WHITE);
        g2d.drawString("Ship: "+Math.round(ship.getX())+" , "+Math.round(ship.getY()),2, 150);
        g2d.drawString("Face Angle: "+Math.toRadians(ship.getFaceAngle()),5, 30);
        g2d.drawString("Move Angle: "+Math.toRadians(ship.getMoveAngle())+90,5,50);

        drawShip();
        paint(g);
    }
    public void drawShip(){
        g2d.setTransform(identity);
        g2d.translate(ship.getX(),ship.getY());
        g2d.rotate(Math.toRadians(ship.getFaceAngle()));
        g2d.setColor(Color.ORANGE);
        g2d.fill(ship.getShape());
    }
}

I hope you guys get a better idea with all the code in place. Just wanted to know on the part of Ship class why are the ships x and y cordinates such as under:

public class ship extends BaseShapeClass{
private int[] shipx = {-6,3,0,3,6,0};
private int[] shipy = {6,7,7,7,6,-7};
}

I cant follow on how those values will make upto a Polygon??

Upvotes: 0

Views: 2389

Answers (2)

Paul Richter
Paul Richter

Reputation: 11072

This post is in answer to your comment in Kronion's answer; I was going to post it as a comment, but there is too much to say and I wanted to show you some code, which is not as legible in the comments.

As Kronion said, the Polygon class does indeed accept an array of X coordinates, and an array of Y coordinates. The reason for this is that the X and Y coordinate are stored at the same position in both arrays. So if int index = 0, then that X,Y coordinate pair would be xArray[index] and yArray[index].

If that doesn't make any sense, examine the Polygon class source code. For example, you'll see this happening in the contains method, here:

for (int i = 0; i < npoints; lastx = curx, lasty = cury, i++) {

    curx = xpoints[i];
    cury = ypoints[i];

    // remainder of loop
}

So in short, they are assigned in this manner because the X and Y are paired by their index positions.

Hope that helps.

Upvotes: 0

kronion
kronion

Reputation: 711

Ship(){
  setShape(new Polygon(shipx,shipy,shipx.length));
  setAlive(true);
}

You can see that the two arrays you are confused about go into the initialization of a Polygon. These two arrays, taken as a pair, give the x and y coordinates of each point in the Polygon.

Upvotes: 1

Related Questions