import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.MIDletStateChangeException;
public class MainCanvas extends Canvas implements Runnable {
private boolean gameLive = true;// 游戏线程开关。
private int gameState; // 游戏状态
private final int TIME_PER_FRAME = 33; // 游戏速度的控制
private long timetaken; // 单帧结束时间
private long gameCount; // 游戏主计数器
private Image imglogo1, imglogo2; // 游戏logo1,logo2
private Image imgmenu;// 游戏菜单
private Image imgbg1, imgbg2;// 两个背景实现卷轴
private int imgbg1X, imgbg1Y;
private boolean gameBl ;// 游戏总的boolean值
private int logoCount; // 计数器
private boolean isUp, isDown, isLeft, isRight, isFire, isRightR; // 方向键的判断
private int num;// 公主闪的变量
MoFaGongZhuMidlet midlet;
GongZhu gongzhu; // 公主
GongZhu donghua;// 公主死亡动画
private int donghuaX, donghuaY; // 死亡动画的X,Y坐标
CaiDan caidan; // 菜单类
CaiDan dajue; // 公主绝招
XiaoGuai[] xiaoguai; // 小怪类
XiaoGuai xiaoguaidonghua; // 小怪死亡动画
XiaoGuai Boss; // boss图片
// ZhiDan huohua; // 火花
int index = 0; // 小怪索引
int innum, xgnum, xg1num, xg2num, xginnum = 6;// 公主重生动画变量控制,小怪重生变量
VolumeControl vc;
public MainCanvas(MoFaGongZhuMidlet midlet) {
this.midlet = midlet;
System.gc();
gameStart();
initGame();
Player p1, p2; //声音
try {
imglogo1 = Image.createImage("/logo1.png");
imglogo2 = Image.createImage("/logo2.png");
imgmenu = Image.createImage("/menu.png");
} catch (IOException e) {
e.printStackTrace();
}
InputStream is = getClass().getResourceAsStream("/3.wav");
InputStream is1 = getClass().getResourceAsStream("/bg.mid");
try {
p1 = Manager.createPlayer(is, "audio/x-wav");
p1.realize();
p1.prefetch();
p2 = Manager.createPlayer(is1, "audio/midi");
p2.prefetch();
vc = (VolumeControl) p2.getControl("VolumeControl");
p2.setLoopCount(-1);
p2.start();
} catch (IOException e) {
} catch (MediaException e) {
}
gameState = PublicDataTable.GAME_LOGO;
}
/**
* 初始化游戏
*/
private void initGame() {
this.setFullScreenMode(true);
try {
imgbg1 = Image.createImage("/beijing1.png");
imgbg2 = imgbg1;
gongzhu = new GongZhu("/gongzhu.png", 22, 29);
gongzhu.setPossion((PublicDataTable.WIDTH - gongzhu.getW()) / 2,
PublicDataTable.HEIGHT - gongzhu.getH());
donghua = new GongZhu("/donghua.png", 40, 40);
xiaoguaidonghua = new XiaoGuai("/xiaoguaidonghua.png", 40, 40);
caidan = new CaiDan("/caidan.png", 13, 13);
dajue = new CaiDan("/dajue2.png", 32, 32);
// huohua = new ZhiDan("/huohua.png", 12, 11);
xiaoguai = new XiaoGuai[3];
Boss = new XiaoGuai("/Boss.png", 41, 61);
Boss.setPossion((PublicDataTable.WIDTH - Boss.getW()) / 3, -Boss
.getH() / 4);
for (int i = 0; i < xiaoguai.length; i++) {
xiaoguai[0] = new XiaoGuai("/xiaoguai1.png", 15, 15);
xiaoguai[1] = new XiaoGuai("/xiaoguai2.png", 15, 18);
xiaoguai[2] = new XiaoGuai("/xiaoguai3.png", 17, 19);
}
xiaoguai[0].setPossion(0, 0);
xiaoguai[1].setPossion(0, 0);
xiaoguai[2].setPossion(20, PublicDataTable.HEIGHT
+ xiaoguai[2].getH());
} catch (IOException e) {
e.printStackTrace();
}
gameState = PublicDataTable.GAME_RUN;
}
/**
* 启动游戏主线程
*/
protected void gameStart() {
new Thread(this).start();
}
/**
* 设置游戏状态
*
* @param gameState
* 游戏新状态
*/
public void setGameState(short gameState) {
this.gameState = gameState;
}
protected void paint(Graphics g) {
switch (gameState) {
case PublicDataTable.GAME_LOGO: // LOGO
if (logoCount == 0) {
g.drawImage(imglogo1, 0, 0, Graphics.LEFT | Graphics.TOP);
} else if (logoCount == 1) {
g.drawImage(imglogo2, 0, 0, Graphics.LEFT | Graphics.TOP);
} else if (logoCount == 2) {
gameState = PublicDataTable.GAME_MENU;
}
break;
case PublicDataTable.GAME_MENU: // 菜单
g.drawImage(imgmenu, 0, 0, Graphics.LEFT | Graphics.TOP);
caidan.drawIcon(g);
switch (caidan.up) {
case 0:
caidan.drawRect(g);
break;
case 1:
caidan.drawRect(g);
break;
case 2:
caidan.drawRect(g);
break;
}
break;
case PublicDataTable.GAME_LOAD: // 加载
break;
case PublicDataTable.GAME_RUN: // 游戏运行
drawbg(g); // 绘制背景
if (gameCount % 6 == 0) {
caidan.drawIcon(g);// 绘制小图标
}
if (gongzhu.isLive()) {
gameBl=true;
if (num < 25) {
Boss.setBh(true);
xiaoguai[0].setBh(true);
xiaoguai[1].setBh(true);// 在闪的过程中子弹碰到后不死
xiaoguai[2].setBh(true);
if (gameCount % 2 == 0) {
num++;
gongzhu.drawGongZhu(g);
}
} else {
Boss.setBh(false);
xiaoguai[0].setBh(false);
xiaoguai[1].setBh(false);// 在正常过程中恢复
xiaoguai[2].setBh(false);
gongzhu.drawGongZhu(g);
}
} else {
if (gongzhu.lifeCount != 0) {
donghuaX = gongzhu.getX(); // 设置公主死亡动画坐标
donghuaY = gongzhu.getY();
donghua.setPossion(donghuaX, donghuaY);
donghua.drawDh(g);
if (innum >= 5) {
chusheng();
}
} else {
gameBl = false;
if (!gameBl) {
drawbg(g); // 绘制背景
gameState=PublicDataTable.GAME_OVER;
}
}
}
// 生命图--------------------------------------------------------
switch (gongzhu.lifeCount) {
case 5:
caidan.drawLf5(g);
break;
case 4:
caidan.drawLf4(g);
break;
case 3:
caidan.drawLf3(g);
break;
case 2:
caidan.drawLf2(g);
break;
case 1:
caidan.drawLf1(g);
break;
}
// 公主保护图---------------------------------------------------------------
// if (gongzhu.lifeCount != 0) {
//
// switch (caidan.egis) {
// case 1:
// caidan.drawEgis1(g);
// break;
// case 2:
// caidan.drawEgis2(g);
// break;
// case 3:
// caidan.drawEgis3(g);
// break;
// }
// }
if (gongzhu.isLive()) {
gongzhu.drawBullets(g);
}
// 如果按了左功能键公主发大绝招
// if (isRightR) {
// caidan.drawDajue(g);
// }
if (gameCount < 1000) {
if (xiaoguai[0].isLive()) {
xiaoguai[0].drawGuai(g);
}
if (xiaoguai[1].isLive()) {
xiaoguai[1].drawGuai(g);
}
xiaoguai[1].drawBullets(g);
if (xiaoguai[2].isLive()) {
xiaoguai[2].drawGuai(g);
}
xiaoguai[2].drawBullets2(g);
if (xgnum < xginnum && !xiaoguai[0].isLive()) { // 公主子弹或者公主碰上小怪1的时候小怪1死亡
xiaoguaidonghua.setPossion(xiaoguai[0].getX(), xiaoguai[0]
.getY());
xiaoguaidonghua.drawXgDh(g);
}
if (xg1num < xginnum && !xiaoguai[1].isLive()) { // 公主子弹或者公主碰上小怪1的时候小怪1死亡
xiaoguaidonghua.setPossion(xiaoguai[1].getX(), xiaoguai[1]
.getY());
xiaoguaidonghua.drawXgDh(g);
}
if (xg2num < xginnum && !xiaoguai[2].isLive()) { // 公主子弹或者公主碰上小怪1的时候小怪1死亡
xiaoguaidonghua.setPossion(xiaoguai[2].getX(), xiaoguai[2]
.getY());
xiaoguaidonghua.drawXgDh(g);
}
} else {
// 绘制Boss~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
if (Boss.isLive()) {
Boss.drawBoss(g);
Boss.drawBossBullets(g);
}else{
gameBl = false;
drawbg(g);
gameState=PublicDataTable.GAME_WIN;
}
}
break;
case Public