// WarMan Class // WarMan.java // Imports import java.awt.*; import java.applet.*; import java.util.*; import java.io.*; public class WarMan extends Applet implements Runnable { private Image offImage, back, back2, back3, smHero; private AudioClip music, netHit, netMiss, applause; private Graphics offGrfx; private Thread animate; private MediaTracker tracker; private WMVector wmv; private Gecko gecko; private int delay = 83; // 12 fps private Font scoreFont = new Font("Helvetica", Font.PLAIN, 20); private FontMetrics scoreMetrics; private boolean musicOn = true; public static int score, // Tells what your score is lives, // Tells how many lives are left badcount, // Tells if there are still bad guys left keyflag, // Set this to exit level 1 gunflag, // Set this to shoot level, // Indicates what level you're on bulletcount, // Counter so only 5 shots at once scorpioncount, // Not currently used room, // Indicates what room you're in doorcount, // Has the freedom door been blown up roomchange; // Flag to indicate room change private Random rand = new Random(System.currentTimeMillis()); public String getAppletInfo() { return (new String("War Man by Robert Los")); } // End getAppletInfo function public void init() { /** Load and track the images.*/ tracker = new MediaTracker(this); back = getImage(getCodeBase(), "Res/Back.gif"); back2 = getImage(getCodeBase(), "Res/Back2.gif"); back3 = getImage(getCodeBase(), "Res/Back3.gif"); tracker.addImage(back, 0); smHero = getImage(getCodeBase(), "Res/SmHero.gif"); tracker.addImage(smHero, 0); Gecko.initResources(this, tracker, 0); Geckocide.initResources(this, tracker, 0); GilaMonster.initResources(this, tracker, 0); Spider.initResources(this, tracker, 0); Spiderling.initResources(this, tracker, 0); Spidercide.initResources(this, tracker, 0); Bullet.initResources(this, tracker, 0); Mushroom1.initResources(this, tracker, 0); Mushroom2.initResources(this, tracker, 0); Wall.initResources(this, tracker, 0); Door.initResources(this, tracker, 0); Key.initResources(this, tracker, 0); Gun.initResources(this, tracker, 0); Cliff.initResources(this, tracker, 0); Tree.initResources(this, tracker, 0); Block.initResources(this, tracker, 0); Boulder.initResources(this, tracker, 0); /** Load the audio clips.*/ music = getAudioClip(getCodeBase(), "Res/Music.au"); // music = getAudioClip(getCodeBase(), "Res/Music.wav"); netHit = getAudioClip(getCodeBase(), "Res/NetHit.au"); netMiss = getAudioClip(getCodeBase(), "Res/NetMiss.au"); applause = getAudioClip(getCodeBase(), "Res/Applause.au"); } // End init function public void start() { /**Start animation.*/ if (animate == null) { animate = new Thread(this); animate.start(); } } // End start function public void stop() { /**Stop animation.*/ if (animate != null) { animate.stop(); animate = null; } } // End stop function public void run() { try { tracker.waitForID(0); } catch (InterruptedException e) { return; } /**Setup a new game.*/ newGame(); /** Update everything.*/ long t = System.currentTimeMillis(); while (Thread.currentThread() == animate) { wmv.update(); repaint(); try { t += delay; Thread.sleep(Math.max(0, t - System.currentTimeMillis())); } catch (InterruptedException e) { break; } } // End while loop } // End run function public void update(Graphics g) { /** Create the offscreen graphics context.*/ if (offGrfx == null) { offImage = createImage(size().width, size().height); offGrfx = offImage.getGraphics(); scoreMetrics = offGrfx.getFontMetrics(scoreFont); } // End if statement /** Draw the sprites.*/ wmv.draw(offGrfx); /** Draw the score.*/ offGrfx.setFont(scoreFont); offGrfx.drawString(String.valueOf(score), 10, 5 + scoreMetrics.getAscent()); /** Draw the number of lives.*/ for (int i = 0; i < (lives - 1); i++) offGrfx.drawImage(smHero, 65 + i * (smHero.getWidth(this) + 1), 10, this); /** Level 1 room changing*/ if ((level == 1) && (roomchange >= 1)) { roomchange = 0; bulletcount = 0; wmv = new WMVector(new ImageBackground(this, back), this); wmv.add(gecko); if (room == 1) { for (int i = 0; i < 5; i++) { wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls if (i != 2) wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls if (i != 2) wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if ((i != 0) && (i != 4)) wmv.add(new Wall(this, 1, new Point(101,(i * 100)))); // Inner walls if ((i != 0) && (i != 1)) wmv.add(new Wall(this, 0, new Point((i * 100),201))); // Inner walls if ((i != 0) && (i != 1)) wmv.add(new Wall(this, 0, new Point((i * 100),301))); // Inner walls } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Door(this, 1, new Point(0,200))); // Door for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) if (keyflag <= 0) { Font f = new Font("Helvetica", Font.BOLD, 14); FontMetrics fm = offGrfx.getFontMetrics(f); String s = new String("The door is locked./n Go find the key."); offGrfx.setFont(f); offGrfx.drawString(s, (size().width - fm.stringWidth(s)) / 2, ((size().height - fm.getHeight()) / 2) + fm.getAscent()); } // End if statement } // End if statement--if (room == 1) else if (room == 2) { for (int i = 0; i < 5; i++) { wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls if (i != 2) { wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls } if (i != 0) { wmv.add(new Wall(this, 1, new Point(200,(i * 100)))); // Inside walls wmv.add(new Wall(this, 1, new Point(300,(i * 100)))); // Inside walls } if (i != 4) { wmv.add(new Wall(this, 1, new Point(100,(i * 100)))); // Inside walls wmv.add(new Wall(this, 1, new Point(400,(i * 100)))); // Inside walls } } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 1, new Point(0, 120))); // Left walls wmv.add(new Wall(this, 1, new Point(480, 120))); // Right walls for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 2) else if (room == 3) { for (int i = 0; i < 5; i++) { wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls if (i != 4) wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls if (i != 2) wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if (i != 0) wmv.add(new Wall(this, 1, new Point(400,(i * 100)))); // Inside walls if ((i != 3) && (i != 4)) wmv.add(new Wall(this, 0, new Point((i * 100), 200))); // Inside walls } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 0, new Point(300, 300))); // Inside walls wmv.add(new Wall(this, 0, new Point(0, 300))); // Inside walls for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 3) else if (room == 4) { for (int i = 0; i < 5; i++) { wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls if (i != 3) wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls if (i != 2) wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if (i != 2) wmv.add(new Wall(this, 1, new Point(400,(i * 100)))); // Inside walls if (i != 2) wmv.add(new Wall(this, 1, new Point(300,(i * 100)))); // Inside walls } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 0, new Point(0, 200))); // Inside walls wmv.add(new Wall(this, 0, new Point(0, 300))); // Inside walls wmv.add(new Wall(this, 1, new Point(480, 120))); // Right walls for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 4) else if (room == 5) { for (int i = 0; i < 5; i++) { if (i != 2) wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls if (i != 2) wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls if (i != 2) wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if ((i != 1) && (i != 3)) { wmv.add(new Wall(this, 1, new Point(200, (i * 100)))); // Inside walls wmv.add(new Wall(this, 1, new Point(300, (i * 100)))); // Inside walls wmv.add(new Wall(this, 0, new Point((i * 100), 200))); // Inside walls wmv.add(new Wall(this, 0, new Point((i * 100), 300))); // Inside walls } } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 0, new Point(220, 300))); // Inside walls for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 5) else if (room == 6) { for (int i = 0; i < 5; i++) { if (i != 4) wmv.add(new Wall(this, 0, new Point((i * 100), 0))); // Top walls if (i != 4) wmv.add(new Wall(this, 0, new Point((i * 100), 480))); // Bottom walls wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls wmv.add(new Wall(this, 1, new Point(480, (i * 100)))); // Right walls if (i != 0) wmv.add(new Wall(this, 0, new Point((i * 100), 200))); // Inside walls if (i != 0) wmv.add(new Wall(this, 0, new Point((i * 100), 300))); // Inside walls } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 0, new Point(320, 0))); // Top walls wmv.add(new Key(this, 0, new Point(420, 235))); // Key for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 6) else if (room == 7) { for (int i = 0; i < 5; i++) { if (i != 3) wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls if (i != 1) wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if ((i != 0) && (i != 4)) wmv.add(new Wall(this, 0, new Point((i * 100),201))); // Inside walls if ((i != 0) && (i != 4)) wmv.add(new Wall(this, 1, new Point(401,(i * 100)))); // Inside walls } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 0, new Point(0,300))); // Inside walls wmv.add(new Wall(this, 0, new Point(100,300))); // Inside walls wmv.add(new Wall(this, 0, new Point(400,100))); // Inside walls wmv.add(new Wall(this, 1, new Point(100,100))); // Inside walls wmv.add(new Wall(this, 1, new Point(100,400))); // Inside walls wmv.add(new Wall(this, 1, new Point(200,300))); // Inside walls wmv.add(new Wall(this, 0, new Point(220, 0))); // Top walls if (gunflag == 0) wmv.add(new Gun(this, 0, new Point(320, 235))); // Gun for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 7) else if (room == 8) { for (int i = 0; i < 5; i++) { if (i != 2) wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls if (i != 1) wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls if (i != 4) wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if (i != 0) wmv.add(new Wall(this, 1, new Point(400,(i * 100)))); // Inside walls if (i != 4) wmv.add(new Wall(this, 0, new Point((i * 100), 100))); // Inside walls } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 0, new Point(120, 0))); // Top walls wmv.add(new Wall(this, 1, new Point(480, 320))); // Right walls for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 8) else if (room == 9) { for (int i = 0; i < 5; i++) { if (i != 4) wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls if (i != 4) wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if (i != 0) wmv.add(new Wall(this, 0, new Point((i * 100), 100))); // Inside walls if (i != 4) wmv.add(new Wall(this, 0, new Point((i * 100), 400))); // Inside walls } // End for statement--for (int i = 0; i < 5; i++) wmv.add(new Wall(this, 1, new Point(200, 200))); // Inside walls for (int i = 0; i < 6; i++) { // Add 6 bad guys Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) } // End if statement--else if (room == 9) } // End if statement--if ((level == 1) && (roomchange == 1)) /** Switch Levels.*/ if (badcount <= 0) { if (level == 2) { // Go to level 3 // if (scorpioncount <= 0) { level++; wmv = new WMVector(new ImageBackground(this, back3), this); wmv.add(gecko); gunflag = 1; badcount = 3; // CHANGE THIS TO A CENTIPEDE VVVVVVVV wmv.add(new GilaMonster(this, new Point(0,10))); wmv.add(new GilaMonster(this)); wmv.add(new GilaMonster(this, new Point(0,80))); for (int i = 0; i < 20; i++) { Point pos = wmv.getEmptyPosition(new Dimension( Mushroom1.image[0].getWidth(this), Mushroom1.image[0].getHeight(this))); if ((pos.x < 15) || (pos.x > 480) || (pos.y < 15) || (pos.y > 350)) i--; else wmv.add(new Mushroom1(this, pos)); } // End for statement bulletcount = 0; // } // End if statement--if (scorpioncount <= 0) } // End if statement--if (level == 2) //------------------------------------------------------------------------------------------------ //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV else if (level == 1) { // Go to level 2 level++; wmv = new WMVector(new ImageBackground(this, back2), this); for (int i = 0; i < 5; i++) { wmv.add(new Cliff(this, 0, new Point(60,(i * 100)))); // Left cliff wmv.add(new Cliff(this, 1, new Point(420,(i * 100)))); // Right cliff } wmv.add(gecko); gecko.setPosition(new Point(142, 437)); gunflag = 1; badcount = 30; for (int i = 1; i < 5; i++) { wmv.add(new Spider(this, new Point((i * 100), 0))); } wmv.add(new Tree(this, 0, new Point(300, 300))); wmv.add(new Tree(this, 0, new Point(200, 100))); wmv.add(new Boulder(this, 0, new Point(250, 350))); bulletcount = 0; } // End else if statement--else if (level == 1) //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA //------------------------------------------------------------------------------------------------ else { Font f = new Font("Helvetica", Font.BOLD, 36); FontMetrics fm = offGrfx.getFontMetrics(f); String s = new String("You Win!"); offGrfx.setFont(f); offGrfx.drawString(s, (size().width - fm.stringWidth(s)) / 2, ((size().height - fm.getHeight()) / 2) + fm.getAscent()); /**Stop the music.*/ music.stop(); } // End else statement } // End if statement--if (badcount <= 0) if (level == 2) { //Keeps bad guys coming in level 2 if (badcount <= 10) { badcount = 20; wmv.add(new Block(this, 0, new Point(80, 0))); // Freedom Door for (int i = 1; i < 4; i++) { wmv.add(new Spider(this, new Point((i * 100), 0))); wmv.add(new Spider(this, new Point((i * 100), 0))); } } if ((rand.nextInt() % (20 - level / 2)) == 0) wmv.add(new Spider(this, new Point(200, 0))); } /** Draw the game over message.*/ if (lives <= 0) { Font f = new Font("Helvetica", Font.BOLD, 36); FontMetrics fm = offGrfx.getFontMetrics(f); String s = new String("Game Over"); offGrfx.setFont(f); offGrfx.drawString(s, (size().width - fm.stringWidth(s)) / 2, ((size().height - fm.getHeight()) / 2) + fm.getAscent()); /**Stop the music.*/ music.stop(); } // End if statement /** Draw the image onto the screen.*/ g.drawImage(offImage, 0, 0, null); } // End update function public void paint(Graphics g) { if ((tracker.statusID(0, true) & MediaTracker.ERRORED) != 0) { /** Draw the error rectangle.*/ g.setColor(Color.red); g.fillRect(0, 0, size().width, size().height); return; } // End if statement if ((tracker.statusID(0, true) & MediaTracker.COMPLETE) != 0) { // Draw the offscreen image g.drawImage(offImage, 0, 0, null); } // End if statement else { /** Draw the title message (while the images load)*/ Font f1 = new Font("TimesRoman", Font.BOLD, 28), f2 = new Font("Helvetica", Font.PLAIN, 16); FontMetrics fm1 = g.getFontMetrics(f1), fm2 = g.getFontMetrics(f2); String s1 = new String("War Man"), s2 = new String("Loading images..."); g.setFont(f1); g.drawString(s1, (size().width - fm1.stringWidth(s1)) / 2, ((size().height - fm1.getHeight()) / 2) + fm1.getAscent()); g.setFont(f2); g.drawString(s2, (size().width - fm2.stringWidth(s2)) / 2, size().height - fm2.getHeight() - fm2.getAscent()); } // End else statement } // End paint function /** This is where the code is to allow the user to interact * with the game using the keyboard. */ public boolean keyDown(Event evt, int key) { // Change the gecko velocity based on the key pressed switch (key) { case (int)'8': case Event.UP: if (level == 2) { // Move stuff towards bottom gecko.setVelocity(new Point(8, 4)); } else gecko.setVelocity(new Point(8, 8)); gecko.setDirection(0); break; case (int)'9': if (level == 2) { // Move stuff towards bottom gecko.setVelocity(new Point(8, 4)); } else gecko.setVelocity(new Point(8, 8)); gecko.setDirection(1); break; case (int)'6': case Event.RIGHT: gecko.setVelocity(new Point(8, 8)); gecko.setDirection(2); break; case (int)'3': gecko.setVelocity(new Point(8, 8)); gecko.setDirection(3); break; case (int)'2': case Event.DOWN: gecko.setVelocity(new Point(8, 8)); gecko.setDirection(4); break; case (int)'1': gecko.setVelocity(new Point(8, 8)); gecko.setDirection(5); break; case (int)'4': case Event.LEFT: gecko.setVelocity(new Point(8, 8)); gecko.setDirection(6); break; case (int)'7': if (level == 2) { // Move stuff towards bottom gecko.setVelocity(new Point(8, 4)); } else gecko.setVelocity(new Point(8, 8)); gecko.setDirection(7); break; case (int)'5': case (int)' ': if ((bulletcount < 5) && (gunflag >=1)) { bulletcount++; gecko.shoot = 1; netHit.play(); } break; case (int)'N': case (int)'n': newGame(); break; case (int)'Q': case (int)'q': lives = 0; break; case (int)'M': case (int)'m': musicOn = !musicOn; if (musicOn) music.loop(); else music.stop(); break; } // End switch statement return true; } // End keyDown function void newGame() { /** Setup a new game.*/ level = 1; lives = 5; score = 0; gunflag = 0; keyflag = 0; doorcount = 6; badcount = 100; wmv = new WMVector(new ImageBackground(this, back), this); gecko = new Gecko(this); wmv.add(gecko); room = 7; roomchange = 0; for (int i = 0; i < 5; i++) { if (i != 3) wmv.add(new Wall(this, 0, new Point((i * 100),0))); // Top walls wmv.add(new Wall(this, 0, new Point((i * 100),480))); // Bottom walls wmv.add(new Wall(this, 1, new Point(0,(i * 100)))); // Left walls if (i != 1) wmv.add(new Wall(this, 1, new Point(480,(i * 100)))); // Right walls if ((i != 0) && (i != 4)) wmv.add(new Wall(this, 0, new Point((i * 100),201))); // Inside walls if ((i != 0) && (i != 4)) wmv.add(new Wall(this, 1, new Point(401,(i * 100)))); // Inside walls } wmv.add(new Wall(this, 0, new Point(0,301))); // Inside walls wmv.add(new Wall(this, 0, new Point(101,301))); // Inside walls wmv.add(new Wall(this, 0, new Point(401,101))); // Inside walls wmv.add(new Wall(this, 1, new Point(101,101))); // Inside walls wmv.add(new Wall(this, 1, new Point(101,401))); // Inside walls wmv.add(new Wall(this, 1, new Point(201,301))); // Inside walls wmv.add(new Wall(this, 0, new Point(220, 0))); // Top walls wmv.add(new Gun(this, 0, new Point(320, 235))); // Gun for (int i = 0; i < 6; i++) { Point pos = wmv.getEmptyPosition(new Dimension(41, 41)); wmv.add(new Spider(this, pos)); } // End for statement--for (int i = 0; i < 6; i++) // wmv.add(new GilaMonster(this)); // wmv.add(new GilaMonster(this, new Point(0,80))); bulletcount = 0; if (musicOn) music.loop(); } // End newGame function } // End War Man class