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.  
  3. import java.lang.*;
  4.  
  5. import java.awt.*;
  6.  
  7. import java.applet.*;
  8.  
  9. import java.net.*;
  10.  
  11.  
  12.  
  13. /* This is all original material, with the exception of the double buffering, which 
  14.  
  15. * I hacked out of a quote-ticker program.  The game is a GREAT example of object-heirarchy.
  16.  
  17. * I also supported multithreading, but it just made things run slower, actually!  
  18.  
  19. * For this reason the multithreading was removed...
  20.  
  21.  
  22.  
  23. * PARAMETERS: 
  24.  
  25. *    wid: the width of the applet (integer)
  26.  
  27. *    hei: the height of the applet (integer)
  28.  
  29.  
  30.  
  31. * Feel free to modify or distribute as you wish, as long as you include:
  32.  
  33.  
  34.  
  35. Original programmed by: Ben Sigelman, sigelman@crocker.com
  36.  
  37. http://www.crocker.com/~sigelman/   |||  FOR HIRE! ALWAYS!
  38.  
  39.  
  40.  
  41. * Appletviewer has its bugs.... netscape _is_ a bug.
  42.  
  43. */
  44.  
  45.  
  46.  
  47. public class aster extends java.applet.Applet implements Runnable { //this is the main program, aster(noid)
  48.  
  49.     ShotHandler shotH; //this is an implementation of my "shothandler" class, which keeps track of all your shots.
  50.  
  51.     PlayerHandler playH; //this is an implementation of my "playerhandler" class, which keeps track of your players needs... leaves open support for multi-player
  52.  
  53.     AstHandler astH; //same deal:  handling class for all of the "asteroids" in the game. (the things you shoot, that is)
  54.  
  55.     Color col;  //i like to define things like this in case their needed... this one is used to change colors
  56.  
  57.     Image im; //this is used in the double-buffering lines.
  58.  
  59.     Graphics offscreen; //more double-buffering crap
  60.  
  61.     int keyp, i1, i2, i3, i4; //keyp was used for debugging.  the rest are just temp. integers
  62.  
  63.     public static int score; //self-explanatory... its the score accumulated
  64.  
  65.     int level = 0; //this is the current level
  66.  
  67.     int levelend = 0; //this is used in the pause at the end of every level
  68.  
  69.     int levelstart = 0; //this is used in the invulnerability period that starts each level.
  70.  
  71.     double d1, d2, d3, d4; //temp double things
  72.  
  73.     int keys[] = new int[3]; //VERY IMPORTANT! this has to do with overriding the default keyboard handling
  74.  
  75.     AudioClip /*shotsound, */explode; //the explosion sound in the game... shotsound was too obnoxious to include
  76.  
  77.     URL codeb; //code base URL.  this is the documents url... a kind of base directory
  78.  
  79.     int width, height; //width and height as specified by the html file
  80.  
  81.  
  82.  
  83.     public boolean keyDown(Event ev, int key) { //keyDown event automatically calls this function.
  84.  
  85.         int li1; //temp integer
  86.  
  87.         if (key == 32) { //space bar: shoot
  88.  
  89.             if (shotH.shotdown == 1) {shotH.shotdown = 2;} //these lines dont allow you to hold down
  90.  
  91.             if (shotH.shotdown == 0) {shotH.shotdown = 1;} //space to shoot.  no shots fired at "2" or "0"
  92.  
  93.             }
  94.  
  95.         keyp = key; //for debugging of key codes only... deleteable
  96.  
  97.         for (li1 = 0; li1 < 3; li1++) { //cycles through all three key variables
  98.  
  99.             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
  100.  
  101.             }
  102.  
  103.         return true; //required by java
  104.  
  105.         }
  106.  
  107.     public boolean keyUp(Event ev, int key) { //keyUp event automatically calls this function
  108.  
  109.         int li1; //temp integer
  110.  
  111.         for (li1 = 0; li1 < 3; li1++) { //cycle through keys
  112.  
  113.             if (keys[li1] == key) {keys[li1] = -1;} //"unpresses" the key from the games perspective
  114.  
  115.             }
  116.  
  117.         if (key == 122) {playH.players.shield = false;} //turns off the shield immediatly after key is released
  118.  
  119.         shotH.shotdown = 0; //lets shots be fired on next keydown
  120.  
  121.         return true; //required by java
  122.  
  123.         }
  124.  
  125.     public void checkKeys(int key) { //checks for the given key (in the array)
  126.  
  127.         playH.keyDown(key); //tells the playerhandler that the key has been pressed.
  128.  
  129.         shotH.keyDown(key, playH.players.ang, playH.players.xco, playH.players.yco); //tells the shothandler the given key has been pressed
  130.  
  131.         }
  132.  
  133.     public void init() {
  134.  
  135.         level++; //change level to 1
  136.  
  137.         score = 0; //obvious...
  138.  
  139.         keys[0] = -1; //unpresses key #1
  140.  
  141.         keys[1] = -1; //unpresses key #2
  142.  
  143.         keys[2] = -1; //unpresses key #3
  144.  
  145.         width = 640; //default width
  146.  
  147.         height = 480; //default height
  148.  
  149.         width = Integer.valueOf(getParameter("wid")).intValue(); //gets the width of the applet
  150.  
  151.         height = Integer.valueOf(getParameter("hei")).intValue(); //gets the height of the applet
  152.  
  153.         shotH = new ShotHandler(); //starts up shotH
  154.  
  155.         playH = new PlayerHandler(width, height); //starts up playerH
  156.  
  157.  
  158.  
  159.         astH = new AstHandler(); //starts up astH
  160.  
  161.         astH.AstCreate(level, width, height); //makes the first few asteroids... #asteroids relates to #level
  162.  
  163. /*these next lines regard double buffer initialization.*/
  164.  
  165.         try {
  166.  
  167.             im = createImage(width,height); //applet size is width*height
  168.  
  169.             offscreen = im.getGraphics();
  170.  
  171.             }
  172.  
  173.         catch (Exception e) {
  174.  
  175.             offscreen = null;
  176.  
  177.             }
  178.  
  179. /*end of double buffering*/
  180.  
  181.         codeb = getCodeBase(); //gets the code base, described in variable definition of "codeb"
  182.  
  183.         explode = getAudioClip(codeb, "explode.au"); //gets the explosion audio clip in the applet's base directory
  184.  
  185.         }
  186.  
  187.     public void start() {
  188.  
  189.         (new Thread(this)).start(); //initializes main thread
  190.  
  191.         Thread.currentThread().setPriority(Thread.MAX_PRIORITY); //its necessary... try changing it sometime!
  192.  
  193.         }
  194.  
  195.  
  196.  
  197.     public void run() {
  198.  
  199.         while (true) { //always runs until stopped
  200.  
  201.             levelstart++; //50 ticks of invulnerability to begin each level
  202.  
  203.             if (levelstart>75) {levelstart = 51;} //doesn't let the number get out of hand.
  204.  
  205.             checkKeys(keys[0]); //checks key#1
  206.  
  207.             checkKeys(keys[1]); //checks key#2
  208.  
  209.             checkKeys(keys[2]); //checks key#3
  210.  
  211.             try {
  212.  
  213.                 astH.move(); //tells astH to move all its asteroids
  214.  
  215.                 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"
  216.  
  217.                 shotH.move(); //tells shotH to move all the shots
  218.  
  219.                 shotH.check(width, height); //tells shotH to check the shots' movement
  220.  
  221.                 playH.move(); //moves player(s)
  222.  
  223.                 playH.check(astH, levelstart, width, height); //checks the player... need invulnerability info, and location of all asteroids.
  224.  
  225.                 } catch(NullPointerException e) {}
  226.  
  227.             repaint(); //repaints the screen
  228.  
  229.             if (i2 == -1) {levelend++;} //i2 is -1 when all asteroids are destroyed - it will increase levelend ticker
  230.  
  231.             if (i2 != -1) {score = i2;} //i2 is usually the score (returned by astH.check).  updates the score
  232.  
  233.             if (levelend > 50) {nextlevel();} //if enough ticks have gone by, next level is initialized
  234.  
  235.             try {Thread.currentThread().sleep(35);} catch (InterruptedException e){} //adds some time to keep the speed normal
  236.  
  237.             Thread.currentThread().yield(); //REALLY REALLY REALLY REALLY IMPORTANT! THIS WILL DRAMATICALLY ENHANCE NETSACAPE PERFORMANCE!
  238.  
  239.             }
  240.  
  241.         }
  242.  
  243.     public void nextlevel() {
  244.  
  245.         score = score + (100*level); //"finish level" bonus
  246.  
  247.         levelend = 0; //resets levelend ticker
  248.  
  249.         playH.ss = playH.ss + (level/2)*10; //gives a shield bonus for the player
  250.  
  251.         if (playH.ss>100) {playH.ss = 100;} //caps shield at 100
  252.  
  253.         level++; //next level (finally)
  254.  
  255.         keys[0] = -1; //resets key#1
  256.  
  257.         keys[1] = -1; //resets key#2
  258.  
  259.         keys[2] = -1; //resets key#3
  260.  
  261.         astH = new AstHandler(); //makes a new set of asts
  262.  
  263.         astH.AstCreate(level, width, height); //creates more asteroids in relation to the level
  264.  
  265.         levelstart = 0; //allows 50 more ticks of invulnerability at start of next level
  266.  
  267.         }        
  268.  
  269.     public void update(Graphics g) {
  270.  
  271.         paint(g); //overrides update
  272.  
  273.         }
  274.  
  275.     public void paint(Graphics g) {
  276.  
  277.         if (offscreen!=null) { //more double-buffering crap
  278.  
  279.             paintApplet(offscreen); //the REAL paint method
  280.  
  281.             g.drawImage(im, 0, 0, this);
  282.  
  283.             }
  284.  
  285.         else {
  286.  
  287.             paintApplet(g); //the REAL paint method
  288.  
  289.             }
  290.  
  291.         }
  292.  
  293.     public void paintApplet(Graphics g) {
  294.  
  295.         g.setColor(col.black); //prepares screen wash
  296.  
  297.         g.fillRect(0,0,width,height); //makes big, black rectangle washover
  298.  
  299.         playH.paint(g, width, height); //paints the player
  300.  
  301.         astH.paint(g); //paints the asteroids
  302.  
  303.         try {shotH.paint(g);} catch(NullPointerException e) {} //shotH.paint likes to crash applet, for some reason
  304.  
  305.         g.setColor(col.white); //you know this, hopefully
  306.  
  307.         this.showStatus("level: " + level); //puts the level number in the status bar
  308.  
  309.         g.drawString("SCORE:  " + score, 0, 30); //shows the current score
  310.  
  311.         }
  312.  
  313.     }
  314.  
  315.  
  316.  
  317. class ShotHandler extends java.lang.Object/* implements Runnable*/ { //handles basic instructions and breaks them down to a shot-by-shot level
  318.  
  319.     Shot shots[] = new Shot[17]; //makes all 17 shots (program only uses 16: 17 is to prevent buggy array access exceptions)
  320.  
  321.     Color col1; //temp color variable
  322.  
  323.     int shotdown = 0; //is there a shot being fired? 1 is true
  324.  
  325.     private int li1, li2; //temp integers
  326.  
  327.     public ShotHandler() { //constructor when a new instance is called for
  328.  
  329.         for (li1 = 0; li1 < 16; li1++) { //cycles through the shots
  330.  
  331.             shots[li1] = new Shot(); //new instance of shot for each segment of array
  332.  
  333.             }
  334.  
  335.         }
  336.  
  337.     public void keyDown(int key, double ang, double xcor, double ycor) { //handles what is passed in from the main applet's keydown event
  338.  
  339.         int freeshot = -1; //initializes as nonexistent
  340.  
  341.         if (key == 32) { //if the key is space bar,
  342.  
  343.             for(li1=0; li1<16; li1++) { //cycle shots
  344.  
  345.                 if (shots[li1].active==false) {freeshot = li1;} //if shot isn't being used, make temp = shot #
  346.  
  347.                 }
  348.  
  349.             if ((shotdown == 1)&&(freeshot != -1)) { //if this is the FIRST pressing of space (not held) and freeshot "found a home" then:
  350.  
  351.                 shots[freeshot].shoot(16, ang, xcor, ycor); //makes a shot of velocity 16, the players current angle, player's xcord, and player's ycord
  352.  
  353.                 }
  354.  
  355.             shotdown = 2; //space has been held down
  356.  
  357.             }
  358.  
  359.         }
  360.  
  361.     public void check(int wid, int hei) {
  362.  
  363.         for (li1 = 0; li1 < 16; li1++) { //cycle shots
  364.  
  365.             if (shots[li1].active) { //if shot exists
  366.  
  367.                 shots[li1].check(wid, hei); //call shot's individual check method
  368.  
  369.                 if (shots[li1].cycles>16) { //if shot has been alive for 16 cycles
  370.  
  371.                     shots[li1].stop(); //kill the friggin' thing
  372.  
  373.                     }
  374.  
  375.                 }
  376.  
  377.             }
  378.  
  379.         }
  380.  
  381.     public void move() { //obvious...
  382.  
  383.         for (li1 = 0; li1 < 16; li1++) { //cycle shots
  384.  
  385.             if (shots[li1].active) { //if alive
  386.  
  387.                 shots[li1].move(); //call upon individual move method
  388.  
  389.                 }
  390.  
  391.             }
  392.  
  393.         }
  394.  
  395.     public void paint(Graphics g) { //the shotH paint method.
  396.  
  397.         li1 = 0; //init temp var... this solves a NullPointerException often encountered... not TOTALLY sure why
  398.  
  399.         for (li1 = 0; li1 < 16; li1++) { //cycle shots (again :-[)
  400.  
  401.             if (shots[li1].active) { //if alive
  402.  
  403.                 shots[li1].paint(g); //call individual paint method
  404.  
  405.                 }
  406.  
  407.             }
  408.  
  409.         }
  410.  
  411.     }
  412.  
  413.             
  414.  
  415. class Shot extends java.lang.Object { //an individual shot method, owned by ShotH's "shots" array
  416.  
  417.     boolean active = false; //default: shot is non-existent
  418.  
  419.     Color col1; //temp color variable
  420.  
  421.     int cycles, rot;  //sequence of color
  422.  
  423.     double pxc, pyc, xco, yco; //prev xcord, prev ycord, curr xcord, curr ycord
  424.  
  425.     double ang, vel; //shot angle direction (in radians), velocity in units moved per program tick
  426.  
  427.     public Shot() { //the constructor
  428.  
  429.         active = false; //yeah, its redundant
  430.  
  431.         }
  432.  
  433.     public void shoot(double veloc, double angle, double xcor, double ycor) { //make an active shot with the given attributes
  434.  
  435.         xco = xcor; //transfer x coordinate
  436.  
  437.         yco = ycor; //transfer y coordinate
  438.  
  439.         vel = veloc; //transfer velocity
  440.  
  441.         ang = angle; //transfer angle
  442.  
  443.         active = true; //make shot active, or visible.
  444.  
  445.         cycles = 0; //after 16 cycles, the shot is destroyed.
  446.  
  447.         rot = 0; //color rotation variable
  448.  
  449.         pxc = xco; //initializes the previous xcord variable
  450.  
  451.         pyc = yco; //initializes the previous ycord variable
  452.  
  453.         }
  454.  
  455.     public void stop() { //kills the shot
  456.  
  457.         active = false; //take a guess...
  458.  
  459.         }
  460.  
  461.     public void paint(Graphics g) { //this is the shots paint method... draws a line from its last position to its current position, basically
  462.  
  463.         rot++; //advances color rotation
  464.  
  465.         if (rot == 16) {rot = 0;} //keeps rotation below 17
  466.  
  467.         col1 = new Color(255-(rot*8),0,127+rot*8); //makes the new color
  468.  
  469.         g.setColor(col1); //sets the current color to the rotation color
  470.  
  471.         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
  472.  
  473.         }
  474.  
  475.     public void move() { //moves the shot
  476.  
  477.         cycles++; //advances the shot's "age" in cycles
  478.  
  479.         pxc = xco; //makes a new prev. xcord
  480.  
  481.         pyc = yco; //makes a new prev. ycord
  482.  
  483.         xco = pxc + (Math.cos(ang)*vel); //defines the new xcord as a trig function utilizing angle and velocity (hypothenuse)
  484.  
  485.         yco = pyc + (Math.sin(ang)*vel); //defines the new ycord as a trig function utilizing angle and velocity (hypothenuse)
  486.  
  487.         }
  488.  
  489.     public void check(int wi, int he) { //checks the shot's coords
  490.  
  491.         if (xco>wi + 10) { //if xcord is more than 10 off the right side
  492.  
  493.             xco = xco - (wi+20); //move to the left side of screen
  494.  
  495.             pxc = xco; //disable the possibility of drawing a line across the screen
  496.  
  497.             }
  498.  
  499.         if (yco>he + 10) { //if ycord is more than 10 below the bottom
  500.  
  501.             yco = yco - (he+20); //move to the top of screen
  502.  
  503.             pyc = yco; //disable the possibility of drawing a line across the screen
  504.  
  505.             }
  506.  
  507.         if (xco<-10) { //if xcord is more than 10 off the left side
  508.  
  509.             xco = xco + wi+20; //move to the right side of screen
  510.  
  511.             pxc = xco; //disable the possibility of drawing a line across the screen
  512.  
  513.             }
  514.  
  515.         if (yco<-10) { //if ycord is more than 10 off the top
  516.  
  517.             yco = yco + he+20; //move to the bottom of screen
  518.  
  519.             pyc = yco; //disable the possibility of drawing a line across the screen
  520.  
  521.             }
  522.  
  523.         }
  524.  
  525.     }
  526.  
  527.  
  528.  
  529. class Player extends java.lang.Object { //a player object
  530.  
  531.     private int li1, li2; //some temp ints
  532.  
  533.     boolean active = true; //is player alive?
  534.  
  535.     boolean shield = false; //is shield on?
  536.  
  537.     int rot; //color rotation
  538.  
  539.     Color col2; //temp color variable
  540.  
  541.     double pxc, pyc, xco, yco; //prev xcord, prev ycord, curr xcord, curr ycord
  542.  
  543.     double ang, angdriftx, angdrifty, vel; //player angle direction, actual movement direction (x), actual movement direction (y), speed
  544.  
  545.     public Player() { //the default constructor
  546.  
  547.         active = true; //redundant? yes...
  548.  
  549.         }
  550.  
  551.     public void start(int wi, int he) { //the REAL constructor
  552.  
  553.         xco = (Math.random()*wi); //chooses a random x value
  554.  
  555.         yco = (Math.random()*he); //chooses a random y value
  556.  
  557.         pxc = xco; //inits the prev xcord
  558.  
  559.         pyc = yco; //inits the prev ycord
  560.  
  561.         vel = 0; //sets velocity to 0
  562.  
  563.         ang = .01; //makes angle a little past default: this eliminates trigonometry errors computing the angle (ie tan(0))
  564.  
  565.         angdriftx = 0; //no movement
  566.  
  567.         angdrifty = 0; //no movement
  568.  
  569.         rot = (int) (Math.random()*15); //gets a random value for the color rotation
  570.  
  571.         }
  572.  
  573.     public void thrust(double amount) { //if the letter "k" is pressed, the ship must make movement adjustments
  574.  
  575.         double lld1, lld2, lld3, lld4; //temp doubles
  576.  
  577.         lld1 = angdriftx; //assigned to the x angle-drift
  578.  
  579.         lld2 = angdrifty; //assigned to the y angle-drift
  580.  
  581.         lld3 = (Math.cos(ang)*amount); //assigned to the amount of x-change there must be
  582.  
  583.         lld4 = (Math.sin(ang)*amount); //assigned to the amount of y-change there must be
  584.  
  585.         angdriftx = (lld1+lld3); //just what you'd expect next
  586.  
  587.         angdrifty = (lld2+lld4); //same thing...
  588.  
  589.         vel = Math.pow((angdriftx*angdriftx+angdrifty*angdrifty), .5); //distance formula modification.  pow(x, .5) takes the square root of x
  590.  
  591.         if (angdriftx>8) { //if fast x movement
  592.  
  593.             angdrifty = 8*(angdrifty/angdriftx); //keeps things in proportion
  594.  
  595.             angdriftx = 8; //speed limiter
  596.  
  597.             }
  598.  
  599.         if (angdrifty>8) { //if fast y movement
  600.  
  601.             angdriftx = 8*(angdriftx/angdrifty); //keeps things in proportion
  602.  
  603.             angdrifty = 8; //speed limiter
  604.  
  605.             }
  606.  
  607.         if (angdriftx<-8) { //if fast x movement
  608.  
  609.             angdrifty = -8*(angdrifty/angdriftx); //keeps things in proportion
  610.  
  611.             angdriftx = -8; //speed limiter
  612.  
  613.             }
  614.  
  615.         if (angdrifty<-8) { //if fast y movement
  616.  
  617.             angdriftx = -8*(angdriftx/angdrifty); //keeps things in proportion
  618.  
  619.             angdrifty = -8; //speed limiter
  620.  
  621.             }
  622.  
  623.         }
  624.  
  625.     public void rotate(double degree) { //change angle of ship
  626.  
  627.         ang = ang + degree; //simple math
  628.  
  629.         }
  630.  
  631.     public boolean alive() { //function to check if player is alive
  632.  
  633.         return active; //returns the active variable
  634.  
  635.         }
  636.  
  637.     public void paint(Graphics g) { //paints the player to the screen
  638.  
  639.         if (shield) { //if shield is on
  640.  
  641.             col2 = new Color(0, 64+rot*4, 0); //define a color based on the color rotater
  642.  
  643.             g.setColor(col2); //set the new color
  644.  
  645.             g.fillOval((int)(xco-12-(rot/2)), (int)(yco-12-(rot/2)), 24+rot, 24+rot); //make a quickly expanding circle
  646.  
  647.             }
  648.  
  649.         for (li1 = 0; li1 < 13; li1++) { //draw all thirteen "speed indicator" (?) lines
  650.  
  651.             rot++; //cycle through another color rotation
  652.  
  653.             if (rot == 15) {rot = 0;} //put cieling on color rotation
  654.  
  655.             col2 = new Color(0,255-(rot*16),rot*16); //define new color
  656.  
  657.             g.setColor(col2); //set new color
  658.  
  659.             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
  660.  
  661.             }
  662.  
  663.         col2 = new Color(0, 172, 0); //make another new color
  664.  
  665.         g.setColor(col2); //set new color
  666.  
  667.         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
  668.  
  669.         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
  670.  
  671.         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
  672.  
  673.         }
  674.  
  675.     public void move() { //moves the player
  676.  
  677.         pxc = xco; //redefines prev xcord
  678.  
  679.         pyc = yco; //redefines prev ycord
  680.  
  681.         xco = pxc + angdriftx; //adds the drift to the current xcord
  682.  
  683.         yco = pyc + angdrifty; //adds the drift to the current ycord
  684.  
  685.         }
  686.  
  687.     public void check(AstHandler ah, int starting, int wi, int he) { //checks for hits, position (using the list of asteroids)
  688.  
  689.         int li1;
  690.  
  691.         if (xco>wi + 10) { //if player is off right side of screen
  692.  
  693.             xco = xco - (wi+20); //put player on left side of screen
  694.  
  695.             }
  696.  
  697.         if (yco>he + 10) { //if player is off bottom of screen
  698.  
  699.             yco = yco - (he+20); //put player on top of screen
  700.  
  701.             }
  702.  
  703.         if (xco<-10) { //if player is off left side of screen
  704.  
  705.             xco = xco + wi+20; //put player on right side of screen
  706.  
  707.             }
  708.  
  709.         if (yco<-10) { //if player is off top of screen
  710.  
  711.             yco = yco + he+20; //put player on bottom of screen
  712.  
  713.             }
  714.  
  715.         if ((shield==false)&&(starting > 50)) { //if the shield ain't on...
  716.  
  717.             for (li1 = 0; li1 < 40; li1++) { //cycle through all possible asteroids
  718.  
  719.                 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:
  720.  
  721.                     active = false; //player is DEAD!
  722.  
  723.                     }
  724.  
  725.                 }
  726.  
  727.             }
  728.  
  729.         }
  730.  
  731.     public void stop() { //if program is stopped
  732.  
  733.         active = false; //player is killed... oh, the humanity...
  734.  
  735.         }
  736.  
  737.     }
  738.  
  739. class PlayerHandler extends java.lang.Object/* implements Runnable*/ { //handles the player(s)
  740.  
  741.     Player players = new Player(); //make the new player
  742.  
  743.     int ss; //shield strength remaining
  744.  
  745.     private int li1, li2; //temp integers
  746.  
  747.     public PlayerHandler(int wid, int hei) { //constructor
  748.  
  749.         players.start(wid, hei); //define player
  750.  
  751.         ss = 100; //put the shield at 100
  752.  
  753.         }
  754.  
  755.     public void keyDown(int key) {
  756.  
  757.         if (key == 107) {players.thrust((double) .4);} //if key is "k", use the thrust(double) method to move player
  758.  
  759.         if (key == 106) {players.rotate((double) (Math.PI / -24));} //if key is "j", rotate left
  760.  
  761.         if (key == 108) {players.rotate((double) (Math.PI / 24));} //if key is "k", rotate right
  762.  
  763.         if (key == 122) { //if key is "z"
  764.  
  765.             players.shield = true; //turn on shield
  766.  
  767.             ss = ss - 1; //take of the shield strength by one unit
  768.  
  769.             if (ss < 0) { //if there isn't any shield left....
  770.  
  771.                 ss = -1; //"turn off" shield strength
  772.  
  773.                 players.shield = false; //turn off shield
  774.  
  775.                 }
  776.  
  777.             }
  778.  
  779.         }
  780.  
  781.     public void check(AstHandler ah, int sta, int wid, int hei) { //check for asteroid hits, etc
  782.  
  783.         players.check(ah, sta, wid, hei); //let the player object do the REAL work... just passes arguments through the "chain of command"
  784.  
  785.         if (players.alive()==false) { //if our boy is wounded...
  786.  
  787.             players.stop(); //kill him, have mercy!
  788.  
  789.             }
  790.  
  791.         }
  792.  
  793.     public void move() { //move the player
  794.  
  795.         if (players.active) { //if its alive...
  796.  
  797.             players.move(); //it should be moving, right?
  798.  
  799.             }
  800.  
  801.         }
  802.  
  803.     public void paint(Graphics g, int wid, int hei) { //paint the player, do the shield display
  804.  
  805.         Color col2 = new Color(0); //make a color... must instantiate for future references
  806.  
  807.         if (players.active) { //if player is alive
  808.  
  809.             g.setColor(col2.white); //set the color to white
  810.  
  811.             g.drawString("Shield strength: ", 0, 10); //type shield strength on the graphics window
  812.  
  813.             g.drawRect(100, 0, wid-101, 10); //make a bar shaped rectangle
  814.  
  815.             g.setColor(col2.red); //set color to red
  816.  
  817.             g.fillRect(101, 1, (int) (ss*((wid-101.5)/100)), 9); //make a rectangle with a width of the "ss" shield strength variable
  818.  
  819.             players.paint(g); //call the player paint method
  820.  
  821.             }
  822.  
  823.         }
  824.  
  825.     }
  826.  
  827.  
  828.  
  829. ///////////////////////////////////////
  830.  
  831.  
  832.  
  833. class AstHandler extends java.lang.Object/* implements Runnable*/ { //asteroid handling class
  834.  
  835.     Ast asts[] = new Ast[41]; //make the asteroids! extra to avoid array exceptions
  836.  
  837.     Color col1; //temp color variable
  838.  
  839.     private int li1, li2; //temp integers
  840.  
  841.     public AstHandler() { //asteroid handler constructor
  842.  
  843.         for (li1 = 0; li1 < 41; li1++) { //cycle asteroids
  844.  
  845.             asts[li1] = new Ast(); //make a new asteroid
  846.  
  847.             }
  848.  
  849.         }
  850.  
  851.     public void AstCreate(int lev, int wid, int hei) { //create the asteroids with speed and number matching level
  852.  
  853.         for (li1 = 0; ((li1 < (int) (Math.random()*4+2+lev*2))&&(li1<40)); li1 ++) { //cycle through a number of asteroids related to level number
  854.  
  855.             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
  856.  
  857.             }
  858.  
  859.         }
  860.  
  861.  
  862.  
  863.     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)
  864.  
  865.         int li2 = -1; //start li2 out as "-1"
  866.  
  867.         for (li1 = 0; li1 < 40; li1++) { //cycle asteroids
  868.  
  869.             if (asts[li1].state == 1) { //if its alive and kicking:
  870.  
  871.                 try {asts[li1].check(shotH, wid, hei);} catch(NullPointerException e) {} //try to call the individual asteroids check method using the shot handler
  872.  
  873.                 li2++; //advance li2
  874.  
  875.                 }
  876.  
  877.             if (asts[li1].state == 2) { //if asteroid has JUST been hit (ready to be split)
  878.  
  879.                 try {split(li1, leve);} catch(NullPointerException e) {} //split the asteroid into two smaller ones
  880.  
  881.                 sco = sco+((int) (5*leve)); //advance score
  882.  
  883.                 expl.play(); //play the audio clip
  884.  
  885.                 }
  886.  
  887.             if (asts[li1].state == 3) { //if asteroid is exploding...
  888.  
  889.                 try {asts[li1].kill();} catch(NullPointerException e) {} //kill the asteroid
  890.  
  891.                 }
  892.  
  893.             }
  894.  
  895.         li1 = 0; //to descrease array exceptions
  896.  
  897.         if (li2==-1) {sco = li2;} //if no asteroids were detected, tell main app. that the level is over
  898.  
  899.         return sco; //return the score (or indication that level is over)
  900.  
  901.         }
  902.  
  903.     public void split(int num, int lev) { //splits an asteroid into two new ones
  904.  
  905.         int li1, li2; //temp ints
  906.  
  907.         int freespot[] = new int[2]; //array for free asteroid spaces
  908.  
  909.         freespot[0] = -1; //make first space null
  910.  
  911.         freespot[1] = -1; //make second space also null
  912.  
  913.         for(li1=0; li1<40; li1++) { //cycle asteroids
  914.  
  915.             if (asts[li1].state == -1) { //if the space is free
  916.  
  917.                 if (freespot[0] == -1) {freespot[0] = li1;} //claim it if the freespot has not already been defined
  918.  
  919.                 }
  920.  
  921.             }
  922.  
  923.         for(li1=0; li1<40; li1++) { //cycle asteroids
  924.  
  925.             if (asts[li1].state == -1) { //if the space is free
  926.  
  927.                 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
  928.  
  929.                 }
  930.  
  931.             }
  932.  
  933.         for(li1 = 0; li1<2; li1++) { //cycle freespots
  934.  
  935.             if (freespot[li1]!=-1) { //if this freespot found a place to take
  936.  
  937.                 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
  938.  
  939.                 }
  940.  
  941.             }
  942.  
  943.         asts[num].state = 3; //mark this asteroid for certain death
  944.  
  945.         }
  946.  
  947.     public void move() { //moves the asteroids
  948.  
  949.         for (li1 = 0; li1 < 40; li1++) { //cycle through asteroids
  950.  
  951.             if (asts[li1].state == 1) { //if asteroid is alive...
  952.  
  953.                 asts[li1].move(); //move it.
  954.  
  955.                 }
  956.  
  957.             }
  958.  
  959.         }
  960.  
  961.     public void paint(Graphics g) { //paint the asteroids
  962.  
  963.         for (li1 = 0; li1 < 40; li1++) { //cycle asteroids
  964.  
  965.             if (asts[li1].state != -1) { //if asteroid isn't totally dead
  966.  
  967.                 asts[li1].paint(g); //draw the asteroid
  968.  
  969.                 }
  970.  
  971.             }
  972.  
  973.         }
  974.  
  975.     }
  976.  
  977.  
  978.  
  979. class Ast extends java.lang.Object { //a single asteroid object
  980.  
  981.     int state = -1; //start off as non-existent
  982.  
  983.     Color col1; //temp color variable
  984.  
  985.     int dierot, rot;  //the "death color rotation", normal color rotation
  986.  
  987.     double size, pxc, pyc, xco, yco; //asteroid size, prev xcord, prev ycord, current xcord, current ycord
  988.  
  989.     double ang, vel; //asteroid angle direction, asteroid velocity
  990.  
  991.     public Ast() { //asteroid constructor... not much goin' on here, really
  992.  
  993.         state = -1; //asteroid is dead/non-existent
  994.  
  995.         }
  996.  
  997.     public void create(double xcor, double ycor, double angle, double veloc, double siz) { //makes a new asteroid based on paramaters
  998.  
  999.         xco = xcor; //transfer xcord
  1000.  
  1001.         yco = ycor; //transfer ycord
  1002.  
  1003.         vel = veloc; //transfer velocity
  1004.  
  1005.         ang = angle; //transfer angle
  1006.  
  1007.         state = 1; //make the asteroid alive
  1008.  
  1009.         size = siz; //transfer size
  1010.  
  1011.         rot = 0; //make a new color rotator
  1012.  
  1013.         dierot = 0; //make a new death color rotator
  1014.  
  1015.         pxc = xco; //define a new prev xcord
  1016.  
  1017.         pyc = yco; //define a new prev ycord
  1018.  
  1019.         }
  1020.  
  1021.     public void kill() { //destroy the asteroid
  1022.  
  1023.         dierot++; //move the dierot up one
  1024.  
  1025.         if (dierot > 32) { //if its been 32 ticks off dierot,
  1026.  
  1027.             state = -1; //finish the job and destroy asteroid for good
  1028.  
  1029.             }
  1030.  
  1031.         }
  1032.  
  1033.     public void paint(Graphics g) { //draws asteroid
  1034.  
  1035.         int li1; //temp integer
  1036.  
  1037.         if (state == 1) { //if asteroid is in normal state
  1038.  
  1039.             rot++; //advance color rotation
  1040.  
  1041.             if (rot == 16) {rot = 0;} //restart color rot at 16
  1042.  
  1043.             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
  1044.  
  1045.             g.setColor(col1); //set the new color
  1046.  
  1047.             g.fillOval((int)(xco-(size/2)), (int)(yco-(size/2)), (int)(size), (int)(size)); //draw the asteroid
  1048.  
  1049.             }
  1050.  
  1051.         if ((state == 2)||(state == 3)) { //if asteroid is splitting or dying
  1052.  
  1053.             if (dierot<17) { //first half of death
  1054.  
  1055.                 col1 = new Color(255, dierot*8, 0); //new color (red->orange->yellow)
  1056.  
  1057.                 g.setColor(col1); //set the new color
  1058.  
  1059.                 g.fillOval((int)(xco-dierot/2-4), (int)(yco-dierot/2-4), (int)((dierot+8)), (int)((dierot+8))); //make the explosion circle
  1060.  
  1061.                 }
  1062.  
  1063.             if (dierot>16) { //second half of death
  1064.  
  1065.                 col1 = new Color(255, (dierot-16)*5+127, 0); //new color (red->orange->yellow)
  1066.  
  1067.                 g.setColor(col1); //set the new color
  1068.  
  1069.                 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
  1070.  
  1071.                 }
  1072.  
  1073.             }
  1074.  
  1075.         }
  1076.  
  1077.     public void move() { //moves the asteroid
  1078.  
  1079.         pxc = xco; //new prev xcord
  1080.  
  1081.         pyc = yco; //new prev ycord
  1082.  
  1083.         xco = pxc + (Math.cos(ang)*vel); //new xcord
  1084.  
  1085.         yco = pyc + (Math.sin(ang)*vel); //new ycord
  1086.  
  1087.         }
  1088.  
  1089.     public void check(ShotHandler sh, int wi, int he) { //check asteroid coordinates, see if its been hit (uses the shot handler)
  1090.  
  1091.         int li1, li2; //temp integers
  1092.  
  1093.         double sx[] = new double[16]; //array of shot xcords
  1094.  
  1095.         double sy[] = new double[16]; //array of shot ycords
  1096.  
  1097.         if (xco>wi + 10) { //if asteroid is off the right side of screen
  1098.  
  1099.             xco = xco - (wi+20); //put asteroid on left side of screen
  1100.  
  1101.             pxc = xco; //define new prev xcord to prevent drawing problems
  1102.  
  1103.             }
  1104.  
  1105.         if (yco>he + 10) { //if asteroid is off the bottom of screen
  1106.  
  1107.             yco = yco - (he+20); //put asteroid on top of screen
  1108.  
  1109.             pyc = yco; //define new prev ycord to prevent drawing problems
  1110.  
  1111.             }
  1112.  
  1113.         if (xco<-10) { //if asteroid is off the left side of screen
  1114.  
  1115.             xco = xco + wi+20; //put asteroid on right side of screen
  1116.  
  1117.             pxc = xco; //define new prev xcord to prevent drawing problems
  1118.  
  1119.             }
  1120.  
  1121.         if (yco<-10) { //if asteroid is off the top of screen
  1122.  
  1123.             yco = yco + he+20; //put asteroid on bottom of screen
  1124.  
  1125.             pyc = yco; //define new prev ycord to prevent drawing problems
  1126.  
  1127.             }
  1128.  
  1129.         for (li1 = 0; li1 < 16; li1++) { //cycle through shots in the imported shot handler
  1130.  
  1131.             sx[li1] = sh.shots[li1].xco; //move the xcord to the local array
  1132.  
  1133.             sy[li1] = sh.shots[li1].yco; //move the ycord to the local array
  1134.  
  1135.             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
  1136.  
  1137.             }
  1138.  
  1139.         for (li1 = 0; li1 < 16; li1++) { //cycle local "virtual shot array"
  1140.  
  1141.             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...
  1142.  
  1143.                 state = 2; //prepare asteroid for splitting
  1144.  
  1145.                 sh.shots[li1].stop(); //kill the shot, as well
  1146.  
  1147.                 }
  1148.  
  1149.             }
  1150.  
  1151.         }
  1152.  
  1153.     }
  1154.  
  1155.  
  1156.  
  1157. /* THATS ALL, FOLKS!
  1158.  
  1159. * once again, sigelman@crocker.com is my email address, and you 
  1160.  
  1161. * can see this applet (crash netscape) at my home page, 
  1162.  
  1163. * "Ben Sigelman's Black Russian Page (JAVA)" - http://www.crocker.com/~sigelman/
  1164.  
  1165. *
  1166.  
  1167. * Thanx for looking at my applet!
  1168.  
  1169. */