// Spider Class // Spider.java // Imports import java.awt.*; import java.applet.Applet; import java.util.*; public class Spider extends DirectionalSprite { // Spider action flags (extended sprite action flags, must be > 2) public static final int SA_ADDSPIDERCIDE = 5; public static Image[][] image; protected static Random rand = new Random( System.currentTimeMillis()); /**Constructor*/ public Spider(Component comp, Point pos) { super(comp, image, 0, 1, 2, pos, new Point(3, 3), 30, Sprite.BA_STOP, 4); } /** Initializes the images.*/ public static void initResources(Applet app, MediaTracker tracker, int id) { image = new Image[8][2]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 2; j++) { image[i][j] = app.getImage(app.getCodeBase(), "Res/Tarant" + i + j + ".gif"); tracker.addImage(image[i][j], id); } } } public BitSet update() { // Randomly change direction if ((rand.nextInt() % 7) == 0) { velocity.x = velocity.y = 3; setDirection((rand.nextInt() % 4) * 2); } if (direction == 7) { velocity.x = velocity.y = 2; setDirection(4); } if (WarMan.level == 2) { if (direction == 0) velocity.y = -2; } // Call parent's update() BitSet action = super.update(); return action; } protected Sprite addSprite(BitSet action) { // Add spidercide? if (action.get(Spider.SA_ADDSPIDERCIDE)) return new Spidercide(component, new Point(position.x, position.y)); return null; } protected void setCollision() { collision = new Rectangle(position.x + 4, position.y + 4, position.width - 4, position.height - 4); } }