Instructions: A=Left, D=Right, W=Shoot
SOURCE CODE:
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Rectangle;
import java.util.Random;
public class SpaceInvaders extends Applet implements KeyListener, MouseListener
{
public static SpaceInvaders reference;
public static Image playerShip,badShip,pow,shot,badshot,playerPOW;
public static int score;
public static int level;
public static SpaceShip p;
public static Image backbuffer;
public static Graphics backg;
public static Timer t;
public static SpaceShip playerShot;
public static boolean clearWait;
public static boolean diedWait;
public static int difficultyRC,difficultyT;
public static SpaceShip alienShot;
public static boolean started;
public void init()
{
started = false;
clearWait=false;
diedWait = false;
removeKeyListener(this);
removeMouseListener(this);
addKeyListener(this);
addMouseListener(this);
reference = this;
backbuffer = createImage(getWidth(),getHeight());
backg = backbuffer.getGraphics();
backg.setColor(Color.white);
playerPOW = getImage(getDocumentBase(),"playerPOW.GIF");
playerShip = getImage(getDocumentBase(),"player.GIF");
badShip = getImage(getDocumentBase(),"badship.GIF");
pow = getImage(getDocumentBase(),"explosion.GIF");
shot = getImage(getDocumentBase(),"shot.GIF");
badshot = getImage(getDocumentBase(),"badshot.GIF");
difficultyRC = 3;
difficultyT = 100;
score = 0;
level = 1;
p = new SpaceShip(250,350,playerShip,29,29);
playerShot = new SpaceShip();
playerShot.shooting = false;
playerShot.width=7;
playerShot.height=8;
alienShot=new SpaceShip();
alienShot.shooting=false;
alienShot.width=4;
alienShot.height=7;
AlienGroup.newGroup(difficultyRC,difficultyRC);
t= new Timer();
}
public void refresh()
{
backg.setColor(Color.black);
backg.fillRect(0,0,501,501);
backg.setColor(Color.white);
backg.drawRect(0,0,500,500);
backg.drawLine(0,400,500,400);
backg.setFont(new Font("courier",Font.ITALIC,20));
backg.drawString(("Score: " + score),10,430);
backg.drawString(("Level: " + level),10,460);
backg.setColor(new Color(200,0,0));
backg.drawLine(1,300,499,300);
backg.setColor(Color.green);
backg.setFont(new Font("courier",Font.ITALIC,40));
backg.drawString("SPACE INVADERS",150,450);
backg.setFont(new Font("courier",Font.ITALIC,12));
backg.drawString("By Billy Harmon",250,470);
backg.drawImage(playerShip,p.x,p.y,null);
ArrayList temp;
for(int i = 0; i < AlienGroup.theGroup.size(); i++)
{
temp = (ArrayList)AlienGroup.theGroup.get(i);
for(int j = 0; j < temp.size(); j++)
{
if(((SpaceShip)temp.get(j)).alive)
backg.drawImage(((SpaceShip)temp.get(j)).img,((SpaceShip)temp.get(j)).x,((SpaceShip)temp.get(j)).y,null);
}
}
if(playerShot.shooting)
{
backg.drawImage(shot,playerShot.x,playerShot.y,null);
}
if(AlienGroup.allDead)
{
t.cancel();
backg.setColor(Color.green);
backg.setFont(new Font("courier",Font.ITALIC,40));
backg.drawString("Zone Cleared!",100,210);
backg.setFont(new Font("courier",Font.ITALIC,20));
backg.drawString("click to continue",150,250);
clearWait = true;
}
if(!p.alive)
{
t.cancel();
backg.drawImage(playerPOW,p.x,p.y,null);
backg.setColor(Color.red);
backg.setFont(new Font("courier",Font.ITALIC,40));
backg.drawString("GAME OVER",100,210);
backg.setFont(new Font("courier",Font.ITALIC,20));
backg.drawString("click to continue",150,250);
diedWait = true;
}
if(alienShot.shooting)
{
backg.drawImage(badshot,alienShot.x+8,alienShot.y,null);
}
if(!started)
{
backg.setColor(Color.white);
backg.drawString("CLICK TO START",200,200);
}
}
public void update( Graphics g )
{
refresh();
g.drawImage( backbuffer, 0, 0, this );
}
public void paint(Graphics g)
{
update(g);
}
public void keyTyped(KeyEvent e)
{
switch(e.getKeyChar())
{
case 'w':
{
if(!playerShot.shooting)
{
playerShot.shooting = true;
playerShot.x = p.x+14;
playerShot.y = p.y+10;
SpaceInvaders.reference.t.scheduleAtFixedRate(new MovePlayerShots(),30, 30);
}
break;
}
case 'a':
{
if(SpaceInvaders.reference.p.x>=20)
SpaceInvaders.reference.p.x-=20;
repaint();
break;
}
case 'd':
{
if(SpaceInvaders.reference.p.x<=450)
SpaceInvaders.reference.p.x+=20;
repaint();
break;
}
}
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void mouseClicked(MouseEvent e)
{
if(clearWait)
{
t = new Timer();
difficultyRC++;
difficultyT-=10;
level++;
alienShot.shooting=false;
AlienGroup.newGroup(difficultyRC,difficultyRC);
t.scheduleAtFixedRate(new MoveAliens(),difficultyT,difficultyT);
clearWait=false;
}
if(diedWait)
{
init();
}
if(!started)
{
started=true;
t.scheduleAtFixedRate(new MoveAliens(),difficultyT,difficultyT);
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
class SpaceShip extends Rectangle
{
public SpaceShip(){}
public boolean shooting;
public boolean alive;
public Image img;
public SpaceShip(int cx, int cy, Image i,int h, int w)
{
super(cx,cy,w,h);
img = i;
alive = true;
}
}
final class AlienGroup
{
public static ArrayList theGroup;
private static ArrayList rows;
public static int row,col;
public static boolean forward = true;
public static boolean allDead=false;
public static int deathCount=0;
public static SpaceShip randomShip()
{
Random r = new Random();
SpaceShip returnShip;
do
{
returnShip = (SpaceShip) ( (ArrayList) theGroup.get(adjustRows() - 1)).get(r.nextInt(col));
}while (!returnShip.alive);
return returnShip;
}
public static void newGroup(int r,int c)
{
row = r; col = c;
int drawX=20;
int drawY=0;
deathCount=0;
allDead = false;
forward = true;
theGroup = new ArrayList();
rows = new ArrayList();
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
rows.add(new SpaceShip(drawX,drawY+30,SpaceInvaders.reference.badShip,15,21));
drawX+=20;
}
theGroup.add(rows);
rows = new ArrayList();
drawX=20;
drawY+=30;
}
}
public static int adjustRows()
{
boolean all = true;
int returnRow = 0;
for(int i = 0; i < theGroup.size(); i++)
{
for(int j = 0; j < ((ArrayList)theGroup.get(i)).size(); j++)
{
if(((SpaceShip)((ArrayList)theGroup.get(i)).get(j)).alive)
{
all = false;
}
}
if(all)
returnRow++;
all = true;
}
return row-returnRow;
}
public static void down()
{
for(int i = 0; i < theGroup.size(); i++)
{
for(int j = 0; j < ((ArrayList)theGroup.get(i)).size(); j++)
{
((SpaceShip)((ArrayList)theGroup.get(i)).get(j)).y+=20;
}
}
}
public static void left()
{
for(int i = 0; i < theGroup.size(); i++)
{
for(int j = 0; j < ((ArrayList)theGroup.get(i)).size(); j++)
{
((SpaceShip)((ArrayList)theGroup.get(i)).get(j)).x-=5;
}
}
}
public static void right()
{
for(int i = 0; i < theGroup.size(); i++)
{
for(int j = 0; j < ((ArrayList)theGroup.get(i)).size(); j++)
{
((SpaceShip)((ArrayList)theGroup.get(i)).get(j)).x+=5;
}
}
}
public static int[] hit()
{
for(int i = 0; i < theGroup.size(); i++)
{
for(int j = 0; j < ((ArrayList)theGroup.get(i)).size(); j++)
{
if(((SpaceShip)((ArrayList)theGroup.get(i)).get(j)).intersects(SpaceInvaders.reference.playerShot)&&
((SpaceShip)((ArrayList)theGroup.get(i)).get(j)).alive)
{
deathCount++;
SpaceInvaders.reference.score+=10;
if(deathCount>=row*col)
{
allDead=true;
}
return new int[] {j,i};
}
}
}
return new int[] {-1,-1};
}
}
class MoveAliens extends TimerTask
{
public void run()
{
if(AlienGroup.forward)
{
if(((SpaceShip)((ArrayList)AlienGroup.theGroup.get(0)).get(AlienGroup.col-1)).x <= 470)
{
AlienGroup.right();
}
else
{
AlienGroup.down();
AlienGroup.forward = false;
}
}
else
{
if(((SpaceShip)((ArrayList)AlienGroup.theGroup.get(0)).get(0)).x >= 20)
{
AlienGroup.left();
}
else
{
AlienGroup.down();
AlienGroup.forward = true;
}
}
if(((SpaceShip)((ArrayList)AlienGroup.theGroup.get(AlienGroup.adjustRows()-1)).get(0)).y>=278)
{
SpaceInvaders.reference.p.alive=false;
}
if(SpaceInvaders.reference.clearWait)
cancel();
if(!SpaceInvaders.reference.alienShot.shooting)
{
SpaceInvaders.reference.alienShot.shooting=true;
SpaceShip temp = AlienGroup.randomShip();
SpaceInvaders.reference.alienShot.x=temp.x;
SpaceInvaders.reference.alienShot.y=temp.y;
SpaceInvaders.reference.t.scheduleAtFixedRate(new MoveAlienShots(),30,30);
}
SpaceInvaders.reference.repaint();
}
}
class MovePlayerShots extends TimerTask
{
public void run()
{
int[] checkHit = AlienGroup.hit();
if(checkHit[0] == -1)
{
SpaceInvaders.reference.playerShot.y-=10;
}
else if(checkHit[0] != -1)
{
((SpaceShip)((ArrayList)AlienGroup.theGroup.get(checkHit[1])).get(checkHit[0])).img =
SpaceInvaders.reference.pow;
SpaceInvaders.reference.t.schedule(new PowDelay(
((SpaceShip)((ArrayList)AlienGroup.theGroup.get(checkHit[1])).get(checkHit[0])))
,400);
SpaceInvaders.reference.playerShot.shooting=false;
cancel();
}
if(SpaceInvaders.reference.playerShot.y<=0)
{
SpaceInvaders.reference.playerShot.shooting=false;
cancel();
}
SpaceInvaders.reference.repaint();
}
}
class PowDelay extends TimerTask
{
public SpaceShip dead;
public PowDelay(SpaceShip d)
{
dead = d;
}
public void run()
{
dead.alive = false;
cancel();
}
}
class MoveAlienShots extends TimerTask
{
public void run()
{
if(SpaceInvaders.reference.p.intersects(SpaceInvaders.alienShot))
{
SpaceInvaders.reference.p.alive=false;
}
else
SpaceInvaders.reference.alienShot.y+=10;
if(SpaceInvaders.reference.alienShot.y>=400)
{
SpaceInvaders.reference.alienShot.shooting=false;
cancel();
}
SpaceInvaders.reference.repaint();
}
}