?? homecanvas.java
字號(hào):
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.midlet.*;
public class HomeCanvas extends GameCanvas implements CommandListener, Runnable {
private Command backCommand;
private PetMIDlet midlet;
private Image home;
private Display display;
private WormPit theGame;
private Thread myThread;
private TetrisGameCanvas theTertis;
private int drawX = 0;
private int drawY = 0;
private int dx=0,dy=0;
private int i, j;
private int x,y;
private Sprite sprite;
private Image titledImage, spriteImage;
private Graphics g;
private TiledLayer background;
private LayerManager layManager;
int a1=6;
int a2=0;
int a3=3;
int a4=9;
private Command wanshua = new Command("玩耍", Command.OK, 2);
private Command wanwanju = new Command("玩玩具", Command.OK, 2);
private Command tanchishe = new Command("貪吃蛇", Command.OK, 2);
private Command e = new Command("俄羅斯方塊", Command.OK, 2);
private Command sleep = new Command("睡覺", Command.OK, 2);
private Command eating = new Command("喂食", Command.OK, 2);
public HomeCanvas(PetMIDlet midlet) {
super(true);
this.midlet = midlet;
backCommand = new Command("返回", Command.BACK, 1);
addCommand(backCommand);
addCommand(wanshua);
addCommand(wanwanju);
addCommand(tanchishe);
addCommand(e);
addCommand(sleep);
addCommand(eating);
setCommandListener(this);
drawY = 79;
try {
spriteImage = Image.createImage("/man.png");
titledImage = Image.createImage("/background.png");
} catch (Exception e) {
}
//精靈單元寬32高48
sprite = new Sprite(spriteImage, 32, 48);
// sprite.setFrameSequence(right_seq);
g = this.getGraphics();
//背景的初始化
background = new TiledLayer(1, 1, titledImage, 753, 420);
background.setCell(0,0,1);
// background.move(0,drawY - 50);
layManager = new LayerManager();
Thread thread = new Thread(this);
thread.start();
}
public void run() {
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
input();
//sprite.nextFrame();
sprite.setPosition(drawX + dx , drawY+dy);
layManager.append(sprite);
layManager.append(background);
layManager.setViewWindow(dx,dy,getWidth() ,158);
g.setColor(0, 0, 0);
g.drawString("寵物的小窩",0,0,Graphics.TOP|Graphics.LEFT);
g.drawLine(0,19,getWidth(),19);
layManager.paint(g,0,20);
flushGraphics();
}
}
//小人行走畫面的坐標(biāo)控制
public void input() {
int keystates = getKeyStates();
switch (keystates) {
case UP_PRESSED:{
if(dy-5>-drawY)
dy=dy-5;
else dy=-drawY;
if(a1+1<9)
a1++;
else a1=6;
sprite.setFrame(a1);
break;
}
case DOWN_PRESSED:
if(dy+5<300)
dy+=5;
else dy=300;
if(a2+1<3)
a2++;
else a2=0;
sprite.setFrame(a2);
break;
case LEFT_PRESSED:
if(dx-5>-drawX)
dx=dx-5;
else dx=-drawX;
if(a3+1<6)
a3++;
else a3=3;
sprite.setFrame(a3);
break;
case RIGHT_PRESSED:
if(dx+5<(723-drawX))
dx=dx+5;
else dx=(723-drawX);
if(a4+1<12)
a4++;
else a4=9;
sprite.setFrame(a4);
break;
}
x=dx;y=dy;
if(x<0)
x=0;
else if(x>(753-getWidth()))
x=753-getWidth();
if(y<0)
y=0;
else if(y>260)
y=260;
}
//按鈕響應(yīng)事件
public void commandAction(Command c, Displayable d) {
if (c == wanshua) {
} else if (c == wanwanju) {
// 進(jìn)入玩玩具界面
}
else if (c == tanchishe) {
//添加貪吃蛇游戲
theGame = new WormPit(midlet);
display=midlet.getDisplay();
display.setCurrent(theGame);
try {
// Start the game in its own thread
myThread = new Thread(theGame);
//ensure the game thread will work after pause
theGame.setDestroyed(false);
myThread.start();
} catch (Error e) {
}// 進(jìn)入游戲界面(貪吃蛇)
}
else if (c == e) {
theTertis=new TetrisGameCanvas(midlet);
Display.getDisplay(midlet).setCurrent(theTertis);
// 進(jìn)入游戲界面(俄羅斯方塊)
} else if (c == sleep) {
// 顯示睡覺的動(dòng)作
} else if (c == eating) {
// 進(jìn)入喂食界面
}
else if(c==backCommand){
midlet.LookInfoBackmenu();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -