📄 gdgamecanvas.java
字号:
package Com.JXT.MIDlet;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class GDGameCanvas extends GameCanvas implements Runnable{
private Image img,img1;
private ManCanvas man;
GDGameCanvas g;
int direction=0;
int x=0,y=0;
private Graphics gra;
private TiledLayer tlRoad;
private TiledLayer tlWall;
private TiledLayer tlPT;
LayerManager Layer;
protected GDGameCanvas(boolean t) {
super( t);
g=this;
Layer=new LayerManager();//创建图层管理器
gra=this.getGraphics(); //
img=this.createImage11();//创建地图
img1=this.createImage2();
tlRoad=new TiledLayer(8,8,img,img.getWidth()/2,img.getHeight());
tlWall=new TiledLayer(8,8,img,img.getWidth()/2,img.getHeight());
tlPT =new TiledLayer(3,4,img1, img1.getWidth()/3,img1.getHeight()/4);
int cellShuzhu[][]=new int[][]
{
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 2, 2, 2, 2, 2, 2},
{ 1, 1, 2, 1, 1, 1, 1, 1 },
{ 2, 2, 2, 2, 2, 2, 2, 2 },
{ 1, 1, 2, 1, 1, 1, 1, 1 },
{ 1, 1, 2, 2, 2, 2, 2, 2 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 }
};
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(cellShuzhu[i][j]==1)
{
tlRoad.setCell(j, i, cellShuzhu[i][j]);
}
else
{
tlWall.setCell(j, i, cellShuzhu[i][j]);
}
}
}
Layer.append(tlPT);
Layer.append(tlRoad); //添加图层
Layer.append(tlWall);
new Thread(this).start();
}
public void run() {
while(true)
{
synchronized(this)
{
gra.setColor(255, 255, 255);
gra.fillRect(0, 0, this.getWidth(), this.getHeight());
Layer.setViewWindow(x, y, this.getWidth(), this.getHeight()); //设置窗口可视
tlPT.setCell(2, 2, 10);
Layer.paint(gra, 0, 0);
switch(direction)
{
case 1:
tlPT.setCell(2, 2, 7);
break;
case 2:
tlPT.setCell(2, 2, 1);
break;
case 3:
tlPT.setCell(2, 2, 4);
break;
case 4:
tlPT.setCell(2, 2, 10);
break;
}
this.flushGraphics();
x++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Image createImage11()
{
Image img1 = null;
try {
img1=Image.createImage("/2ge.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img1;
}
public Image createImage2()
{
Image img1 = null;
try {
img1=Image.createImage("/man.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img1;
}
public void keyPressed(int key)
{
switch(getGameAction(key) )
{
case UP:
direction=1;
break;
case DOWN:
direction=2;
break;
case LEFT:
direction=3;
break;
case RIGHT:
direction=4;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -