Sunteți pe pagina 1din 32

/*

* To change this template, choose Tools | Templates


* and open the template in the editor.
*/
package space.monster.shoot.em.up;

/**
*
* @author n.fletcher
*/
public class Directions {
public static final int STOP = 0;
public static final int NORTH = 1;
public static final int NORTH_WEST = 2;
public static final int WEST = 3;
public static final int SOUTH_WEST = 4;
public static final int SOUTH = 5;
public static final int SOUTH_EAST = 6;
public static final int EAST = 7;
public static final int NORTH_EAST = 8;

public static final int UP = NORTH;


public static final int DOWN = SOUTH;
public static final int LEFT = WEST;
public static final int RIGHT = EAST;
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

/**
*
* @author n.fletcher
*/
public class Coordinates {
public int x, y, width, height, top, bottom, left, right, direction, amount;

public Coordinates() {
dispose();
}

public final void dispose() {


x = y = width = height = top = bottom = left = right = direction =
amount = 0;
}
public void recalculate() {
left = x;
top = y;
right = left + width;
bottom = top + height;
}

public void moveUp() {


direction = Directions.UP;
y = y - amount;
recalculate();
}

public void moveDown() {


direction = Directions.DOWN;
y = y + amount;
recalculate();
}

public void moveLeft() {


direction = Directions.LEFT;
x = x - amount;
recalculate();
}

public void moveRight() {


direction = Directions.RIGHT;
x = x + amount;
recalculate();
}

public void moveNorth() {


moveUp();
direction = Directions.NORTH;
}

public void moveNorthWest() {


moveUp();
moveLeft();
direction = Directions.NORTH_WEST;
}

public void moveWest() {


moveLeft();
direction = Directions.WEST;
}

public void moveSouthWest() {


moveDown();
moveLeft();
direction = Directions.SOUTH_WEST;
}

public void moveSouth() {


moveDown();
direction = Directions.SOUTH;
}

public void moveSouthEast() {


moveDown();
moveRight();
direction = Directions.SOUTH_EAST;
}

public void moveEast() {


moveRight();
direction = Directions.EAST;
}

public void moveNorthEast() {


moveUp();
moveRight();
direction = Directions.NORTH_EAST;
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import javax.swing.JLabel;
/**
*
* @author n.fletcher
*/
public class GameObject {
protected Coordinates coordinates;
public JLabel image;
private boolean isAlive;

public GameObject(JLabel image) {


dispose();
this.image = image;
this.image.setText("");
this.image.setBorder(null);
}

public void update() {


coordinates.x = image.getX();
coordinates.y = image.getY();
coordinates.width = image.getWidth();
coordinates.height = image.getHeight();
coordinates.recalculate();
}

public final void dispose() {


image = null;
isAlive = false;
coordinates = new Coordinates();
coordinates.dispose();
}

public boolean isAlive() {


return isAlive;
}

public void spawn() {


isAlive = true;
update();
}

public void kill() {


isAlive = false;
}

public void moveNorth() {


coordinates.moveNorth();
}

public void moveNorthWest() {


coordinates.moveNorthWest();
}

public void moveWest() {


coordinates.moveWest();
}

public void moveSouthWest() {


coordinates.moveSouthWest();
}

public void moveSouth() {


coordinates.moveSouth();
}

public void moveSouthEast() {


coordinates.moveSouthEast();
}
public void moveEast() {
coordinates.moveEast();
}

public void moveNorthEast() {


coordinates.moveNorthEast();
}

public void redraw() {


image.setBounds(coordinates.x, coordinates.y, coordinates.width,
coordinates.height);
}

public boolean isCollidingWith(GameObject target) {


if (isCollidingHorizontally(target) && isCollidingVertically(target))
return true;
return false;
}

public boolean isCollidingVertically(GameObject target) {


if (target.isAlive && this.isAlive) {
if (this.coordinates.top >= target.coordinates.top &&
this.coordinates.top <= target.coordinates.bottom) {
return true;
}
else if (target.coordinates.top >= this.coordinates.top &&
target.coordinates.top <= this.coordinates.bottom) {
return true;
}
else if (this.coordinates.bottom >= target.coordinates.top &&
this.coordinates.bottom <= target.coordinates.bottom) {
return true;
}
else if (target.coordinates.bottom >= this.coordinates.top &&
target.coordinates.bottom <= this.coordinates.bottom) {
return true;
}
}
return false;
}

public boolean isCollidingHorizontally(GameObject target) {


if (target.isAlive && this.isAlive) {
if (this.coordinates.left >= target.coordinates.left &&
this.coordinates.left <= target.coordinates.right) {
return true;
}
else if (target.coordinates.left >= this.coordinates.left &&
target.coordinates.left <= this.coordinates.right) {
return true;
}
else if (this.coordinates.right >= target.coordinates.left &&
this.coordinates.right <= target.coordinates.right) {
return true;
}
else if (target.coordinates.right >= this.coordinates.left &&
target.coordinates.right <= this.coordinates.right) {
return true;
}
}
return false;
}

public void stickTo(GameObject target) {


if (coordinates.direction == Directions.UP)
coordinates.y = target.coordinates.y + target.coordinates.height + 1;
else if (coordinates.direction == Directions.DOWN)
coordinates.y = target.coordinates.y - coordinates.height - 1;
else if (coordinates.direction == Directions.RIGHT)
coordinates.x = target.coordinates.x - coordinates.width - 1;
else if (coordinates.direction == Directions.LEFT)
coordinates.x = target.coordinates.x + target.coordinates.width + 1;
coordinates.recalculate();
}

public void bounceOff(GameObject target) {


stickTo(target);
rebound();
}

private void rebound() {


if (coordinates.direction == Directions.NORTH)
coordinates.direction = Directions.SOUTH;
else if (coordinates.direction == Directions.NORTH_WEST)
coordinates.direction = Directions.NORTH_EAST;
else if (coordinates.direction == Directions.WEST)
coordinates.direction = Directions.EAST;
else if (coordinates.direction == Directions.SOUTH_WEST)
coordinates.direction = Directions.SOUTH_EAST;
else if (coordinates.direction == Directions.SOUTH)
coordinates.direction = Directions.NORTH;
else if (coordinates.direction == Directions.SOUTH_EAST)
coordinates.direction = Directions.SOUTH_WEST;
else if (coordinates.direction == Directions.EAST)
coordinates.direction = Directions.WEST;
else if (coordinates.direction == Directions.NORTH_EAST)
coordinates.direction = Directions.NORTH_WEST;
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.Timer;
/**
*
* @author n.fletcher
*/
public abstract class GameCharacter extends GameObject {
private int delay;
private ActionListener listener;
public Timer timer;

public GameCharacter(JLabel image, int amount, int delay) {


super(image);
this.coordinates.amount = amount;
this.delay = delay;
listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
action();
}
};
timer = new Timer(delay,listener);
}

public abstract void action();

@Override
public void spawn() {
super.spawn();
super.image.setVisible(true);
// this.coordinates.direction = Directions.STOP;
timer.start();
}
@Override
public void kill() {
super.kill();
super.image.setVisible(false);
timer.stop();
}

public void checkDirection() {


if (this.coordinates.direction == Directions.NORTH) this.moveNorth();
else if (this.coordinates.direction == Directions.NORTH_WEST)
this.moveNorthWest();
else if (this.coordinates.direction == Directions.WEST) this.moveWest();
else if (this.coordinates.direction == Directions.SOUTH_WEST)
this.moveSouthWest();
else if (this.coordinates.direction == Directions.SOUTH) this.moveSouth();
else if (this.coordinates.direction == Directions.SOUTH_EAST)
this.moveSouthEast();
else if (this.coordinates.direction == Directions.EAST) this.moveEast();
else if (this.coordinates.direction == Directions.NORTH_EAST)
this.moveNorthEast();
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
/**
*
* @author n.fletcher
*/
public class Background extends GameObject{

public Background(JLabel image) {


super(image);
Icon background = new ImageIcon("F:\\My Pictures\\Starfield.png");
super.image.setIcon(background);
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
*
* @author Nicholas
*/
public class CShot extends GameCharacter{
private final Wall[] walls;
private final Wachs hero;
private final Cruiser cruiser;

public CShot(JLabel image, Wall[] walls, Wachs hero, Cruiser cruiser) {


super(image,1,1);
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Cruiser Bullet.png");
super.image.setIcon(background);
this.hero = hero;
this.walls = walls;
this.cruiser = cruiser;
}

private void checkWalls() {


for (int i = 0; i < walls.length; i++) {
if (isCollidingWith(walls[i])) {
super.kill();
shoot();
return;
}
}
}

private void checkHero() {


if (isCollidingWith(hero)) {
super.kill();
hero.destroy();
}
}

@Override
public void action() {
super.checkDirection();
checkWalls();
checkHero();
super.redraw();
}

public void shoot() {


super.spawn();
super.coordinates.y = cruiser.coordinates.bottom;
super.coordinates.x = cruiser.coordinates.x +
(cruiser.coordinates.width / 2) -
(super.coordinates.width / 2);
super.coordinates.direction = Directions.SOUTH;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;

/**
*
* @author Nicholas
*/
public class Cruiser extends GameCharacter{

private final Wall[] walls;


private final Wachs hero;
private CShot cShot;
private Timer explosionDelay;

public Cruiser(JLabel image, Wall[] walls, Wachs hero) {


super(image,1,1);
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Bird of Prey.png");
this.image.setIcon(background);
this.hero = hero;
this.walls = walls;
explosionDelay = new Timer(750,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endAnimation();
}
});
}

@Override
public void action() {
super.checkDirection();
shoot();
checkWalls();
super.redraw();
}

private void checkWalls() {


for (int i = 0; i < walls.length; i++) {
if (isCollidingWith(walls[i])) {
bounceOff(walls[i]);
}
}
}

@Override
public void spawn() {
super.spawn();
coordinates.x = -170;
coordinates.y = 150;
coordinates.width = 170;
coordinates.height = 105;
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Bird of Prey.png");
this.image.setIcon(background);
super.coordinates.direction = randomDirection();
}

private int randomDirection() {


int random = (int)((2-1+1)*Math.random()+1);
if (random == 1) return Directions.WEST;
else if (random == 2) return Directions.EAST;
else return Directions.STOP;
}

void connectBullet(CShot cShot) {


this.cShot = cShot;
cShot.shoot();
}

private void shoot() {


if (cShot.isAlive() == false) cShot.shoot();
}
public void destroy() {
Icon destroyed = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Explosion.gif");
this.image.setIcon(destroyed);
coordinates.width = 142;
coordinates.height = 200;
super.coordinates.direction = Directions.STOP;
explosionDelay.start();
}

private void endAnimation() {


this.image.setVisible(false);
spawn();
explosionDelay.stop();
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;

/**
*
* @author Nicholas
*/
public class DiveBomber extends GameCharacter{

private final Wall[] walls;


private final Wachs hero;
private Timer explosionDelay;

public DiveBomber(JLabel image, Wall[] walls, Wachs hero) {


super(image,1,1);
this.hero = hero;
this.walls = walls;
explosionDelay = new Timer(750,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endAnimation();
}
});
}

@Override
public void action() {
super.checkDirection();
checkWalls();
checkHero();
super.redraw();
}

private void checkWalls() {


for (int i = 0; i < walls.length; i++) {
if (isCollidingWith(walls[i])) {
spawn();
}
}
}

private void checkHero() {


if (isCollidingWith(hero)) {
this.destroy();
hero.destroy();
}
}

@Override
public void spawn() {
super.spawn();
int random = (int)((2-1+1)*Math.random()+1);
if (random == 1)
{
Icon background1 = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Space War 1.png");
this.image.setIcon(background1);
coordinates.x = setX();
coordinates.y = setY();
coordinates.width = 52;
coordinates.height = 100;
}
else
{
Icon background2 = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Space War 2.png");
this.image.setIcon(background2);
coordinates.x = setX();
coordinates.y = setY();
coordinates.width = 30;
coordinates.height = 135;
}
super.coordinates.direction = Directions.SOUTH;
}

public int setX() {


return (int)Math.floor(Math.random()*(900-100+1)+100);
}

public int setY() {


return -100;
}

public void destroy() {


Icon destroyed = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Explosion.gif");
this.image.setIcon(destroyed);
coordinates.width = 142;
coordinates.height = 200;
super.coordinates.direction = Directions.STOP;
explosionDelay.start();
}

private void endAnimation() {


this.image.setVisible(false);
spawn();
explosionDelay.stop();
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
*
* @author n.fletcher
*/
public class FShot extends GameCharacter {

private final Wall[] walls;


private final Wachs hero;
private final Fighter fighter;

public FShot(JLabel image, Wall[] walls, Wachs hero, Fighter fighter) {


super(image,1,1);
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Fighter Bullet.png");
super.image.setIcon(background);
this.hero = hero;
this.walls = walls;
this.fighter = fighter;
}

private void checkWalls() {


for (int i = 0; i < walls.length; i++) {
if (isCollidingWith(walls[i])) {
super.kill();
shoot();
return;
}
}
}

private void checkHero() {


if (isCollidingWith(hero)) {
super.kill();
hero.destroy();
}
}

@Override
public void action() {
super.checkDirection();
checkWalls();
checkHero();
super.redraw();
}

public void shoot() {


super.spawn();
super.coordinates.y = fighter.coordinates.bottom;
super.coordinates.x = fighter.coordinates.x +
(fighter.coordinates.width / 2) -
(super.coordinates.width / 2);
super.coordinates.direction = Directions.SOUTH;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;
/**
*
* @author n.fletcher
*/
public class Fighter extends GameCharacter{

private final Wall[] walls;


private final Wachs hero;
private FShot fShot;
private Timer explosionDelay;

public Fighter(JLabel image, Wall[] walls, Wachs hero) {


super(image,1,1);
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "TIE Fighter.png");
this.image.setIcon(background);
this.hero = hero;
this.walls = walls;
explosionDelay = new Timer(750,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endAnimation();
}
});
}

@Override
public void action() {
super.checkDirection();
shoot();
checkWalls();
checkHero();
super.redraw();
}

private void checkWalls() {


for (int i = 0; i < walls.length; i++) {
if (isCollidingWith(walls[i])) {
spawn();
return;
}
}
}

private void checkHero() {


if (isCollidingWith(hero)) {
this.destroy();
hero.destroy();
}
}

@Override
public void spawn() {
super.spawn();
coordinates.x = setX();
coordinates.y = setY();
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "TIE Fighter.png");
this.image.setIcon(background);
coordinates.width = 100;
coordinates.height = 86;
super.coordinates.direction = randomDirection();
}

private int randomDirection() {


int random = (int)((2-1+1)*Math.random()+1);
if (random == 1) return Directions.SOUTH_WEST;
else if (random == 2) return Directions.SOUTH_EAST;
else return Directions.STOP;
}

public int setX() {


return (int)Math.floor(Math.random()*(900-100+1)+100);
}

public int setY() {


return -100;
}

void connectBullet(FShot fShot) {


this.fShot = fShot;
fShot.shoot();
}

private void shoot() {


if (fShot.isAlive() == false) fShot.shoot();
}

public void destroy() {


Icon destroyed = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Explosion.gif");
this.image.setIcon(destroyed);
coordinates.width = 142;
coordinates.height = 200;
super.coordinates.direction = Directions.STOP;
explosionDelay.start();
}

private void endAnimation() {


this.image.setVisible(false);
spawn();
explosionDelay.stop();
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;

/**
*
* @author Nicholas
*/
public class GameTime {

public Timer timer;


private final JFrame gui;

public GameTime(JFrame gui) {


this.gui = gui;
timer = new Timer(12000,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endAnimation();
}
});
}

private void endAnimation() {


timer.stop();
gui.dispose();
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
*
* @author Nicholas
*/
public class HShot extends GameCharacter {

private final Wall[] walls;


private final Fighter[] fighters;
private final DiveBomber[] bombers;
private final Cruiser cruiser;
private final Wachs hero;
int hit = 0;
int score = 0;
boolean isFired = false;

public HShot(JLabel image, Wall[] walls, Wachs hero, Fighter[] fighters,


DiveBomber[] bombers, Cruiser cruiser) {
super(image,1,1);
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Hero Bullet.png");
super.image.setIcon(background);
this.hero = hero;
this.walls = walls;
this.fighters = fighters;
this.bombers = bombers;
this.cruiser = cruiser;
}

private void checkWalls() {


for (int i = 0; i < walls.length; i++) {
if (isCollidingWith(walls[i])) {
super.kill();
isFired = false;
}
}
}

private void checkFighters() {


for (int i = 0; i < fighters.length; i++) {
if (isCollidingWith(fighters[i])) {
super.kill();
isFired = false;
score = score + 100;
fighters[i].destroy();
}
}
}

private void checkBombers() {


for (int i = 0; i < bombers.length; i++) {
if (isCollidingWith(bombers[i])) {
super.kill();
isFired =false;
score = score + 50;
bombers[i].destroy();
}
}
}

private void checkCruisers() {


if (hit == 0)
{
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Bird of Prey.png");
cruiser.image.setIcon(background);
}
if (isCollidingWith(cruiser)) {
super.kill();
isFired = false;
score = score + 300;
hit = hit + 1;
if (hit == 1)
{
Icon hit1 = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Bird of Prey(Hit 1).png");
cruiser.image.setIcon(hit1);
}
else if (hit == 2)
{
Icon hit2 = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Bird of Prey(Hit 2).png");
cruiser.image.setIcon(hit2);
}
else if (hit == 3)
{
cruiser.destroy();
hit = hit - 3;
}
}
}

@Override
public void action() {
super.checkDirection();
checkWalls();
checkFighters();
checkBombers();
checkCruisers();
super.redraw();
}

public void shoot() {


super.spawn();
isFired = true;
super.coordinates.y = hero.coordinates.top;
super.coordinates.x = hero.coordinates.x +
(hero.coordinates.width / 2) -
(super.coordinates.width / 2);
super.coordinates.direction = Directions.NORTH;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
*
* @author Nicholas
*/
public class HeroLife extends GameObject{

public HeroLife(JLabel image) {


super(image);
coordinates.width = 141;
coordinates.height = 80;
Icon fullLife = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "LiveCount(3).png");
this.image.setIcon(fullLife);
}

public void Hit1() {


coordinates.width = 95;
coordinates.height = 80;
Icon Lost1Life = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "LiveCount(2).png");
this.image.setIcon(Lost1Life);
}

public void Hit2() {


coordinates.width = 50;
coordinates.height = 80;
Icon Lost2Life = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "LiveCount(1).png");
this.image.setIcon(Lost2Life);
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;
/**
*
* @author n.fletcher
*/
public class Wachs extends GameCharacter{

private final Wall[] walls;


private HShot hShot;
private HeroLife lives;
private Timer explosionDelay;
int hit = 0;

public Wachs(JLabel image, Wall[] walls, HeroLife lives) {


super(image,1,2);
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Wachs' Ship.png");
super.image.setIcon(background);
this.walls = walls;
this.lives = lives;
explosionDelay = new Timer(750,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endAnimation();
}
});
}

@Override
public void action() {
super.checkDirection();
checkWalls();
super.redraw();
}

public void keyPress(KeyEvent event) {


if (event.getKeyCode() == KeyEvent.VK_LEFT)
super.coordinates.direction = Directions.LEFT;
else if (event.getKeyCode() == KeyEvent.VK_RIGHT)
super.coordinates.direction = Directions.RIGHT;
else if (event.getKeyCode() == KeyEvent.VK_SPACE)
if (hShot.isFired == false)
{
hShot.shoot();
}
}

private void checkWalls() {


for (int i = 0; i < walls.length; i++) {
if (isCollidingWith(walls[i])) {
bounceOff(walls[i]);
}
}
}

void connectBullet(HShot hShot) {


this.hShot = hShot;
}
@Override
public void spawn() {
super.spawn();
hit = hit + 1;
if (hit == 2)
{
lives.Hit1();
}
else if (hit == 3)
{
lives.Hit2();
}
else if (hit == 4)
{
System.exit(0);
}
coordinates.x = 450;
coordinates.y = 535;
coordinates.width = 95;
coordinates.height = 162;
Icon background = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Wachs' Ship.png");
super.image.setIcon(background);
}

public void destroy() {


Icon destroyed = new ImageIcon("F:\\My Pictures\\SMSEU Enemies\\"
+ "Explosion.gif");
this.image.setIcon(destroyed);
coordinates.width = 142;
coordinates.height = 200;
super.coordinates.direction = Directions.STOP;
explosionDelay.start();
}

private void endAnimation() {


this.image.setVisible(false);
spawn();
explosionDelay.stop();
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;
import java.awt.Color;
import javax.swing.JLabel;
/**
*
* @author n.fletcher
*/
public class Wall extends GameObject{

public Wall(JLabel image) {


super(image);
super.image.setBackground(Color.BLUE);
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author n.fletcher
*/
public class GameEngine {

private final Wall[] walls;


private final Fighter[] fighters;
private final CShot[] cShots;
private final FShot[] fShots;
private final HShot[] hShots;
private final DiveBomber[] bombers;
private final Cruiser cruiser;
public Wachs hero;
private final Background background;
private final HeroLife lives;
private final JFrame gui;
private final GameTime time;

public GameEngine(JLabel wachsImage, JLabel backgroundImage,


JLabel livesImage, JLabel[] bomberImages, JLabel cruiserImage,
JLabel[] cShotImages, JLabel[] fShotImages, JLabel[] hShotImages,
JLabel[] wallImages, JLabel[] fighterImages, JFrame gui) {
this.gui = gui;
background = new Background(backgroundImage);
time = new GameTime(gui);
lives = new HeroLife(livesImage);
walls = new Wall[wallImages.length];
hero = new Wachs(wachsImage,walls,lives);
fighters = new Fighter[fighterImages.length];
bombers = new DiveBomber[bomberImages.length];
cruiser = new Cruiser(cruiserImage,walls,hero);
cShots = new CShot[cShotImages.length];
fShots = new FShot[fShotImages.length];
hShots = new HShot[hShotImages.length];
for (int i = 0; i < walls.length; i++) {
walls[i] = new Wall(wallImages[i]);
}
for (int i = 0; i < fighters.length; i++) {
fighters[i] = new Fighter(fighterImages[i],walls,hero);
}
for (int i = 0; i < bombers.length; i++) {
bombers[i] = new DiveBomber(bomberImages[i],walls,hero);
}
for (int i = 0; i < cShots.length; i++) {
cShots[i] = new CShot(cShotImages[i],walls,hero,cruiser);
}
for (int i = 0; i < fShots.length; i++) {
fShots[i] = new FShot(fShotImages[i],walls,hero,fighters[i]);
}
for (int i = 0; i < hShots.length; i++) {
hShots[i] = new HShot(hShotImages[i],walls,hero,fighters,bombers,
cruiser);
}
for (int i = 0; i < cShots.length; i++) {
cruiser.connectBullet(cShots[i]);
}
for (int i = 0; i < fighters.length; i++) {
fighters[i].connectBullet(fShots[i]);
}
for (int i = 0; i < hShots.length; i++) {
hero.connectBullet(hShots[i]);
hShots[i].image.setVisible(false);
}

public void start() {


hero.spawn();
background.spawn();
lives.spawn();
for (int i = 0; i < fighters.length; i++) {
fighters[i].spawn();
}
for (int i = 0; i < bombers.length; i++) {
bombers[i].spawn();
}
cruiser.spawn();
for (int i = 0; i < walls.length; i++) {
walls[i].spawn();
}
for (int i = 0; i < cShots.length; i++) {
cShots[i].spawn();
}
for (int i = 0; i < fShots.length; i++) {
fShots[i].spawn();
}
for (int i = 0; i < hShots.length; i++) {
hShots[i].spawn();
}
gui.setSize(1000,750);
gui.setVisible(true);
time.timer.start();
}

public void keyPress(KeyEvent event) {


hero.keyPress(event);
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

/**
*
* @author n.fletcher
*/
public class SpaceMonsterShootEmUp {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GUI gui = new GUI();
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package space.monster.shoot.em.up;

import javax.swing.JLabel;
/**
*
* @author n.fletcher
*/
public class GUI extends javax.swing.JFrame {

private GameEngine engine;


/**
* Creates new form GUI
*/
public GUI() {
initComponents();
JLabel[] wallLabels = {wall1, wall2, wall3, wall4};
JLabel[] fighterLabels = {fighter1, fighter2};
JLabel[] bomberLabels = {bomber1, bomber2};
JLabel cruiserLabel = cruiser1;
JLabel[] cShotLabels = {cShot1};
JLabel[] fShotLabels = {fShot1,fShot2};
JLabel[] hShotLabels = {hShot1};
JLabel backgroundLabel = background;
JLabel wachsLabel = wachs;
JLabel lifeLabel = lives;
wall1.setBounds(-200, -200, 1, 1150);
wall2.setBounds(1200, -200, 1, 1150);
wall3.setBounds(-200, -200, 1400, 1);
wall4.setBounds(-200, 950, 1400, 1);
hShot1.setBounds(-100, -100, 10, 30);
lives.setBounds(0, 630, 141, 80);
engine = new GameEngine(wachsLabel, backgroundLabel, lifeLabel,
bomberLabels, cruiserLabel, cShotLabels, fShotLabels,
hShotLabels, wallLabels, fighterLabels, this);
engine.start();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


wall1 = new javax.swing.JLabel();
wall2 = new javax.swing.JLabel();
wall3 = new javax.swing.JLabel();
wall4 = new javax.swing.JLabel();
wachs = new javax.swing.JLabel();
fighter1 = new javax.swing.JLabel();
fighter2 = new javax.swing.JLabel();
fShot1 = new javax.swing.JLabel();
fShot2 = new javax.swing.JLabel();
bomber1 = new javax.swing.JLabel();
bomber2 = new javax.swing.JLabel();
hShot1 = new javax.swing.JLabel();
cruiser1 = new javax.swing.JLabel();
cShot1 = new javax.swing.JLabel();
lives = new javax.swing.JLabel();
background = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(1000, 750));
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});

jPanel1.setOpaque(false);
jPanel1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jPanel1KeyPressed(evt);
}
});
jPanel1.setLayout(null);
jPanel1.add(wall1);
wall1.setBounds(0, 0, 0, 750);
jPanel1.add(wall2);
wall2.setBounds(1000, 0, 0, 750);
jPanel1.add(wall3);
wall3.setBounds(0, 0, 1000, 0);
jPanel1.add(wall4);
wall4.setBounds(0, 750, 1000, 0);
jPanel1.add(wachs);
wachs.setBounds(450, 535, 95, 162);
jPanel1.add(fighter1);
fighter1.setBounds(340, 100, 100, 86);
jPanel1.add(fighter2);
fighter2.setBounds(581, 79, 100, 86);
jPanel1.add(fShot1);
fShot1.setBounds(800, 180, 30, 30);
jPanel1.add(fShot2);
fShot2.setBounds(701, 179, 30, 30);
jPanel1.add(bomber1);
bomber1.setBounds(240, 140, 52, 100);
jPanel1.add(bomber2);
bomber2.setBounds(240, 140, 52, 100);
jPanel1.add(hShot1);
hShot1.setBounds(130, 30, 10, 30);
jPanel1.add(cruiser1);
cruiser1.setBounds(0, 0, 170, 105);

cShot1.setToolTipText("");
jPanel1.add(cShot1);
cShot1.setBounds(0, 0, 10, 30);
jPanel1.add(lives);
lives.setBounds(0, 670, 141, 80);
jPanel1.add(background);
background.setBounds(0, 0, 1040, 750);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 1050,
Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 750,
Short.MAX_VALUE)
);

pack();
}// </editor-fold>

private void jPanel1KeyPressed(java.awt.event.KeyEvent evt) {


engine.keyPress(evt);
}

private void formKeyPressed(java.awt.event.KeyEvent evt) {


engine.keyPress(evt);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel background;
private javax.swing.JLabel bomber1;
private javax.swing.JLabel bomber2;
private javax.swing.JLabel cShot1;
private javax.swing.JLabel cruiser1;
private javax.swing.JLabel fShot1;
private javax.swing.JLabel fShot2;
private javax.swing.JLabel fighter1;
private javax.swing.JLabel fighter2;
private javax.swing.JLabel hShot1;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lives;
private javax.swing.JLabel wachs;
private javax.swing.JLabel wall1;
private javax.swing.JLabel wall2;
private javax.swing.JLabel wall3;
private javax.swing.JLabel wall4;
// End of variables declaration
}

S-ar putea să vă placă și