home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-08-10 | 16.7 KB | 581 lines |
- /*********************************************************
- Applet : Shooter.java - My Third Java Applet Game
- Version : 1.30
- Author : Riza Purwo Nugroho <rizapn@ratelindo.co.id>
- Date : August 5, 2000
- Update : August 8, 2000
- ----------------------------------------------------------
- This is my third Java Applet game.
- Hope anybody who read this will enjoy to learn more
- about Java Applet programming.
- Sorry if some code is not too clear to understand.
- **********************************************************/
-
- import java.applet.*;
- import java.awt.*;
- import java.awt.image.*;
- import java.net.*;
- import java.util.*;
-
- public class Shooter extends Applet implements Runnable
- {
- static int MAX_X=9, MAX_Y=15, MAX_LVL=100, MAX_IMG=12;
- static int X_0 = 30, Y_0 = 5, BALL_CODE = 7;
-
- Thread game0 = null;
-
- private Image[] xImg = new Image[MAX_IMG+1];
- private int gLevel, mvPos, maxMove, shPos, dlyBall, num;
- private byte[][] currCanvas = new byte[MAX_Y+1][MAX_X+1];
- private byte firstTime;
-
- private Button btn1 = new Button("X");
- private Button btnN = new Button(">");
- private Button btnP = new Button("<");
- private Button btnBP = new Button("+");
- private Button btnBM = new Button("-");
-
- private static String stGameEnd = " [GAME END]";
- private static String stYouLose = " [YOU LOSE]";
-
- private static String ascCode = "-ABCDEFGHIJKLMNOP";
- private static String mCode = " #= *.oO@+,123456789abcdefghij";
- private static int xCoord[][] =
- {{ 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},
- {100,20,20,20}, { 0,20,20,20}, { 20,20,20,20}, { 40,20,20,20}, { 60,20,20,20}, { 80,20,20,20}};
-
- private static String biDat[] = {
- "#9,#3,#2,#1,#,,,,,O+2,.O2,o3,+.2 4o,#9,",
- "#9,#3,#2,#1,#,,,,,O.2,+O2,o.2,.O2 4*,#9,",
- "#9,#3,#2,#1,#,,,,,.3,o2.,.3,oO+. 4*,#9,",
- "#9,#3,#2,#1,#,,,,,O.O.,O.O.,.O.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,40,40,40,40,40,40,40,
- 40,40,40,40,40,40,40,40,40,40,
- 40,40,40,40,40,40,40,40,40,40,
- };
-
- private Image mainImg;
- private Graphics g;
- private TextField txt1;
-
- public void init()
- {
- String param;
- int i;
-
- if (g==null) g = getGraphics();
-
- URL url = getCodeBase();
-
- String str = "";
- int b = 0;
-
- MediaTracker tracker = new MediaTracker(this);
- mainImg = getImage(url, "Balls.gif");
- tracker.addImage(mainImg,0);
-
- for (i=0; i<=MAX_IMG; i++)
- {
- xImg[i] = extractImage(xCoord[i]);
- tracker.addImage(xImg[i], i+1);
- }
-
- try { tracker.waitForAll(); }
- catch (InterruptedException e) {}
-
- setLayout(new BorderLayout());
-
- txt1 = new TextField(17);
- txt1.setForeground(new Color(255,255,204));
- txt1.setBackground(new Color(102,51,51));
- txt1.setEditable(false);
-
- Panel p = new Panel();
-
- p.add(txt1);
- p.add(btnBM);
- p.add(btnBP);
- p.add(btnP);
- p.add(btnN);
- p.add(btn1);
-
- add("South", p);
-
- gLevel = 0;
- dat2Canvas(0);
- firstTime = 1;
- dlyBall = 5;
- }
-
- public void start()
- {
- if (game0==null)
- {
- game0 = new Thread(this);
- game0.start();
- }
- }
-
- public void stop()
- {
- if (game0 != null)
- {
- game0.stop();
- game0 = null;
- }
- }
-
- public void run()
- {
- }
-
- public boolean dat2Canvas(int dlvl)
- {
- int i,x,y,tmp0;
- byte z = 0, oz = 0;
- String param;
-
- try
- {
- gLevel += dlvl;
- if (gLevel<0) gLevel = 0;
-
- try { i = Integer.parseInt(getParameter("maxmove"+gLevel)); }
- catch (NumberFormatException e) { i = 0; }
-
- param = getParameter("data"+gLevel);
- if (i>0) maxMove = i;
-
- if (param==null || i<=0)
- {
- param = biDat[gLevel];
- maxMove = aMove[gLevel];
- }
- }
- catch (ArrayIndexOutOfBoundsException exc)
- {
- gLevel -= dlvl;
- param = biDat[gLevel];
- maxMove = aMove[gLevel];
- }
-
- for (y=0; y<=MAX_Y; y++)
- { for (x=0; x<=MAX_X; x++)
- { currCanvas[y][x] = 0; }
- }
-
- x = y = 0;
- for (i=0; i<param.length(); i++)
- {
- z = (byte)(mCode.indexOf(param.charAt(i)));
- if (z>MAX_IMG)
- {
- if ((tmp0 = (z-MAX_IMG-1))>0)
- {
- for (int j=0; j<tmp0; j++)
- {
- try { currCanvas[y][x++] = oz; }
- catch (ArrayIndexOutOfBoundsException exc) { j = tmp0; }
- }
- }
- else
- {
- x = 0; y++;
- if (y>MAX_Y) break;
- }
- }
- else if (x<=MAX_X)
- {
- if (z<0) z = 0;
- currCanvas[y][x++] = z;
- oz = z;
- }
- }
- mvPos = 0;
- shPos = 12;
-
- if (maxMove>0) { txt1.setText(" Level "+gLevel+" ("+maxMove+" step max.)"); }
- else { txt1.setText(" Level "+gLevel); }
- if (firstTime==1) firstTime = 0;
-
- return true;
- }
-
- public boolean action(Event e, Object arg)
- {
- int d = 0;
- int h = -1;
-
- if (e.target==btnP) h = 44;
- else if (e.target==btnN) h = 46;
- else if (e.target==btn1) h = 27;
- else if (e.target==btnBP) { d = 1; if (dlyBall<1000) dlyBall += 5; }
- else if (e.target==btnBM) { d = 1; if (dlyBall>5) dlyBall -= 5; }
-
- if (h>0) keyDown(e, h);
- else if (d==1) txt1.setText(" Animation delay = "+dlyBall);
-
- return true;
- }
-
- public boolean keyDown(Event e, int c)
- {
- int dx = 0;
- int dy = 0;
- int ch = 0;
- int h = 0;
- byte z0,z2;
- byte z1 = 0;
- int x2,y2,y3;
-
- switch (c)
- {
- case e.UP: dy = -1; break;
- case e.DOWN: dy = 1; break;
- case 10: dx = 1; break;
- case 27:
- if (mvPos==0 && firstTime==0) { firstTime = 1; paint(null); return true; }
- h = -100; break;
- case 44: h = -101; break;
- case 46: h = -99; break;
- case 14: h = -90; break;
- case 16: h = -110; break;
-
- default:
- //txt1.setText(" ^P < > ^N [Esc] [BS]");
- txt1.setText("Key #"+c+" is not defined");
- break;
- }
-
- if ((dx==0 && dy==0) || firstTime==1)
- {
- if (h<0) { if (dat2Canvas(h+100)) paint(null); }
- return true;
- }
-
- if (dx==0)
- {
- z0 = currCanvas[shPos][9];
- y2 = shPos+dy;
- if (currCanvas[y2][9]==0)
- {
- currCanvas[shPos][9] = 0;
- g.drawImage(xImg[0],X_0+20*9,Y_0+20*shPos,this);
- currCanvas[y2][9] = z0;
- g.drawImage(xImg[z0],X_0+20*9,Y_0+20*y2,this);
- shPos = y2;
- }
- return true;
- }
-
- h = moveBall(shPos,1);
-
- if (h>0) { }
- else { txt1.setText(" Please see instruction "); return true; }
-
- h = GameEnd();
-
- if (h==1) { txt1.setText(stGameEnd); ch = -98; }
- else if (h==-1) { txt1.setText(stYouLose); ch = -99; }
- else
- {
- mvPos++;
- if (maxMove>0)
- {
- if (mvPos>=maxMove) { txt1.setText(stYouLose); ch = -99; }
- else { txt1.setText(" Have "+(maxMove-mvPos)+" more step."); }
- }
- if (ch==0) { return true; }
- }
-
- z0 = currCanvas[shPos][9];
- h = 0; y2 = shPos; dy = -1;
- while (h<3)
- {
- try { game0.sleep(3*dlyBall); }
- catch (InterruptedException exp) {}
- currCanvas[y2][9] = 0;
- g.drawImage(xImg[0],X_0+20*9,Y_0+20*y2,this);
- y2 += dy;
- if (currCanvas[y2][9]!=0) { dy = -dy; y2 += 2*dy; h++; }
- currCanvas[y2][9] = z0;
- g.drawImage(xImg[z0],X_0+20*9,Y_0+20*y2,this);
- if (h==1 && y2==12)
- {
- animBall( 1,12,9,(byte)0);
- animBall(-1,12,9,(byte)3);
- break;
- }
- }
- if (dat2Canvas(ch+99)) paint(null);
-
- return true;
- }
-
- public void animBall(int dx, int y2, int x2, byte zx)
- {
- byte zc,z1;
-
- if (dx==1) z1 = 3; else { z1 = 6; dx = -1; }
- for (zc=0; zc<=4; zc++)
- {
- g.drawImage(xImg[z1],X_0+20*x2,Y_0+20*y2,this);
- try { game0.sleep(dlyBall); }
- catch (InterruptedException exp) {}
- z1 += dx;
- if (zc==3) z1 = zx;
- }
- }
-
- public int moveBall(int xsh, int xdraw)
- {
- int ch = 0;
- int h = 0;
- int dx = -1;
- int dy = 0;
- int x2 = 9;
- byte z0, z1,z2;
- int y2,y3;
-
- z0 = currCanvas[xsh][9];
- y2 = xsh;
- while (x2>=0 && y2<13 && ch==0)
- {
- if (xdraw>0)
- {
- if (xdraw==1)
- {
- if (h>0 && currCanvas[y2][x2]!=0) animBall(1,y2,x2,(byte)0);
- else
- {
- try { game0.sleep(dlyBall); }
- catch (InterruptedException exp) {}
- g.drawImage(xImg[0],X_0+20*x2,Y_0+20*y2,this);
- }
- }
- currCanvas[y2][x2] = 0;
- }
- z1 = currCanvas[y2+dy][x2+dx];
- if (z1==z0 || (z0==BALL_CODE && z1>BALL_CODE))
- {
- h++; if (z1!=0) z0 = z1;
- if (xdraw==0) return h;
- }
- else if (dy==0 && (z1==1 || z1==2)) { dx = 0; dy = 1; }
- else if (z1>BALL_CODE) { ch = 1; }
- if (z1<=2 || h>0)
- {
- if (xdraw>0 && h>0)
- {
- y3 = y2-1; z2 = (byte)(BALL_CODE+1);
- while (true)
- {
- z2 = currCanvas[y3][x2];
- if (z2<=BALL_CODE) break;
- if (xdraw==1)
- {
- try { game0.sleep(dlyBall/2); }
- catch (InterruptedException exp) {}
- g.drawImage(xImg[0],X_0+20*x2,Y_0+20*y3,this);
- g.drawImage(xImg[z2],X_0+20*x2,Y_0+20*(y3+1),this);
- }
- currCanvas[y3][x2] = 0;
- currCanvas[y3+1][x2] = z2;
- y3--;
- }
- }
- x2 += dx;
- while (true) { y2 += dy; if (dy==0 || y2>=12 || currCanvas[y2][x2]!=2) break; }
- if (x2>=0 && y2<13)
- {
- if (xdraw>0)
- {
- currCanvas[y2][x2] = z0;
- if (xdraw==1)
- {
- if (ch==1) animBall(1,y2,x2,(byte)0);
- g.drawImage(xImg[z0],X_0+20*x2,Y_0+20*y2,this);
- }
- }
- if ((x2==0 || (h>0 && currCanvas[y2+1][x2]==0)) && dy==0) { dx = 0; dy = 1; }
- if (ch==1) { z0 = z1; }
- }
- }
- }
-
- if (xdraw>0)
- {
- currCanvas[xsh][9] = z0;
- if (xdraw==1) g.drawImage(xImg[z0],X_0+20*9,Y_0+20*xsh,this);
- }
-
- return h;
- }
-
- public int GameEnd()
- {
- int x,y,y2;
- byte z0,z1;
- int h = 1;
- int h0[] = new int[MAX_IMG+1];
-
- for (y=0; y<=MAX_IMG; y++) h0[y] = 0;
-
- for (y=0; y<=MAX_Y; y++)
- {
- for (x=0; x<=MAX_X; x++) { h0[currCanvas[y][x]]++; }
- }
-
- y = BALL_CODE+1;
- while (y<=MAX_IMG)
- {
- x = h0[y];
- if (x>1)
- {
- h = -1;
- y2 = 1;
- z0 = currCanvas[shPos][9];
- while (y2<=13 && h<0)
- {
- z1 = currCanvas[y2][9];
- if (z1!=1)
- {
- currCanvas[y2][9] = z0;
- if (moveBall(y2,0)>0) h = 0;
- currCanvas[y2][9] = z1;
- }
- y2++;
- }
- y = MAX_IMG+1;
- }
- y++;
- }
-
- return h;
- }
-
- private Image extractImage(int[] xyCoord)
- {
- Image newImage;
- ImageFilter filter;
- ImageProducer producer;
-
- filter = new CropImageFilter(xyCoord[0],xyCoord[1],xyCoord[2],xyCoord[3]);
- producer = new FilteredImageSource(mainImg.getSource(), filter);
- newImage = createImage(producer);
- return newImage;
- }
-
- public void paint(Graphics g)
- {
- byte z;
- int x2,y2,xe,x,y;
-
- if (g==null) g = getGraphics();
- y2 = Y_0;
- xe = X_0+20*(MAX_X+1);
- for (y=0; y<=MAX_Y; y++)
- {
- g.drawImage(xImg[1],X_0-20,y2,this);
- g.drawImage(xImg[1],xe,y2,this);
- x2 = X_0;
- for (x=0; x<=MAX_X; x++)
- {
- z = currCanvas[y][x];
- if (firstTime==1 || z>MAX_IMG) z = 0;
- g.drawImage(xImg[z],x2,y2,this);
- x2 += 20;
- }
- y2 += 20;
- }
- if (firstTime==1) doFirstTime();
- }
-
- public void doFirstTime()
- {
- int y,x;
- int c;
-
- y = 20;
- x = 5+X_0;
- g.setColor(new Color(255,255,204));
- String str = "* Shooter v1.30 *";
- g.drawString(str,x,y);
- txt1.setText(str);
- y += 20; g.drawString("(Java Applet version) 2000 by Riza PN",x,y);
- y += 20; g.drawString("The goal is destroy all duplicated bombs",x,y);
- y += 15; g.drawString("by shooting it with the key bomb (in the",x,y);
- y += 15; g.drawString("bottom-right corner). Bomb can be",x,y);
- y += 15; g.drawString("destroyed if the key has same color or it",x,y);
- y += 15; g.drawString("is star. Use up and down arrow to move",x,y);
- y += 15; g.drawString("the key, or press ENTER to run it.",x,y);
- y += 15; g.drawString("It will run to the left, but if there is a wall,",x,y);
- y += 15; g.drawString("it will go down. Game will be finished if it",x,y);
- y += 15; g.drawString("is impossible to use a key (YOU LOSE)",x,y);
- y += 15; g.drawString("or if no more duplicated ones (WIN)",x,y);
- y += 20; g.drawString("Hope you enjoy it...",x,y);
- y += 20; g.drawString("Salam",x,y);
- y += 15; g.drawString("Riza Purwo Nugroho, Jakarta-Indonesia",x,y);
- }
-
- }
-