home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / APPLETS / ASTER.JAV next >
Encoding:
Text File  |  1997-02-27  |  29.7 KB  |  585 lines

  1. import java.util.*;
  2. import java.lang.*;
  3. import java.awt.*;
  4. import java.applet.*;
  5. import java.net.*;
  6.  
  7. /* This is all original material, with the exception of the double buffering, which 
  8. * I hacked out of a quote-ticker program.  The game is a GREAT example of object-heirarchy.
  9. * I also supported multithreading, but it just made things run slower, actually!  
  10. * For this reason the multithreading was removed...
  11.  
  12. * PARAMETERS: 
  13. *    wid: the width of the applet (integer)
  14. *    hei: the height of the applet (integer)
  15.  
  16. * Feel free to modify or distribute as you wish, as long as you include:
  17.  
  18. Original programmed by: Ben Sigelman, sigelman@crocker.com
  19. http://www.crocker.com/~sigelman/   |||  FOR HIRE! ALWAYS!
  20.  
  21. * Appletviewer has its bugs.... netscape _is_ a bug.
  22. */
  23.  
  24. public class aster extends java.applet.Applet implements Runnable { //this is the main program, aster(noid)
  25.     ShotHandler shotH; //this is an implementation of my "shothandler" class, which keeps track of all your shots.
  26.     PlayerHandler playH; //this is an implementation of my "playerhandler" class, which keeps track of your players needs... leaves open support for multi-player
  27.     AstHandler astH; //same deal:  handling class for all of the "asteroids" in the game. (the things you shoot, that is)
  28.     Color col;  //i like to define things like this in case their needed... this one is used to change colors
  29.     Image im; //this is used in the double-buffering lines.
  30.     Graphics offscreen; //more double-buffering crap
  31.     int keyp, i1, i2, i3, i4; //keyp was used for debugging.  the rest are just temp. integers
  32.     public static int score; //self-explanatory... its the score accumulated
  33.     int level = 0; //this is the current level
  34.     int levelend = 0; //this is used in the pause at the end of every level
  35.     int levelstart = 0; //this is used in the invulnerability period that starts each level.
  36.     double d1, d2, d3, d4; //temp double things
  37.     int keys[] = new int[3]; //VERY IMPORTANT! this has to do with overriding the default keyboard handling
  38.     AudioClip /*shotsound, */explode; //the explosion sound in the game... shotsound was too obnoxious to include
  39.     URL codeb; //code base URL.  this is the documents url... a kind of base directory
  40.     int width, height; //width and height as specified by the html file
  41.  
  42.     public boolean keyDown(Event ev, int key) { //keyDown event automatically calls this function.
  43.         int li1; //temp integer
  44.         if (key == 32) { //space bar: shoot
  45.             if (shotH.shotdown == 1) {shotH.shotdown = 2;} //these lines dont allow you to hold down
  46.             if (shotH.shotdown == 0) {shotH.shotdown = 1;} //space to shoot.  no shots fired at "2" or "0"
  47.             }
  48.         keyp = key; //for debugging of key codes only... deleteable
  49.         for (li1 = 0; li1 < 3; li1++) { //cycles through all three key variables
  50.             if ((keys[li1] == -1)&&((keys[2] != key)&&(keys[0] != key)&&(keys[1] != key))) {keys[li1] = key;} //assigns the key to any free spaces... no duplicate key refs. "-1" = free
  51.             }
  52.         return true; //required by java
  53.         }
  54.     public boolean keyUp(Event ev, int key) { //keyUp event automatically calls this function
  55.         int li1; //temp integer
  56.         for (li1 = 0; li1 < 3; li1++) { //cycle through keys
  57.             if (keys[li1] == key) {keys[li1] = -1;} //"unpresses" the key from the games perspective
  58.             }
  59.         if (key == 122) {playH.players.shield = false;} //turns off the shield immediatly after key is released
  60.         shotH.shotdown = 0; //lets shots be fired on next keydown
  61.         return true; //required by java
  62.         }
  63.     public void checkKeys(int key) { //checks for the given key (in the array)
  64.         playH.keyDown(key); //tells the playerhandler that the key has been pressed.
  65.         shotH.keyDown(key, playH.players.ang, playH.players.xco, playH.players.yco); //tells the shothandler the given key has been pressed
  66.         }
  67.     public void init() {
  68.         level++; //change level to 1
  69.         score = 0; //obvious...
  70.         keys[0] = -1; //unpresses key #1
  71.         keys[1] = -1; //unpresses key #2
  72.         keys[2] = -1; //unpresses key #3
  73.         width = 640; //default width
  74.         height = 480; //default height
  75.         width = Integer.valueOf(getParameter("wid")).intValue(); //gets the width of the applet
  76.         height = Integer.valueOf(getParameter("hei")).intValue(); //gets the height of the applet
  77.         shotH = new ShotHandler(); //starts up shotH
  78.         playH = new PlayerHandler(width, height); //starts up playerH
  79.  
  80.         astH = new AstHandler(); //starts up astH
  81.         astH.AstCreate(level, width, height); //makes the first few asteroids... #asteroids relates to #level
  82. /*these next lines regard double buffer initialization.*/
  83.         try {
  84.             im = createImage(width,height); //applet size is width*height
  85.             offscreen = im.getGraphics();
  86.             }
  87.         catch (Exception e) {
  88.             offscreen = null;
  89.             }
  90. /*end of double buffering*/
  91.         codeb = getCodeBase(); //gets the code base, described in variable definition of "codeb"
  92.         explode = getAudioClip(codeb, "explode.au"); //gets the explosion audio clip in the applet's base directory
  93.         }
  94.     public void start() {
  95.         (new Thread(this)).start(); //initializes main thread
  96.         Thread.currentThread().setPriority(Thread.MAX_PRIORITY); //its necessary... try changing it sometime!
  97.         }
  98.  
  99.     public void run() {
  100.         while (true) { //always runs until stopped
  101.             levelstart++; //50 ticks of invulnerability to begin each level
  102.             if (levelstart>75) {levelstart = 51;} //doesn't let the number get out of hand.
  103.             checkKeys(keys[0]); //checks key#1
  104.             checkKeys(keys[1]); //checks key#2
  105.             checkKeys(keys[2]); //checks key#3
  106.             try {
  107.                 astH.move(); //tells astH to move all its asteroids
  108.                 i2 = astH.check(shotH, level, score, explode, width, height); //tells it to check its asteroids... needs all shot locations, the level of difficulty, needs soundfx, RETURNS score or a "level-over indicator"
  109.                 shotH.move(); //tells shotH to move all the shots
  110.                 shotH.check(width, height); //tells shotH to check the shots' movement
  111.                 playH.move(); //moves player(s)
  112.                 playH.check(astH, levelstart, width, height); //checks the player... need invulnerability info, and location of all asteroids.
  113.                 } catch(NullPointerException e) {}
  114.             repaint(); //repaints the screen
  115.             if (i2 == -1) {levelend++;} //i2 is -1 when all asteroids are destroyed - it will increase levelend ticker
  116.             if (i2 != -1) {score = i2;} //i2 is usually the score (returned by astH.check).  updates the score
  117.             if (levelend > 50) {nextlevel();} //if enough ticks have gone by, next level is initialized
  118.             try {Thread.currentThread().sleep(35);} catch (InterruptedException e){} //adds some time to keep the speed normal
  119.             Thread.currentThread().yield(); //REALLY REALLY REALLY REALLY IMPORTANT! THIS WILL DRAMATICALLY ENHANCE NETSACAPE PERFORMANCE!
  120.             }
  121.         }
  122.     public void nextlevel() {
  123.         score = score + (100*level); //"finish level" bonus
  124.         levelend = 0; //resets levelend ticker
  125.         playH.ss = playH.ss + (level/2)*10; //gives a shield bonus for the player
  126.         if (playH.ss>100) {playH.ss = 100;} //caps shield at 100
  127.         level++; //next level (finally)
  128.         keys[0] = -1; //resets key#1
  129.         keys[1] = -1; //resets key#2
  130.         keys[2] = -1; //resets key#3
  131.         astH = new AstHandler(); //makes a new set of asts
  132.         astH.AstCreate(level, width, height); //creates more asteroids in relation to the level
  133.         levelstart = 0; //allows 50 more ticks of invulnerability at start of next level
  134.         }        
  135.     public void update(Graphics g) {
  136.         paint(g); //overrides update
  137.         }
  138.     public void paint(Graphics g) {
  139.         if (offscreen!=null) { //more double-buffering crap
  140.             paintApplet(offscreen); //the REAL paint method
  141.             g.drawImage(im, 0, 0, this);
  142.             }
  143.         else {
  144.             paintApplet(g); //the REAL paint method
  145.             }
  146.         }
  147.     public void paintApplet(Graphics g) {
  148.         g.setColor(col.black); //prepares screen wash
  149.         g.fillRect(0,0,width,height); //makes big, black rectangle washover
  150.         playH.paint(g, width, height); //paints the player
  151.         astH.paint(g); //paints the asteroids
  152.         try {shotH.paint(g);} catch(NullPointerException e) {} //shotH.paint likes to crash applet, for some reason
  153.         g.setColor(col.white); //you know this, hopefully
  154.         this.showStatus("level: " + level); //puts the level number in the status bar
  155.         g.drawString("SCORE:  " + score, 0, 30); //shows the current score
  156.         }
  157.     }
  158.  
  159. class ShotHandler extends java.lang.Object/* implements Runnable*/ { //handles basic instructions and breaks them down to a shot-by-shot level
  160.     Shot shots[] = new Shot[17]; //makes all 17 shots (program only uses 16: 17 is to prevent buggy array access exceptions)
  161.     Color col1; //temp color variable
  162.     int shotdown = 0; //is there a shot being fired? 1 is true
  163.     private int li1, li2; //temp integers
  164.     public ShotHandler() { //constructor when a new instance is called for
  165.         for (li1 = 0; li1 < 16; li1++) { //cycles through the shots
  166.             shots[li1] = new Shot(); //new instance of shot for each segment of array
  167.             }
  168.         }
  169.     public void keyDown(int key, double ang, double xcor, double ycor) { //handles what is passed in from the main applet's keydown event
  170.         int freeshot = -1; //initializes as nonexistent
  171.         if (key == 32) { //if the key is space bar,
  172.             for(li1=0; li1<16; li1++) { //cycle shots
  173.                 if (shots[li1].active==false) {freeshot = li1;} //if shot isn't being used, make temp = shot #
  174.                 }
  175.             if ((shotdown == 1)&&(freeshot != -1)) { //if this is the FIRST pressing of space (not held) and freeshot "found a home" then:
  176.                 shots[freeshot].shoot(16, ang, xcor, ycor); //makes a shot of velocity 16, the players current angle, player's xcord, and player's ycord
  177.                 }
  178.             shotdown = 2; //space has been held down
  179.             }
  180.         }
  181.     public void check(int wid, int hei) {
  182.         for (li1 = 0; li1 < 16; li1++) { //cycle shots
  183.             if (shots[li1].active) { //if shot exists
  184.                 shots[li1].check(wid, hei); //call shot's individual check method
  185.                 if (shots[li1].cycles>16) { //if shot has been alive for 16 cycles
  186.                     shots[li1].stop(); //kill the friggin' thing
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.     public void move() { //obvious...
  192.         for (li1 = 0; li1 < 16; li1++) { //cycle shots
  193.             if (shots[li1].active) { //if alive
  194.                 shots[li1].move(); //call upon individual move method
  195.                 }
  196.             }
  197.         }
  198.     public void paint(Graphics g) { //the shotH paint method.
  199.         li1 = 0; //init temp var... this solves a NullPointerException often encountered... not TOTALLY sure why
  200.         for (li1 = 0; li1 < 16; li1++) { //cycle shots (again :-[)
  201.             if (shots[li1].active) { //if alive
  202.                 shots[li1].paint(g); //call individual paint method
  203.                 }
  204.             }
  205.         }
  206.     }
  207.             
  208. class Shot extends java.lang.Object { //an individual shot method, owned by ShotH's "shots" array
  209.     boolean active = false; //default: shot is non-existent
  210.     Color col1; //temp color variable
  211.     int cycles, rot;  //sequence of color
  212.     double pxc, pyc, xco, yco; //prev xcord, prev ycord, curr xcord, curr ycord
  213.     double ang, vel; //shot angle direction (in radians), velocity in units moved per program tick
  214.     public Shot() { //the constructor
  215.         active = false; //yeah, its redundant
  216.         }
  217.     public void shoot(double veloc, double angle, double xcor, double ycor) { //make an active shot with the given attributes
  218.         xco = xcor; //transfer x coordinate
  219.         yco = ycor; //transfer y coordinate
  220.         vel = veloc; //transfer velocity
  221.         ang = angle; //transfer angle
  222.         active = true; //make shot active, or visible.
  223.         cycles = 0; //after 16 cycles, the shot is destroyed.
  224.         rot = 0; //color rotation variable
  225.         pxc = xco; //initializes the previous xcord variable
  226.         pyc = yco; //initializes the previous ycord variable
  227.         }
  228.     public void stop() { //kills the shot
  229.         active = false; //take a guess...
  230.         }
  231.     public void paint(Graphics g) { //this is the shots paint method... draws a line from its last position to its current position, basically
  232.         rot++; //advances color rotation
  233.         if (rot == 16) {rot = 0;} //keeps rotation below 17
  234.         col1 = new Color(255-(rot*8),0,127+rot*8); //makes the new color
  235.         g.setColor(col1); //sets the current color to the rotation color
  236.         g.drawLine((int)xco, (int)yco, (int)(xco-(xco-pxc)), (int)(yco-(yco-pyc))); //draws a line from the current coords to the previous coords
  237.         }
  238.     public void move() { //moves the shot
  239.         cycles++; //advances the shot's "age" in cycles
  240.         pxc = xco; //makes a new prev. xcord
  241.         pyc = yco; //makes a new prev. ycord
  242.         xco = pxc + (Math.cos(ang)*vel); //defines the new xcord as a trig function utilizing angle and velocity (hypothenuse)
  243.         yco = pyc + (Math.sin(ang)*vel); //defines the new ycord as a trig function utilizing angle and velocity (hypothenuse)
  244.         }
  245.     public void check(int wi, int he) { //checks the shot's coords
  246.         if (xco>wi + 10) { //if xcord is more than 10 off the right side
  247.             xco = xco - (wi+20); //move to the left side of screen
  248.             pxc = xco; //disable the possibility of drawing a line across the screen
  249.             }
  250.         if (yco>he + 10) { //if ycord is more than 10 below the bottom
  251.             yco = yco - (he+20); //move to the top of screen
  252.             pyc = yco; //disable the possibility of drawing a line across the screen
  253.             }
  254.         if (xco<-10) { //if xcord is more than 10 off the left side
  255.             xco = xco + wi+20; //move to the right side of screen
  256.             pxc = xco; //disable the possibility of drawing a line across the screen
  257.             }
  258.         if (yco<-10) { //if ycord is more than 10 off the top
  259.             yco = yco + he+20; //move to the bottom of screen
  260.             pyc = yco; //disable the possibility of drawing a line across the screen
  261.             }
  262.         }
  263.     }
  264.  
  265. class Player extends java.lang.Object { //a player object
  266.     private int li1, li2; //some temp ints
  267.     boolean active = true; //is player alive?
  268.     boolean shield = false; //is shield on?
  269.     int rot; //color rotation
  270.     Color col2; //temp color variable
  271.     double pxc, pyc, xco, yco; //prev xcord, prev ycord, curr xcord, curr ycord
  272.     double ang, angdriftx, angdrifty, vel; //player angle direction, actual movement direction (x), actual movement direction (y), speed
  273.     public Player() { //the default constructor
  274.         active = true; //redundant? yes...
  275.         }
  276.     public void start(int wi, int he) { //the REAL constructor
  277.         xco = (Math.random()*wi); //chooses a random x value
  278.         yco = (Math.random()*he); //chooses a random y value
  279.         pxc = xco; //inits the prev xcord
  280.         pyc = yco; //inits the prev ycord
  281.         vel = 0; //sets velocity to 0
  282.         ang = .01; //makes angle a little past default: this eliminates trigonometry errors computing the angle (ie tan(0))
  283.         angdriftx = 0; //no movement
  284.         angdrifty = 0; //no movement
  285.         rot = (int) (Math.random()*15); //gets a random value for the color rotation
  286.         }
  287.     public void thrust(double amount) { //if the letter "k" is pressed, the ship must make movement adjustments
  288.         double lld1, lld2, lld3, lld4; //temp doubles
  289.         lld1 = angdriftx; //assigned to the x angle-drift
  290.         lld2 = angdrifty; //assigned to the y angle-drift
  291.         lld3 = (Math.cos(ang)*amount); //assigned to the amount of x-change there must be
  292.         lld4 = (Math.sin(ang)*amount); //assigned to the amount of y-change there must be
  293.         angdriftx = (lld1+lld3); //just what you'd expect next
  294.         angdrifty = (lld2+lld4); //same thing...
  295.         vel = Math.pow((angdriftx*angdriftx+angdrifty*angdrifty), .5); //distance formula modification.  pow(x, .5) takes the square root of x
  296.         if (angdriftx>8) { //if fast x movement
  297.             angdrifty = 8*(angdrifty/angdriftx); //keeps things in proportion
  298.             angdriftx = 8; //speed limiter
  299.             }
  300.         if (angdrifty>8) { //if fast y movement
  301.             angdriftx = 8*(angdriftx/angdrifty); //keeps things in proportion
  302.             angdrifty = 8; //speed limiter
  303.             }
  304.         if (angdriftx<-8) { //if fast x movement
  305.             angdrifty = -8*(angdrifty/angdriftx); //keeps things in proportion
  306.             angdriftx = -8; //speed limiter
  307.             }
  308.         if (angdrifty<-8) { //if fast y movement
  309.             angdriftx = -8*(angdriftx/angdrifty); //keeps things in proportion
  310.             angdrifty = -8; //speed limiter
  311.             }
  312.         }
  313.     public void rotate(double degree) { //change angle of ship
  314.         ang = ang + degree; //simple math
  315.         }
  316.     public boolean alive() { //function to check if player is alive
  317.         return active; //returns the active variable
  318.         }
  319.     public void paint(Graphics g) { //paints the player to the screen
  320.         if (shield) { //if shield is on
  321.             col2 = new Color(0, 64+rot*4, 0); //define a color based on the color rotater
  322.             g.setColor(col2); //set the new color
  323.             g.fillOval((int)(xco-12-(rot/2)), (int)(yco-12-(rot/2)), 24+rot, 24+rot); //make a quickly expanding circle
  324.             }
  325.         for (li1 = 0; li1 < 13; li1++) { //draw all thirteen "speed indicator" (?) lines
  326.             rot++; //cycle through another color rotation
  327.             if (rot == 15) {rot = 0;} //put cieling on color rotation
  328.             col2 = new Color(0,255-(rot*16),rot*16); //define new color
  329.             g.setColor(col2); //set new color
  330.             g.drawLine((int) (xco-(Math.cos(ang-Math.PI/8)*li1*vel/2)), (int) (yco-(Math.sin(ang-Math.PI/8)*li1*vel/2)), (int) (xco-(Math.cos(ang+Math.PI/8)*li1*vel/2)), (int) (yco-(Math.sin(ang+Math.PI/8)*li1*vel/2))); //draw a line from a distance&-angle to distance&+angle behind the ship
  331.             }
  332.         col2 = new Color(0, 172, 0); //make another new color
  333.         g.setColor(col2); //set new color
  334.         g.drawLine((int) (xco-(Math.cos(ang)*14)), (int) (yco-(Math.sin(ang)*14)), (int) (xco+(Math.cos(ang)*5)), (int) (yco+(Math.sin(ang)*5))); //make another trig line going behind player
  335.         g.drawLine((int) (xco-(Math.cos(ang-Math.PI/6)*14)), (int) (yco-(Math.sin(ang-Math.PI/6)*14)), (int) xco, (int) yco); //make another trig line going diagonally behind player
  336.         g.drawLine((int) (xco-(Math.cos(ang+Math.PI/6)*14)), (int) (yco-(Math.sin(ang+Math.PI/6)*14)), (int) xco, (int) yco); //make another trig line going diagonally behind player
  337.         }
  338.     public void move() { //moves the player
  339.         pxc = xco; //redefines prev xcord
  340.         pyc = yco; //redefines prev ycord
  341.         xco = pxc + angdriftx; //adds the drift to the current xcord
  342.         yco = pyc + angdrifty; //adds the drift to the current ycord
  343.         }
  344.     public void check(AstHandler ah, int starting, int wi, int he) { //checks for hits, position (using the list of asteroids)
  345.         int li1;
  346.         if (xco>wi + 10) { //if player is off right side of screen
  347.             xco = xco - (wi+20); //put player on left side of screen
  348.             }
  349.         if (yco>he + 10) { //if player is off bottom of screen
  350.             yco = yco - (he+20); //put player on top of screen
  351.             }
  352.         if (xco<-10) { //if player is off left side of screen
  353.             xco = xco + wi+20; //put player on right side of screen
  354.             }
  355.         if (yco<-10) { //if player is off top of screen
  356.             yco = yco + he+20; //put player on bottom of screen
  357.             }
  358.         if ((shield==false)&&(starting > 50)) { //if the shield ain't on...
  359.             for (li1 = 0; li1 < 40; li1++) { //cycle through all possible asteroids
  360.                 if ((ah.asts[li1].state==1)&&(Math.abs(ah.asts[li1].xco-xco)<ah.asts[li1].size)&&(Math.abs(ah.asts[li1].yco-yco)<ah.asts[li1].size)) { //if the asteroid exists and is touching the player, then:
  361.                     active = false; //player is DEAD!
  362.                     }
  363.                 }
  364.             }
  365.         }
  366.     public void stop() { //if program is stopped
  367.         active = false; //player is killed... oh, the humanity...
  368.         }
  369.     }
  370. class PlayerHandler extends java.lang.Object/* implements Runnable*/ { //handles the player(s)
  371.     Player players = new Player(); //make the new player
  372.     int ss; //shield strength remaining
  373.     private int li1, li2; //temp integers
  374.     public PlayerHandler(int wid, int hei) { //constructor
  375.         players.start(wid, hei); //define player
  376.         ss = 100; //put the shield at 100
  377.         }
  378.     public void keyDown(int key) {
  379.         if (key == 107) {players.thrust((double) .4);} //if key is "k", use the thrust(double) method to move player
  380.         if (key == 106) {players.rotate((double) (Math.PI / -24));} //if key is "j", rotate left
  381.         if (key == 108) {players.rotate((double) (Math.PI / 24));} //if key is "k", rotate right
  382.         if (key == 122) { //if key is "z"
  383.             players.shield = true; //turn on shield
  384.             ss = ss - 1; //take of the shield strength by one unit
  385.             if (ss < 0) { //if there isn't any shield left....
  386.                 ss = -1; //"turn off" shield strength
  387.                 players.shield = false; //turn off shield
  388.                 }
  389.             }
  390.         }
  391.     public void check(AstHandler ah, int sta, int wid, int hei) { //check for asteroid hits, etc
  392.         players.check(ah, sta, wid, hei); //let the player object do the REAL work... just passes arguments through the "chain of command"
  393.         if (players.alive()==false) { //if our boy is wounded...
  394.             players.stop(); //kill him, have mercy!
  395.             }
  396.         }
  397.     public void move() { //move the player
  398.         if (players.active) { //if its alive...
  399.             players.move(); //it should be moving, right?
  400.             }
  401.         }
  402.     public void paint(Graphics g, int wid, int hei) { //paint the player, do the shield display
  403.         Color col2 = new Color(0); //make a color... must instantiate for future references
  404.         if (players.active) { //if player is alive
  405.             g.setColor(col2.white); //set the color to white
  406.             g.drawString("Shield strength: ", 0, 10); //type shield strength on the graphics window
  407.             g.drawRect(100, 0, wid-101, 10); //make a bar shaped rectangle
  408.             g.setColor(col2.red); //set color to red
  409.             g.fillRect(101, 1, (int) (ss*((wid-101.5)/100)), 9); //make a rectangle with a width of the "ss" shield strength variable
  410.             players.paint(g); //call the player paint method
  411.             }
  412.         }
  413.     }
  414.  
  415. ///////////////////////////////////////
  416.  
  417. class AstHandler extends java.lang.Object/* implements Runnable*/ { //asteroid handling class
  418.     Ast asts[] = new Ast[41]; //make the asteroids! extra to avoid array exceptions
  419.     Color col1; //temp color variable
  420.     private int li1, li2; //temp integers
  421.     public AstHandler() { //asteroid handler constructor
  422.         for (li1 = 0; li1 < 41; li1++) { //cycle asteroids
  423.             asts[li1] = new Ast(); //make a new asteroid
  424.             }
  425.         }
  426.     public void AstCreate(int lev, int wid, int hei) { //create the asteroids with speed and number matching level
  427.         for (li1 = 0; ((li1 < (int) (Math.random()*4+2+lev*2))&&(li1<40)); li1 ++) { //cycle through a number of asteroids related to level number
  428.             asts[li1].create(Math.random()*wid, Math.random()*hei, Math.random()*2*Math.PI, Math.random()*(lev/2 + 1), Math.random()*28 + 16); //make that asteroid with random xcord, ycord, angle, velocity (defined w/ level #), and random size
  429.             }
  430.         }
  431.  
  432.     public int check(ShotHandler shotH, int leve, int sco, AudioClip expl, int wid, int hei) { //check using the shot handler, level number, score, and explosion audio clip.  Returns the score or a level end value (-1)
  433.         int li2 = -1; //start li2 out as "-1"
  434.         for (li1 = 0; li1 < 40; li1++) { //cycle asteroids
  435.             if (asts[li1].state == 1) { //if its alive and kicking:
  436.                 try {asts[li1].check(shotH, wid, hei);} catch(NullPointerException e) {} //try to call the individual asteroids check method using the shot handler
  437.                 li2++; //advance li2
  438.                 }
  439.             if (asts[li1].state == 2) { //if asteroid has JUST been hit (ready to be split)
  440.                 try {split(li1, leve);} catch(NullPointerException e) {} //split the asteroid into two smaller ones
  441.                 sco = sco+((int) (5*leve)); //advance score
  442.                 expl.play(); //play the audio clip
  443.                 }
  444.             if (asts[li1].state == 3) { //if asteroid is exploding...
  445.                 try {asts[li1].kill();} catch(NullPointerException e) {} //kill the asteroid
  446.                 }
  447.             }
  448.         li1 = 0; //to descrease array exceptions
  449.         if (li2==-1) {sco = li2;} //if no asteroids were detected, tell main app. that the level is over
  450.         return sco; //return the score (or indication that level is over)
  451.         }
  452.     public void split(int num, int lev) { //splits an asteroid into two new ones
  453.         int li1, li2; //temp ints
  454.         int freespot[] = new int[2]; //array for free asteroid spaces
  455.         freespot[0] = -1; //make first space null
  456.         freespot[1] = -1; //make second space also null
  457.         for(li1=0; li1<40; li1++) { //cycle asteroids
  458.             if (asts[li1].state == -1) { //if the space is free
  459.                 if (freespot[0] == -1) {freespot[0] = li1;} //claim it if the freespot has not already been defined
  460.                 }
  461.             }
  462.         for(li1=0; li1<40; li1++) { //cycle asteroids
  463.             if (asts[li1].state == -1) { //if the space is free
  464.                 if ((freespot[0] != li1)&&(freespot[1] == -1)) {freespot[1] = li1;} //if this isn't freespot[0]'s spot, and freespot[1] is still undefined, claim the space
  465.                 }
  466.             }
  467.         for(li1 = 0; li1<2; li1++) { //cycle freespots
  468.             if (freespot[li1]!=-1) { //if this freespot found a place to take
  469.                 if (asts[num].size > 12) {asts[freespot[li1]].create(asts[num].xco, asts[num].yco, Math.random()*2*Math.PI, Math.random()*(lev/2 + 2), asts[num].size/2);} //IF THIS ASTEROID ISN'T TOO SMALL, make the (li1)'th new asteroid
  470.                 }
  471.             }
  472.         asts[num].state = 3; //mark this asteroid for certain death
  473.         }
  474.     public void move() { //moves the asteroids
  475.         for (li1 = 0; li1 < 40; li1++) { //cycle through asteroids
  476.             if (asts[li1].state == 1) { //if asteroid is alive...
  477.                 asts[li1].move(); //move it.
  478.                 }
  479.             }
  480.         }
  481.     public void paint(Graphics g) { //paint the asteroids
  482.         for (li1 = 0; li1 < 40; li1++) { //cycle asteroids
  483.             if (asts[li1].state != -1) { //if asteroid isn't totally dead
  484.                 asts[li1].paint(g); //draw the asteroid
  485.                 }
  486.             }
  487.         }
  488.     }
  489.  
  490. class Ast extends java.lang.Object { //a single asteroid object
  491.     int state = -1; //start off as non-existent
  492.     Color col1; //temp color variable
  493.     int dierot, rot;  //the "death color rotation", normal color rotation
  494.     double size, pxc, pyc, xco, yco; //asteroid size, prev xcord, prev ycord, current xcord, current ycord
  495.     double ang, vel; //asteroid angle direction, asteroid velocity
  496.     public Ast() { //asteroid constructor... not much goin' on here, really
  497.         state = -1; //asteroid is dead/non-existent
  498.         }
  499.     public void create(double xcor, double ycor, double angle, double veloc, double siz) { //makes a new asteroid based on paramaters
  500.         xco = xcor; //transfer xcord
  501.         yco = ycor; //transfer ycord
  502.         vel = veloc; //transfer velocity
  503.         ang = angle; //transfer angle
  504.         state = 1; //make the asteroid alive
  505.         size = siz; //transfer size
  506.         rot = 0; //make a new color rotator
  507.         dierot = 0; //make a new death color rotator
  508.         pxc = xco; //define a new prev xcord
  509.         pyc = yco; //define a new prev ycord
  510.         }
  511.     public void kill() { //destroy the asteroid
  512.         dierot++; //move the dierot up one
  513.         if (dierot > 32) { //if its been 32 ticks off dierot,
  514.             state = -1; //finish the job and destroy asteroid for good
  515.             }
  516.         }
  517.     public void paint(Graphics g) { //draws asteroid
  518.         int li1; //temp integer
  519.         if (state == 1) { //if asteroid is in normal state
  520.             rot++; //advance color rotation
  521.             if (rot == 16) {rot = 0;} //restart color rot at 16
  522.             col1 = new Color(64+(Math.abs(rot-8)*6),64+Math.abs(rot-8)*5,255-(64+Math.abs(rot-8)*6)); //define grey-blue asteroid color
  523.             g.setColor(col1); //set the new color
  524.             g.fillOval((int)(xco-(size/2)), (int)(yco-(size/2)), (int)(size), (int)(size)); //draw the asteroid
  525.             }
  526.         if ((state == 2)||(state == 3)) { //if asteroid is splitting or dying
  527.             if (dierot<17) { //first half of death
  528.                 col1 = new Color(255, dierot*8, 0); //new color (red->orange->yellow)
  529.                 g.setColor(col1); //set the new color
  530.                 g.fillOval((int)(xco-dierot/2-4), (int)(yco-dierot/2-4), (int)((dierot+8)), (int)((dierot+8))); //make the explosion circle
  531.                 }
  532.             if (dierot>16) { //second half of death
  533.                 col1 = new Color(255, (dierot-16)*5+127, 0); //new color (red->orange->yellow)
  534.                 g.setColor(col1); //set the new color
  535.                 g.fillOval((int)(xco-(32-dierot)/2-4), (int)(yco-(32-dierot)/2-4), (int)((32-dierot)+8), (int)((32-dierot)+8)); //make the explosion circle
  536.                 }
  537.             }
  538.         }
  539.     public void move() { //moves the asteroid
  540.         pxc = xco; //new prev xcord
  541.         pyc = yco; //new prev ycord
  542.         xco = pxc + (Math.cos(ang)*vel); //new xcord
  543.         yco = pyc + (Math.sin(ang)*vel); //new ycord
  544.         }
  545.     public void check(ShotHandler sh, int wi, int he) { //check asteroid coordinates, see if its been hit (uses the shot handler)
  546.         int li1, li2; //temp integers
  547.         double sx[] = new double[16]; //array of shot xcords
  548.         double sy[] = new double[16]; //array of shot ycords
  549.         if (xco>wi + 10) { //if asteroid is off the right side of screen
  550.             xco = xco - (wi+20); //put asteroid on left side of screen
  551.             pxc = xco; //define new prev xcord to prevent drawing problems
  552.             }
  553.         if (yco>he + 10) { //if asteroid is off the bottom of screen
  554.             yco = yco - (he+20); //put asteroid on top of screen
  555.             pyc = yco; //define new prev ycord to prevent drawing problems
  556.             }
  557.         if (xco<-10) { //if asteroid is off the left side of screen
  558.             xco = xco + wi+20; //put asteroid on right side of screen
  559.             pxc = xco; //define new prev xcord to prevent drawing problems
  560.             }
  561.         if (yco<-10) { //if asteroid is off the top of screen
  562.             yco = yco + he+20; //put asteroid on bottom of screen
  563.             pyc = yco; //define new prev ycord to prevent drawing problems
  564.             }
  565.         for (li1 = 0; li1 < 16; li1++) { //cycle through shots in the imported shot handler
  566.             sx[li1] = sh.shots[li1].xco; //move the xcord to the local array
  567.             sy[li1] = sh.shots[li1].yco; //move the ycord to the local array
  568.             if (sh.shots[li1].active!=true) {sx[li1] = -127;} //if shot is non-existent, raise red flag w/ the -127 value.  -1 is a possible xcord, so I couldn't use that
  569.             }
  570.         for (li1 = 0; li1 < 16; li1++) { //cycle local "virtual shot array"
  571.             if ((sx[li1] != -127)&&(Math.abs(sx[li1]-xco)<size)&&(Math.abs(sy[li1]-yco)<size)) { //if the shot exists, and is close enough to the asteroid...
  572.                 state = 2; //prepare asteroid for splitting
  573.                 sh.shots[li1].stop(); //kill the shot, as well
  574.                 }
  575.             }
  576.         }
  577.     }
  578.  
  579. /* THATS ALL, FOLKS!
  580. * once again, sigelman@crocker.com is my email address, and you 
  581. * can see this applet (crash netscape) at my home page, 
  582. * "Ben Sigelman's Black Russian Page (JAVA)" - http://www.crocker.com/~sigelman/
  583. *
  584. * Thanx for looking at my applet!
  585. */