Space Invader Game

<< Gridworld Dice Games | FinalProjectsTrailIndex | Slider Puzzle >>

Player.java

import java.awt.Color;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class Player extends Actor {

	public Player() {
		this.setColor(Color.red);
	}
	public void act(){
		//do nothing
	}
	public void move(String d){
		if (d.equals("LEFT") ){
			Location newLoc=getLocation().getAdjacentLocation(Location.WEST);
			Grid gr = getGrid();
			if(gr!=null && gr.isValid(newLoc)){
				moveTo(newLoc);
			}
		}
		if (d.equals("RIGHT") ){
			Location newLoc=getLocation().getAdjacentLocation(Location.EAST);
			Grid gr = getGrid();
			if(gr!=null && gr.isValid(newLoc)){
				moveTo(newLoc);
			}
		}

	}

}

Ship.java

import java.awt.Color;
import info.gridworld.actor.Actor;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class Ship extends Actor {
	private int dir;
	private double probability;
	public Ship(double p) {
		setColor(Color.CYAN);
		dir=Location.EAST;
		probability=p;
	}
	public void act(){
		Location newLoc=getLocation().getAdjacentLocation(dir);
		Grid<Actor> gr = getGrid();
		if(gr.isValid(newLoc)){
			moveTo(newLoc);
		}else{
			if(dir==Location.EAST)
				dir=Location.WEST;
			else
				dir=Location.EAST;
			newLoc=getLocation().getAdjacentLocation(dir);
			if(gr.isValid(newLoc))
				moveTo(newLoc);

		}
		if (Math.random()< probability)
				dropBomb(gr, newLoc);
	}
	public void dropBomb(Grid gr, Location loc){
		Location bombLoc =loc.getAdjacentLocation(Location.SOUTH);
		Bomb bomb=new Bomb();
		bomb.putSelfInGrid(gr, bombLoc);
	}

}

Bomb.java

import java.awt.Color;

import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;


public class Bomb extends Actor {
	public Bomb(){
		setColor(Color.BLUE);

	}
	public void act(){
		Grid gr=getGrid();
		Location newLoc=getLocation().getAdjacentLocation(Location.SOUTH	);
		if(gr.isValid(newLoc))
			moveTo(newLoc);

	}
}

InvaderGame.java

import java.util.ArrayList;

import info.gridworld.actor.Actor;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.*;
public class InvaderGame extends ActorWorld
{
    private Grid gr;
    private Player player;
    private Ship ship;
    public InvaderGame(){
        gr=new BoundedGrid(15,15);
        this.setGrid(gr);
        System.setProperty("info.gridworld.gui.selection", "hide");
    		System.setProperty("info.gridworld.gui.frametitle", "Invader Game");
    		System.setProperty("info.gridworld.gui.tooltips", "hide");
    		setMessage("Use arrows to move, Run to start game");
    		player = new Player();
    		ship = new Ship(.4);
    		add(new Location(14,2),player);
    		add(new Location(0,0),ship);
    }

    public boolean locationClicked(Location loc){

    		return false;
    }
    public boolean keyPressed(String description, Location loc){

    		player.move(description);

    		return false;
    }

    public void step(){

       Location playerLoc=player.getLocation();
       if (playerLoc !=null) {

    	   boolean alive=true;
       ArrayList<Location> places=(ArrayList<Location>)gr.getOccupiedLocations();
       for (int i=0; i<places.size();i++){
    	   	    Actor a=(Actor) gr.get(places.get(i));

    	   	    if (a instanceof Bomb){
    	   	    		a.act();
    	   	    		if (playerLoc.equals(a.getLocation()))
    	   	    			alive=false;
    	   	    		if (playerLoc.getRow()==a.getLocation().getRow())
    	   	    			a.removeSelfFromGrid();
    	   	    }
       }
       ship.act();
       if (!alive){
    	   	setMessage("You got hit");

       }
       }
    }
    public static void main(String[] args)
	{
             InvaderGame game=new InvaderGame();
             game.show();
	}
}