// Eyes Class // Eyes.java // Project 3, CIS 525 // Fall 2000 // By Terrence Burrows // Imports import java.awt.*; import java.applet.Applet; import java.util.*; /** This is the class of the "Eyes" object - creatures that move back and forth in the game. This class extends the "Sprite" class. It sets the initial location and behavior of the "eyes" bitmaps in the game. It also initializes the eye images. This class overrides the Sprite class "update" and "incframe" methods. */ public class Eyes extends Sprite { /** "Eye" image frame array */ protected static Image[][] image = new Image[2][4]; /** "Eye" blink rate and trigger variables */ protected int blinkDelay, blinkTrigger; /** Random number generator */ private static Random rand = new Random(System.currentTimeMillis()); /** Eye class constructor method - set eye initial location and "blink" rate. */ public Eyes (Component comp, int dir, int speedInc, int bd) { super(comp, image[dir], 0, 1, 1, new Point((dir == 0) ? (comp.size().width - image[dir][0].getWidth(comp)) : 0, 60 + Math.abs(rand.nextInt() % 5) * 44), new Point((dir == 0) ? (-5 - speedInc) : (5 + speedInc), 0), 10, Sprite.BA_DIE); blinkTrigger = blinkDelay = bd; } /** Initialize Resources - get bitmap images and synchronize with the Media Tracker. */ public static void initResources(Applet app, MediaTracker tracker, int id) { for (int i = 0; i < 4; i++) { image[0][i] = app.getImage(app.getCodeBase(), "Res/SmEye" + i + ".gif"); tracker.addImage(image[0][i], id); image[1][i] = app.getImage(app.getCodeBase(), "Res/LgEye" + i + ".gif"); tracker.addImage(image[1][i], id); } } /** Set the collision boundary rectangle for the image. Also used for determining coordinates for the isPointInside() method in the Sprite class. */ protected void setCollision() { collision = new Rectangle(position.x, position.y, position.width, position.height); } /** See if the "Eye" bitmap image escaped the screen. ***This will increment the "lost" counter in the NightHunter class. This is not A very object oriented like approach. */ public BitSet update() { // See if the eye escaped the screen BitSet action = super.update(); if (action.get(Sprite.SA_KILL)) NightHunter.lost++; //increment "lost" counter in NightHunter class return action; //object } /** Increment image frames and set frames to synchronize with the "blink" delay. */ protected void incFrame() { if ((frameDelay > 0) && (--frameTrigger <= 0) && (--blinkTrigger <= 0)) { // Reset the frame trigger frameTrigger = frameDelay; // Increment the frame frame += frameInc; if (frame >= 4) { frame = 3; frameInc = -1; } else if (frame <= 0) { frame = 0; frameInc = 1; blinkTrigger = blinkDelay; } } } }