/*
* FeijiMidlet.java
* Download by http://www.codefans.net
* Created on 2007年2月24日, 下午9:11
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author money
* @version
*/
public class FeijiMidlet extends MIDlet implements CommandListener{
private Canvas canvas=new FeijiCanvas();
private Display display;
private Command exit = new Command("退出", Command.EXIT,2);
private Command ok = new Command("开始游戏", Command.OK,1);
boolean kaishi=false;
TextBox textbox = new TextBox("飞行游戏!","伟哥出品!请单击右下角的开始游戏,如有问题请咨询QQ154267142", 1024, TextField.ANY);
public void startApp() {
display=Display.getDisplay(this);
display.setCurrent(textbox);
textbox.addCommand(exit);
textbox.addCommand(ok);
textbox.setCommandListener(this);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command cmd,Displayable displayable){
if(cmd==ok){
display=Display.getDisplay(this);
display.setCurrent(canvas);
canvas.addCommand(exit);
canvas.setCommandListener(this);
}
if(cmd==exit){
destroyApp(true);
notifyDestroyed();
}
}
}
class FeijiCanvas extends Canvas implements Runnable{
boolean gameover=false;
boolean xiancheng=true;
boolean up=false;
boolean down=false;
boolean left=false;
boolean right=false;
int feidan1x=getHeight()/4-8;
int feidan1y=getWidth();
int feidan2x=getHeight()/4+getHeight()/4-30;
int feidan2y=getWidth();
int feidan3x=getHeight()/4+getHeight()/4+getHeight()/4-50;
int feidan3y=0;
int feidan4x=getHeight()/4+getHeight()/4+getHeight()/4+getHeight()/4-60;
int feidan4y=0;
int x=10;
int y=getHeight()/2;
int life =3;
int fenshu=0;
Image ditu=null;
Image feidan1=null;
Image feidan2=null;
Image feidan3=null;
Image feidan4=null;
Image feiji=null;
public FeijiCanvas(){
try{
ditu = Image.createImage("/tiankong.png");
feidan1 = Image.createImage("/feidan1.PNG");
feidan2 = Image.createImage("/feidan2.PNG");
feidan3 = Image.createImage("/feidan3.PNG");
feidan4 = Image.createImage("/feidan4.PNG");
feiji = Image.createImage("/feiji.png");
}catch(Exception e){
System.err.println("图片错误");
}
Thread th = new Thread(this);//新建线程
th.start();//开启线程
}
protected void paint(Graphics g){
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());//绘制方格
g.setColor(0);
g.drawString("生命="+life,10,10,Graphics.LEFT|Graphics.TOP);
g.drawString("分数="+fenshu,50,10,Graphics.LEFT|Graphics.TOP);
if(gameover){
g.setColor(0);
g.drawString("你死了!你得了"+fenshu+"分,希望你继续努力!",10,getHeight()-20,Graphics.LEFT|Graphics.TOP);
xiancheng = false;
}
g.drawImage(ditu,getWidth()/2,getHeight()/2,Graphics.HCENTER|Graphics.VCENTER);//显示背景
g.drawImage(feidan1,feidan1x,feidan1y,Graphics.HCENTER|Graphics.VCENTER);
g.drawImage(feidan2,feidan2x,feidan2y,Graphics.HCENTER|Graphics.VCENTER);
g.drawImage(feidan3,feidan3x,feidan3y,Graphics.HCENTER|Graphics.VCENTER);
g.drawImage(feidan4,feidan4x,feidan4y,Graphics.HCENTER|Graphics.VCENTER);
g.drawImage(feiji,x,y,Graphics.HCENTER|Graphics.VCENTER);
}
public void run(){
while(xiancheng){
if(up)y=y-2;
if(down)y=y+2;
if(left)x=x-2;
if(right)x=x+2;
feidan1y=feidan1y-3;
feidan2y=feidan2y-5;
feidan3y=feidan3y+5;
feidan4y=feidan4y+7;
repaint();
try{Thread.sleep(50); }catch(InterruptedException e){}
if(feidan1y<=0)feidan1y=getWidth();
if(feidan2y<=0)feidan2y=getWidth();
if(feidan3y>getWidth())feidan3y=0;
if(feidan4y>getWidth())feidan4y=0;
if(x-feiji.getWidth()/2 < feidan1x + feidan1.getWidth()/2 && x+feiji.getWidth()/2 > feidan1x - feidan1.getWidth()/2){
if(y-feiji.getHeight()/2 < feidan1y + feidan1.getHeight()/2 && y+feiji.getHeight()/2 > feidan1y - feidan1.getHeight()/2){
//碰撞后实现代码
life=life-1;
x=10;
y=getHeight()/2;
}
}
if(x-feiji.getWidth()/2 < feidan2x + feidan2.getWidth()/2 && x+feiji.getWidth()/2 > feidan2x - feidan2.getWidth()/2){
if(y-feiji.getHeight()/2 < feidan2y + feidan2.getHeight()/2 && y+feiji.getHeight()/2 > feidan2y - feidan2.getHeight()/2){
//碰撞后实现代码
life=life-1;
x=10;
y=getHeight()/2;
}
}
if(x-feiji.getWidth()/2 < feidan3x + feidan3.getWidth()/2 && x+feiji.getWidth()/2 > feidan3x - feidan3.getWidth()/2){
if(y-feiji.getHeight()/2 < feidan3y + feidan3.getHeight()/2 && y+feiji.getHeight()/2 > feidan3y - feidan3.getHeight()/2){
//碰撞后实现代码
life=life-1;
x=10;
y=getHeight()/2;
}
}
if(x-feiji.getWidth()/2 < feidan4x + feidan4.getWidth()/2 && x+feiji.getWidth()/2 > feidan4x - feidan4.getWidth()/2){
if(y-feiji.getHeight()/2 < feidan4y + feidan4.getHeight()/2 && y+feiji.getHeight()/2 > feidan4y - feidan4.getHeight()/2){
//碰撞后实现代码
life=life-1;
x=10;
y=getHeight()/2;
}
}
if(life<=0){
gameover=true;
}
if(x==getWidth()){
fenshu=fenshu+10;
x=10;
y=getHeight()/2;
}
}
}
protected void keyPressed(int key) { //当键被按下使标志位为 TRUE 来改变坐标控制车运动
int gameaction = getGameAction(key);
switch(gameaction){
case Canvas.UP:
//y=y-3;
up = true;
break;
case Canvas.DOWN:
//y=y+3;
down = true;
break;
case Canvas.LEFT:
// x=x-3;
left = true;
break;
case Canvas.RIGHT:
// x=x+3;
right=true;
break;
default :
}
}
protected void keyReleased(int key) {//当键被松开使标志位为 FALSE 来让车停止
int gameaction = getGameAction(key);
switch(gameaction){
case Canvas.UP:
//y=y-3;
up = false;
break;
case Canvas.DOWN:
//y=y+3;
down = false;
break;
case Canvas.LEFT:
// x=x-3;
left = false;
break;
case Canvas.RIGHT:
// x=x+3;
right=false;
break;
default :
}
}
}