Space Invaders Applet

<< TileGridApplet | FinalProjectsTrailIndex | Astronomy >>

SpaceInvaderApplet.java

Demo Video

you will need the gif files from SpaceInvaderGame and tilegrid.jar library installed

import sfhs.cct.tilegrid.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.Timer;

import sfhs.cct.tilegrid.*;


public class SpaceInvaderApplet extends TileGridApplet 
{
	private Player player;
	private Ship ship;
	private Color skyBlue=new Color(0,100,155);
	private final int LEFT =37;
	private final int UP=38;
	private final int RIGHT=39;
	private final int DOWN=40;
	private Timer timeToMove;
	private int speed=150;

	public void init(){
		super.init();
		grid=new TileGrid(10,10);
		grid.setShowLines(false);
		grid.setBGColor(skyBlue);
		this.requestFocus();
		timeToMove = new Timer(speed, this);
	}
	public void newRound(){
		resetTimer();
        startTimer();
        timeToMove.start();
        grid= new TileGrid(10, 10 );
        grid.setShowLines(false);
        grid.setBGColor(skyBlue);
        player = new Player();
        grid.add(player, 9, 5);
        ship = new Ship(player);
        ship.setProbability(.4);
        grid.add(ship, 0, 0);
	}
	public void gameOver(){
		stopTimer();
        timeToMove.stop();
        setMessage(getMessage()+"Click Start to begin a New Round");

	}
	public void paint(Graphics g)
    {
        // We draw using the object methods of
        // the graphics buffer, not what is
        // currently on the screen
    	    virtualMem = createImage(getWidth(),getHeight());
        Graphics gBuffer = virtualMem.getGraphics();
        gBuffer.setColor(Color.white);
        gBuffer.fillRect(0,0,getWidth(),getHeight());
        gBuffer.setColor(Color.black);

        grid.draw(gBuffer, getWidth()/12, getHeight()/12, 
        		getWidth()/12);
        gBuffer.drawString("Time: "+getTime(), 20,20);

        gBuffer.drawString(getMessage(), 20, getHeight()-20);

        //Now we send the result to the screen
        g.drawImage(virtualMem,0,0,this);
    }
	//implement the ActionListener
	public void actionPerformed(ActionEvent e) {

        if ( isTimerEvent(e)) {
            setTime(getTime()+1);           
        }else if (e.getSource()==timeToMove){
        		//ship moves, and bombs fall
        		if (player.getGrid()==null){
        			int r=player.getRow();
        			int c=player.getCol();
        			Tile splat=new Tile(Color.RED);
        			grid.add(splat, r, c);
        			setMessage("You died");
        			gameOver();
        		}
        		grid.activate();

        }else{
            //restart button pressed

              if (isTimerRunning()) {
                  gameOver();
               }else{
                   newRound();
                   setMessage("Use Arrows to Avoid Bombs");
               }
        }
        this.requestFocus();
        repaint();
	}
	//KeyListener Methods
	public void keyPressed(KeyEvent e) {
		int keyCode=e.getKeyCode();
		if(player.getGrid()!=null){
			int col=player.getCol();

			if (keyCode==LEFT && col>0){
				col--;
			}else if (keyCode==RIGHT && col<9){
				col++;
			}
			if (grid.isEmpty(9, col)){
				grid.move(player, 9, col);
			} else {
				player.removeSelfFromGrid();
			}
			repaint();
		}else{
			stopTimer();
            timeToMove.stop();
            setMessage("You died");
		}
	}
	public void mouseReleased(MouseEvent e) {
		this.requestFocus();

	}
}


Bomb.java

import java.awt.image.*;
import java.io.*;

import javax.imageio.*;

import sfhs.cct.tilegrid.*;

public class Bomb extends Tile 
{
	private Tile target;
	public Bomb(Tile target){
		super();
		this.target=target;
		File f=new File("Bomb.gif");

		try {
			BufferedImage pic = ImageIO.read(f);
			setImg(pic);

		} catch (IOException err) {
			err.printStackTrace();
		}
	}
	public void act(){
		if (grid!=null){
			int row=this.getRow();
			int col=this.getCol();
			if (row<9){
				row++;
			}
			if (grid.isEmpty(row, col)){
				grid.move(this, row, col);
			}else if (grid.get(row, col)==target){
				target.removeSelfFromGrid();
			}
			if (row==9){
				removeSelfFromGrid();
			}
		}
	}

}

Player.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import sfhs.cct.tilegrid.Tile;


public class Player extends Tile 
{
	public Player(){
		super();
		File f=new File("Player.gif");

		try {
			BufferedImage pic = ImageIO.read(f);
			setImg(pic);

		} catch (IOException err) {
			err.printStackTrace();
		}
	}


}

Ship.java

import java.awt.image.*;
import java.io.*;

import javax.imageio.*;

import sfhs.cct.tilegrid.*;

public class Ship extends Tile 
{	
	private int direction=1;
	private double probability=.8;
	private Tile target;
	public Ship(Tile target){
		super();
		this.target=target;
		File f=new File("Ship.gif");

		try {
			BufferedImage pic = ImageIO.read(f);
			setImg(pic);

		} catch (IOException err) {
			err.printStackTrace();
		}
	}
	public void setProbability(double p){
		this.probability=p;
	}
	public double getProbability(){
		return probability;
	}
	public void act(){
		int col=getCol();
		int row=getRow();
		if (!grid.isValid(0, col+direction))
			direction*=-1;
		grid.move(this, row, col+direction);
		if (Math.random()<probability){
			Bomb b=new Bomb(target);
			grid.add(b, row+1, col);
		}
	}
}