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

📄 maincanvas.java~2~

📁 太空大战J2ME版本
💻 JAVA~2~
字号:
package game0925;

import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import java.util.Random;
import javax.microedition.media.*;
import java.io.*;

/**
 * <p>Title:太空生存 </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MainCanvas extends Canvas implements CommandListener, Runnable {

    Command ok;
    protected static MainCanvas self;
    private Random rnd = new Random();
    protected int score;
    protected static int D_SCORE = 5;
    protected int alive = 100;
    protected static int DEF_A = 1;

    //******************************************************
//     protected Vector ufomissV = new Vector(); //敌机子弹
//    protected int ufomissCount = 0; //敌机子弹数量

     protected Vector mymissV = new Vector(); //自机子弹

    protected int mymissCount = 0; //自机子弹数量

    protected UFOMiss[] ufomiss = new UFOMiss[] {
                                  new UFOMiss(),
                                  new UFOMiss(),
                                  new UFOMiss(),
                                  new UFOMiss(),
                                  new UFOMiss()
    };

    protected Vector ufoV = new Vector(); //UFO

    protected int ufoCount = 0; //敌机数量
    //*****************************************************
     Thread thread;
    public int gameAction;
    public boolean keyEvent;
    private Timer timer = new Timer();
    public Task task;
    protected PlayTask play = new PlayTask();
    protected int gamestate;
    protected static final int GAME_TITLE = 0;
    protected static final int GAME_PLAYING = 1;
    protected static final int GAME_END = 2;
    protected static final int GAME_PAUSE = 3;
    private int screenwidth;
    private int screenheight;
    private Image background;
    private Image off;
    private MyShip myship = new MyShip(); //自机
//    List l = new List(" ", 1);
    public MainCanvas() {
        self = this;
        ok = new Command("重新开始", Command.OK, 1);
//        this.addCommand(ok);
        //   this.setCommandListener(this);


        task = new Task(this);
        if (background == null) {
            background = ImageTools.getImage("/img/backg.PNG");
        }
        if (!this.isDoubleBuffered()) {
            off = Image.createImage(getWidth(), getHeight());
        }
        screenwidth = this.getWidth();
        screenheight = this.getHeight();

        gameInit();
    }

    public void keyPressed(int keycode) {
        gameAction = getGameAction(keycode);
        keyEvent = true;
    }

    public void keyReleased(int keycode) {
        keyEvent = false;
    }

    public void commandAction(Command command, Displayable displayable) {
        /** @todo Add command handling code */
        if (command.getCommandType() == Command.EXIT) {
            // stop the MIDlet
            MaiMIDlet.quitApp();
        } else if (command == ok) {
            gameInit();
        }
    }

    void gameInit() {
        if (thread == null) {
            thread = new Thread(this);
        }
        myship.isAlive = true;
        myship.setX(screenwidth / 2);
        myship.setY(screenheight - myship.getHeight() - 10);
        gamestate = GAME_PLAYING;
        thread.start();
        score = 0;
        alive = 100;
    }

    protected void paint(Graphics g) {
        Graphics old = g;
        if (off != null) {
            g = off.getGraphics();
        }
        g.drawImage(background, screenwidth / 2, screenheight / 2,
                    Graphics.HCENTER | Graphics.VCENTER);
        switch (gamestate) {
        case GAME_TITLE:
            g.setColor(0xffffff);
            g.drawString("Chao copyright 2005", 10, 5,
                         Graphics.TOP | Graphics.LEFT);
            break;
        case GAME_PLAYING:

            //************************显示部分**********************
             myship.paint(g);

            for (int i = 0; i < mymissV.size(); i++) { //自机子弹
                MyMiss miss = (MyMiss) mymissV.elementAt(i);
                miss.paint(g);
            }

            for (int i = 0; i < ufoV.size(); i++) { //UFO
                UFO ufo = (UFO) ufoV.elementAt(i);
                ufo.paint(g);
            }
            for (int i = 0; i < ufomiss.length; i++) { //ufo 子弹
                ufomiss[i].paint(g);
            }

            //********************************************************

             //---------积分显示--------------------------
             g.setColor(0x00ff0000);
            g.drawString("S:" + score, this.getWidth() - 80, 5,
                         Graphics.TOP | Graphics.LEFT);
            g.drawString("L:" + alive, 5, this.getHeight() - 20,
                         Graphics.TOP | Graphics.LEFT);

            //----------------------------------------
            break;
        case GAME_END:
            g.setColor(0xffffff);
            g.drawString("Chao.com copyright 2005", this.getWidth() - 150,
                         this.getHeight() - 40, Graphics.TOP | Graphics.LEFT);
            g.drawString("G A M E   O V E R !", this.getWidth() / 2 - 35,
                         this.getHeight() / 2, Graphics.TOP | Graphics.LEFT);
            break;
        }
        if (g != old) {
            g.drawImage(off, 0, 0, 20);
        }
    }

    public void showNotify() {
        timer.schedule(task, 0);
//        timer.schedule(play, 0, 40);
    }

    void addUFO() { //加入敌机
        UFO ufo;
        int _temp = rnd.nextInt() % 2;
        int x_s;
        int _x = this.getWidth() - 50;
        int _y;
        int _speed = rnd.nextInt(30);
        if (_temp == 1 | _temp == -1) {
            _temp = rnd.nextInt() % 2;
        }
        if (_temp < 0) {
            _temp = ( -1) * _temp;
        }
        x_s = _temp * 2 + 12;
        _y = 16 * _temp;
        _x = _x - _temp * 2;
        if (_temp == 0) {
            ufoV.addElement(new UFO(x_s, _y, UFO.DIR_LEFT, true, _speed));
        } else {
            ufoV.addElement(new UFO(_x, _y, UFO.DIR_RIGHT, true, _speed));
        }
    }

    public void keyR() { //飞机按键事件
        switch (gameAction) {
        case Canvas.UP:
            if (myship.getY() > 0) {
                myship.y -= 5;
            }
            break;
        case Canvas.DOWN:
            if (myship.getY() < screenheight - myship.getHeight() - 10) {
                myship.y += 5;
            }
            break;
        case Canvas.LEFT:
            if (myship.getX() > 0) {
                myship.x -= 5;
            }
            break;
        case Canvas.RIGHT:
            if (myship.getX() < screenwidth - myship.getWidth() - 1) {
                myship.x += 5;
            }
            break;
        }
        repaint();
    }

    public void run() {
        int temp;
        int speed;
        switch (gamestate) {
        case GAME_TITLE:
            try {
                Thread.sleep(1500);
                gamestate = GAME_PLAYING;
                repaint();
            } catch (Exception e) {}

            break
                    ;
        case GAME_PLAYING:
            repaint();
            while (thread != null) {

                if (alive <= 0) {
                    gamestate = GAME_END;
                }
                for (int i = 0; i < mymissV.size(); i++) { //处理自机子弹
                    MyMiss miss = (MyMiss) mymissV.elementAt(i);
                    if (miss.isAlive() && miss.getY() + miss.getHeight() > 0) {
                        miss.doMove();
                        for (int j = 0; j < ufoV.size(); j++) {
                            UFO ufo = (UFO) ufoV.elementAt(j);
                            if (ufo.isAlive()) {
                                if (miss.isOver(ufo)) {
                                    score = score + D_SCORE;
                                    ufo.setAlive(false);
                                    ufoV.removeElement(ufo);
                                    mymissV.removeElement(miss);
                                    break;
                                }
                            }
                        }

                    } else if (miss.isAlive() &&
                               miss.getY() + miss.getHeight() < 0) {

                        if (miss.getY() + miss.getHeight() < 0) {
                            mymissV.removeElement(miss);
                        }
                    }
                }

                for (int i = 0; i < ufoV.size(); i++) { //处理UFO
                    UFO ufo = (UFO) ufoV.elementAt(i);
                    if (ufo.isAlive()) {
                        if (ufo.getX() + ufo.getWidth() > 0 &&
                            ufo.getX() < this.getWidth()) {
                            ufo.doMove();
                        } else {
                            ufo.setHit(true);
                            ufo.setAlive(false);
                            ufoV.removeElement(ufo);
                            break;
                        }
                    }

                    if (isCenter(ufo) && ufo.isAlive()) { //如果处于最佳攻击位置则发射子弹
                        for (int j = 0; j < ufomiss.length; j++) { //激活子弹
                            if (!ufomiss[j].isAlive) {
                                ufomiss[j].setX(ufo.getX() + ufo.getWidth() / 2);
                                ufomiss[j].setY(ufo.getY());
                                ufomiss[j].setAlive(true);
                                break;
                            }
                        }
                    }

                }

                temp = rnd.nextInt(30);
                speed = rnd.nextInt(5);

                for (int i = 0; i < ufomiss.length; i++) {
                    if (ufomiss[i].getY() > this.getHeight() - 10) {
                        ufomiss[i].setAlive(false);
                        ufomiss[i].setHit(false);
                    }
                    if (ufomiss[i].isAlive()) {
                        ufomiss[i].doMove();
                    }
                    if (ufomiss[i].isOver(myship) && ufomiss[i].isAlive()) {
                        alive = alive - DEF_A;
                        ufomiss[i].setAlive(false);

                        break;
                    }
                }
                if (mymissV.size() < 5) {
                    mymissV.addElement(new MyMiss(myship.getX() +
                                                  myship.getWidth() / 2 - 2,
                                                  myship.getY() - 6, true));
                }
                if (ufoV.size() < 5) {
                    addUFO(); //加入敌机
                }
                try {
                    Thread.sleep(40);
                } catch (Exception e) {}
                repaint();
            }
            break;
        case GAME_END:
            repaint();
            try {
                thread = null;
                Thread.sleep(5000);

            } catch (Exception e) {}
            break
                    ;

        }
    }

    boolean isCenter(UFO ufo) { //判定敌机是否处于最佳攻击位置
        if (ufo.getX() + ufo.getWidth() / 2 >= myship.getX() - ufo.getWidth() &&
            ufo.getX() + ufo.getWidth() <=
            myship.getX() + myship.getWidth() * 2
                ) {
            return true;
        }
        return false;
    }
}


class PlayTask extends TimerTask {
    Player p;
//    MainCanvas my;
//    public PlayTask(MainCanvas my) {
//        this.my = my;
//    }

    public void run() {
        try {
            InputStream is = getClass().getResourceAsStream(
                    "/music/tightspot.mid");
            p = Manager.createPlayer(is, "audio/midi");
            p.realize();
            p.start();
        } catch (Exception e) {}
    }
}


class Task extends TimerTask { //模拟连续按键
    MainCanvas my;
    public Task(MainCanvas my) {
        this.my = my;
    }

    public void run() {
        while (true) {
            while (my.keyEvent) {
                try {
                    my.keyR();
                    Thread.sleep(40);
                } catch (Exception e) {}
                my.repaint();
            }
        }
    }
}

⌨️ 快捷键说明

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