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

📄 monstercanvas.java

📁 以传奇中的人物和怪物作为角色
💻 JAVA
字号:
package com.liuwan.j2me.monster;

import java.io.*;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 * <p>Title: Develop game research</p>
 *
 * <p>Description: Develop game research</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Liuwan studio</p>
 *
 * @author Liuwan
 * @version 1.0
 */
public class MonsterCanvas extends GameCanvas implements CommandListener,Runnable {
    private int imageIndex;
    private int enemyImgIndex;

    private boolean running = true;
    private Monster monster;
    private Enemy enemy;
    private Human human;
    private int monsterX;
    private int monsterY;
    private int humanX;
    private int humanY;
    private int enemyX;
    private int enemyY;
    private Image imgLife;
    private boolean colliding = false; // 显示sprite是否相撞的 flag
    public MonsterCanvas() throws IOException{
        super(true);
        initMonster();
        //initEnemy();
        initHuman();
    }
    public void initMonster() {
        try {
            Image monsterImg = Image.createImage(
                    "/com/liuwan/j2me/monster/images/monster.png");
            monster = new Monster(monsterImg, 40, 59);
            monsterX = getWidth() / 2;
            monsterY = getHeight() / 2;
            monster.setPosition(monsterX,monsterY);
            imgLife = Image.createImage("/com/liuwan/j2me/monster/images/life.png");
        } catch (Exception ex) {

        }

    }
    public void initHuman() {
        try {
            Image humanImg = Image.createImage(
                    "/com/liuwan/j2me/monster/images/human.png");
            human = new Human(humanImg, 44, 55);
            humanX = getWidth() / 4;
            humanY = getHeight() / 4;
            human.setPosition(humanX,humanY);
            human.setGraphics(getGraphics());
            human.setGameCanvas(this);
            human.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
    public void initEnemy() {
        try {
            Image enemyImg = Image.createImage(
                    "/com/liuwan/j2me/monster/images/enemy.png");

            enemy = new Enemy(enemyImg,49,50);
            enemyX = getWidth() / 3;
            enemyY = getHeight() / 3;
            enemy.setPosition(enemyX,enemyY);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

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

    public void start(){
        Thread t = new Thread(this);
        t.start();
    }


   public void run() {
       Graphics g = getGraphics();
       draw(g);
       while (running) {

// 每经过一定时间的处理

// 取得按键的状态
           int keyStates = getKeyStates();
// 根据按键状态变更移动方向

           if ((keyStates & this.FIRE_PRESSED) != 0) {
              beat();
           }
           int step = 3;
           if ((keyStates & LEFT_PRESSED) != 0) {
               walk(-step, 0);
           }
           if ((keyStates & RIGHT_PRESSED) != 0) {
               walk(step, 0);
           }
           if ((keyStates & UP_PRESSED) != 0) {
               walk(0, -step);
           }
           if ((keyStates & DOWN_PRESSED) != 0) {
               walk(0, step);
           }
           //设定Enemy向Monster行走
           //enemyWalk();
           try {
               Thread.sleep(100);
           } catch (InterruptedException e) {

           }
        }

   }

    /**
     * 每经过一定时间的处理
     */
    private void tick() {
        imageIndex = (imageIndex + 1) % 6;
        monster.setFrame(imageIndex);
        monsterX += 5;
        if (monsterX > getWidth()) {
            monsterX = 0;
        }
        monster.setPosition(monsterX,getHeight() / 2);



    }

    /**
     * 描绘的更新
     */
    public void draw(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        drawLife(g);
        monster.paint(g);
        //enemy.paint(g);
        human.paint(g);
        //paint(g);
        flushGraphics();
    }

    private void drawLife(Graphics g){
        //描绘图片左边的弧形 x=0,y=0,width=3,height=8,左上角开始描绘
        drawClip(g, 0, 5, imgLife, 0, 0, 3, 8, Graphics.LEFT | Graphics.TOP);
        //描绘图片右边的弧形 x=4,y=0,width=3,height=8,左上角开始描绘
        drawClip(g, 3 + (monster.getLife() - 1)*2, 5, imgLife, 4, 0, 3, 8, Graphics.LEFT | Graphics.TOP);
        //循环描绘图片中间的点 x=3,y=0,width=1,height=8
        for (int i = 0; i < (monster.getLife() - 1) * 2; i++) {
            drawClip(g, 3 + i, 5, imgLife, 3, 0, 1, 8, Graphics.LEFT | Graphics.TOP);
        }
    }


    /**
     * drawClip 画一个图片的一部分。clip窗口可使用anchor。
     *
     * @param g
     *            Graphics
     * @param x
     *            int 要画的位置x
     * @param y
     *            int 要画的位置y
     * @param image
     *            Image 图片
     * @param clipX
     *            int clip在图片中的坐标x
     * @param clipY
     *            int clip在图片中的坐标y
     * @param clipWidth
     *            int clip在图片中的宽度
     * @param clipHeight
     *            int clip在图片中的高度
     * @param anchor
     *            int 链接锚点
     */
    public static void drawClip(Graphics g, int x, int y, Image image,
            int clipX, int clipY, int clipWidth, int clipHeight, int anchor) {
        int[] old = {
                g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight()
        };
        switch (anchor) {
        case Graphics.LEFT | Graphics.TOP:
            g.setClip(x, y, clipWidth, clipHeight);
            g.drawImage(image, x - clipX, y - clipY, Graphics.LEFT
                    | Graphics.TOP);
            break;
        case Graphics.HCENTER | Graphics.TOP:
            g.setClip(x - clipWidth / 2, y, clipWidth, clipHeight);
            g.drawImage(image, x - clipX - clipWidth / 2, y - clipY,
                    Graphics.LEFT | Graphics.TOP);
            break;
        case Graphics.RIGHT | Graphics.TOP:
            g.setClip(x - clipWidth, y, clipWidth, clipHeight);
            g.drawImage(image, x - clipX - clipWidth, y - clipY, Graphics.LEFT
                    | Graphics.TOP);
            break;
        case Graphics.LEFT | Graphics.VCENTER:
            g.setClip(x, y - clipHeight / 2, clipWidth, clipHeight);
            g.drawImage(image, x - clipX, y - clipY - clipHeight / 2,
                    Graphics.LEFT | Graphics.TOP);
            break;
        case Graphics.HCENTER | Graphics.VCENTER:
            g.setClip(x - clipWidth / 2, y - clipHeight / 2, clipWidth,
                    clipHeight);
            g.drawImage(image, x - clipX - clipWidth / 2, y - clipY
                    - clipHeight / 2, Graphics.LEFT | Graphics.TOP);
            break;
        case Graphics.RIGHT | Graphics.VCENTER:
            g.setClip(x - clipWidth, y - clipHeight / 2, clipWidth, clipHeight);
            g.drawImage(image, x - clipX - clipWidth, y - clipY - clipHeight
                    / 2, Graphics.LEFT | Graphics.TOP);
            break;
        case Graphics.LEFT | Graphics.BOTTOM:
            g.setClip(x, y - clipHeight, clipWidth, clipHeight);
            g.drawImage(image, x - clipX, y - clipY - clipHeight, Graphics.LEFT
                    | Graphics.TOP);
            break;
        case Graphics.HCENTER | Graphics.BOTTOM:
            g.setClip(x - clipWidth / 2, y - clipHeight, clipWidth, clipHeight);
            g.drawImage(image, x - clipX - clipWidth / 2, y - clipY
                    - clipHeight, Graphics.LEFT | Graphics.TOP);
            break;
        case Graphics.RIGHT | Graphics.BOTTOM:
            g.setClip(x - clipWidth, y - clipHeight, clipWidth, clipHeight);
            g.drawImage(image, x - clipX - clipWidth, y - clipY - clipHeight,
                    Graphics.LEFT | Graphics.TOP);
            break;
        }
        g.setClip(old[0], old[1], old[2], old[3]);
    }

    public void beat(){
        while (imageIndex < 6) {
            monster.setFrame(imageIndex);
            monster.setPosition(monsterX, monsterY);
            draw(getGraphics());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {

            }
            imageIndex++;
        }


        imageIndex = 0;
    }


    public void walk(int x,int y){
        int walkIndex = 0;

        monster.setFrame(walkIndex);
        monsterX = monsterX + x;
        monsterY = monsterY + y;
        if(monsterX > 128){
            monsterX = 128;
        }
        if(monsterX < 0){
            monsterX = 0;
        }
        if (monsterY > 128) {
            monsterY = 128;
        }
        if (monsterY < 0) {
            monsterY = 0;
        }
        monster.setPosition(monsterX, monsterY);
        if (monster.collidesWith(human, true)) {
            human.setOnBeat(true);
            colliding = true;
        }else{
            human.setOnBeat(false);
        }
        draw(getGraphics());
    }

    public void enemyWalk(){
        int x = 0;
        int y = 0;

        if(enemy.collidesWith(monster,true)){
            colliding = true;
        }else{
            colliding = false;
            if (enemy.getX() > monster.getX()) {
                x = enemy.getX() - 1;
            } else {
                x = enemy.getX() + 1;
            }
            if (enemy.getY() > monster.getY()) {
                y = enemy.getY() - 1;
            } else {
                y = enemy.getY() + 1;
            }
            enemy.setPosition(x,y);
        }
        draw(getGraphics());
    }

    public Monster getMonster() {
        return monster;
    }

}

⌨️ 快捷键说明

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