//***************************** // Harry Goes Home.java: Applet //***************************** import java.applet.*; import java.awt.*; import java.lang.*; public class harry extends Applet { public Image Harry,Mine,Key,House,Grass,Fence,Footstep; public int harryx,harryy,mines,status; public boolean gotkey; public int land[][] = new int[30][20]; //0=grass, 1=footstep, 2=mine, 3=key, 4=house, 5=fence public void init() { int x,y; status = 0; gotkey = false; resize(600,500); Harry = getImage(getCodeBase(),"harry.gif"); Mine = getImage(getCodeBase(),"mine.gif"); Key = getImage(getCodeBase(),"key.gif"); House = getImage(getCodeBase(),"house.gif"); Grass = getImage(getCodeBase(),"grass.gif"); Fence = getImage(getCodeBase(),"fence.gif"); Footstep = getImage(getCodeBase(),"footstep.gif"); for(y=0 ; y<20 ; y++) { for(x=0 ; x<30 ; x++) { land[x][y] = 0; if (Math.random() < 0.1) land[x][y] = 2; if (x == 0 || x == 29 || y == 0 || y == 19) land[x][y] =5; } } land[(int)(Math.random()*20)+5][10] = 3; //key land[(int)(Math.random()*20)+5][1] = 4; //house harryx = (int)(Math.random()*20)+5; harryy = 18; land[harryx][harryy] = 0; } public void update(Graphics g) { g.setColor(getBackground()); g.fillRect(0,400,600,100); g.setColor(getForeground()); paint(g); } public void paint(Graphics g) { int x,y; for(y=0 ; y<20 ; y++) { for(x=0 ; x<30 ; x++) { if (land[x][y] == 0 || land[x][y] == 2) g.drawImage(Grass,x*20,y*20,this); if (land[x][y] == 2 && status > 0) g.drawImage(Mine,x*20,y*20,this); else if (land[x][y] == 1) g.drawImage(Footstep,x*20,y*20,this); else if (land[x][y] == 3) g.drawImage(Key,x*20,y*20,this); else if (land[x][y] == 4) g.drawImage(House,x*20,y*20,this); else if (land[x][y] == 5) g.drawImage(Fence,x*20,y*20,this); } } g.drawImage(Harry,harryx*20,harryy*20,this); if (status == 0 && mines > 0) { g.drawString("There are " + mines + " mines next to you.",10,430); } else if (status == 1) { g.drawString("You have stood on a mine!!",10,450); } if (land[harryx][harryy] == 3) { g.drawString("You have got the key.",10,450); } if (land[harryx][harryy] == 4) { if (gotkey == true) { g.drawString("You have made it with the key!!!",10,450); status = 2; } else { g.drawString("You have not got the key!",10,450); } } } public void start() { repaint(); } public boolean mouseDown(Event evt, int x, int y) { if (land[harryx][harryy] != 4) land[harryx][harryy] = 1; if (x < 200 && harryx > 1) harryx--; if (x > 400 && harryx < 28) harryx++; if (y < 150 && harryy > 1) harryy--; if (y > 350 && harryy < 18) harryy++; if(land[harryx][harryy] == 3) gotkey = true; if(land[harryx][harryy] == 2) status = 1; CalcMines(); repaint(); return true; } private void CalcMines() { int u,v; mines = 0; for(u=harryy-1 ; u