Code:
import java.awt.event.*;
import java.awt.*;
import java.aw*****lor;
import java.applet.*;
import java.awt.Image;
public class Shooter extends Applet implements MouseListener, MouseMotionListener, KeyListener
{
Image bgImg;
Image mission1;
Image bgLvl1;
Image enemy1;
Image healthPack;
Image menu;
Image heli;
AudioClip shoot;
private int ammo;
private int reload;
private int clips;
private int health;
private int level;
private int kills;
private int numEnemy;
private int x;
private int y;
private int menuClick;
private int xHeli;
private int yHeli;
private int xHealth;
private int yHealth;
private int xOffset;
private int yOffset;
private int[] enemyCounter;
private int[] heliCounter;
private boolean[] shot;
private boolean[] healthShot;
private boolean[] ammoShot;
private boolean[] heliShot;
private boolean mouseDown;
private boolean startReload;
private boolean startLevel1;
private Waiter shootWait = new Waiter(110);
private Waiter reloadWait = new Waiter(800);
private Waiter enemyWait = new Waiter(500);
private Waiter heliWait = new Waiter(500);
private Graphics backg;
private Image backbuffer;
public void init()
{
clips = 60;
ammo = 30;
health = 100;
level = 1;
numEnemy = 10;
xOffset = 4;
yOffset = 3;
xHealth = 0;
yHealth = 100;
xHeli = 300;
yHeli = 50;
shot = new boolean[10];
healthShot = new boolean[10];
ammoShot = new boolean[10];
enemyCounter = new int[10];
heliCounter = new int[10];
heliShot = new boolean[10];
for(int i = 0; i < shot.length; i++)
{
shot[i] = false;
healthShot[i] = false;
ammoShot[i] = false;
heliShot[i] = false;
enemyCounter[i] = 0;
heliCounter[i] = 0;
}
mouseDown = false;
startReload = false;
startLevel1 = false;
menuClick = -1;
setBackground(Color.black);
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
backbuffer = createImage(getSize().width, getSize().height);
backg = backbuffer.getGraphics();
bgImg = getImage(getCodeBase(), "blackBG.png");
mission1 = getImage(getCodeBase(), "mission1.png");
bgLvl1 = getImage(getCodeBase(), "lvl1.jpg");
enemy1 = getImage(getCodeBase(), "terrorist.png");
healthPack = getImage(getCodeBase(), "healthPack.png");
menu = getImage(getCodeBase(), "menu.png");
heli = getImage(getCodeBase(), "heli.png");
//shoot = getAudioClip(getDocumentBase(), "shoot.au");
//shoot.play();
System.out.println(getCodeBase());
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g)
{
map();
status();
shoot();
enemy();
healthPack();
ammoPack();
helicopter();
crosshair();
buttons();
if(startReload == true && !reloadWait.isWaiting())
{
reload();
startReload = false;
}
g.drawImage(backbuffer, 0,0, this);
try {
Thread.sleep(10);
} catch(InterruptedException e) {}
repaint();
}
public void map()
{
if(level == 1)
{
backg.drawImage(mission1, 0, 0, 1200, 800, this);
if(startLevel1)
{
backg.drawImage(bgImg, 0, 0, 1200, 800, this);
backg.setColor(Color.WHITE);
backg.drawString("Saudi Arabia!", 560, 20);
backg.drawImage(bgLvl1, 0, 40, 1200, 730, this);
}
}
}
public void status()
{
if(startLevel1)
{
backg.setColor(Color.WHITE);
backg.drawString("Health: " + health, 1100, 20);
backg.drawString("Ammo: " + ammo + "/" + clips, 1100, 785);
backg.drawString("Level: " + level, 10, 20);
backg.drawString("Kills: " + kills, 10, 785);
backg.setColor(Color.RED);
backg.drawLine(100, 770, 100, 800);
backg.drawLine(1075, 0, 1075, 30);
backg.drawLine(1075, 770, 1075, 800);
backg.drawLine(100, 0, 100, 30);
}
}
public void shoot()
{
if(mouseDown == true && startLevel1 == true && ammo > 0 && menuClick == -1 && y >= 40 && y <= 770 && !shootWait.isWaiting())
{
ammo--;
shootWait.startWaiting();
}
}
public void enemy()
{
if(level == 1 && health > 0 && menuClick == -1 && startLevel1 == true)
{
if((shot[0] == false || shot[1] == false || shot[2] == false) && !enemyWait.isWaiting())
{
health -= 5;
enemyWait.startWaiting();
}
if(shot[0] == false)
{
backg.drawImage(enemy1, 810, 645, 60, 125, this);
}
if(x >= 810 && x <= 870 && y >= 645 && y <= 770 && ammo > 0 && shot[0] == false)
{
enemyCounter[0] += 1;
if(enemyCounter[0] == 3)
{
shot[0] = true;
kills++;
}
}
if(shot[1] == false)
{
backg.drawImage(enemy1, 360, 665, 50, 100, this);
}
if(x >= 360 && x <= 410 && y >= 665 && y <= 765 && ammo > 0 && shot[1] == false)
{
shot[1] = true;
kills++;
}
if(shot[2] == false)
{
backg.drawImage(enemy1, 150, 434, 40, 80, this);
}
if(x >= 150 && x <= 190 && y >= 434 && y <= 514 && ammo > 0 && shot[2] == false)
{
shot[2] = true;
kills++;
}
}
repaint();
}
public void reload()
{
reload = 30 - ammo;
clips -= reload;
ammo += reload;
if(clips < 0)
{
clips = -clips;
ammo -= clips;
clips = 0;
}
}
public void healthPack()
{
if(level == 1 && health > 0 && menuClick == -1 && startLevel1 == true)
{
if(kills >= 3 && healthShot[0] == false)
{
xHealth += xOffset;
yHealth += yOffset;
if(xHealth < 0 || xHealth > 160)
xOffset = -xOffset;
if(yHealth < 40 || yHealth > 400)
yOffset = -yOffset;
backg.drawImage(healthPack, xHealth, yHealth, 60, 30, this);
}
if(x >= xHealth && x <= xHealth + 60 && y >= yHealth && y <= yHealth + 30 && healthShot[0] == false)
{
healthShot[0] = true;
health += 30;
if(health > 100)
health = 100;
}
}
}
public void ammoPack()
{
}
public void helicopter()
{
if(level == 1 && health > 0 && menuClick == -1 && startLevel1 == true)
{
if(kills >= 3 && heliShot[0] == false)
{
xHeli += xOffset;
if(xHeli < 300 || xHeli > 900)
xOffset = -xOffset;
backg.drawImage(heli, xHeli, yHeli, 100, 80, this);
}
if(x >= xHeli && x <= xHeli + 100 && y >= yHeli && y <= yHeli + 80 && heliShot[0] == false)
{
heliCounter[0] += 1;
if(heliCounter[0] == 8)
{
heliShot[0] = true;
}
}
}
}
public void crosshair()
{
Frame browserFrame;
Component parentComponent;
parentComponent = getParent();
while ( parentComponent != null &&
!(parentComponent instanceof Frame)) {
parentComponent = parentComponent.getParent();
}
browserFrame = (Frame) parentComponent;
browserFrame.setCursor(Frame.CROSSHAIR_CURSOR);
/*
-:CURSOR OPTIONS:-
CROSSHAIR_CURSOR, DEFAULT_CURSOR,
E_RESIZE_CURSOR, HAND_CURSOR
MOVE_CURSOR, N_RESIZE_CURSOR,
NE_RESIZE_CURSOR, NW_RESIZE_CURSOR;
S_RESIZE_CURSOR,SE_RESIZE_CURSOR,
SW_RESIZE_CURSOR, TEXT_CURSOR,
W_RESIZE_CURSOR, WAIT_CURSOR
*/
}
public void buttons()
{
if(!startLevel1)
{
backg.setColor(Color.BLACK);
backg.fillRect(970, 720, 150, 45);
backg.setColor(Color.WHITE);
backg.drawString("Start!", 1028, 745);
}
// Menu
if(startLevel1)
{
backg.setColor(Color.RED);
backg.fillRect(561, 776, 80, 25);
backg.setColor(Color.WHITE);
backg.drawRect(559, 774, 83, 27);
backg.drawString("Menu", 585, 790);
if(menuClick == 1)
{
backg.drawImage(menu, 350, 500, 500, 273, this);
backg.drawString("Controls", 400, 600);
backg.drawLine(400, 603, 445, 603);
backg.drawString("R - Reload", 400, 620);
backg.drawString("Left Mouse - Shoot", 400, 640);
backg.drawString("M - Menu", 400, 660);
backg.drawString("S - Start Over", 400, 680);
}
}
}
public void mouseClicked(MouseEvent e)
{
int xPos = e.getX();
int yPos = e.getY();
if(xPos >= 560 && xPos <= 640 && yPos >= 775 && yPos <= 800)
menuClick = -menuClick;
if(xPos >= 970 && xPos <= 970 + 120 && yPos >= 720 && yPos <= 765)
startLevel1 = true;
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
mouseDown = true;
}
public void mouseReleased(MouseEvent e)
{
mouseDown = false;
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
e.consume();
}
public void keyPressed(KeyEvent e)
{
char c = e.getKeyChar();
if(health > 0 && clips > 0 && ammo < 30 && menuClick == -1 && startLevel1 == true)
{
if(c == 'r' || c == 'R')
{
startReload = true;
reloadWait.startWaiting();
}
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
if(c == 'm' || c == 'M')
menuClick = -menuClick;
if(c == 's' || c == 'S')
{
startLevel1 = false;
}
}
}
Waiter class: