/**
* <p>Title: 模块</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: jams</p>
* @author jams
* @version 1.0
*/
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.IOException;
public final class ModelCanvas
extends Canvas
implements Runnable, CommandListener {
ModelMIDlet game;
private Thread gameThread;
// private MyMissile mymiss ;
private MyShip myShip = new MyShip();
private int myMissileCount;
private MyMissile[] myMiss = new MyMissile[]{
new MyMissile(),
new MyMissile(),
} ;
private int ufoCount;
private int ufoMissCount;
private Image[] img_map = null; //地图
private int ay = 0;
private Image Map = null; //地图 //背景地图
private UFO[] ufos = new UFO[]{
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
new UFO(myShip),
} ;
private UfoMissile[] ufoMiss = new UfoMissile[]{
new UfoMissile(),
new UfoMissile(),
} ;
private long score ;
private int life;
private int tickCount;
private Random random = new Random();
private boolean isGameover;
// Fields
public static final int KEY_SOFTKEY1 = -6;
public static final int KEY_SOFTKEY2 = -7;
public static final int KEY_SEND = -10;
public static final int KEY_END = -11;
public static final int KEY_SOFTKEY3 = -5;
public static final int KEY_UP_ARROW = -1;
public static final int KEY_DOWN_ARROW = -2;
public static final int KEY_LEFT_ARROW = -3;
public static final int KEY_RIGHT_ARROW = -4;
Command left_command = new Command("确定", Command.SCREEN, 1);
Command right_command = new Command("返回", Command.SCREEN, 2);
//rms
RecordStore rStore;
private Font sysFont; //保存系统字型
boolean load;
//游戏图片变量
private int actY=20;
private int logo_c;
// private int ey; //屏幕偏移
private int key_delay = 0; //用来控制按键时的延迟时间
private boolean key_press = false; //立即按键,快速执行
private boolean keyRepeat; //控制按键
private int key; //键值
//游戏屏幕的宽度和高度
private final int WIDTH = getWidth();
private final int HEIGHT = getHeight();
private final int TIME_PER_FRAME = 80; //游戏速度的控制
//调试错误的变量,游戏制作完成后要删除的
int error;
String errorString;
final int GAME_ERROR = 999;
/* about variable of game state */
private int game_state;
private final int GAME_LOAD = 0; //游戏最先进入的状态
private final int GAME_INIT = 1; //初始化状态,载入图片,声音等资源,初始化变量等。
private final int GAME_START = 2; //游戏开始,设置每关的信息
private final int GAME_RUN = 3; //游戏运行状态
private final int GAME_OVER = 4; //游戏结束
private final int GAME_WIN = 5; //游戏胜利
private final int GAME_MENU = 6; //游戏主菜单
private final int GAME_PICTURE = 8; //得到拼图
private final int GAME_TELCALL = 11; //
private final int GAME_ABOUT = 12; //
private final int GAME_HELP = 13; //
private final int GAME_SET = 14; //
private final int GAME_PAUSE = 15; //游戏暂停
private final int GAME_CUE = 16; //提示
private boolean quitGame = false; //游戏结束
private int MainMenuId = 1; //字菜单
private int helpY, helpX;
private int mainX = 55;
public ModelCanvas(ModelMIDlet game) {
this.game = game;
this.setCommandListener(this);
}
//游戏初始化
private final void game_init() {
sysFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
myShip.x = (WIDTH-myShip.getwidth())/2;
myShip.y = HEIGHT-2*myShip.getheight();
life = 4 ;
score = 0;
for(int i=0;i<myMiss.length;i++){
myMiss[i].setAlive(false);
}
for(int i=0;i<ufos.length;i++){
ufos[i].setAlive(false);
}
ufoCount = 0 ;
for(int i=0;i<ufoMiss.length;i++){
ufoMiss[i].setAlive(false);
}
ufoMissCount = 0;
myMissileCount = 0;
tickCount = 0;
helpY = sysFont.getHeight();
helpX = 3;
Map = null;
Map = Image.createImage(WIDTH, 2 * HEIGHT);
ay = 0;
}
//开始线程
public final void game_start() {
gameThread = new Thread(this);
gameThread.start();
}
public final void game_stop() {
gameThread = null;
System.gc();
}
protected final void paint(Graphics g) {
g.setFont(sysFont);
try {
switch (game_state) {
case GAME_LOAD: //最先进入得画面,显示一些loading画面
g.setColor(0);
g.fillRect(0, 0, WIDTH, HEIGHT);
Image image_point = null;
image_point = Image.createImage("/point.png");
g.setClip( (WIDTH - 137) / 2 + 20,
(HEIGHT - image_point.getHeight()) / 2 + 10,
(logo_c / 3) * 7, 20);
g.drawImage(image_point, (WIDTH - 137) / 2 + 20,
(HEIGHT - image_point.getHeight()) / 2 + 10, 20);
if (logo_c >= 18) {
logo_c = 0;
game_init();
commandChange("确定", "", 1, 0);
game_state = GAME_START;
}
break;
case GAME_START:
g.setColor(0);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(0xffffFF);
Image me[] = new Image[1];
me[0] = Image.createImage("/myshipImg1.png");
g.drawImage(me[0],80,20,20);
break;
case GAME_RUN:
g.setColor(0x33CCFF);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 1; i <= 5; i++) {
g.drawImage(Map, 0, - (2 * i - 1) * HEIGHT + ay, 20);
}
g.setColor(0);
g.drawString("S"+score,10,2,20);
g.drawString("L"+life,60,2,20);
myShip.doDraw(g);//画自己飞机
for(int i=0;i<myMiss.length;i++){//画自己飞机子弹
myMiss[i].doDraw(g);
}
//画敌人飞机
for(int i=0;i<ufos.length;i++){
ufos[i].doDraw(g);
}
//画敌人飞机子弹
for(int i=0;i<ufoMiss.length;i++){
ufoMiss[i].doDraw(g);
}
break;
case GAME_PAUSE:
break;
case GAME_CUE:
break;
case GAME_WIN:
break;
case GAME_OVER:
g.setColor(0);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(0xffffFF);
g.drawString("你挂了!!!", 50, 50, 20);
break;
case GAME_SET:
break;
case GAME_ABOUT:
break;
case GAME_HELP:
break;
case GAME_ERROR: //游戏错误状态
g.drawString("" + error, 20, 40, 20);
break;
}
}
catch (Exception e) {
// //游戏完成后请删除try{} catch{}
System.out.println("paint: " + e);
g.setClip(0, 0, WIDTH, HEIGHT);
g.setColor(0x000000);
// //将错误信息画在屏幕上
g.drawString("1. error:" + e.toString(), 0, 0, 20);
g.drawString("2. error:" + error, 0, 30, 20);
g.drawString("3. game state:" + game_state, 60, 0, 20);
long timenow = System.currentTimeMillis();
while (System.currentTimeMillis() - timenow < 10000) { //10秒后退出
}
quitGame = true;
}
}
public final void run() {
error = 1;
long starttime = 0;
long timetaken = 0;
try {
while (!quitGame) {
starttime = System.currentTimeMillis();
switch (game_state) {
case GAME_RUN:
if (ay >= 1250) {
ay = 1250;
}
else {
ay = ay + 1;