home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 May / comcd0502.iso / homepage / javaspecial / 09_01 / shooter / Shooter.java < prev   
Encoding:
Java Source  |  2000-08-10  |  16.7 KB  |  581 lines

  1. /*********************************************************
  2.    Applet  : Shooter.java - My Third Java Applet Game
  3.    Version : 1.30
  4.    Author  : Riza Purwo Nugroho <rizapn@ratelindo.co.id>
  5.    Date    : August 5, 2000
  6.    Update  : August 8, 2000
  7. ----------------------------------------------------------
  8.    This is my third Java Applet game.
  9.    Hope anybody who read this will enjoy to learn more
  10.    about Java Applet programming.
  11.    Sorry if some code is not too clear to understand.
  12. **********************************************************/
  13.  
  14. import java.applet.*;
  15. import java.awt.*;
  16. import java.awt.image.*;
  17. import java.net.*;
  18. import java.util.*;
  19.  
  20. public class Shooter extends Applet implements Runnable
  21. {
  22.   static int MAX_X=9, MAX_Y=15, MAX_LVL=100, MAX_IMG=12;
  23.   static int X_0 = 30, Y_0 = 5, BALL_CODE = 7;
  24.  
  25.   Thread game0 = null;
  26.  
  27.   private Image[] xImg = new Image[MAX_IMG+1];
  28.   private int gLevel, mvPos, maxMove, shPos, dlyBall, num;
  29.   private byte[][] currCanvas = new byte[MAX_Y+1][MAX_X+1];
  30.   private byte firstTime;
  31.  
  32.   private Button btn1  = new Button("X");
  33.   private Button btnN  = new Button(">");
  34.   private Button btnP  = new Button("<");
  35.   private Button btnBP = new Button("+");
  36.   private Button btnBM = new Button("-");
  37.  
  38.   private static String stGameEnd = " [GAME END]";
  39.   private static String stYouLose = " [YOU LOSE]";
  40.  
  41.   private static String ascCode = "-ABCDEFGHIJKLMNOP";
  42.   private static String mCode = " #=    *.oO@+,123456789abcdefghij";
  43.   private static int xCoord[][] =
  44.     {{  0, 0,20,20}, { 20, 0,20,20}, {120, 0,20,20}, { 40, 0,20,20}, { 60, 0,20,20}, { 80, 0,20,20}, {100, 0,20,20},
  45.      {100,20,20,20}, {  0,20,20,20}, { 20,20,20,20}, { 40,20,20,20}, { 60,20,20,20}, { 80,20,20,20}};
  46.  
  47.   private static String biDat[] = {
  48.     "#9,#3,#2,#1,#,,,,,O+2,.O2,o3,+.2 4o,#9,",
  49.     "#9,#3,#2,#1,#,,,,,O.2,+O2,o.2,.O2 4*,#9,",
  50.     "#9,#3,#2,#1,#,,,,,.3,o2.,.3,oO+. 4*,#9,",
  51.     "#9,#3,#2,#1,#,,,,,O.O.,O.O.,.O.O,+o.O 4*,#9,",
  52.     "#9,#3,#2,#1,#,,,,,O+o.,+o.O,+o.O,+o.O 4*,#9,",
  53.     "#9,#3=,#1,#,,,,, 5=,o1+1,.1o1 1=,oO2,.+oO 4+,#9,",
  54.     "#9,#3,#2,#1,#,,,,,O+1o,oO1o,o2+,.O.o 4o,#9,",
  55.     "#9,#3,#2,#1,#,,,,,+1O1,o+1o,o1.1,.o+1 4O,#9,",
  56.     "#9,#3,#2,#1,#,,,,,o1+O,.oOo,o.+.,+Oo. 4+,#9,",
  57.     "#9,#3,#2,#,#,,,,,O.o.,.+1.,oO.1,O+o. 4.,#9,",
  58.     "#9,#3,#2,#,#,,,,,o.+O,+O2,.o+o,+.o. 4.,#9,",
  59.     "#9,#3,#2,#1,#,,,,,O+1O,.O.1,o+.o,Oo1+ 4O,#9,",
  60.     "#9,#3,#2,#1,#,,,,,O1+o,+.1+,o.+.,o+O+ 4o,#9,",
  61.     "#9,#3,#2,#1,#,,,,,.o.O,.1o+,O.1+,+1o1 4.,#9,",
  62.     "#9,#3,#2,#1,#,,,,,.2+,+1.+,o+.o,+1Oo 4*,#9,",
  63.     "#9,#3,#2,#,#,,,,O4,O2oO,.O+oO,oOoO1,oO.1O 3*,#9,",
  64.     "#9,#3,#2,#1,#,,,,O4,Oo+O1,O.1O1,O4,.3O 3O,#9,",
  65.     "#9,#3,#2,#,#,,,,o.+2,.oO.1,oO3,o.O2,o+O2 3*,#9,",
  66.     "#9,#3,#1,#1,#,,,,o4,o2Oo,oOo1+,oO.o1 =,+o3 3o,#9,",
  67.     "#9,#3,#2,#1,#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.,O3.,.1oOo,o+o1O=,o+2O 3*,#9,",
  101.     "#9,#2,#2,#1,#,=,=,+.1Oo,.2O1,o.o.o,OoO+o,O1+o+,o+3 3*,#9,",
  102.     "#9,#3,#2,#,#,, =,+1.2,.o1.O=,.+2O,O1+1o =,.o.o1,OoO1o 3*,#9,",
  103.     "#9,#3 2=,#2,#1 =,#, 3=,,O.O.1O,+.1O1o,.3+1,o+o+2,o1O+1o,oO1oOo 2*,#9,",
  104.   };
  105.  
  106.   private static int aMove[] =
  107.     { 4, 5, 4, 9,11, 8, 7, 7,10,10,
  108.      11,12,10, 9,12,13, 7,13,10,13,
  109.      22,22,22,22,22,22,22,22,22,22,
  110.      22,22,22,22,22, 9,33,33,33,33,
  111.      40,40,40,40,40,40,40,40,40,40,
  112.      40,40,40,40,40,40,40,40,40,40,
  113.      40,40,40,40,40,40,40,40,40,40,
  114.     };
  115.  
  116.   private Image mainImg;
  117.   private Graphics g;
  118.   private TextField txt1;
  119.  
  120.   public void init()
  121.   {
  122.     String param;
  123.     int i;
  124.  
  125.     if (g==null) g = getGraphics();
  126.  
  127.     URL url = getCodeBase();
  128.  
  129.     String str = "";
  130.     int b = 0;
  131.  
  132.     MediaTracker tracker = new MediaTracker(this);
  133.     mainImg = getImage(url, "Balls.gif");
  134.     tracker.addImage(mainImg,0);
  135.  
  136.     for (i=0; i<=MAX_IMG; i++)
  137.       {
  138.         xImg[i] = extractImage(xCoord[i]);
  139.         tracker.addImage(xImg[i], i+1);
  140.       }
  141.  
  142.     try { tracker.waitForAll(); }
  143.     catch (InterruptedException e) {}
  144.  
  145.     setLayout(new BorderLayout());
  146.  
  147.     txt1 = new TextField(17);
  148.     txt1.setForeground(new Color(255,255,204));
  149.     txt1.setBackground(new Color(102,51,51));
  150.     txt1.setEditable(false);
  151.  
  152.     Panel p = new Panel();
  153.  
  154.     p.add(txt1);
  155.     p.add(btnBM);
  156.     p.add(btnBP);
  157.     p.add(btnP);
  158.     p.add(btnN);
  159.     p.add(btn1);
  160.  
  161.     add("South", p);
  162.  
  163.     gLevel = 0;
  164.     dat2Canvas(0);
  165.     firstTime = 1;
  166.     dlyBall = 5;
  167.   }
  168.  
  169.   public void start()
  170.   {
  171.     if (game0==null) 
  172.      {
  173.        game0 = new Thread(this);
  174.        game0.start();
  175.      }
  176.   }
  177.  
  178.   public void stop()
  179.   {
  180.     if (game0 != null)
  181.      {
  182.        game0.stop();
  183.        game0 = null;
  184.      }
  185.   }
  186.  
  187.   public void run()
  188.   {
  189.   }
  190.  
  191.   public boolean dat2Canvas(int dlvl)
  192.   {
  193.     int i,x,y,tmp0;
  194.     byte z = 0, oz = 0;
  195.     String param;
  196.  
  197.     try
  198.      {
  199.        gLevel += dlvl;
  200.        if (gLevel<0) gLevel = 0;
  201.  
  202.        try { i = Integer.parseInt(getParameter("maxmove"+gLevel)); }
  203.        catch (NumberFormatException e) { i = 0; }
  204.  
  205.        param = getParameter("data"+gLevel);
  206.        if (i>0) maxMove = i;
  207.  
  208.        if (param==null || i<=0)
  209.         { 
  210.           param = biDat[gLevel];
  211.           maxMove = aMove[gLevel];
  212.         }
  213.      }
  214.     catch (ArrayIndexOutOfBoundsException exc)
  215.      { 
  216.        gLevel -= dlvl; 
  217.        param = biDat[gLevel];
  218.        maxMove = aMove[gLevel];
  219.      }
  220.  
  221.     for (y=0; y<=MAX_Y; y++)
  222.       { for (x=0; x<=MAX_X; x++)
  223.           { currCanvas[y][x] = 0; }
  224.       }
  225.  
  226.     x = y = 0;
  227.     for (i=0; i<param.length(); i++)
  228.       {
  229.         z = (byte)(mCode.indexOf(param.charAt(i)));
  230.         if (z>MAX_IMG)
  231.          {
  232.            if ((tmp0 = (z-MAX_IMG-1))>0)
  233.             {
  234.               for (int j=0; j<tmp0; j++)
  235.                 {
  236.                   try { currCanvas[y][x++] = oz; }
  237.                   catch (ArrayIndexOutOfBoundsException exc) { j = tmp0; }
  238.                 }
  239.             }
  240.            else
  241.             {
  242.               x = 0; y++;
  243.               if (y>MAX_Y) break;
  244.             }
  245.          }
  246.         else if (x<=MAX_X)
  247.          {
  248.            if (z<0) z = 0;
  249.            currCanvas[y][x++] = z;
  250.            oz = z;
  251.          }
  252.       }
  253.     mvPos = 0;
  254.     shPos = 12;
  255.  
  256.     if (maxMove>0) { txt1.setText(" Level "+gLevel+" ("+maxMove+" step max.)"); }
  257.        else { txt1.setText(" Level "+gLevel); }
  258.     if (firstTime==1) firstTime = 0;
  259.  
  260.     return true;
  261.   }
  262.  
  263.   public boolean action(Event e, Object arg)
  264.   {
  265.     int d = 0;
  266.     int h = -1;
  267.  
  268.     if      (e.target==btnP)  h = 44;
  269.     else if (e.target==btnN)  h = 46;
  270.     else if (e.target==btn1)  h = 27;
  271.     else if (e.target==btnBP) { d = 1; if (dlyBall<1000) dlyBall += 5; }
  272.     else if (e.target==btnBM) { d = 1; if (dlyBall>5) dlyBall -= 5; }
  273.  
  274.     if (h>0) keyDown(e, h);
  275.     else if (d==1) txt1.setText(" Animation delay = "+dlyBall);
  276.  
  277.     return true;
  278.   }
  279.  
  280.   public boolean keyDown(Event e, int c)
  281.   {
  282.     int dx = 0;
  283.     int dy = 0;
  284.     int ch = 0;
  285.     int h  = 0;
  286.     byte z0,z2;
  287.     byte z1 = 0;
  288.     int  x2,y2,y3;
  289.  
  290.     switch (c)
  291.     {
  292.       case e.UP:    dy = -1; break;
  293.       case e.DOWN:  dy =  1; break;
  294.       case 10:      dx =  1; break;
  295.       case 27:
  296.         if (mvPos==0 && firstTime==0) { firstTime = 1; paint(null); return true; }
  297.         h = -100; break;
  298.       case 44: h = -101; break;
  299.       case 46: h =  -99; break;
  300.       case 14: h =  -90; break;
  301.       case 16: h = -110; break;
  302.         
  303.       default:
  304.         //txt1.setText(" ^P < > ^N [Esc] [BS]");
  305.         txt1.setText("Key #"+c+" is not defined");
  306.         break;
  307.     }
  308.  
  309.     if ((dx==0 && dy==0) || firstTime==1)
  310.      {
  311.        if (h<0) { if (dat2Canvas(h+100)) paint(null); }
  312.        return true;
  313.      }
  314.  
  315.     if (dx==0)
  316.      {
  317.        z0 = currCanvas[shPos][9];
  318.        y2 = shPos+dy;
  319.        if (currCanvas[y2][9]==0)
  320.         {
  321.           currCanvas[shPos][9] = 0;
  322.           g.drawImage(xImg[0],X_0+20*9,Y_0+20*shPos,this);
  323.           currCanvas[y2][9] = z0;
  324.           g.drawImage(xImg[z0],X_0+20*9,Y_0+20*y2,this);
  325.           shPos = y2;
  326.         }
  327.        return true;
  328.      }
  329.  
  330.     h = moveBall(shPos,1);
  331.  
  332.     if (h>0) { }
  333.     else { txt1.setText(" Please see instruction "); return true; }
  334.  
  335.     h = GameEnd();
  336.  
  337.     if (h==1)       { txt1.setText(stGameEnd); ch = -98; }
  338.     else if (h==-1) { txt1.setText(stYouLose); ch = -99; }
  339.     else
  340.      {
  341.        mvPos++;
  342.        if (maxMove>0)
  343.         {
  344.           if (mvPos>=maxMove) { txt1.setText(stYouLose); ch = -99; }
  345.           else { txt1.setText(" Have "+(maxMove-mvPos)+" more step."); }
  346.         }
  347.        if (ch==0) { return true; }
  348.      }
  349.  
  350.     z0 = currCanvas[shPos][9];
  351.     h = 0; y2 = shPos; dy = -1;
  352.     while (h<3)
  353.     {
  354.       try { game0.sleep(3*dlyBall); }
  355.       catch (InterruptedException exp) {}
  356.       currCanvas[y2][9] = 0;
  357.       g.drawImage(xImg[0],X_0+20*9,Y_0+20*y2,this);
  358.       y2 += dy;
  359.       if (currCanvas[y2][9]!=0) { dy = -dy; y2 += 2*dy; h++; }
  360.       currCanvas[y2][9] = z0;
  361.       g.drawImage(xImg[z0],X_0+20*9,Y_0+20*y2,this);
  362.       if (h==1 && y2==12)
  363.        {
  364.          animBall( 1,12,9,(byte)0);
  365.          animBall(-1,12,9,(byte)3);
  366.          break;
  367.        }
  368.     }
  369.     if (dat2Canvas(ch+99)) paint(null);
  370.  
  371.     return true;
  372.   }
  373.  
  374.   public void animBall(int dx, int y2, int x2, byte zx)
  375.   {
  376.     byte zc,z1;
  377.  
  378.     if (dx==1) z1 = 3; else { z1 = 6; dx = -1; }
  379.     for (zc=0; zc<=4; zc++)
  380.       {
  381.         g.drawImage(xImg[z1],X_0+20*x2,Y_0+20*y2,this);
  382.         try { game0.sleep(dlyBall); }
  383.         catch (InterruptedException exp) {}
  384.         z1 += dx;
  385.         if (zc==3) z1 = zx;
  386.       }
  387.   }
  388.  
  389.   public int moveBall(int xsh, int xdraw)
  390.   {
  391.     int  ch = 0;
  392.     int  h  = 0;
  393.     int  dx = -1; 
  394.     int  dy = 0;
  395.     int  x2 = 9;
  396.     byte z0, z1,z2;
  397.     int  y2,y3;
  398.  
  399.     z0 = currCanvas[xsh][9];
  400.     y2 = xsh;
  401.     while (x2>=0 && y2<13 && ch==0)
  402.     {
  403.       if (xdraw>0)
  404.        {
  405.          if (xdraw==1)
  406.           {
  407.             if (h>0 && currCanvas[y2][x2]!=0) animBall(1,y2,x2,(byte)0);
  408.             else
  409.              {
  410.                try { game0.sleep(dlyBall); }
  411.                catch (InterruptedException exp) {}
  412.                g.drawImage(xImg[0],X_0+20*x2,Y_0+20*y2,this);
  413.              }
  414.           }
  415.          currCanvas[y2][x2] = 0;
  416.        }
  417.       z1 = currCanvas[y2+dy][x2+dx];
  418.       if (z1==z0 || (z0==BALL_CODE && z1>BALL_CODE))
  419.        { 
  420.          h++; if (z1!=0) z0 = z1;
  421.          if (xdraw==0) return h;
  422.        }
  423.       else if (dy==0 && (z1==1 || z1==2)) { dx = 0; dy = 1; }
  424.       else if (z1>BALL_CODE) { ch = 1; }
  425.       if (z1<=2 || h>0)
  426.        {
  427.          if (xdraw>0 && h>0)
  428.           {
  429.             y3 = y2-1; z2 = (byte)(BALL_CODE+1);
  430.             while (true)
  431.             {
  432.               z2 = currCanvas[y3][x2];
  433.               if (z2<=BALL_CODE) break;
  434.               if (xdraw==1)
  435.                {
  436.                  try { game0.sleep(dlyBall/2); }
  437.                  catch (InterruptedException exp) {}
  438.                  g.drawImage(xImg[0],X_0+20*x2,Y_0+20*y3,this);
  439.                  g.drawImage(xImg[z2],X_0+20*x2,Y_0+20*(y3+1),this);
  440.                }
  441.               currCanvas[y3][x2] = 0;
  442.               currCanvas[y3+1][x2] = z2;
  443.               y3--;
  444.             }
  445.           }
  446.          x2 += dx;
  447.          while (true) { y2 += dy; if (dy==0 || y2>=12 || currCanvas[y2][x2]!=2) break; }
  448.      if (x2>=0 && y2<13)
  449.           {
  450.             if (xdraw>0)
  451.              {
  452.                currCanvas[y2][x2] = z0;
  453.                if (xdraw==1)
  454.                 {
  455.                   if (ch==1) animBall(1,y2,x2,(byte)0);
  456.                   g.drawImage(xImg[z0],X_0+20*x2,Y_0+20*y2,this);
  457.                 }
  458.              }
  459.             if ((x2==0 || (h>0 && currCanvas[y2+1][x2]==0)) && dy==0) { dx = 0; dy = 1; }
  460.             if (ch==1) { z0 = z1; }
  461.           }
  462.        }
  463.     }
  464.  
  465.     if (xdraw>0)
  466.      {
  467.        currCanvas[xsh][9] = z0;
  468.        if (xdraw==1) g.drawImage(xImg[z0],X_0+20*9,Y_0+20*xsh,this);
  469.      }
  470.  
  471.     return h;
  472.   }
  473.  
  474.   public int GameEnd()
  475.   {
  476.     int  x,y,y2;
  477.     byte z0,z1;
  478.     int h = 1;
  479.     int h0[] = new int[MAX_IMG+1];
  480.  
  481.     for (y=0; y<=MAX_IMG; y++) h0[y] = 0;
  482.  
  483.     for (y=0; y<=MAX_Y; y++)
  484.     {
  485.       for (x=0; x<=MAX_X; x++) { h0[currCanvas[y][x]]++; }
  486.     }
  487.  
  488.     y = BALL_CODE+1;
  489.     while (y<=MAX_IMG)
  490.     {
  491.       x = h0[y];
  492.       if (x>1) 
  493.        { 
  494.          h = -1;
  495.          y2 = 1;
  496.          z0 = currCanvas[shPos][9];
  497.          while (y2<=13 && h<0)
  498.          {
  499.            z1 = currCanvas[y2][9];
  500.            if (z1!=1) 
  501.             {
  502.               currCanvas[y2][9] = z0;
  503.               if (moveBall(y2,0)>0) h = 0;
  504.               currCanvas[y2][9] = z1;
  505.             }
  506.            y2++;
  507.          }
  508.          y = MAX_IMG+1;
  509.        }
  510.       y++;
  511.     }
  512.  
  513.     return h;
  514.   }
  515.  
  516.   private Image extractImage(int[] xyCoord)
  517.   {
  518.     Image newImage;
  519.     ImageFilter filter;
  520.     ImageProducer producer;
  521.  
  522.     filter = new CropImageFilter(xyCoord[0],xyCoord[1],xyCoord[2],xyCoord[3]);
  523.     producer = new FilteredImageSource(mainImg.getSource(), filter);
  524.     newImage = createImage(producer);
  525.     return newImage;
  526.    }
  527.  
  528.   public void paint(Graphics g)
  529.   {
  530.     byte z;
  531.     int  x2,y2,xe,x,y;
  532.  
  533.     if (g==null) g = getGraphics();
  534.     y2 = Y_0;
  535.     xe = X_0+20*(MAX_X+1);
  536.     for (y=0; y<=MAX_Y; y++)
  537.       {
  538.         g.drawImage(xImg[1],X_0-20,y2,this);
  539.         g.drawImage(xImg[1],xe,y2,this);
  540.         x2 = X_0;
  541.         for (x=0; x<=MAX_X; x++)
  542.           {
  543.             z = currCanvas[y][x];
  544.             if (firstTime==1 || z>MAX_IMG) z = 0;
  545.             g.drawImage(xImg[z],x2,y2,this);
  546.             x2 += 20;
  547.           }
  548.         y2 += 20;
  549.       }
  550.     if (firstTime==1) doFirstTime();
  551.   }
  552.  
  553.   public void doFirstTime()
  554.   {
  555.     int y,x;
  556.     int c;
  557.  
  558.     y = 20;
  559.     x = 5+X_0;
  560.     g.setColor(new Color(255,255,204));
  561.     String str = "* Shooter v1.30 *";
  562.     g.drawString(str,x,y);
  563.     txt1.setText(str);
  564.     y += 20; g.drawString("(Java Applet version) 2000 by Riza PN",x,y);
  565.     y += 20; g.drawString("The goal is destroy all duplicated bombs",x,y);
  566.     y += 15; g.drawString("by shooting it with the key bomb (in the",x,y);
  567.     y += 15; g.drawString("bottom-right corner). Bomb can be",x,y);
  568.     y += 15; g.drawString("destroyed if the key has same color or it",x,y);
  569.     y += 15; g.drawString("is star. Use up and down arrow to move",x,y);
  570.     y += 15; g.drawString("the key, or press ENTER to run it.",x,y);
  571.     y += 15; g.drawString("It will run to the left, but if there is a wall,",x,y);
  572.     y += 15; g.drawString("it will go down. Game will be finished if it",x,y);
  573.     y += 15; g.drawString("is impossible to use a key (YOU LOSE)",x,y);
  574.     y += 15; g.drawString("or if no more duplicated ones (WIN)",x,y);
  575.     y += 20; g.drawString("Hope you enjoy it...",x,y);
  576.     y += 20; g.drawString("Salam",x,y);
  577.     y += 15; g.drawString("Riza Purwo Nugroho, Jakarta-Indonesia",x,y);
  578.   }
  579.  
  580. }
  581.