| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 559 人关注过本帖
标题:[求助]帮个忙!好吗?????
只看楼主 加入收藏
lp213
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-17
收藏
 问题点数:0 回复次数:6 
[求助]帮个忙!好吗?????

说能将这里面的代码解释一遍
谢谢你们了!!!!!

2007-01-23 10:31
曾小
Rank: 1
等 级:新手上路
威 望:1
帖 子:239
专家分:0
注 册:2006-9-27
收藏
得分:0 
代码在哪???

2007-01-23 18:03
lp213
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-17
收藏
得分:0 

就是下面的代码

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);
}
}

2007-01-24 10:03
lp213
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-17
收藏
得分:0 
上面的是推箱子的代码
麻烦你帮我解释一下
2007-01-24 10:04
白展堂
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-1-2
收藏
得分:0 
JAVA代码怎么跑到汇编论坛里来了???

2007-01-24 10:17
brucewood
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-1-17
收藏
得分:0 
懒得去想







太难了....
2007-03-15 19:20
爱以走远
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:7542
专家分:21
注 册:2007-3-16
收藏
得分:0 
   

   好好活着,因为我们会死很久!!!
2007-03-17 22:43
快速回复:[求助]帮个忙!好吗?????
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032643 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved