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