Java 2D Asteroids Tutorial Part 5
This class is the space object class that will keep track of the space objects in the game. The only information it is keeping is the location and color of the object.
SpaceObjects.java
import java.awt.Color;
import java.awt.Point;
public class SpaceObjects
{
private Point point1;
private Color color;
public SpaceObjects()
{
point1=new Point();
}
public void setLocation(int x1,int y1)
{
point1.setLocation(x1,y1);
}
public Point getLocation()
{
return point1;
}
public void setColor(Color c)
{
color=c;
}
public Color getColor()
{
return color;
}
}
MovingSpaceObject.java
public class MovingSpaceObject extends SpaceObjects
{
private double heading;
private int speed;
public MovingSpaceObject()
{
heading=0;
speed=0;
}
public void setHeading(double h)
{
heading=h;
}
public void leftHeading()
{
heading =6;
if(heading>359)
heading=0;
}
public void rightHeading()
{
heading-=6;
if(heading < 0)
heading=359;
}
public void setSpeed(int s)
{
speed=s;
}
public void increaseSpeed()
{
if(speed!=200)
speed =8;
}
public void decreaseSpeed()
{
if(speed!=-200)
{
speed-=8;
}
}
public double getHeading()
{
return heading;
}
public int getSpeed()
{
return speed;
}
}
The MovingSpaceObject.java class keeps track of the information required for moving objects. You have two variables which is speed and heading. The heading will be a 360 degree number for a complete circle. It has two methods to allow the user to increase and decrease the speed of the object.
Go To Part 6

0 comments:
Post a Comment