📄 maincanvas.java
字号:
//package bushfighting;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import com.nokia.mid.ui.FullCanvas;
//街舞秀的游戏主界面类
public class MainCanvas extends FullCanvas implements Runnable {
//private Image mPauseImage = null;
private Image mEndImage = null;//游戏结束的图片
private Image mWinImage = null;
private boolean mRunning = false; //是否正在运行的标志
private boolean mPaused = false; //游戏是否暂停的标志
private Image mBGImage = null; //用作后台绘制的缓冲区
private Graphics mBGraphics = null; //
private int mCanvasWidth, mCanvasHeight; //场景的大小
private int mPauseIndex = 0; //菜单的当前选择项
private int mGameState = 0; //0表示显示杀敌数目的提示对话框,1表示开始游戏开始
private int mLevel = 1; //当前的关号
private SceneModel mSceneModel = null;
private Score mScore = null; //所得到的分数
private InfoForm mInfoForm = null;
private NetDataForm mDataForm = null; //用于提示用户输入信息
private MainMenu mMenu = null;
private DisplayManager mDisplayManager = null;
private java.util.Random mRandom = new java.util.Random(System.currentTimeMillis());
private static final int TICKTIME = 130; //每个周期的时间,单位毫秒
private static final int MAX_LEVEL = 5;
public MainCanvas(MainMenu menu, NetDataForm dataForm, Score score, Image bufferImg) {
mCanvasWidth = getWidth();
mCanvasHeight = getHeight();
mMenu = menu;
mDataForm = dataForm;
mScore = score;
mBGImage = bufferImg;
if (mBGImage == null)
mBGImage = Image.createImage(mCanvasWidth, mCanvasHeight);
mBGraphics = mBGImage.getGraphics();
mSceneModel = new SceneModel(mCanvasWidth,mCanvasHeight,mScore);
}
//初始化游戏环境
public void init() {
try {
mEndImage = HF.loadImage("end.png");
mWinImage = HF.loadImage("win.png");
//mPauseImage = HF.loadImage("pause.png");
} catch (Exception e) {
System.out.println("error loading the scene images");
}
}
public void setDisplayManager(DisplayManager manager) {
mDisplayManager = manager;
}
//绘制游戏
protected void paint(Graphics g) {
mBGraphics.setColor(HF.COLOR_LIGHTBLUE);
mBGraphics.fillRect(0, 0, mCanvasWidth, mCanvasHeight);
drawAnimation(g);
if (mRunning == false&&mSceneModel.isGameOver() == true) {
int x = mCanvasWidth - mEndImage.getWidth();
int y = mCanvasHeight - mEndImage.getHeight();
g.drawImage(mEndImage, x / 2, y / 2, Graphics.LEFT | Graphics.TOP);
} else if (mRunning == false&&mSceneModel.isPassLevel() == true) {
if(mLevel== MAX_LEVEL){
int x = mCanvasWidth - mWinImage.getWidth();
int y = mCanvasHeight - mWinImage.getHeight();
g.drawImage(mWinImage, x / 2, y / 2, Graphics.LEFT | Graphics.TOP);
}
}
//g.setColor(0xff0000);
//g.setFont(HF.SMALL_FONT);
//g.drawString("试玩版",10,10,Graphics.LEFT|Graphics.TOP);
}
//重置场景的信息
private void reset(){
mRunning = true;
mPaused = false;
mScore.reset();
mLevel = 1;
}
//启动游戏线程
public void start() {
if(mRunning == true){
return;//只允许一个线程运行
}
reset();
mLevel =1;
mSceneModel.loadLevel(mLevel);
mGameState = 1;
Thread thread = new Thread(this);
thread.start();
}
//暂停处理
public synchronized void pause() {
mPaused = true;
if(HF.sound)
mSceneModel.back.pause();
repaint(0, 0, mCanvasWidth, mCanvasHeight);
}
//恢复处理
public synchronized void resume() {
mPaused = false;
if(HF.sound)
mSceneModel.back.resume();
repaint(0, 0, mCanvasWidth, mCanvasHeight);
}
//停止游戏
public void stop() {
mRunning = false;
}
//处理游戏进行时用户的按键操作
public void keyPressed(int keyCode) {
if (mPaused == true) {
keyPressPause(keyCode);
return;
} else if (keyCode == -6) { //左右上角的按键
pause(); //暂停游戏
return;
}
int action = getGameAction(keyCode);
if(mGameState == 0){
if(action == FIRE){
mGameState = 1;//按确定键切换到开始状态
mLevel++;
if(mLevel <=MAX_LEVEL)
mSceneModel.loadLevel(mLevel);
else{
stop();
}
}
return;
}
/*
else
{
if(mSceneModel.isTalk == true)
{
mSceneModel.isTalk=false;
}
}
*/
switch (action) {
case LEFT:
mSceneModel.setPlayerState(HF.STATE_MOVE_LEFT);
break;
case RIGHT:
mSceneModel.setPlayerState(HF.STATE_MOVE_RIGHT);
break;
case UP:
mSceneModel.setPlayerState(HF.STATE_MOVE_UP);
break;
case DOWN:
mSceneModel.setPlayerState(HF.STATE_MOVE_DOWN);
break;
case FIRE:
if(mSceneModel.takeRes()){
mSceneModel.setPlayerState(HF.STATE_J);
}else{
mSceneModel.setPlayerState(HF.STATE_ATTACK);
}
break;
}
//repaint(0, 0, mCanvasWidth, mCanvasHeight);
}
public void keyReleased(int keyCode) {
mSceneModel.setPlayerState(HF.STATE_STILL);
}
//处理游戏暂停时用户的按键操作
public void keyPressPause(int keyCode) {
if (keyCode == -7) { //右上角的按键
resume();
return;
}
if (keyCode == -6) {
mDisplayManager.setParent(this);
{
switch(mPauseIndex) {
case 1:
mMenu.itemSelected(1);
break;
case 3:
mMenu.itemSelected(3);
break;
case 4:
mDisplayManager.setParent(mMenu);
mDisplayManager.back();
mMenu.bIsGame = false;
break;
case 0:
resume();
break;
case 2:
HF.sound = !HF.sound;
if(!HF.sound) {
mSceneModel.back.stop();
} else {
mSceneModel.back.play();
}
break;
}
}
return;
}
int action = getGameAction(keyCode);
System.out.println("暂停选择"+mPauseIndex);
switch (action) {
case DOWN:
mPauseIndex = (mPauseIndex + 1) % 5;
repaint(0, 0, mCanvasWidth, mCanvasHeight);
break;
case UP:
mPauseIndex = (mPauseIndex + 4) % 5;
repaint(0, 0, mCanvasWidth, mCanvasHeight);
break;
case FIRE:
mDisplayManager.setParent(this);
switch(mPauseIndex) {
case 1:
mMenu.itemSelected(1);
break;
case 3:
mMenu.itemSelected(3);
break;
case 4:
mDisplayManager.setParent(mMenu);
mDisplayManager.back();
mMenu.bIsGame = false;
break;
case 0:
resume();
break;
case 2:
HF.sound = !HF.sound;
if(!HF.sound) {
mSceneModel.back.stop();
} else {
mSceneModel.back.play();
}
break;
}
break;
}
}
//执行游戏的绘制工作
private void drawAnimation(Graphics g) {
mSceneModel.draw(mBGraphics);
//mBGraphics.drawImage(mPauseImage, mCanvasWidth-27, mCanvasHeight - 15, Graphics.LEFT | Graphics.TOP);
if (mPaused)
drawPauseMenu(mBGraphics);
g.drawImage(mBGImage, 0, 0, Graphics.LEFT | Graphics.TOP);
}
/*
//绘制场景信息
private void drawSceneInfo(Graphics g)
{
}
*/
//绘制在暂停时的对话框
public void drawPauseMenu(Graphics g) {
g.setFont(HF.MEDIUM_FONT);
NetDataForm.drawStatusBar(g, HF.COLOR_LIGHTBLUE, "选择", "继续", mCanvasWidth, mCanvasHeight);
MainMenu.drawMenuList(g, "继续游戏&游戏帮助&音乐-"+(HF.sound?"开":"关")+"&关于游戏&返回主菜单&", HF.COLOR_LIGHTBLUE, HF.COLOR_BLUE,
mCanvasWidth, mCanvasHeight, mPauseIndex);
}
//释放占用的资源
public void release() {
stop();
}
public void tick() {
mSceneModel.refresh();
}
//实现线程的处理工作
public void run() {
boolean flag = true; //为了控制游戏的在不断的暂停和恢复中保持正常
long beginTime = System.currentTimeMillis();
boolean normalStopped = false; //是否正常结束的标志,即不是中途退出
long timeSpan = 0;
try {
while (mRunning == true) {
//System.out.println("运行中");
if (mGameState ==1&&mPaused == false) {
long startTime = System.currentTimeMillis();
//refresh();
tick();
repaint(0, 0, mCanvasWidth, mCanvasHeight);
serviceRepaints();
if(mSceneModel.isGameOver() == true){
normalStopped = true;
//System.out.println("Game over");
stop();
} else if (mSceneModel.isPassLevel() == true){
if(mLevel== MAX_LEVEL){
normalStopped = true;
}
mGameState = 0;
//Thread.sleep(1500);
}
long timeTaken = System.currentTimeMillis() - startTime;
int tickTime = TICKTIME;
if (timeTaken < tickTime){
Thread.sleep(tickTime - timeTaken);
} else{
//Thread.yield();
Thread.sleep(10);
}
} else {
if (flag == true) {
timeSpan = System.currentTimeMillis() - beginTime;
flag = false;
}
beginTime = System.currentTimeMillis() - timeSpan;
Thread.sleep(50);
}
}
mDisplayManager.setParent(mMenu); //使得可以正常回到主菜单
if (normalStopped) {
repaint(0, 0, mCanvasWidth, mCanvasHeight);
if(mSceneModel.isGameOver()==true||mSceneModel.isPassLevel() == true&&mLevel== MAX_LEVEL)//停顿一下显示,Game Over
Thread.sleep(1500);
}
mDisplayManager.back();
mMenu.bIsGame = false;
} catch (InterruptedException e) {
}
}
protected void showNotify() {
//resume();
}
boolean bPause = false;
protected void hideNotify() {
pause();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -