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

📄 demogamecanvas.java

📁 j2me的坦克大战 元代码 单机版和蓝牙联网版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.io.IOException;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.GameCanvas;import javax.microedition.lcdui.game.LayerManager;import javax.microedition.lcdui.game.Sprite;import javax.microedition.lcdui.game.TiledLayer;/** * @author wutianyi */public class DemoGameCanvas extends GameCanvas implements Runnable, CommandListener {    /**     * constructor     */    private Sprite tunk;    private Sprite tunk1;    private TiledLayer base;    private TiledLayer tree;    // private TiledLayer water;    private TiledLayer brutal;    private GameDesign2 gameDesign;    private Timer timer;    private LayerManager lm;    private static final int step = 3;//    private TileAnimationTask w;    private int wWater;    private int[] wWaterSequence;    private int lastState;    private boolean isPlay;    //private TankMidlet tan;    private Command cmdExit;    private boolean isServer;    private Connection2 conn;    private Vector bullets = new Vector();    private Vector tunkBullet = new Vector();    private Bullet bullet;    private int bullet_x,  bullet_y;    private Image img,  explode;    private int i;    private boolean is = true;    private boolean colleis = true;    private int firstPositionX,  firstPositionY;    private int firstPositionX1,  firstPositionY1;    private Sprite exploit;    private int lifeA = 3;    private int lifeB = 3;    private Sprite tunkHelp;    private Sprite tunk1Help;    private GameMidlet gameMidlet;    public DemoGameCanvas(boolean isServer, GameMidlet gameMidlet) {        super(true);        //this.tan = tan;        this.isServer = isServer;        this.gameMidlet = gameMidlet;        conn = new Connection2(isServer);        conn.start();        cmdExit = new Command("back", Command.BACK, 1);        this.addCommand(cmdExit);        this.setCommandListener(this);        try {            init();        } catch (IOException ex) {            ex.printStackTrace();        }        try {            img = Image.createImage("/ico7n.png");            explode = Image.createImage("/explode.png");        } catch (Exception e) {        }    }    public void start() {        isPlay = true;        Thread thread = new Thread(this);        thread.start();        Thread th = new Thread() {            public void run() {                boolean t = false;                                int bullet_x = 0, bullet_y = 0;                int laststate = 0;                while (isPlay) {                    if (isServer) {                        if (conn.getMessage3() == 0) {                            tunk1.setPosition(tunk1.getX(), tunk1.getY());                        } else {                            tunk1.setPosition(conn.getMessage1(), conn.getMessage2());                            switch (conn.getMessage3()) {                                case 1:                                    tunk1.setTransform(Sprite.TRANS_MIRROR_ROT90);                                    bullet_x = tunk1.getX();                                    bullet_y = tunk1.getY() + tunk1.getHeight() / 2;                                    laststate = LEFT_PRESSED;                                    break;                                case 2:                                    tunk1.setTransform(Sprite.TRANS_MIRROR_ROT270);                                    bullet_x = tunk1.getX() + tunk1.getWidth();                                    bullet_y = tunk1.getY() + tunk1.getHeight() / 2;                                    laststate = RIGHT_PRESSED;                                    break;                                case 3:                                    tunk1.setTransform(Sprite.TRANS_MIRROR_ROT180);                                    bullet_x = tunk1.getX() + tunk1.getWidth() / 2;                                    bullet_y = tunk1.getY();                                    laststate = UP_PRESSED;                                    break;                                case 4:                                    tunk1.setTransform(Sprite.TRANS_MIRROR);                                    bullet_x = tunk1.getX() + tunk1.getWidth() / 2;                                    bullet_y = tunk1.getY() + tunk1.getHeight();                                    laststate = DOWN_PRESSED;                                    break;                            }                                                      if (conn.getMessage4() != 0) {                                bullet = new Bullet(img);                                if (colleis) {                                    if (bullet != null) {                                        bullet.setPosition(bullet_x, bullet_y);                                        bullet.setDirection(laststate);                                        bullet.setCanvasSize(getWidth(), getHeight());                                        tunkBullet.addElement(bullet);                                    }                                    colleis = false;                                }                                conn.setMessage4(0);                            }                            conn.setMessage3(0);                        }                    } else {                        if (conn.getMessage3() == 0) {                            tunk.setPosition(tunk.getX(), tunk.getY());                        } else {                            tunk1.setPosition(conn.getMessage1(), conn.getMessage2());                            switch (conn.getMessage3()) {                                case 1:                                    tunk1.setTransform(Sprite.TRANS_MIRROR_ROT90);                                    bullet_x = tunk1.getX();                                    bullet_y = tunk1.getY() + tunk.getHeight() / 2;                                    laststate = LEFT_PRESSED;                                    break;                                case 2:                                    tunk1.setTransform(Sprite.TRANS_MIRROR_ROT270);                                    bullet_x = tunk1.getX() + tunk1.getWidth();                                    bullet_y = tunk1.getY() + tunk1.getHeight() / 2;                                    laststate = RIGHT_PRESSED;                                    break;                                case 3:                                    tunk1.setTransform(Sprite.TRANS_MIRROR_ROT180);                                    bullet_x = tunk1.getX() + tunk1.getWidth() / 2;                                    bullet_y = tunk1.getY();                                    laststate = UP_PRESSED;                                    break;                                case 4:                                    tunk1.setTransform(Sprite.TRANS_MIRROR);                                    bullet_x = tunk1.getX() + tunk1.getWidth() / 2;                                    bullet_y = tunk1.getY() + tunk1.getHeight();                                    laststate = DOWN_PRESSED;                                    break;                            }                            if (conn.getMessage3() != 0) {                                t = true;                                conn.setMessage3(0);                            }                            if (t) {                                conn.sentMessage2("" + 0, "" + 0, "" + 0, "" + 0);                                t = false;                            }                            if (conn.getMessage4() != 0) {                                bullet = new Bullet(img);                                if (colleis) {                                    if (bullet != null) {                                        bullet.setPosition(bullet_x, bullet_y);                                        bullet.setDirection(laststate);                                        bullet.setCanvasSize(getWidth(), getHeight());                                        tunkBullet.addElement(bullet);                                    }                                    colleis = false;                                }                                conn.setMessage4(0);                            }                        }                    }                }            }        };        th.start();    }    public void stop() {        isPlay = false;    }    private void init() throws IOException {        this.timer = new Timer();        this.gameDesign = new GameDesign2();        if (isServer) {            tunk = gameDesign.getTunk();            tunk1 = gameDesign.getTunk1();            firstPositionX = getWidth() / 2;            firstPositionY = tunk.getHeight();            firstPositionX1 = getWidth() / 2;            firstPositionY1 = getHeight() - tunk.getHeight();        } else {            tunk = gameDesign.getTunk1();            tunk1 = gameDesign.getTunk();            firstPositionX1 = getWidth() / 2;            firstPositionY1 = tunk.getHeight();            firstPositionX = getWidth() / 2;            firstPositionY = getHeight() - tunk1.getHeight();        }        this.tunk1.defineReferencePixel(this.tunk1.getWidth() / 2, this.tunk1.getHeight() / 2);        this.tunk.defineReferencePixel(this.tunk.getWidth() / 2, this.tunk.getHeight() / 2);        this.base = gameDesign.getBase();        this.tree = gameDesign.getTree();        this.brutal = gameDesign.getBrutal();        this.lm = new LayerManager();        gameDesign.updateLayerManagerForManger(lm);    }    public boolean spriteCollides(Sprite sprite) {        return tunk.collidesWith(tree, true) || tunk.collidesWith(brutal, true) || tunk.collidesWith(tunk1, is);    }    private class TileAnimationTask extends TimerTask {        private boolean moving = true;        private boolean forwared = true;        private TiledLayer tiledLayer;        private int animatedTiledIndex;        private int[] sequence;        private int sequenceIndex;        public TileAnimationTask(TiledLayer tiledLayer, int animatedTiledIndex, int[] sequence, boolean forwared) {            this.tiledLayer = tiledLayer;            this.animatedTiledIndex = animatedTiledIndex;            this.sequence = sequence;            this.forwared = forwared;        }        public void run() {            if (!this.moving) {                return;            }            if (forwared) {                if (++this.sequenceIndex >= this.sequence.length) {                    sequenceIndex = 0;                }            } else {                if (--this.sequenceIndex < 0) {                    sequenceIndex = this.sequence.length - 1;                }            }            this.tiledLayer.setAnimatedTile(this.animatedTiledIndex, this.sequence[sequenceIndex]);        }        public void forwared() {            this.forwared = true;            this.moving = true;        }        public void backwared() {            this.forwared = false;            this.moving = true;        }        public void setMoving(boolean isMoving) {            this.moving = isMoving;        }    }    public void run() {        Graphics g = getGraphics();        int timeStep = 100;        while (isPlay) {            try {

⌨️ 快捷键说明

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