📄 cgamecanvas.java
字号:
/*
* CGameCanvas.java
*
* Created on 2006年4月16日, 下午12:03
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package gamepacket;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
/**
*
* @author zxh
*=======游戏类=========
*/
public class CGameCanvas extends GameCanvas implements Runnable
{
private static final short Game_Menu=0;//游戏标题状态
private static final short Game_Loop=1;//游戏运行状态
private static final short Game_Over=2;//游戏结束
private static final short Game_Stop=3;//游戏暂停
private short GameState;//游戏状态
private int CpuNum;//敌人数量
private int MaxScore;//最高分
private long tempTime;//记录玩家发射炸弹的延时
private int width,height;//屏幕大小
private LayerManager layermanager;//层管理
private LayerManager layermanager_menu;//菜单层
private Graphics g;
//背景图
private Sprite spt_bg;
//音乐部分
private Player pbomb;//播放爆炸效果
private Player psound;//播放背景音乐
private ShipMain sm;//midlet
private CPlayerShip m_PlayerShip;//玩家船
private CMenu m_Menu;//菜单
private CCpuShip[] m_CpuShip;//敌人
/** Creates a new instance of CGameCanvas */
public CGameCanvas (ShipMain s)
{
super(true);
this.sm=s;
CpuNum=5;
MaxScore=800;
g=this.getGraphics ();
width=this.getWidth ();
height=this.getHeight ();
layermanager=new LayerManager();
layermanager_menu=new LayerManager();
m_Menu=new CMenu(layermanager_menu);//菜单
m_PlayerShip=new CPlayerShip(width,height,layermanager);//玩家对象
m_CpuShip=new CCpuShip[]{
new CCpuShip(width,height,layermanager,0),
new CCpuShip(width,height,layermanager,2),
new CCpuShip(width,height,layermanager,5),
new CCpuShip(width,height,layermanager,1),
new CCpuShip(width,height,layermanager,4),
new CCpuShip(width,height,layermanager,6),
new CCpuShip(width,height,layermanager,3),
new CCpuShip(width,height,layermanager,7),
};//敌人
//游戏背景
try
{
spt_bg=new Sprite(Image.createImage ("/img/bg.png"));
} catch (IOException ex)
{sm.exit ();}
layermanager.append (spt_bg);//把背景加入层中
//音乐部分
try{
InputStream in1 = getClass().getResourceAsStream("/sound/sound1.mid");//背景音乐
InputStream in2 = getClass().getResourceAsStream("/sound/bomb.wav");//爆炸音效
pbomb=Manager.createPlayer(in2, "audio/x-wav");
psound=Manager.createPlayer (in1,"audio/midi");
}catch(Exception e) {sm.exit ();}
try
{
//音量设定
pbomb.realize ();
} catch (MediaException ex)
{
sm.exit ();
}
VolumeControl control=(VolumeControl)pbomb.getControl ("VolumeControl");
if(control!=null)
{
control.setLevel(40);
}
GameState=Game_Menu;
try
{
psound.start ();//播放音乐
} catch (MediaException ex)
{sm.exit ();}
tempTime=System.currentTimeMillis ();
}
public void run ()
{
while(true)//GameState!=Game_Stop)
{
switch(GameState)
{
case Game_Menu:
this.KeyInput ();//接收按键
this.DrawScreen (g);//屏幕
break;
case Game_Loop:
this.KeyInput ();//接收按键
this.Action ();//炸弹,敌人等运行动作
this.DrawScreen (g);//屏幕
break;
case Game_Over:
this.KeyInput ();
this.DrawScreen (g);
break;
case Game_Stop://暂停
this.KeyInput ();
this.DrawScreen (g);
break;
default:
break;
}
try
{
Thread.sleep (20);
} catch (InterruptedException ex){sm.exit ();}
}//end while
}
public void start()
{
Thread t=new Thread(this);//线程开始
t.start ();
}
//==================================================
//按键输入
public void KeyInput()
{
int state=this.getKeyStates ();
long starttime=System.currentTimeMillis ();
switch(GameState)
{
//菜单选择
case Game_Menu:
if(((state & DOWN_PRESSED)!=0)||((state & UP_PRESSED)!=0))
{
if((int)(starttime-m_Menu.tempTime)>=200)
{
m_Menu.MoveUp ();
m_Menu.tempTime=System.currentTimeMillis ();
}
}
if((state & FIRE_PRESSED)!=0)
{
if((int)(starttime-m_Menu.tempTime)>=500)
{
if(m_Menu.GetDy ()==135)
{
this.InitGame ();//初始游戏数据
GameState=Game_Loop;
}
else
{
sm.exit ();
}
m_Menu.tempTime=System.currentTimeMillis ();
}
}
break;
//游戏运行
case Game_Loop:
if(m_PlayerShip.IsDead==false)
{
if((state & LEFT_PRESSED)!=0)
{
m_PlayerShip.MoveLeft ();//玩家向左移动
}
if((state & RIGHT_PRESSED)!=0)
{
m_PlayerShip.MoveRight ();//玩家右移
}
//发射炸弹
if((state & DOWN_PRESSED)!=0)
{
if((int)(starttime-tempTime)>=300)
{
m_PlayerShip.SendBomb (); //发射炸弹
tempTime=System.currentTimeMillis ();
}
}
if((state & FIRE_PRESSED)!=0)
{
if((int)(starttime-tempTime)>=300)
{
GameState=Game_Stop; //暂停
tempTime=System.currentTimeMillis ();
}
}
}
break;
case Game_Over:
if((state & FIRE_PRESSED)!=0)
{
if((int)(starttime-m_Menu.tempTime)>=500)
{
GameState=Game_Menu; //返回游戏菜单
m_Menu.tempTime=System.currentTimeMillis ();
}
}
break;
case Game_Stop:
if((state & FIRE_PRESSED)!=0)
{
if((int)(starttime-tempTime)>=300)
{
GameState=Game_Loop; //暂停
tempTime=System.currentTimeMillis ();
}
}
break;
default:
break;
}//end switch
}
//游戏画面
public void DrawScreen(Graphics g)
{
g.setColor (0,255,0);
g.fillRect (0,0,width,height);
switch(GameState)
{
case Game_Menu:
layermanager_menu.paint (g,0,0);
break;
case Game_Loop:
//code
layermanager.setViewWindow (m_PlayerShip.MapX,m_PlayerShip.MapY,width,height);
layermanager.paint (g,0,0);
//画界面文字信息
g.setColor (255,0,0);
g.drawString ("score:"+m_PlayerShip.Score,0,5,Graphics.LEFT|Graphics.TOP);//玩家得分
g.drawString ("hs:"+MaxScore+"(lv"+MaxScore/800+")",60,5,Graphics.LEFT|Graphics.TOP);//最高分
g.drawString ("life:"+m_PlayerShip.Life,150,5,Graphics.LEFT|Graphics.TOP);//玩家生命
break;
case Game_Over:
g.setColor (0,0,0);
g.fillRect (0,0,width,height);
g.setColor (255,255,255);
g.drawString ("GameOver",width/3,height/2,Graphics.LEFT|Graphics.TOP);
break;
case Game_Stop:
layermanager.setViewWindow (m_PlayerShip.MapX,m_PlayerShip.MapY,width,height);
layermanager.paint (g,0,0);
//画界面文字信息
g.setColor (255,0,0);
g.drawString ("score:"+m_PlayerShip.Score,0,5,Graphics.LEFT|Graphics.TOP);//玩家得分
g.drawString ("hs:"+MaxScore+"(lv"+MaxScore/800+")",60,5,Graphics.LEFT|Graphics.TOP);//最高分
g.drawString ("life:"+m_PlayerShip.Life,150,5,Graphics.LEFT|Graphics.TOP);//玩家生命
g.drawString ("暂停",width/2,height/2,Graphics.LEFT|Graphics.TOP);
break;
default:
break;
}
this.flushGraphics ();
}
//游戏运行
public void Action()
{
for(int i=0;i<3;i++)
{
if(m_PlayerShip.Bomb[i].GetShow ()==true)//玩家炸弹移动
{
m_PlayerShip.Bomb[i].Move ();
//检测是否命中敌人
for(int j=0;j<CpuNum;j++)
{
if(m_PlayerShip.Bomb[i].IsOver (m_CpuShip[j])==true)
{
m_PlayerShip.Score+=m_CpuShip[j].GetScore ();//加分
m_CpuShip[j].m_Bao.SetShow (true);//显示爆炸
m_CpuShip[j].m_Bao.SetPoint (m_CpuShip[j].dx,m_CpuShip[j].dy);
m_CpuShip[j].IsDead=true;
m_CpuShip[j].spt_cpuship.setVisible (false);
m_CpuShip[j].SetPoint (m_CpuShip[j].TypeId);//初始敌人位置
try
{
pbomb.start ();} catch (MediaException ex)
{sm.exit ();}
//超过最高分,增加敌人
if(m_PlayerShip.Score>MaxScore)
{
if(CpuNum<8)
{
MaxScore+=800;
CpuNum++;//敌人增加一个
}
}
}
}
}
}
for(int k=0;k<CpuNum;k++)
{
if(m_CpuShip[k].IsDead==false)
{
m_CpuShip[k].Move (m_PlayerShip);
}
else
{
m_CpuShip[k].IsDead=false;
}
if(m_CpuShip[k].Bomb.GetShow ()==true)//敌人移动炸弹
{
m_CpuShip[k].Bomb.Move ();
if(m_CpuShip[k].Bomb.IsOver (m_PlayerShip)==true)
{
m_PlayerShip.Life-=1;
m_PlayerShip.m_Bao.SetShow (true);
m_PlayerShip.m_Bao.SetPoint (m_PlayerShip.dx,m_PlayerShip.dy+7);
try
{
pbomb.start ();} catch (MediaException ex)
{sm.exit ();}
if(m_PlayerShip.Life<=0)
{
GameState=Game_Over;//游戏结束
}
}
}
//显示爆炸
m_CpuShip[k].m_Bao.ShowBao (System.currentTimeMillis ());
}//end for(k)
m_PlayerShip.m_Bao.ShowBao (System.currentTimeMillis ());
}
//初始游戏数据
public void InitGame()
{
m_PlayerShip.Life=3;
m_PlayerShip.Score=0;
//m_Play
MaxScore=800;
for(int i=0;i<CpuNum;i++)
{
m_CpuShip[i].IsDead=true;
m_CpuShip[i].SetPoint (m_CpuShip[i].TypeId);//初始敌人位置
m_CpuShip[i].spt_cpuship.setVisible (false);
m_CpuShip[i].Bomb.SetShow (false);
m_CpuShip[i].Bomb.spt_cpubomb.setVisible (false);
m_CpuShip[i].m_Bao.spt_bao.setVisible (false);//隐藏爆炸图
}
for(int j=0;j<3;j++)
{
m_PlayerShip.Bomb[j].SetShow (false);
m_PlayerShip.Bomb[j].spt_playerbomb.setVisible (false);
m_PlayerShip.m_Bao.spt_bao.setVisible (false);
}
CpuNum=5;
}
//关闭全部音乐
public void CloseSound()
{
try
{
pbomb.stop ();
psound.stop ();
} catch (MediaException ex)
{
sm.exit ();
}
pbomb.close ();
psound.close ();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -