说能将这里面的代码解释一遍
谢谢你们了!!!!!
就是下面的代码
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class BoxMan extends Applet implements Runnable, KeyListener, ActionListener {
private boolean isComplete, keyEvent, missionAccomplished, flash;
private Image[] showImages;
private Image currentImage, human1, human2;
private Label prompt = new Label("");//提示信息
private TextField input = new TextField(7);
private int totalImages = 14,
imageLength = 32,
currentLevel = 2,
mapWidth = 15,
currentX, currentY, keyCode,
manX,manY,tmp1ManX,tmp1ManY;
private BasicUnit[][] block = new BasicUnit[mapWidth][mapWidth];
private Thread thread;
private int[][] map,tmp1Map; //存储地图
public void init() {
isComplete = false; keyEvent = false; missionAccomplished = false; flash = false;
keyCode = 0;
prompt.setText(" BoxMan Author:mhb Level:");
add(prompt);
add(input);
LevelMap basicMap = new LevelMap(0);
map = basicMap.getLevelMap();
manX = basicMap.getManX();
manY = basicMap.getManY();
boolean takeMap = false;
LevelMap tmpMap = new LevelMap(currentLevel);//当前级别的地图
tmp1Map = tmpMap.getLevelMap();
tmp1ManX = tmpMap.getManX();
tmp1ManY = tmpMap.getManY();
manX = tmp1ManX;
manY = tmp1ManY;
for(int i=0; i<mapWidth; i++)
for(int j=0; j<mapWidth; j++)
map[i][j] = tmp1Map[i][j];
takeMap = true;
if(!takeMap){}
showImages = new Image[totalImages];//14个图像
for(int i=0; i<totalImages; i++) {
showImages[i] = getImage(getDocumentBase(),"bmp\\Bmp"+i+".gif");
}
human1 = showImages[10];
human2 = showImages[11];
currentX = manX; currentY = manY;
input.setText(""+currentLevel);
this.addKeyListener(this);
input.addActionListener(this);
}
public void start() {
for(int i=0; i<mapWidth; i++)
for(int j=0; j<mapWidth; j++) {
currentImage = showImages[map[j][i]];
block[i][j] = new BasicUnit(currentImage,i*imageLength,j*imageLength);
}
thread = new Thread(this, "human");
thread.start();
}
public void paint(Graphics g) {
for(int i=0; i<mapWidth; i++)
for(int j=0; j<mapWidth; j++)
block[i][j].drawSelf(g);
//g.drawImage(currentImage,x,y,this);
if(missionAccomplished&(!flash)) {
g.drawImage(human1,currentX*32,currentY*32,this);
victory();
}
}
public void run() {
int count = 0;
Graphics g;
g = getGraphics();
while((!isComplete) && (!flash)) {
isComplete = true;
if(count<=5) {
g.drawImage(human1,currentX*32,currentY*32,this);
for(int i=0;i<mapWidth;i++)
for(int j=0;j<mapWidth;j++) {
if(map[j][i] == 4)
g.drawImage(showImages[3],i*32,j*32,this);
if(map[j][i] == 3)
isComplete = false;
}
} else {
g.drawImage(human2,currentX*32,currentY*32,this);
for(int i=0;i<mapWidth;i++)
for(int j=0;j<mapWidth;j++) {
if(map[j][i] ==4)
g.drawImage(showImages[4],i*32,j*32,this);
if(map[j][i] == 3)
isComplete = false;
}
if(count == 10)
count = 0;
}
try{Thread.sleep(100);}catch(InterruptedException e){}
count++;
if(keyEvent) {
switch(keyCode) {
case 37 : moveLeft(); break;
case 38 : moveUp(); break;
case 39 : moveRight(); break;
case 40 : moveDown(); break;
default :
}
keyCode = 0; keyEvent = false;
}
}
missionAccomplished = true;
if(!flash)
victory();
}
public void victory() {
Graphics g;
g = getGraphics();
Color oldColor;
Font oldFont;
g.setColor(new Color(192,192,192));
g.fillRect(120,150,250,100);
g.setColor(new Color(250,250,250));
g.drawLine(120,150,370,150);
g.drawLine(120,150,120,250);
g.setColor(new Color(0,0,0));
g.drawLine(370,250,120,250);
g.drawLine(370,250,370,150);
g.setColor(new Color(255,0,0));
g.setFont(new Font("TimesRoman",Font.BOLD,12));
g.drawString("Congratulate! Press 'Enter' to nextlevel.",170,200);
}
public void reLoad() {
destroy();
currentLevel++;
init();
start();
repaint();
}
public void keyPressed(KeyEvent e) {
keyCode = e.getKeyCode();
keyEvent = true;
if(missionAccomplished & keyCode == 10) reLoad();
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent e) {
currentLevel = Integer.parseInt(input.getText());
currentLevel--;
flash = true;
while(missionAccomplished)
try{Thread.sleep(100);}catch(InterruptedException ioe){}
try{Thread.sleep(100);}catch(InterruptedException ioe){}
reLoad();
}
public void paintBlock(int x, int y) {
Graphics g;
g = getGraphics();
block[x][y].currentImage = showImages[map[y][x]];
block[x][y].drawSelf(g);
}
public void moveLeft() {
if(map[currentY][currentX-1] != 1) {
if(map[currentY][currentX-1] == 2 | map[currentY][currentX-1] == 5) {
paintBlock(currentX,currentY);
currentX = currentX -1;
human1 = showImages[6];
human2 = showImages[7];
} else {
if((map[currentY][currentX-1] == 3 | map[currentY][currentX-1] == 4) & (map[currentY][currentX-2] == 2 | map[currentY][currentX-2] == 5)) {
human1 = showImages[6];
human2 = showImages[7];
if(map[currentY][currentX-1] == 3) {
if(map[currentY][currentX-2] == 2) map[currentY][currentX-2] = 3;
else map[currentY][currentX-2] = 4;
map[currentY][currentX-1] = 2;
paintBlock(currentX,currentY);
paintBlock(currentX-2,currentY);
currentX = currentX -1;
} else {
if(map[currentY][currentX-2] == 2)
map[currentY][currentX-2] = 3;
else
map[currentY][currentX-2] = 4;
map[currentY][currentX-1] = 5;
paintBlock(currentX,currentY);
paintBlock(currentX-2,currentY);
currentX = currentX -1;
}
}
}
}
}
public void moveUp()
{
if(map[currentY-1][currentX] != 1)
{
if(map[currentY-1][currentX] == 2 | map[currentY-1][currentX] == 5)
{
paintBlock(currentX,currentY);
currentY = currentY -1;
human1 = showImages[8];
human2 = showImages[9];
}
else
{
if((map[currentY-1][currentX] == 3 | map[currentY-1][currentX] == 4) & (map[currentY-2][currentX] == 2 | map[currentY-2][currentX] == 5))
{
human1 = showImages[8];
human2 = showImages[9];
if(map[currentY-1][currentX] == 3)
{
if(map[currentY-2][currentX] == 2)
map[currentY-2][currentX] = 3;
else
map[currentY-2][currentX] = 4;
map[currentY-1][currentX] = 2;
paintBlock(currentX,currentY);
paintBlock(currentX,currentY-2);
currentY = currentY -1;
}
else
{
if(map[currentY-2][currentX] == 2)
map[currentY-2][currentX] = 3;
else
map[currentY-2][currentX] = 4;
map[currentY-1][currentX] = 5;
paintBlock(currentX,currentY);
paintBlock(currentX,currentY-2);
currentY = currentY -1;
}
}
}
}
}
public void moveRight()
{
if(map[currentY][currentX+1] != 1)
{
if(map[currentY][currentX+1] == 2 | map[currentY][currentX+1] == 5)
{
paintBlock(currentX,currentY);
currentX = currentX +1;
human1 = showImages[12];
human2 = showImages[13];
}
else
{
if((map[currentY][currentX+1] == 3 | map[currentY][currentX+1] == 4) & (map[currentY][currentX+2] == 2 | map[currentY][currentX+2] == 5))
{
human1 = showImages[12];
human2 = showImages[13];
if(map[currentY][currentX+1] == 3)
{
if(map[currentY][currentX+2] == 2)
map[currentY][currentX+2] = 3;
else
map[currentY][currentX+2] = 4;
map[currentY][currentX+1] = 2;
paintBlock(currentX,currentY);
paintBlock(currentX+2,currentY);
currentX = currentX +1;
}
else
{
if(map[currentY][currentX+2] == 2)
map[currentY][currentX+2] = 3;
else
map[currentY][currentX+2] = 4;
map[currentY][currentX+1] = 5;
paintBlock(currentX,currentY);
paintBlock(currentX+2,currentY);
currentX = currentX +1;
}
}
}
}
}
public void moveDown() {
//下一格不是墙
if(map[currentY+1][currentX] != 1) {
//下一格是空地或目的地
if(map[currentY+1][currentX] == 2 || map[currentY+1][currentX] == 5) {
paintBlock(currentX,currentY);
currentY = currentY +1;
human1 = showImages[10];
human2 = showImages[11];
} else {
//下一格是箱子,而且再下一格是空地或目的地
if((map[currentY+1][currentX] == 3 || map[currentY+1][currentX] == 4) && (map[currentY+2][currentX] == 2 || map[currentY+2][currentX] == 5)) {
human1 = showImages[10];
human2 = showImages[11];
if(map[currentY+1][currentX] == 3) {
if(map[currentY+2][currentX] == 2) map[currentY+2][currentX] = 3;
else map[currentY+2][currentX] = 4;
map[currentY+1][currentX] = 2;
paintBlock(currentX,currentY);
paintBlock(currentX,currentY+2);
currentY = currentY +1;
} else {
if(map[currentY+2][currentX] == 2) map[currentY+2][currentX] = 3;
else map[currentY+2][currentX] = 4;
map[currentY+1][currentX] = 5;
paintBlock(currentX,currentY);
paintBlock(currentX,currentY+2);
currentY = currentY +1;
}
}
}
}
}
}
//****************************************************************************
class BasicUnit extends Applet {
Image currentImage;
int x, y;
BasicUnit(Image tmpImage, int tmpX, int tmpY) {
currentImage = tmpImage;
x = tmpX;
y = tmpY;
}
void drawSelf(Graphics g) {
g.drawImage(currentImage,x,y,this);
}
}