package pengzhuang;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//import People.Tools;
public class MyGameCanvas extends GameCanvas implements Runnable {
int currentX,currentY;
int keyCode;
int width,height;
Graphics g;
Image image=null,image1=null;
Sprite sprite;
LayerManager layermanager;
TiledLayer tiledlayer,tiledlayer1;
protected MyGameCanvas() {
super(false);
currentX=0;
currentY=0;
width=getWidth();
height=getHeight();
try {
image=Image.createImage("/forest01.png");
image1=Image.createImage("/lb.png");
} catch (IOException e) {
}
tiledlayer=new TiledLayer(10, 15, image, 32, 32);
tiledlayer1=new TiledLayer(1,1,image,32,32);
sprite=new Sprite(image1, 32, 48);
layermanager=new LayerManager();
byte[][] map=MapData.getMap();
byte[][] map1=MapData.getMap1();
for(int i=0;i<map.length;i++)
{
for(int j=0;j<map[i].length;j++)
{
tiledlayer.setCell(j, i, map[i][j]);
}
}
tiledlayer1.setCell(0, 0, map1[0][0]); //***************
layermanager.append(sprite);
layermanager.append(tiledlayer1); //先写距离近的
layermanager.append(tiledlayer);
sprite.setPosition(width/2,height/2);
tiledlayer1.setPosition(50, 50);
new Thread(this).start();
}
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
layermanager.setViewWindow(currentX, currentY, 320, 480);
layermanager.paint(g, 0, 0);
g.drawString("DefaultColorPhone模拟器适用",width/2,height/2,Graphics.BOTTOM|Graphics.HCENTER);
}
public void run() {
long startTime,endTime;
g=getGraphics();
while(true)
{
startTime=System.currentTimeMillis();
switch(keyCode)
{
case Tools.KEYRIGHT:
if(sprite.getFrame()<8||sprite.getFrame()>=11)
{
sprite.setFrame(8);
}
else
{
sprite.nextFrame();
}
sprite.move(4,0);
currentX+=4;
if(currentX+Tools.SCREEN_WIDTH>tiledlayer.getWidth()){
currentX-=4;
if(sprite.getX()+sprite.getWidth()>tiledlayer.getWidth())
{
sprite.move(-4, 0);
}
}
if(sprite.collidesWith(tiledlayer1, false))
{
sprite.move(-4, 0);
currentX=currentX-4;
}
// if(sprite.getX()+10+sprite.getWidth()<width) //控制右边距
// {
// sprite.move(4,0);
// }
// if(sprite.getX()+tiledlayer.getX()+sprite.getWidth()+10>width)
// {
// if(sprite.getX()+sprite.getWidth()<tiledlayer.getWidth())
// {currentX=currentX+4;//****************
// if(sprite.getX()+sprite.getWidth()<tiledlayer.getWidth())
// {
// sprite.move(4, 0); //****************//到右边距时背景图向左移动
// }
// }
// }
// else
// {
// currentX=currentX-1; //控制背景向左移动
// }
break;
case Tools.KEYLEFT:
if(sprite.getFrame()<4||sprite.getFrame()>=7)
{
sprite.setFrame(4);
}
else
{
sprite.nextFrame();
}
sprite.move(-4, 0);
currentX=currentX-4;
if(currentX<0)
{
currentX=currentX+4;
if(sprite.getX()<0) //控制小人向左走地图是否停止移动
{
sprite.move(4, 0);
}
}
if(sprite.collidesWith(tiledlayer1, false)) //控制小人和石头是否发生撞击
{
sprite.move(4, 0);
}
// if(sprite.getX()+tiledlayer.getX()>0) //控制左边距
// {
//
// sprite.move(-4,0);
// if(sprite.collidesWith(tiledlayer1, false))
// {
// sprite.move(4, 0); //小人向左走与石头图层碰撞
// }
// }
// else
// {
// currentX=currentX-4;
// }
// if(sprite.getX()+tiledlayer.getX()<0)
// {
// currentX=currentX-4; //控制背景向右移
// sprite.move(-4, 0); //到左边距时背景图向右移动
// }
break;
case Tools.KEYUP:
if(sprite.getFrame()<12||sprite.getFrame()>=15)
{
sprite.setFrame(12);
}
else
{
sprite.nextFrame();
}
// if(sprite.getY()>0) //控制上边距
// {
// sprite.move(0,-4);
// if(sprite.collidesWith(tiledlayer1, false)) //控制小人向上走与石头是否发生碰撞
// {
// sprite.move(0,4);
// }
// }
sprite.move(0, -4);
currentY=currentY-4;
if(currentY<0) //控制小人向上走地图是否移动
{
currentY=currentY+4;
if(sprite.getY()<0)
{
sprite.move(0, 4); //控制小人与上边距是否碰撞
}
}
if(sprite.collidesWith(tiledlayer1, false)) //控制小人向上走与石头是否发生碰撞
{
sprite.move(0,4);
}
break;
case Tools.KEYDOWN:
if(sprite.getFrame()>=3)
{
sprite.setFrame(0);
}
else
{
sprite.nextFrame();
}
sprite.move(0,4);
currentY=currentY+4;
if(currentY+Tools.SCREEN_HEIGHT>tiledlayer.getHeight())
{
currentY=currentY-4; //控制地图是否移动
if(sprite.getY()+sprite.getHeight()>tiledlayer.getHeight())
{
sprite.move(0, -4); //控制小人不出界
}
}
if(sprite.collidesWith(tiledlayer1, false)) //控制小人向下走与石头是否发生碰撞
{
sprite.move(0, -4);
}
break;
}
paint(g);
flushGraphics();
endTime=System.currentTimeMillis();
if(endTime-startTime<100){
try {
Thread.sleep(100-endTime+startTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
protected void keyPressed(int keyCode) {
//super.keyPressed(keyCode);
this.keyCode=keyCode;
}
protected void keyReleased(int keyCode) {
//super.keyReleased(keyCode);
this.keyCode=0;
switch(keyCode)
{
case Tools.KEYRIGHT:
sprite.setFrame(8); //恢复站立
break;
case Tools.KEYLEFT:
sprite.setFrame(4); //恢复站立
break;
case Tools.KEYUP:
sprite.setFrame(12); //恢复站立
break;
case Tools.KEYDOWN:
sprite.setFrame(0); //恢复站立
break;
}
}
}