Java ME手机应用开发大全一书的配套光盘上的源码

源代码在线查看: boatgamecanvas.java

软件大小: 4439 K
上传用户: zhiver
关键词: Java 手机 应用开发 光盘
下载地址: 免注册下载 普通下载 VIP

相关代码

				package boat;
				
				import javax.microedition.lcdui.*;
				
				/**
				 * 游戏界面
				 */
				public class BoatGameCanvas extends Canvas implements Runnable {
				    /**背景图片*/
				    private Image bgImage;
				    /**军舰图片数组,下标0代表向右,1代表向左*/
				    private Image boatImage[] = new Image[2];
				    /**军舰对象*/
				    Boat boat;
				    /**潜艇对象数组*/
				    Submarine[] submarine = new Submarine[2];
				    /**第一组潜艇图片*/
				    private Image[] sImage1 = new Image[2];
				    /**第二组潜艇图片*/
				    private Image[] sImage2 = new Image[2];
				    /**爆炸图片*/
				    private Image[] boomImage = new Image[3];
				    /**炸弹图片*/
				    private Image bombImage;
				    /**炸弹对象*/
				    private Bomb bomb;
				
				    //双缓冲
				    /**次画面*/
				    private Image offScreen;
				    /**次画笔*/
				    private Graphics offG;
				    /**屏幕宽度*/
				    private int width;
				    /**屏幕高度*/
				    private int height;
				    /**是否运行*/
				    private boolean isRunning = true;
				    /**是否连续移动*/
				    private boolean isRepeated = false;
				    /**军舰移动方向*/
				    private int boatDirection;
				    /**游戏得分*/
				    private int score = 0;
				    public BoatGameCanvas() {
				        try {
				            //导入背景图片
				            bgImage = Image.createImage("/res/bac.png");
				            //导入军舰图片
				            boatImage[0] = Image.createImage("/res/ship0.png");
				            boatImage[1] = Image.createImage("/res/ship1.png");
				            //导入炸弹图片
				            bombImage = Image.createImage("/res/boom.png");
				            //导入潜艇图片
				            sImage1[0] = Image.createImage("/res/q1.png");
				            sImage1[1] = Image.createImage("/res/h2.png");
				
				            sImage2[0] = Image.createImage("/res/r1.png");
				            sImage2[1] = Image.createImage("/res/q2.png");
				            //导入炸弹图片
				            boomImage[0] = Image.createImage("/res/b.png");
				            boomImage[1] = Image.createImage("/res/f1.png");
				            boomImage[2] = Image.createImage("/res/b2.png");
				
				        } catch (Exception e) {
				            e.printStackTrace();
				        }
				        //获得屏幕宽度和高度
				        width = this.getWidth();
				        height = this.getHeight();
				        //创建次画面和次画笔
				        offScreen = Image.createImage(width, height);
				        offG = offScreen.getGraphics();
				
				        //创建军舰对象
				        boat = new Boat(boatImage, width);
				        //创建潜艇对象
				        init(0);
				        init(1);
				        //创建炸弹对象
				        bomb = new Bomb(bombImage, 0, 0, height);
				
				        //创建线程
				        Thread t = new Thread(this);
				        //启动线程
				        t.start();
				    }
				
				    private void init(int num) {
				        if (num == 0) {
				            submarine[0] = new Submarine(sImage1, width, height, 0, boomImage);
				        }
				        if (num == 1) {
				            submarine[1] = new Submarine(sImage2, width, height, 1, boomImage);
				        }
				    }
				
				    /**
				     * 绘制方法
				     * @param g 系统画笔
				     */
				    protected void paint(Graphics g) {
				        //清屏
				        offG.setColor(0xffffff);
				        offG.fillRect(0, 0, width, height);
				        offG.setColor(0);
				
				        //绘制背景图片
				        offG.drawImage(bgImage, 0, 0, Graphics.TOP | Graphics.LEFT);
				
				        //绘制军舰
				        boat.paint(offG);
				        //绘制潜艇
				        submarine[0].paint(offG);
				        submarine[1].paint(offG);
				        //绘制炸弹
				        if (bomb.isDisplay) {
				            bomb.paint(offG);
				        }
				        //绘制得分
				        offG.drawString("得分:" + score,4,4,Graphics.TOP | Graphics.LEFT);
				
				        //将次画面贴到主画面
				        g.drawImage(offScreen, 0, 0, Graphics.TOP | Graphics.LEFT);
				    }
				
				    public void keyPressed(int keyCode) {
				        //转换为游戏动作
				        int action = this.getGameAction(keyCode);
				
				        switch (action) {
				        case Canvas.LEFT: //向左移动军舰
				            isRepeated = true;
				            boatDirection = Boat.BOAT_MOVE_LEFT;
				            break;
				        case Canvas.RIGHT:
				            isRepeated = true;
				            boatDirection = Boat.BOAT_MOVE_RIGHT;
				            break;
				        case Canvas.FIRE:
				
				            //判断是否可以发射
				            if (!bomb.isDisplay) {
				                bomb.isDisplay = true;
				                //设置坐标
				                bomb.setX(boat.x + boat.getBoatImageWidth() / 2 -
				                          bomb.getImageWidth() / 2);
				                bomb.setY(boat.Y + boat.getBoatImageHeight() -
				                          bomb.getImageHeight());
				            }
				            break;
				        }
				        //重新绘制
				        repaint();
				    }
				
				    public void keyReleased(int keyCode) {
				        //转换为游戏动作
				        int action = this.getGameAction(keyCode);
				        switch (action) {
				        case Canvas.LEFT: //向左移动军舰
				            isRepeated = false;
				            break;
				        case Canvas.RIGHT:
				            isRepeated = false;
				            break;
				        }
				    }
				
				    /**
				     * 运行方法
				     */
				    public void run() {
				        while (isRunning) {
				            //暂停
				            try {
				                Thread.sleep(100);
				            } catch (Exception e) {}
				            //移动军舰
				            if (isRepeated) {
				                boat.move(boatDirection);
				            }
				            //未爆炸则移动潜艇
				            if (!submarine[0].isIsBoom()) {
				                submarine[0].move();
				            } else { //播放爆炸效果
				                submarine[0].boom();
				            }
				            if (!submarine[1].isIsBoom()) {
				                submarine[1].move();
				            } else { //播放爆炸效果
				                submarine[1].boom();
				            }
				            //移动炸弹
				            if (bomb.isDisplay) {
				                bomb.move();
				                //判别是否碰撞
				                if (bomb.collidesWith(submarine[0])) {
				                    bomb.handlecollidesWith(submarine[0]);
				                    //分数增加
				                    score += 10;
				                }
				                if (bomb.collidesWith(submarine[1])) {
				                    bomb.handlecollidesWith(submarine[1]);
				                    //分数增加
				                    score += 10;
				                }
				
				            }
				
				            //垃圾回收
				            System.gc();
				
				            //重绘
				            repaint();
				        }
				    }
				}
							

相关资源