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 / WATER / WATER.EXE / Water.java < prev   
Encoding:
Java Source  |  1996-05-20  |  11.3 KB  |  390 lines

  1.  
  2.  
  3.  
  4.  
  5. import java.awt.*;
  6.  
  7. import java.applet.Applet;
  8.  
  9.  
  10.  
  11. public class Water extends java.applet.Applet implements Runnable {
  12.  
  13.    Thread killme = null;
  14.  
  15.    ImageCanvas board;
  16.  
  17.  
  18.  
  19. public void init() {
  20.  
  21.    // setLayout() is a java.awt.Container member.  It sets the LayoutManager
  22.  
  23.    // for the container.  Without it, the ImagePanels would get different
  24.  
  25.    // relative positions when the viewer is resized.
  26.  
  27.    setLayout(new BorderLayout());
  28.  
  29.  
  30.  
  31.    // Place the ImageHelp at the top of the applet.
  32.  
  33.    ImageHelp imHelp = new ImageHelp();
  34.  
  35.    add("North", imHelp);
  36.  
  37.  
  38.  
  39.    // Place the ImagePanel at the center of the applet.
  40.  
  41.    ImagePanel ip = new ImagePanel(this, imHelp.getLabel());
  42.  
  43.    add("Center", ip);
  44.  
  45.    board = ip.getBoard();
  46.  
  47. }
  48.  
  49.  
  50.  
  51. public void run() {
  52.  
  53.    while (killme != null) {
  54.  
  55.       try {Thread.sleep(100);} catch (InterruptedException e){}
  56.  
  57.  
  58.  
  59.       board.tick();
  60.  
  61.    }
  62.  
  63.    killme = null;
  64.  
  65. }
  66.  
  67.  
  68.  
  69. public void start() {
  70.  
  71.    if (killme == null) {
  72.  
  73.       killme = new Thread(this);
  74.  
  75.       killme.start();
  76.  
  77.    }
  78.  
  79. }
  80.  
  81.  
  82.  
  83. public void stop() {
  84.  
  85.    killme = null;
  86.  
  87. }
  88.  
  89.  
  90.  
  91. }
  92.  
  93.  
  94.  
  95. class ImageHelp extends Panel {
  96.  
  97.    Label l;
  98.  
  99.    public ImageHelp() {
  100.  
  101.       setLayout(new GridLayout(2, 1));
  102.  
  103.       // Create a new label, centered in the panel
  104.  
  105.       add(new Label("Move the tuber using the arrow keys", Label.CENTER));
  106.  
  107.       l = new Label("Score: ");
  108.  
  109.       add(l);
  110.  
  111.    }
  112.  
  113.  
  114.  
  115.    public Label getLabel() {
  116.  
  117.       return l;
  118.  
  119.    }
  120.  
  121. }
  122.  
  123.  
  124.  
  125. class ImagePanel extends Panel {
  126.  
  127.    ImageCanvas board;
  128.  
  129.  
  130.  
  131.    public ImagePanel(Applet app, Label l) {
  132.  
  133.       // Set the layout so that it is one column full size of the panel
  134.  
  135.       //
  136.  
  137.       setLayout(new GridLayout(0, 1));
  138.  
  139.  
  140.  
  141.       board = new ImageCanvas(app, l);
  142.  
  143.       add(board);
  144.  
  145.     }
  146.  
  147.  
  148.  
  149.    public ImageCanvas getBoard() {
  150.  
  151.       return board;
  152.  
  153.    }
  154.  
  155. }
  156.  
  157.  
  158.  
  159. class ImageCanvas extends Canvas /*implements ImageObserver*/ {
  160.  
  161.    int banks[][] = new int [2][12];
  162.  
  163.    int xpos = 50, ypos = 50;
  164.  
  165.    int yOff = 0;
  166.  
  167.    int ySpeed = 1;
  168.  
  169.    Image im;
  170.  
  171.    Graphics gOff;
  172.  
  173.    Image imTuber;
  174.  
  175.    Image imFin;
  176.  
  177.    int xFins[] = new int[3];
  178.  
  179.    int yFins[] = new int[3];
  180.  
  181.    int xSpeedFin[] = new int[3];
  182.  
  183.    int nShoreTick = 0;
  184.  
  185.    int nScore = 0;
  186.  
  187.    Label labelScore;
  188.  
  189.    boolean fPaused = true;
  190.  
  191.    boolean fDead = false;
  192.  
  193.    Image imCactus;
  194.  
  195.    int xCactus[] = new int[4];
  196.  
  197.    int yCactus[] = new int[4];
  198.  
  199.  
  200.  
  201.    public ImageCanvas(Applet app, Label l) {
  202.  
  203.       labelScore = l;
  204.  
  205.  
  206.  
  207.       imTuber = app.getImage(app.getCodeBase(), "tuber.gif");
  208.  
  209.       imFin = app.getImage(app.getCodeBase(), "fin.gif");
  210.  
  211.       imCactus = app.getImage(app.getCodeBase(), "cactus.gif");
  212.  
  213.  
  214.  
  215.       initBoard();
  216.  
  217.    }
  218.  
  219.  
  220.  
  221.    void initBoard() {
  222.  
  223.       // Initialize the banks
  224.  
  225.       for (int i = 0; i < 12; i++) {
  226.  
  227.          for (int j = 0; j < 2; j++) {
  228.  
  229.             banks[j][i] = (int)(Math.random() * 30);
  230.  
  231.          }
  232.  
  233.       }
  234.  
  235.  
  236.  
  237.       // Initialize the cacti
  238.  
  239.       for (int i = 0; i < 4; i++)
  240.  
  241.          xCactus[i] = -999;
  242.  
  243.  
  244.  
  245.       // Initialize the sharks
  246.  
  247.       for (int i = 0; i < 3; i++)
  248.  
  249.          xFins[i] = -999;
  250.  
  251.  
  252.  
  253.       xpos = 100;
  254.  
  255.       ypos = 50;
  256.  
  257.       yOff = 0;
  258.  
  259.       ySpeed = 1;
  260.  
  261.       nShoreTick = 0;
  262.  
  263.       nScore = 0;
  264.  
  265.       fDead = false;
  266.  
  267.    }
  268.  
  269.  
  270.  
  271.    public void layout() {
  272.  
  273.       Rectangle r = bounds(); // @@@ Has this been set yet?
  274.  
  275.       im = createImage(r.width, r.height);
  276.  
  277.       gOff = im.getGraphics();
  278.  
  279.    }
  280.  
  281.  
  282.  
  283.    // @@@ Is this member necessary?
  284.  
  285.    public void update(Graphics g) {
  286.  
  287.       paint(g);
  288.  
  289.    }
  290.  
  291.  
  292.  
  293.    public void paint(Graphics g) {
  294.  
  295.       Rectangle r = bounds(); // @@@ Has this been set yet?
  296.  
  297.       paintApplet(gOff);
  298.  
  299.       g.drawImage(im, 0, 0, this);
  300.  
  301.    }
  302.  
  303.  
  304.  
  305.    public void paintApplet(Graphics g) {
  306.  
  307.     Rectangle r = bounds();
  308.  
  309.         g.setColor(Color.blue);
  310.  
  311.         g.fillRect(0, 0, r.width, r.height);
  312.  
  313.         // @@@ Can't use clearRect() 'cause I can't figure out how to set the background color...
  314.  
  315. //        g.clearRect(0, 0, r.width, r.height);
  316.  
  317.  
  318.  
  319.         // Draw the banks
  320.  
  321.         Polygon p1 = new Polygon();
  322.  
  323.         p1.addPoint(0, 0);
  324.  
  325.         Polygon p2 = new Polygon();
  326.  
  327.         p2.addPoint(r.width, 0);
  328.  
  329.         for (int i = 0; i < 12; i++) {
  330.  
  331.            p1.addPoint(banks[0][i], i * r.height / 10 - yOff);
  332.  
  333.            p2.addPoint(r.width - banks[1][i], i * r.height / 10 - yOff);
  334.  
  335.         }
  336.  
  337.         p1.addPoint(0, r.height);
  338.  
  339.         p1.addPoint(0, 0);
  340.  
  341.         p2.addPoint(r.width, r.height);
  342.  
  343.         p2.addPoint(r.width, 0);
  344.  
  345.         g.setColor(Color.yellow);
  346.  
  347.         g.fillPolygon(p1);
  348.  
  349.         g.fillPolygon(p2);
  350.  
  351.  
  352.  
  353.         // Draw the cacti
  354.  
  355.         for (int i = 0; i < 4; i++)
  356.  
  357.            if (xCactus[i] != -999) {
  358.  
  359. //              System.out.println("Drawing a cactus");
  360.  
  361.               g.drawImage(imCactus, xCactus[i], yCactus[i], this);
  362.  
  363.            }
  364.  
  365.  
  366.  
  367.         // Draw the sharks
  368.  
  369.         for (int i = 0; i < 3; i++)
  370.  
  371.            if (xFins[i] != -999)
  372.  
  373.               g.drawImage(imFin, xFins[i], yFins[i], this);
  374.  
  375.  
  376.  
  377.         // Draw the player
  378.  
  379.         g.drawImage(imTuber, xpos, ypos, this);
  380.  
  381.  
  382.  
  383.         // Display any status messages
  384.  
  385.         g.setColor(Color.white);
  386.  
  387.         if (fDead)
  388.  
  389.            g.drawString("Press space bar for new game", 50, 50);
  390.  
  391.         else if (fPaused)
  392.  
  393.            g.drawString("Press space bar to tube", 50, 50);
  394.  
  395.      }
  396.  
  397.  
  398.  
  399.    int leftbank(int y) {
  400.  
  401.       Rectangle r = bounds();
  402.  
  403.       int segment = (y + yOff) * 10 / r.height;
  404.  
  405.       return Math.max(banks[0][segment], banks[0][segment+1]);
  406.  
  407.    }
  408.  
  409.  
  410.  
  411.    int rightbank(int y) {
  412.  
  413.       Rectangle r = bounds();
  414.  
  415.       int segment = (y + yOff) * 10 / r.height;
  416.  
  417.       return r.width - Math.min(banks[1][segment], banks[1][segment+1]);
  418.  
  419.    }
  420.  
  421.  
  422.  
  423.    public synchronized boolean handleEvent(Event e) {
  424.  
  425.       switch (e.id) {
  426.  
  427.      case Event.KEY_ACTION:
  428.  
  429.      case Event.KEY_PRESS:
  430.  
  431.         switch (e.key) {
  432.  
  433.           case Event.UP:
  434.  
  435.               case 'k':
  436.  
  437.                 if (ypos - 5 > 0 && !fPaused)
  438.  
  439.                    ypos -= 5;
  440.  
  441.         repaint();
  442.  
  443.         return true;
  444.  
  445.           case Event.DOWN:
  446.  
  447.               case 'j':
  448.  
  449.                 if (ypos + 5 < bounds().height - 32 && !fPaused)
  450.  
  451.            ypos += 5;
  452.  
  453.         repaint();
  454.  
  455.         return true;
  456.  
  457.           case Event.RIGHT:
  458.  
  459.           case 'l':
  460.  
  461.                 if (xpos + 5 < rightbank(ypos) - 32 && !fPaused)
  462.  
  463.                    xpos += 5;
  464.  
  465.         repaint();
  466.  
  467.         return true;
  468.  
  469.           case Event.LEFT:
  470.  
  471.               case 'h':
  472.  
  473.                 if (xpos - 5 > leftbank(ypos) && !fPaused)
  474.  
  475.                    xpos -= 5;
  476.  
  477.         repaint();
  478.  
  479.         return true;
  480.  
  481. //              case 'z':
  482.  
  483. //                if (ySpeed > 0)
  484.  
  485. //                   ySpeed--;
  486.  
  487. //                return true;
  488.  
  489. //              case 'x':
  490.  
  491. //                if (ySpeed < 10)
  492.  
  493. //                   ySpeed++;
  494.  
  495. //                return true;
  496.  
  497.               case ' ':
  498.  
  499.                  if (fDead) {
  500.  
  501.                     initBoard();
  502.  
  503.                     fPaused = false;
  504.  
  505.                     labelScore.setText("Score: " + String.valueOf(nScore));
  506.  
  507.                  } else
  508.  
  509.                     fPaused = !fPaused;
  510.  
  511.                  return true;
  512.  
  513.           default:
  514.  
  515.         return false;
  516.  
  517.         }
  518.  
  519.  
  520.  
  521.       default:
  522.  
  523.         return false;
  524.  
  525.     }
  526.  
  527.     }
  528.  
  529.  
  530.  
  531.    // tick() is because I couldn't figure out if I made this class Runnable what
  532.  
  533.    // whould I do about start()&stop()
  534.  
  535.    public void tick() {
  536.  
  537.       if (fPaused)
  538.  
  539.          return;
  540.  
  541.  
  542.  
  543.       // Scroll the shore
  544.  
  545.       yOff += ySpeed;
  546.  
  547.  
  548.  
  549.       // Scroll the player
  550.  
  551.       if (ypos-ySpeed > 0) {
  552.  
  553.          ypos -= ySpeed;
  554.  
  555.       }
  556.  
  557.  
  558.  
  559.       if (xpos < leftbank(ypos))
  560.  
  561.          xpos = leftbank(ypos);
  562.  
  563.       else if (xpos > rightbank(ypos) - 32)
  564.  
  565.          xpos = rightbank(ypos) - 32;
  566.  
  567.  
  568.  
  569.       // Scroll the cactus
  570.  
  571.       for (int i = 0; i < 4; i++)
  572.  
  573.          if (xCactus[i] != -999) {
  574.  
  575.             yCactus[i] -= ySpeed;
  576.  
  577.             if (yCactus[i] < 0) {
  578.  
  579. //               System.out.println("Killing a cactus");
  580.  
  581.                xCactus[i] = -999;
  582.  
  583.             }
  584.  
  585.          }
  586.  
  587.  
  588.  
  589.       Rectangle rectPlayer = new Rectangle(xpos+4, ypos+4, 32-4, 32-4);
  590.  
  591.       Rectangle rectShark = new Rectangle();
  592.  
  593.  
  594.  
  595.       // Scroll the sharks
  596.  
  597.       for (int i = 0; i < 3; i++) {
  598.  
  599.          yFins[i] -= ySpeed;
  600.  
  601.          if (xFins[i] != -999 && (yFins[i] < 0 || xFins[i] < leftbank(yFins[i]))) {
  602.  
  603.             // Shark is off-screen.  Destroy.
  604.  
  605. //            System.out.println("Destroyed a shark");
  606.  
  607.             xFins[i] = -999;
  608.  
  609.          }
  610.  
  611.          if (xFins[i] != -999) {
  612.  
  613.             xFins[i] -= xSpeedFin[i] * ySpeed;
  614.  
  615.  
  616.  
  617.             // Did the shark hit the player?
  618.  
  619.             rectShark.reshape(xFins[i]+8, yFins[i]+8, 32-8, 32-8);
  620.  
  621.             if (rectPlayer.intersects(rectShark)) {
  622.  
  623.                fDead = true;
  624.  
  625.                fPaused = true;
  626.  
  627.             }
  628.  
  629.          }
  630.  
  631.       }
  632.  
  633.  
  634.  
  635.       // Update score, if on the bottom half of screen
  636.  
  637.       Rectangle r = bounds();
  638.  
  639.       if (ypos+yOff > r.height/2) {
  640.  
  641.          nScore += ySpeed;
  642.  
  643.          labelScore.setText("Score: " + String.valueOf(nScore));
  644.  
  645.       }
  646.  
  647.  
  648.  
  649.       // Do we need to generate another shore coordinate?
  650.  
  651.       if (yOff >= r.height/10) {
  652.  
  653.          yOff -= r.height/10;
  654.  
  655.  
  656.  
  657.          for (int i = 1; i < 12; i++) {
  658.  
  659.             banks[0][i-1] = banks[0][i];
  660.  
  661.             banks[1][i-1] = banks[1][i];
  662.  
  663.          }
  664.  
  665. //         banks[0][11] = banks[0][10] + 10;
  666.  
  667. //         banks[1][11] = banks[0][10] + 10;
  668.  
  669.          banks[0][11] = banks[0][10] + (int)(Math.random() * 20) - 7;
  670.  
  671.          banks[1][11] = banks[1][10] + (int)(Math.random() * 20) - 7;
  672.  
  673.          if (banks[0][11] < 0)
  674.  
  675.             banks[0][11] = 0;
  676.  
  677.          if (r.width - banks[1][11] < 100)
  678.  
  679.             banks[1][11] -= 64;
  680.  
  681.          if (banks[0][11] >= r.width)
  682.  
  683.             banks[0][11] = r.width-1;
  684.  
  685.          if (banks[1][11] >= r.width)
  686.  
  687.             banks[1][11] = r.width-1;
  688.  
  689.          if (banks[0][11] > banks[1][11]) {
  690.  
  691.             int i = banks[0][11];
  692.  
  693.             banks[0][11] = banks[1][11];
  694.  
  695.             banks[1][11] = i;
  696.  
  697.          }
  698.  
  699.          if (banks[0][11] + 64 > r.width - banks[1][11]) {
  700.  
  701.             banks[0][11] = r.width - banks[1][11] - 40;
  702.  
  703.             banks[1][11] -= 22;
  704.  
  705.          }
  706.  
  707.  
  708.  
  709.          // If necessary, generate a new shark
  710.  
  711.          for (int i = 0; i < 3; i++)
  712.  
  713.             if (xFins[i] == -999 && Math.random() < 0.2) {
  714.  
  715.                xFins[i] = r.width - banks[1][11] - 32;
  716.  
  717.                yFins[i] = 9 * r.height / 10;
  718.  
  719.                xSpeedFin[i] = (int)(Math.random() * 2) + 1;
  720.  
  721. //               System.out.println("Created a shark");
  722.  
  723.                break;
  724.  
  725.             }
  726.  
  727.  
  728.  
  729.          nShoreTick++;
  730.  
  731.          if (nShoreTick > 20) {
  732.  
  733. //            System.out.println("Finished level " + String.valueOf(ySpeed));
  734.  
  735.             nShoreTick = 0;
  736.  
  737.             banks[0][11] = 0;
  738.  
  739.             banks[1][11] = 0;
  740.  
  741.             if (ySpeed < 10)
  742.  
  743.                ySpeed++;
  744.  
  745.             }
  746.  
  747.       }
  748.  
  749.  
  750.  
  751.       // Add any needed cacti
  752.  
  753.       for (int i = 0; i < 4; i++)
  754.  
  755.          if (xCactus[i] == -999 && Math.random() < .02) {
  756.  
  757. //            System.out.println("Making a cactus");
  758.  
  759.             yCactus[i] = r.height;
  760.  
  761.             if (Math.random() < 0.5)
  762.  
  763.                xCactus[i] = (int)(Math.random() * banks[0][11]);
  764.  
  765.             else
  766.  
  767.                xCactus[i] = r.width - (int)(Math.random() * banks[1][11]);
  768.  
  769.          }
  770.  
  771.  
  772.  
  773.       repaint();
  774.  
  775.    }
  776.  
  777. }
  778.  
  779.