📄 gamecanvas.java
字号:
package demo;
import javax.microedition.lcdui.*;
public class GameCanvas extends Canvas{
//手机系统相关变量
public int SCREEN_W = 0;
public int SCREEN_H = 0;
public Image buffer = null;
public Graphics buffer_g = null;
GameMidlet midlet;
//所有图片信息
Image pandaLeftImg[] = null;
Image pandaRightImg[] = null;
Image fruitImg[] = null;
Image winImg = null;
Image overImg = null;
//控制 状态
public final static int LEVELINFO=0; //关卡前提示用户该关通过需要信息(时间/分数)
public final static int GAME=1;
public final static int WIN=2;
public final static int LOST=3;
public final static int GAMEEND=4;
public final static int PAUSE=5;
public int gameState = LEVELINFO ;
public int preGameState = LEVELINFO;
public int nextGameState = LEVELINFO;
int levelLaseTime = 0;
int score =0;
int level =0;
//NPC 设定
//苹果=0 西瓜=1 加分道具=2 加时间道具=3 多水果道具=4 加减速道具=5
public int levelTypes[][]={//每关中允许出现的NPC类型
{0,0,0,0,0,0}, {0,0,0,1,1,1}, {0,0,1,1,2,2},
{0,0,1,1,2,3}, {0,0,1,2,3,4}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {0,1,2,3,4,5}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {2,2,2,3,4,3}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {0,1,2,3,4,5}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {2,3,2,3,2,3}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {0,1,2,3,4,5}
};
public int levelTime[] ={
0,100,100, 100,100,100, 100,100,100,
100,100,100, 100,90,80, 70,60,50,
50,40,30,20
};
public int levelScore[]={0,500,600, 700,800,900, 1000,1200,1400,
1600,1800,2000, 1000,2000,2000, 2000,2000,1000,
2000,2000,2000,2000
};
//玩家控制角色 卡卡(方向) + 绳子(坐标)
public GameCanvas(GameMidlet _midlet){
midlet =_midlet;
SCREEN_W = this.getWidth();
SCREEN_H = this.getHeight();
try{
buffer = Image.createImage(SCREEN_W,SCREEN_H);
buffer_g = buffer.getGraphics();
pandaLeftImg = new Image[]{midlet.loadImage("/panda_l1.png"),
midlet.loadImage("/panda_l2.png")
};
pandaRightImg = new Image[]{midlet.loadImage("/panda_r1.png"),
midlet.loadImage("/panda_r2.png")
};
fruitImg = new Image[]{midlet.loadImage("/f1.png"),
midlet.loadImage("/f2.png"),
midlet.loadImage("/d1.png"),
midlet.loadImage("/d2.png"),
midlet.loadImage("/d3.png"),
midlet.loadImage("/d4.png")
};
winImg = midlet.loadImage("/win.png");
overImg = midlet.loadImage("/over.png");
}catch(Exception e){
e.printStackTrace();
}
}
void initLevle(int _level){
//level =_level;
levelLaseTime = levelTime[_level];
score =levelScore[_level];
}
protected void paint(Graphics g){
buffer_g.setClip(0, 0, SCREEN_W, SCREEN_H);
buffer_g.fillRect(0, 0, SCREEN_W, SCREEN_H);
try{
switch(gameState){
case LEVELINFO: //关卡前提示用户该关通过需要信息(时间/分数)
break;
case GAME:
break;
case WIN:
break;
case LOST:
break;
case GAMEEND:
break;
case PAUSE:
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
//外部事件--打电话 短信
protected void hideNotify(){
preGameState = gameState;
gameState = PAUSE;
}
protected void showNotify(){
gameState = preGameState;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -