import java.awt.*;
import java.applet.*;
class Ship{
public void draw (Graphics g){ //Draws the ship at the current position
g.setColor(Color.white);
double dRadians = myPointDirection*(Math.PI/180);
//convert degrees to radians for sin and cos
int nXcorner1 =16;
int nYcorner1 = 0;
int nXcorner2 =-8;
int nYcorner2 = - 8;
int nXcorner3 = -8;
int nYcorner3 = 8;
//coordinates of corners of ship if center is (0,0)
//and direction is zero degrees (pointing to right)
//change these values to change size of ship
int nXrotate1 = (int)((nXcorner1 * Math.cos(dRadians)) - (nYcorner1 * Math.sin(dRadians))+myCenterX);
int nYrotate1 = (int)((nXcorner1 * Math.sin(dRadians)) + (nYcorner1 * Math.cos(dRadians))+myCenterY);
int nXrotate2 = (int)((nXcorner2 * Math.cos(dRadians)) - (nYcorner2 * Math.sin(dRadians))+myCenterX);
int nYrotate2 = (int)((nXcorner2 * Math.sin(dRadians)) + (nYcorner2 * Math.cos(dRadians))+myCenterY);
int nXrotate3 = (int)((nXcorner3 * Math.cos(dRadians)) - (nYcorner3 * Math.sin(dRadians))+myCenterX);
int nYrotate3 = (int)((nXcorner3 * Math.sin(dRadians)) + (nYcorner3 * Math.cos(dRadians))+myCenterY);
//rotate and translate the coordinates of the ship to current direction
g.drawLine (nXrotate1,nYrotate1,nXrotate2, nYrotate2);
g.drawLine (nXrotate1,nYrotate1,nXrotate3, nYrotate3);
g.drawLine (nXrotate2,nYrotate2,nXrotate3, nYrotate3);
//draw the lines for the ship
}
public void move (){ //Moves the ship towards the coordinates
//myDirectionX and myDirectionY
myCenterX += myDirectionX;
myCenterY += myDirectionY;
//move the ship in the current direction of travel
if(myCenterX >640)
myCenterX = 0;
else if (myCenterX<0)
myCenterX = 640;
if(myCenterY >480)
myCenterY = 0;
else if (myCenterY < 0)
myCenterY = 480;
//wrap around screen
}
public void accelerate (double dAmount){ //Accelerates the ship in
//the direction it is pointing
//(myPointDirection)
double dRadians =myPointDirection*(Math.PI/180);
//the current direction the ship is pointing to radians
myDirectionX += ((dAmount) * Math.cos(dRadians));
myDirectionY += ((dAmount) * Math.sin(dRadians));
//change coordinates of direction of travel
}
void rotate (int nDegreesOfRotation){ //rotates the ship
//by a given number of degrees
myPointDirection+=nDegreesOfRotation;
}
public double returnX(){return myCenterX;} //returns the current X
//coordinate of the center of the ship
//useful to check for collisions
public double returnY(){return myCenterY;} //returns the current Y
//coordinate of the center of the ship
//useful to check for collisions
public int returnDirection(){return (int) myPointDirection;}
//Returns the current direction the ship is
//pointing, useful for shooting
Ship(){ //constructor
myCenterX = nSCREEN_WIDTH/2;
myCenterY = nSCREEN_HEIGHT/2;
//place ship at center of screen
myDirectionX = 0;
myDirectionY = 0;
//Intialize ship with no momentum
myPointDirection=0;
//Ship points to right
}
public static final int nSCREEN_WIDTH = 640;
public static final int nSCREEN_HEIGHT = 480;
private double myCenterX,myCenterY; //holds center coordinates of the ship
private double myDirectionX, myDirectionY; //holds x and y coordinates of
//the vector for direction of travel
private double myPointDirection; //holds current direction the ship is
//pointing in degrees
}