?? jumpscreen.java
字號:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class JumpScreen extends GameCanvas
implements Runnable
{
private Display display;
private GameScreen gameScreen;
private GameMap gameMap;
private boolean isJumping = false;
private int colNum = 3,
rowNum = 7,
itemNum = 21,
curItem = 0,
x,y,
splitH = 15,splitV = 10,
w = 20,h = 10;
private Image borderImage = GameScreen.getImage(GameScreen.IMAGE_BORDER2);
public JumpScreen(Display display,GameScreen gameScreen,
GameMap gameMap)
{
super(false);
this.display = display;
this.gameScreen = gameScreen;
this.gameMap = gameMap;
this.setFullScreenMode(true);
}
private void next()
{
if((curItem+1)<itemNum)curItem ++;
else curItem = 0;
}
private void pre()
{
if((curItem - 1) >= 0) curItem --;
else curItem = itemNum - 1;
}
private void right()
{
if((curItem + rowNum) < itemNum){
curItem += rowNum;
}
}
private void left()
{
if((curItem - rowNum) >= 0){
curItem -= rowNum;
}
}
private void calcXY(int item)
{
//int col = item % colNum;
//int row = (item - col) / rowNum;
int col = item / rowNum;
int row = item - col * rowNum;
x = col * w + splitH * (col + 1)+ gameScreen.borderX -2 ;
y = row * h + splitV * (row + 1)+ gameScreen.borderY;
}
private void drawItem(Graphics g)
{
g.setColor(0x985415);
for(int i = 0;i < itemNum;i ++)
{
calcXY(i);
g.drawString("第"+i+"層",x,y,Graphics.TOP | Graphics.LEFT);
}
}
private void render(Graphics g)
{
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage(borderImage,
gameScreen.borderX,gameScreen.borderY,Graphics.TOP | Graphics.LEFT);
drawItem(g);
calcXY(curItem);
g.setColor(255,200,0);
g.drawString("第"+curItem+"層",x,y,Graphics.TOP | Graphics.LEFT);
flushGraphics();
}
private void tick()
{
int keyState = getKeyStates();
if ((keyState & this.DOWN_PRESSED) != 0){
next();
}else if((keyState & UP_PRESSED) != 0){
pre();
}else if ((keyState & this.RIGHT_PRESSED) != 0){
right();
}else if ((keyState & this.LEFT_PRESSED) != 0){
left();
}else if ((keyState & this.FIRE_PRESSED) != 0){
gameMap.jump(curItem);
stop();
}
}
public void start()
{
isJumping = true;
Thread t = new Thread(this);
t.start();
}
private void stop()
{
isJumping = false;
display.setCurrent(gameScreen);
}
public void run()
{
Graphics g = getGraphics();
long startTime = 0;
long timeTake = 0;
while(isJumping == true)
{
startTime = System.currentTimeMillis();
tick();
render(g);
timeTake = System.currentTimeMillis() - startTime;
if (timeTake < GameScreen.MILLIS_PER_TICK)
{
try
{
Thread.sleep(GameScreen.MILLIS_PER_TICK - timeTake);
} catch(InterruptedException e){System.out.println(e);}
}
}
}
protected void keyPressed(int keyCode)
{
if(keyCode == GameCanvas.KEY_NUM1)stop();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -