⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fightcanvas.java~21~

📁 追踪子弹的小程序
💻 JAVA~21~
字号:
package aibullet;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
/*public class FightCanvas {
    public FightCanvas() {
    }
}*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.IOException;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author Java_bin
 * @version 1.0
 */
public class FightCanvas extends GameCanvas implements Runnable ,CommandListener
{
    boolean running;
    boolean isHit;
    int win_width=getWidth();
    int win_height=getHeight();
    //飞机的一些属性
    Image plane_img;
    Plane plane_sp;
    int img_plane_size=24;
    //爆炸的动画
    Image bomb_img;
    Sprite bomb_sp;
    int img_bomb_size=65;

    //子弹的属性
    Image bullet_img;
    int bullet_size=6;
    AI_Bullet b;
    //背景
    Image cloud_img;
    Image back_img;
    //引用MID类的一些方法
    AI_MIDlet fMIDlet;

    Graphics g;

    public FightCanvas(AI_MIDlet fMIDlet)
    {
        super(true);
        this.fMIDlet=fMIDlet;
        g=getGraphics();
        Command cmdExit=new Command("退出",Command.EXIT,0);
        addCommand(cmdExit);
        setCommandListener(this);
        try
        {
            plane_img=Image.createImage("/res/plane.png");
            bomb_img=Image.createImage("/res/bomb.png");
            cloud_img=Image.createImage("/res/cloud.png");
            back_img=Image.createImage("/res/cloudbackground.png");
            bullet_img=Image.createImage("/res/bullet.png");
        }
        catch(IOException e)
        {
            System.out.print(e.getMessage());
        }
        //爆炸
        bomb_sp=new Sprite(bomb_img,img_bomb_size,img_bomb_size);
        bomb_sp.setFrame(0) ;
        //生成子弹对象
        b=new AI_Bullet(bullet_img,bullet_size,bullet_size) ;
     //   b.setPosition(50,50);
     //   b.setFrame(0);
        //生成飞机对象
        plane_sp = new Plane(plane_img, img_plane_size, img_plane_size);
        plane_sp.setFrame(0);
        //飞机初始位置
        int x = (win_width - img_plane_size) / 2;
        int y = (win_height - img_plane_size) / 2;
        plane_sp.setPosition(x, y);
         b.setPosition(20,10);
        b.setBulletPos(20,10);
        start();
    }
     public void  start()
    {
        running=true;
        Thread t=new Thread(this);
        t.start();
    }
    public void commandAction(Command command, Displayable displayable)
    {
        if(command.getCommandType()==Command.EXIT )
            fMIDlet.quitApp();
    }

    public void run()
    {
        long st=0,et=0,diff=0;
        int rate=50;//刷新频率
        while(running)
        {
            st=System.currentTimeMillis();
            GameKeyInput();
            render();
            et=System.currentTimeMillis();
            diff=st-et;
            if(diff<rate)
            {
                try
                {
                    Thread.sleep(rate-diff);
                }
                catch(InterruptedException ex)
                {
                    stop();
                }
            }
        }
    }
    public void GameKeyInput()
    {

       // curTime=System.currentTimeMillis();//控制子弹的发射频率
        plane_sp.setFrame(0);
        int KeyState=getKeyStates();
        if((KeyState & UP_PRESSED)!=0)
        {
            if(plane_sp.getY()-3<0)
                plane_sp.move(0,-plane_sp.getY());
            else
                plane_sp.move(0,-3);
            plane_sp.setFrame(0);
        }
        if((KeyState & DOWN_PRESSED)!=0)
        {
            if(plane_sp.getY()+3+plane_sp.getHeight() >win_height)
                plane_sp.move(0,win_height-plane_sp.getY()-plane_sp.getHeight());
            else
                plane_sp.move(0,3);
             plane_sp.setFrame(0);
        }
        if((KeyState & LEFT_PRESSED )!=0)
        {
            if(plane_sp.getX()-3<0)
                plane_sp.move(-plane_sp.getX(),0);
            else
                plane_sp.move(-3,0);
             plane_sp.setFrame(1);
        }
        if((KeyState & RIGHT_PRESSED)!=0)
        {
            if(plane_sp.getX()+img_plane_size+3>win_width)
                plane_sp.move(win_width-plane_sp.getX()-img_plane_size,0);
            else
                plane_sp.move(3,0);
             plane_sp.setFrame(2);
        }
        if((KeyState&FIRE_PRESSED)!=0)
        {
           // if(curTime-lastTime>=100)
           // {

           //     plane_sp.Fire();
           //     lastTime=curTime;
           // }
          // b.getTargetPos(plane_sp);

        }
    }
    private void render()
    {
        g.setColor(0xffffff);
        g.fillRect(0,0,win_width,win_height);
        g.drawImage(back_img,0,0,0);
        g.drawImage(cloud_img,20,20,0);
        g.drawImage(cloud_img,90,50,0);
        g.drawImage(cloud_img,30,120,0);
        g.drawImage(cloud_img,120,110,0);

        isHit=b.collidesWith(plane_sp,true);
        if(isHit)
        {
            bomb_sp.setPosition(plane_sp.getX()-(img_bomb_size-img_plane_size)/2,plane_sp.getY()-(img_bomb_size-img_plane_size)/2);
            bomb_sp.nextFrame();
            bomb_sp.paint(g);
        }
        else
        {
            b.AutoSearchTrg(plane_sp);
            b.paint(g);
            plane_sp.paint(g);
        }
        flushGraphics();
    }
    public void stop()
    {
        running=false;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -