package game;
/**
* <p>Title: 游戏界面类</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import javax.microedition.lcdui.*;
import java.util.*;
import main.*;
public class MainCanvas extends Canvas implements Runnable{
//把常用的功能键值设为静态成员,否则在适配各种机型时会很麻烦
public static int WIDTH;
public static int HEIGHT;
public static int SHOW_ROW;
public static int KEYCODE_UP;
public static int KEYCODE_DOWN;
public static int KEYCODE_LEFT;
public static int KEYCODE_RIGHT;
public static int KEYCODE_FIRE;
public static int KEYCODE_SOFTKEY_LEFT;
public static int KEYCODE_SOFTKEY_RIGHT;
//菜单标志
private static final byte MAIN_KEY_START = 0;
private static final byte MAIN_KEY_SETTING = 1;
private static final byte MAIN_KEY_HELP = 2;
private static final byte MAIN_KEY_ABOUT = 3;
private static final byte MAIN_KEY_EXIT = 4;
//设置默认字体,也是为了适配的方便,所以本文只有此处设置了字体,其他地方只需引用
private static final Font afont = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_PLAIN,
Font.SIZE_SMALL);
private int currentKeyCode = MAIN_KEY_START;
private Image flashImage_;//带有百宝箱的闪屏图片
private Image flashM_;//带有移动梦网的闪屏图片
private Image logoImage_;//游戏LOGO
private Thread thread_;//工作者线程
private String[] strsToShow;//帮助信息
private byte drawStatus_;//当前界面标志0:闪屏 1:菜单 2:游戏
private int drawCount_;//临时变量
private int menuStatus_;//帮助状态
private int menuCount_;//帮助状态
private boolean canChange = false;//键盘锁
private Image imageBuffer;
private Graphics gr;
private MainGame _game;
private Tools _tool;
public static boolean runing = true;
public MainCanvas() {
try {
setFullScreenMode(true);
WIDTH = getWidth();
HEIGHT = getHeight();
SHOW_ROW=10;
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = -6;
KEYCODE_SOFTKEY_RIGHT = -7;
inIt();
// getSettingInfo();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void getSettingInfo()
{
String SettingName="";
SettingName=TheMIDlet.instance.getAppProperty("SettingName");//setting.sys
if (SettingName.equals("Nokia7610"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = -6;
KEYCODE_SOFTKEY_RIGHT = -7;
}
else if (SettingName.equals("NokiaS60"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = -6;
KEYCODE_SOFTKEY_RIGHT = -7;
}
else if (SettingName.equals("MOTOV600"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = 22;
KEYCODE_SOFTKEY_RIGHT = 21;
}
else if (SettingName.equals("MOTOE398"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = -22;
KEYCODE_SOFTKEY_RIGHT = -21;
}
else if (SettingName.equals("MOTOV3"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = -22;
KEYCODE_SOFTKEY_RIGHT = -21;
}
else if (SettingName.equals("SEK700C"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = -6;
KEYCODE_SOFTKEY_RIGHT = -7;
}
else if (SettingName.equals("PanasonicX800"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = -6;
KEYCODE_SOFTKEY_RIGHT = -7;
}
else if (SettingName.equals("SamSungD508"))
{
WIDTH = getWidth();
HEIGHT = getHeight();
KEYCODE_UP = getKeyCode(UP);
KEYCODE_DOWN = getKeyCode(DOWN);
KEYCODE_LEFT = getKeyCode(LEFT);
KEYCODE_RIGHT = getKeyCode(RIGHT);
KEYCODE_FIRE = getKeyCode(FIRE);
KEYCODE_SOFTKEY_LEFT = KEY_STAR;
KEYCODE_SOFTKEY_RIGHT = KEY_POUND;
}
}
private void inIt() {
imageBuffer=Image.createImage(WIDTH,HEIGHT);
gr=imageBuffer.getGraphics();
drawStatus_ = 0;
drawCount_ = 0;
menuStatus_ = 0;
menuCount_ = 0;
_tool=new Tools();
start();
}
public void start() {
if (null == thread_) {
runing=true;
thread_ = new Thread(this);
thread_.start();
}
}
public void FreeGameSourse() {
//释放游戏
_game=null;
}
public void paint(Graphics g) {
//gr = g;
try {
switch (drawStatus_) {
case 0:
//画闪屏
drawFlashScreen();
break;
case 1:
//画菜单
drawMainScreen();
break;
case 2:
//画游戏主界面
drawGameScreen();
break;
}
//键盘解锁
g.drawImage(imageBuffer,0,0,0);
canChange = true;
}
catch (Exception e) {
e.printStackTrace();
}
}
public void keyPressed(int keyCode) {
try {
if (canChange) {
System.out.println("keyCode: " + keyCode);
switch (drawStatus_) {
case 0:
//越过闪屏界面
drawCount_ = 0;
flashImage_ = null;
flashM_ = null;
drawStatus_=1;
break;
case 1:
onMainScreen(keyCode);
break;
case 2:
onGameScreen(keyCode);
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private void onMainScreen(int keyCode) {
if (keyCode==KEYCODE_UP)
{
if (menuStatus_ == MAIN_KEY_HELP || menuStatus_ == MAIN_KEY_ABOUT) {
menuCount_--;//帮助/关于向上翻滚
}
else if (menuStatus_==MAIN_KEY_SETTING)
{
MySound.canSound=! MySound.canSound;
}
else if (menuStatus_ == MAIN_KEY_START) {
currentKeyCode = currentKeyCode - 1;
if (currentKeyCode < 0) {
currentKeyCode = MAIN_KEY_EXIT;
}
}
}
if (keyCode==KEYCODE_RIGHT)
{
if (menuStatus_ == MAIN_KEY_START) {
currentKeyCode = (currentKeyCode + 1) % 5;
}
}
if (keyCode==KEYCODE_DOWN)
{