Shooter Game By Josh And Chris

<< Card Game by Chris | OldProjectsTrailIndex | Alexander's Final Project >>

GhostRush

Bullet.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class Bullet implements ActionListener
{
	//variables
	private int y, x;
	private Timer timer;

	//constructor
	public Bullet() 
	{
		y = 240;
		x = 133;
		timer = new Timer(10,this);
		timer.start();
	}

	//getters
	public void setY(int loc)
	{
		if(loc==0) y=240;
		if(loc==1) y=340;
		if(loc==2) y=440;
		if(loc==3) y=540;
	}
	public Rectangle getRect()
	{
		Rectangle r = new Rectangle(x,y,14,15);
		return r;
	}

	//setters
	public int getX() {return x;}
	public void stopIt() {timer.stop();}

	//draw
	public void draw(Graphics g)
	{
		g.setColor(Color.YELLOW);
		g.fillRect(x,y,15,15);
	}
	//event
	public void actionPerformed(ActionEvent arg0) {x+=15;}
}

Enemy.java

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class Enemy implements ActionListener
{
	//variables
	private Image img;
	private Timer timer;
	private int rate, x, y;

	//constructor
	public Enemy(Image pic, int r)
	{
		img = pic;
		rate = r;
		int loc =(int) (Math.random()*4);
		if(loc==0) y=200;
		if(loc==1) y=300;
		if(loc==2) y=400;
		if(loc==3) y=500;
		x = 800;
		timer = new Timer(60/rate,this);
		timer.start();
	}

	//getters
	public int getX() {return x;}
	public Rectangle getRect()
	{
		Rectangle r = new Rectangle(x,y,100,100);
		return r;
	}

	//setters
	public void stopIt()
	{
		timer.stop();
	}

	//draw
	public void draw(Graphics g)
	{
		g.drawImage(img,x,y,100,100, null);
	}

	//event
	public void actionPerformed(ActionEvent arg0) 
	{
		x-= 3*(rate);
	}
}

Grid.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class Grid 
{
	//variables
	int lives;

	//constructor
	public Grid() 
	{
		lives = 5;
	}

	//draw
	public void draw(Graphics g)
	{
		g.setColor(Color.GRAY);
		g.fillRect(0,0,900,600);
		g.setColor(Color.WHITE);
		g.fillRect(0,200,900,400);
		g.setColor(Color.WHITE);
		g.setFont(new Font("Helvetica", Font.BOLD, 100));
		g.drawString("Ghost", 20, 80);
		g.drawString("Rush", 45, 170);
		g.setColor(new Color(16,90,240));
		g.fillRect(550, 40, 250, 80);
		g.fillRect(525, 60, 25, 40);
		g.fillRect(800, 60, 25, 40);
		g.setColor(new Color(255,106,0));
		int xtra = 8;
		for(int l = 0; l < lives; l++)
		{
			g.fillRect(550 + xtra, 50, 43, 60);
			xtra += 48;
		}
	}

	//getters
	public int getLives()
	{
		return lives;
	}

	//setters
	public void hurt()
	{
		lives--;
	}
}

Player.java

import java.awt.Graphics;
import java.awt.Image;

public class Player 
{
	//variables
	private boolean attack;
	private Image stand,shoot;
	private int y,loc;

	//constructor
	public Player(Image stand, Image shoot)
	{
		this.stand = stand;
		this.shoot = shoot;
		attack = false;
		y = 10;
		loc = 0;
	}

	//getters
	public boolean isAttack() {return attack;}
	public int getLoc() {return loc;}

	//setters
	public void fire() {attack=true;}
	public void ceaseFire() {attack=false;}
	public void up() {if(loc > 0) loc--;}
	public void down() {if(loc < 3) loc++;}

	//draw
	public void draw(Graphics g)
	{
		Image img;
		int width = 0;
		if(attack)
		{
			img = shoot;
			width = 400/3;
		}
		else 
		{
			img = stand;
			width = 100;
		}
		if(loc==0) y=200;
		if(loc==1) y=300;
		if(loc==2) y=400;
		if(loc==3)y=500;
		g.drawImage(img,10,y,width,100,null);
	}
}

Runner.java

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import javax.swing.Timer;

public class Runner extends Applet implements KeyListener, ActionListener
{
	//variables
	private static final long serialVersionUID = 1L;
	private static final int WIDTH = 900;
	private static final int HEIGHT = 600;
	private Player mega;
	private Image imageBuffer , shoot, stand, boo;
	private Graphics gBuffer;
	private Grid grid;
	private ArrayList<Bullet> bullets;
	private int rate, count, score,timeRate;
	private Timer timer;
	private ArrayList<Enemy> enemies;
	private boolean start,over;

	//basically the constructor
	public void init()
	{
		resize(WIDTH,HEIGHT);
		this.addKeyListener(this);
		shoot = loadImageFromJar("shoot.jpg");
		stand = loadImageFromJar("stand.jpg");
		boo = loadImageFromJar("newBoo.png");
		mega = new Player(stand,shoot);
		imageBuffer = createImage(WIDTH,HEIGHT);
        gBuffer = imageBuffer.getGraphics();
		grid = new Grid();
		bullets = new ArrayList<Bullet>();
		enemies = new ArrayList<Enemy>();
		rate = 1;
		count = 0;
		score = 0;
		start = false;
		over = false;
		timeRate = 500;
		timer = new Timer(timeRate, this);
		timer.start();
	}

	//draw
	public void paint(Graphics g)
	{
		if(grid.getLives() < 1)
		{
			gBuffer.setColor(Color.BLACK);
			gBuffer.fillRect(0, 0, 900, 600);
			gBuffer.setColor(Color.WHITE);
			gBuffer.setFont(new Font("Helvetica", Font.BOLD, 60));
			gBuffer.drawString("Score "+score, 330, 250);
			gBuffer.drawString("Enter to Play Again", 150, 350);
			g.drawImage(imageBuffer,0,0,this);
			over = true;
		}
		if(!start)
		{
			gBuffer.setColor(Color.BLACK);
			gBuffer.fillRect(0, 0, 900, 600);
			gBuffer.setColor(Color.WHITE);
			gBuffer.setFont(new Font("Helvetica", Font.BOLD, 40));
			gBuffer.drawString("Press Space To Play", 250, 300);
			g.drawImage(imageBuffer,0,0,this);
		}
		if(start && !over  && grid.getLives() > 0)
		{
			grid.draw(gBuffer);
			gBuffer.setColor(Color.WHITE);
			gBuffer.setFont(new Font("Helvetica", Font.BOLD, 40));
			gBuffer.drawString("Score: "+score, 550, 175);
			mega.draw(gBuffer);
			for(Bullet b: bullets)
			{
				if(b.getX() > 1000) b.stopIt();
				else b.draw(gBuffer);	
			}
			try
			{
				for(Enemy e: enemies)
				{
					for(Bullet c : bullets)
					{
						if(e.getRect().intersects(c.getRect()))
						{
							e.stopIt();
							c.stopIt();
							enemies.remove(e);
							bullets.remove(c);	
							score++;
						}
					}
					if(e.getX() < 70)
						{
							e.stopIt();
							enemies.remove(e);
							grid.hurt();
						}
					else e.draw(gBuffer);
				}
				g.drawImage(imageBuffer,0,0,this);
				repaint();
			}
			catch (ConcurrentModificationException e)
			{
				repaint();
			}
		}
	}
	public void update(Graphics g)
    {
        paint(g); 
    }

	//exception catcher
	public Image loadImageFromJar(String fileName)
	{
		Image img = null;
		try
		{
			img = getImage(getClass().getResource("/"+fileName));
		}
		catch (NullPointerException e)
		{
			//e.printStackTrace
		}
		return img;
	}

	//keys
	public void keyPressed(KeyEvent e) 
	{
		if(start && !over && e.getKeyCode()==KeyEvent.VK_SPACE)
		{
			mega.fire();
			Bullet b = new Bullet();
			b.setY(mega.getLoc());
			bullets.add(b);
		}
		if(!start && e.getKeyCode()==KeyEvent.VK_SPACE)
		{
			start = true;
		}
		if(over && e.getKeyCode()==KeyEvent.VK_ENTER)
		{
			grid = new Grid();
			bullets = new ArrayList<Bullet>();
			enemies = new ArrayList<Enemy>();
			rate = 1;
			count = 0;
			score = 0;
			over = false;
			timeRate = 500;
			timer.start();
		}
		if(start && !over && e.getKeyCode()==KeyEvent.VK_UP)
		{
			mega.up();
		}
		if(start && !over && e.getKeyCode()==KeyEvent.VK_DOWN)
		{
			mega.down();
		}
		repaint();
	}
	public void keyReleased(KeyEvent e) 
	{
		if(start && !over && e.getKeyCode()==KeyEvent.VK_SPACE)
		{
			mega.ceaseFire();
		}
		repaint();
	}
	public void keyTyped(KeyEvent e) {}

	//event
	public void actionPerformed(ActionEvent e)
	{
		if(start && !over)
		{
			if(count > 10)
			{
				count = 0;
				rate++;
				timeRate /= 20;
			}
			Enemy b = new Enemy(boo, rate);
			enemies.add(b);
			count++;
		}
	}
}