package com.test;
import java.io.IOException;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.GameCanvas;
/**
* @author Administrator
*/
public class MainApp extends MIDlet
{
private Display display = null;
public MainApp()
{
display = Display.getDisplay(this);
}
public void startApp()
{
Game mc = new Game();
display.setCurrent(mc);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}
class Game extends GameCanvas implements Runnable
{
Graphics g;
Sprite player;
Image img;
private Image playerImage = null;
TiledLayer t1_map;
LayerManager lm;
int map_width;
int map_height;
int screen_width;
int screen_height;
int viewX;
int viewY;
int oldPosX ;
int oldPosY ;
public Game(){
super(true);
setFullScreenMode(true);
screen_width = getWidth();
screen_height = getHeight();
initMap();
new Thread(this).start();
}
//初始化地图
public void initMap()
{
try{
img =Image.createImage("/res/map.png");
playerImage = Image.createImage("/res/hero.png");
}catch (IOException e) {
e.printStackTrace();
}
map_width = backMap[0].length*16;
map_height = backMap.length*16;
lm = new LayerManager();
t1_map = new TiledLayer(backMap[0].length, backMap.length, img, 16, 16);
player = new Sprite(playerImage, playerImage.getWidth()/4 , playerImage.getHeight()/4 );
player.setPosition(screen_width/2 - player.getWidth()/2, screen_height/2 - player.getHeight()/2);
for(int row =0;row <backMap.length; row++)
for(int col = 0; col<backMap[row].length;col++)
{
t1_map .setCell(col,row,backMap[row][col]);
}
//两层,注意次序
lm.append(player);
lm.append(t1_map);
}
public void render(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, screen_width, screen_height);
lm.paint(g,0,0);
}
//得到下一帧数。注意人物图片是要符合。参看hero.png格式
private int get_nextframe(int keyStatus)
{
int num = 0;
num = player.getFrame();
if ((keyStatus & UP_PRESSED) != 0)
{
if (num >= 12 && num <= 15)
{
num++;
if (num > 15)
num = 12;
} else
num = 12;
} else if ((keyStatus & DOWN_PRESSED) != 0)
{
if (num >= 0 && num <= 3)
{
num++;
if (num > 3)
num = 0;
} else
num = 0;
} else if ((keyStatus & LEFT_PRESSED) != 0)
{
if (num >= 4 && num <= 7)
{
num++;
if (num > 7)
num = 4;
} else
num = 4;
} else if ((keyStatus & RIGHT_PRESSED) != 0)
{
if (num >= 8 && num <= 11)
{
num++;
if (num > 11)
num = 8;
} else
num = 8;
}
return num;
}
public void input()
{
int num = 0;
int posX = player.getX();
int posY = player.getY();
int playerWidth = player.getWidth();
int playerHeight = player.getHeight();
int keyStates = getKeyStates();
if ((keyStates&LEFT_PRESSED)!= 0)
{
posX -= 4;
if((posX+playerWidth/2<=map_width-screen_width/2)&&(posX+playerWidth/2>=screen_width/2))
viewX -=4;
}
else if ((keyStates&RIGHT_PRESSED)!= 0)
{
posX +=4;
if((posX+playerWidth/2<=map_width-screen_width/2)&&(posX+playerWidth/2>=screen_width/2))
viewX+=4;
}
else if ((keyStates&UP_PRESSED)!= 0)
{
posY -=4;
if((posY+playerHeight/2<=map_height-screen_height/2)&&(posY+playerHeight/2>=screen_height/2))
viewY-=4;
}
else if ((keyStates&DOWN_PRESSED)!= 0)
{
posY += 4;
if((posY+playerHeight/2<=map_height-screen_height/2)&&(posY+playerHeight/2>=screen_height/2))
viewY+=4;
}
if (posX+playerWidth>=map_width )
posX = oldPosX;
if (posY+playerHeight>=map_height )
posY= oldPosY;
if(viewX <=0)
viewX =0;
if(viewY <=0)
viewY =0;
if (viewX>=map_width-screen_width )
viewX = map_width-screen_width;
if (viewY>=map_height - screen_height )
viewY = map_height - screen_height;
if(posX <=0) posX =0;
if(posY <=0) posY =0;
num = get_nextframe(keyStates);
player.setFrame(num);
player.setPosition(posX, posY);
oldPosY= posY;
oldPosX= posX;
}
public void logic()
{
lm.setViewWindow(viewX,viewY,screen_width,screen_height);
}
public void run()
{
Graphics g = getGraphics();
while(true)
{
long startTime = System.currentTimeMillis();
input();
logic();
render(g);
flushGraphics();
long timeTaken = System.currentTimeMillis()- startTime;
if (timeT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