package aimFire; import java.applet.Applet; import java.awt.Color; import java.awt.Event; import java.awt.Graphics; import java.awt.Image; import java.awt.Point; import java.util.ArrayList; import java.util.Random; public class AimFire extends Applet{ public static final int SPEED_CONSTANT = 500; public static final int MIN_X = 0; public static final int MIN_Y = 21; public static final int MAX_X = 399; public static final int MAX_Y = 399; Image ammoImage; private Random rand; private static final int CLIP = 10; private static final int TOTAL_TARGETS = 20; private int toBeAdded; private ArrayList targets; private int targetsKilled; private int ammo; private int score; private boolean gameActive; public void init(){ setSize(400, 400); setBackground(Color.black); setCursor(getToolkit().createCustomCursor(getImage(this.getClass().getResource("crosshair.gif")), new Point(16, 16), "crosshair")); ammoImage = getImage(this.getClass().getResource("ammo.gif")); rand = new Random(); ammo = CLIP; score = 0; gameActive = true; targetsKilled = 0; toBeAdded = TOTAL_TARGETS; targets = new ArrayList(); sendRandom(); sendRandom(); } public boolean mouseDown(Event e, int x, int y){ if(gameActive && ammo > 0){ ammo--; for(int count = 0; count < targets.size(); count++){ score += targets.get(count).hit(x, y); } repaint(); } return true; } public boolean keyDown(Event e, int key){ if((char)key == ' ' && gameActive){ ammo = CLIP; repaint(); } return true; } private void sendRandom(){ toBeAdded--; Target t = new Target(this, getGraphics(), rand); targets.add(t); new Thread(t).start(); repaint(); } public void notifyOfDeath(Target t){ targetsKilled++; targets.remove(t); if(toBeAdded > 0){ sendRandom(); } if(targets.size()==0){ gameActive = false; } repaint(); } public void paint(Graphics g){ g.setColor(Color.gray); g.drawLine(0, 20, 399, 20); for(int count = 0; count < ammo; count++){ g.drawImage(ammoImage, count*10 + 3, 3, this); } if(ammo == 0){ g.drawString("press space to reload", 3, 13); } g.drawString("Targets: " + targetsKilled + "/" + TOTAL_TARGETS, 230, 13); g.drawString("Score: " + score, 340, 13); if(!gameActive){ g.setColor(Color.gray); g.drawString("Game Over", 170, 200); g.drawString("Score: " + score, 170, 230); } } }