//The first class
//Nothing wrong with this class it is just here to make it so people can test it.
/**
* Write a description of class BaseGameEntity here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class BaseGameEntity extends Object
{
//variables
protected boolean alive;
protected double x, y;
protected double velX, velY;
protected double moveAngle, faceAngle;
//accessor methods
public boolean isAlive() {return alive;}
public double getX() {return x;}
public double getY() {return y;}
public double getVelX() {return velX;}
public double getVelY() {return velY;}
public double getMoveAngle() {return moveAngle;}
public double getFaceAngle() {return faceAngle;}
//mutator methods
public void setAlive(boolean alive) {this.alive = alive;}
public void setX(double x) {this.x = x;}
public void incX(double i) {this.x += i;}
public void setY(double y) {this.y = y;}
public void incY(double i) {this.y += i;}
public void setVelX(double velX) {this.velX = velX;}
public void incVelX(double i) {this.velX += i;}
public void setVelY(double velY) {this.velY = velY;}
public void incVelY(double i) {this.velY += i;}
public void setFaceAngle(double angle) {this.faceAngle = angle;}
public void incFaceAngle(double i) {this.faceAngle += i;}
public void setMoveAngle(double angle) {this.moveAngle = angle;}
public void incMoveAngle(double i) {this.moveAngle += i;}
//default constructor
BaseGameEntity()
{
setAlive(false);
setX(0.0);
setY(0.0);
setVelX(0.0);
setVelY(0.0);
setMoveAngle(0.0);
setFaceAngle(0.0);
}
}
//The second class
//base game class for bitmapped entities
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.net.*;
import java.applet.*;
/**
* Write a description of class ImageEntity here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ImageEntity extends BaseGameEntity
{
//variables
protected Image image;
protected JFrame frame;
protected Applet appFrame;
protected AffineTransform at;
protected Graphics2D g2d;
//default constructor
ImageEntity(JFrame a)
{
frame = a;
setImage(null);
setAlive(true);
}
ImageEntity(Applet a)
{
appFrame = a;
setImage(null);
setAlive(true);
}
public Image getImage() {return image;}
public void setImage(Image image)
{
this.image = image;
double x = frame.getSize().width / 2 - width() / 2;
double y = frame.getSize().height / 2 - height() / 2;
at = AffineTransform.getTranslateInstance(x,y);
}
public int width()
{
if(image != null)
{
return image.getWidth(frame);
}
else
{
return 0;
}
}
public int height()
{
if(image != null)
{
return image.getHeight(frame);
}
else
{
return 0;
}
}
public double getCenterX()
{
return getX() + width() / 2;
}
public double getCenterY()
{
return getY() + height() / 2;
}
public void setGraphics(Graphics2D g)
{
g2d = g;
}
private URL getURL(String filename)
{
URL url = null;
try
{
url = this.getClass().getResource(filename);
}
catch (Exception e)
{
}
return url;
}
public void load(String filename)
{
Toolkit tk = Toolkit.getDefaultToolkit();
image = tk.getImage(getURL(filename));
while(getImage().getWidth(frame) <= 0);
double x = frame.getSize().width / 2 - width() / 2;
double y = frame.getSize().height / 2 - height() / 2;
at = AffineTransform.getTranslateInstance(x,y);
}
public void transform()
{
at.setToIdentity();
at.translate((int)getX() + width() / 2, (int)getY() + height() / 2);
at.rotate(Math.toRadians(getFaceAngle()));
at.translate(-width() / 2, -height() / 2);
}
public void draw()
{
g2d.drawImage(getImage(),at,frame);
}
public void drawApp()
{
g2d.drawImage(getImage(),at,appFrame);
}
//bounding rectangle
public Rectangle getBounds()
{
Rectangle r;
r = new Rectangle((int)getX(), (int)getY(), width(),height());
return r;
}
}
//The third class
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
/**
* Write a description of class AsteroidsMain here.
*
* @author (your name)
* @version (a version number or a date)
*/
//Primary game class
public class GalacticWarCJR extends Applet implements Runnable, KeyListener
{
//the main tread becomes the game loop
Thread gameloop;
//use double buffer
BufferedImage backbuffer;
//the main drawing object for the back buffer
Graphics2D g2d;
//toggle for drawing bounding boxes
boolean showBounds = false;
//create random number generator
Random rand = new Random();
//create the asteroid array
int ASTEROIDS = (rand.nextInt(10) + 10);
AsteriodClass[] ast = new AsteriodClass[ASTEROIDS];
//create the bullet array
int BULLETS = 1000;
BulletClass[] bullet = new BulletClass[BULLETS];
int currentBullet = 0;
int boomShot = 0;
//the player's ship
ImageEntity ship2 = new ImageEntity(this);
//create id transform(0,0)
AffineTransform identity = new AffineTransform();
//load sounds
SoundClip shoot;
SoundClip explode;
//Key Event Control booleans
boolean Up1 = false;
boolean Left1 = false;
boolean Right1 = false;
boolean Shoot1 = false;
boolean Bomb1 = false;
//applet init event
public void init()
{
//create the back buffer for smooth graphics
backbuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
/*
//set up the ship
ship2.setX(320);
ship2.setY(240);
ship2.load("PlayerShip.png");
ship2.setGraphics(g2d);
*/
//set up the bullets
for(int n = 0; n < BULLETS; n++)
{
bullet[n] = new BulletClass();
}
//create asteroids
for(int n2 = 0; n2 < ASTEROIDS; n2++)
{
ast[n2] = new AsteriodClass();
ast[n2].setRotationVelocity(rand.nextInt(30) + 1);
ast[n2].setX((double)rand.nextInt(600) + 20);
ast[n2].setY((double)rand.nextInt(440) + 20);
ast[n2].setMoveAngle(rand.nextInt(360));
double ang = ast[n2].getMoveAngle() - 90;
ast[n2].setVelX(calcAngleMoveX(ang));
ast[n2].setVelY(calcAngleMoveY(ang));
}
shoot = new SoundClip("SmallBlaster.wav");
explode = new SoundClip("Biglaser1.wav");
//start the user input listener
addKeyListener(this);
}
//applet update event to redraw the screen
public void update(Graphics g)
{
//start off transfors at identity
g2d.setTransform(identity);
//erase the background
g2d.setPaint(Color.BLACK);
g2d.fillRect(0,0, getSize().width, getSize().height);
//print some status information
g2d.setColor(Color.YELLOW);
//g2d.drawString( "Ship: " + Math.round(ship2.getX()) + "," + Math.round(ship2.getY()), 5 , 10 );
//g2d.drawString( "Move angle: " + Math.round(ship2.getMoveAngle() + 90), 5, 25 );
//g2d.drawString( "Face angle: " + Math.round(ship2.getFaceAngle()), 5, 40 );
//draw the game graphics
//drawShip();
drawBullets();
drawAsteroids();
//reprint the applet window
paint(g);
}
/*
//draw Ship called by applet update event
public void drawShip()
{
ship2.transform();
ship2.drawApp();
//ship.rotate(Math.toRadians(ship.getFaceAngle()));
if(showBounds)
{
g2d.setTransform(identity);
//g2d.translate(ship.getX(), ship.getY());
g2d.setColor(Color.GREEN);
g2d.fill(ship2.getBounds());
}
}
*/
//draw bullets called by applet update event
public void drawBullets()
{
//iterate through the array of bullets
for(int n = 0; n < BULLETS; n++)
{
//is this bullet currently in use?
if(bullet[n].isAlive())
{
//draw the bullet
g2d.setTransform(identity);
g2d.translate(bullet[n].getX(), bullet[n].getY());
g2d.setColor(Color.RED);
g2d.draw(bullet[n].getShape());
}
}
}
//draw asteroids called by applet
public void drawAsteroids()
{
for(int n2 = 0; n2 < ASTEROIDS; n2++)
{
//is this asteroid being used?
if(ast[n2].isAlive())
{
//draw the asteroid
g2d.setTransform(identity);
g2d.translate(ast[n2].getX(), ast[n2].getY());
g2d.rotate(Math.toRadians(ast[n2].getMoveAngle()));
g2d.setColor(Color.BLUE);
g2d.fill(ast[n2].getShape());
}
}
}
//applet window repaint event - - draw the back buffer
public void paint(Graphics g)
{
//draw the back buffer onto the applet window
g.drawImage(backbuffer, 0, 0, this);
}
//tread start event - start the game loop running
public void start()
{
//create the gameloop tread for real - time updates
gameloop = new Thread(this);
gameloop.start();
}
//thread run event (game loop)
public void run()
{
//acquire the current thread
Thread t = Thread.currentThread();
//keep going as long as the thread is alive
while(t == gameloop)
{
try
{
//update the game loop
gameUpdate();
//target framerate is 50 fps
Thread.sleep(20);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
repaint();
}
}
//thread stop event
public void stop()
{
//kill gameloop thread
gameloop = null;
}
//move and animate the objects in the game
private void gameUpdate()
{
//updateShip();
updateBullets();
updateAsteroids();
checkCollisions();
//keyPressed();
//keyReleased();
//HandleInPuts();
}
/*
//Update the ship position based on velocity
public void updateShip()
{
//update ship's X position
ship2.incX(ship2.getVelX());
//wrap around left/right
if(ship2.getX() < -10)
{
ship2.setX(getSize().width + 10);
}
else if(ship2.getX() > getSize().width + 10)
{
ship2.setX(-10);
}
//update ship's Y position
ship2.incY(ship2.getVelY());
//wrap around up/down
if(ship2.getY() < -10)
{
ship2.setY(getSize().height + 10);
}
else if(ship2.getY() > getSize().height + 10)
{
ship2.setY(-10);
}
}
*/
//update the bullets based on velocity
public void updateBullets()
{
//move each bullet
for(int n = 0; n < BULLETS; n++)
{
//is this bullet being used?
if(bullet[n].isAlive())
{
//update bullet's position
bullet[n].incX(bullet[n].getVelX());
//bullet disappears at left/right edge
if(bullet[n].getX() < 0 || bullet[n].getX() > getSize().width)
{
bullet[n].setAlive(false);
}
//update bullet's Y position
bullet[n].incY(bullet[n].getVelY());
//bullet disappears at top/bottom edge
if(bullet[n].getY() < 0 || bullet[n].getY() > getSize().height)
{
bullet[n].setAlive(false);
}
}
}
}
//update the asteroids based on velocity
public void updateAsteroids()
{
//move and rotate Asteroids
for(int n2 = 0; n2 < ASTEROIDS; n2++)
{
//is this asteroid being used
if(ast[n2].isAlive())
{
//update the asteroid's X value
ast[n2].incX(ast[n2].getVelX());
//wrap the asteroid at left/right
if(ast[n2].getX() < -20)
{
ast[n2].setX(getSize().width + 20);
}
else if(ast[n2].getX() > getSize().width + 20)
{
ast[n2].setX(-20);
}
//update the asteroid's Y value
ast[n2].incY(ast[n2].getVelY());
//wrap the asteroid at left/right
if(ast[n2].getY() < -20)
{
ast[n2].setY(getSize().height + 20);
}
else if(ast[n2].getY() > getSize().height + 20)
{
ast[n2].setY(-20);
}
//update the asteroid's rotation
ast[n2].incMoveAngle(ast[n2].getRotationVelocity());
//keep the asgle within 0-359 degrees
if(ast[n2].getMoveAngle() < 0)
{
ast[n2].setMoveAngle(360 - ast[n2].getRotationVelocity());
}
else if(ast[n2].getMoveAngle() > 360)
{
ast[n2].setMoveAngle(ast[n2].getRotationVelocity());
}
}
}
}
//collision engine for asteroids
public void checkCollisions()
{
//iterate through the asteroids array
for(int m = 0; m < ASTEROIDS; m++)
{
//is this asteroid being used?
if(ast[m].isAlive())
{
//check for collision with bullet
for(int n = 0; n < BULLETS; n++)
{
//is this bullet being used?
if(bullet[n].isAlive())
{
//perform the collision test
if(ast[m].getBounds().contains(bullet[n].getX(), bullet[n].getY()))
{
bullet[n].setAlive(false);
ast[m].setAlive(false);
continue;
}
}
}
/*
//check for collision with ship
if(ast[m].getBounds().intersects(ship2.getBounds()))
{
ast[m].setAlive(false);
ship2.setX(320);
ship2.setY(240);
ship2.setFaceAngle(0);
ship2.setVelX(0);
ship2.setVelY(0);
continue;
}
*/
}
}
}
//key listener events
public void keyReleased(KeyEvent k)
{
int keyCode = k.getKeyCode();
if(KeyEvent.VK_LEFT == keyCode)
{
Left1 = false;
}
if(KeyEvent.VK_RIGHT == keyCode)
{
Right1 = false;
}
if(KeyEvent.VK_UP == keyCode)
{
Up1 = false;
}
if(KeyEvent.VK_SPACE == keyCode)
{
Shoot1 = false;
}
if(KeyEvent.VK_ENTER == keyCode)
{
Bomb1 = false;
}
}
public void keyTyped(KeyEvent k)
{
int keyCode = k.getKeyCode();
}
public void keyPressed(KeyEvent k)
{
int keyCode = k.getKeyCode();
if(KeyEvent.VK_LEFT == keyCode)
{
Left1 = true;
}
if(KeyEvent.VK_RIGHT == keyCode)
{
Right1 = true;
}
if(KeyEvent.VK_UP == keyCode)
{
Up1 = true;
}
if(KeyEvent.VK_SPACE == keyCode)
{
Shoot1 = true;
}
if(KeyEvent.VK_ENTER == keyCode)
{
Bomb1 = true;
}
}
/*
public void HandleInPuts()
{
if(Left1 == true)
{
//left arrow rotates ship left 5 degrees
ship2.incFaceAngle(-5);
if(ship2.getFaceAngle() < 0)
{
ship2.setFaceAngle(360 - 5);
}
}
if(Right1 == true)
{
//right arrow rotates ship right 5 degrees
ship2.incFaceAngle(5);
if(ship2.getFaceAngle() > 360)
{
ship2.setFaceAngle(5);
}
}
if(Up1 == true)
{
//up arrow adds thrust to ship (1/10 normal speed)
ship2.setMoveAngle(ship2.getFaceAngle() - 90);
ship2.incVelX(calcAngleMoveX(ship2.getMoveAngle()) * 0.1);
ship2.incVelY(calcAngleMoveY(ship2.getMoveAngle()) * 0.1);
}
if(Shoot1 == true)
{
//fire a bullet
currentBullet++;
if(currentBullet > BULLETS - 1)
{
currentBullet = 0;
}
bullet[currentBullet].setAlive(true);
//point bullet in same direction ship is facing
bullet[currentBullet].setX(ship2.getX());
bullet[currentBullet].setY(ship2.getY());
bullet[currentBullet].setMoveAngle(ship2.getFaceAngle() - 90);
//fire bullet at angle of the shipbullet[currentBullet]
double angle = ship2.getFaceAngle() - 90;
double svx = ship2.getVelX();
double svy = ship2.getVelY();
bullet[currentBullet].setVelX(svx + calcAngleMoveX(angle) * 15);
bullet[currentBullet].setVelY(svy + calcAngleMoveY(angle) * 15);
}
if(Bomb1 == true)
{
//fire a bullet
for(int C = 0; C < 360; C++)
{
currentBullet++;
boomShot++;
if(boomShot >= 360)
{
boomShot = 0;
}
if(currentBullet > BULLETS - 1)
{
currentBullet = 0;
}
if(currentBullet > 360)
{
bullet[currentBullet].setAlive(true);
//point bullet in same direction ship is facing
bullet[currentBullet].setX(ship2.getX());
bullet[currentBullet].setY(ship2.getY());
bullet[currentBullet].setMoveAngle(ship2.getFaceAngle() - 90);
//fire bullet at angle of the shipbullet[currentBullet]
double angle = ship2.getFaceAngle() - boomShot;
double svx = ship2.getVelX();
double svy = ship2.getVelY();
bullet[currentBullet].setVelX(svx + calcAngleMoveX(angle) * 15);
bullet[currentBullet].setVelY(svy + calcAngleMoveY(angle) * 15);
}
}
}
}
*/
//calculate X movement value based on direction
public double calcAngleMoveX(double angle)
{
return (double)(Math.cos(angle * Math.PI / 180));
}
//calculate Y movement value based on direction
public double calcAngleMoveY(double angle)
{
return (double)(Math.sin(angle * Math.PI / 180));
}
}