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

📄 hero.java~30~

📁 这个是最早期的一个飞行射击游戏 简单实现了地图滚动 子弹碰撞 与 UI等
💻 JAVA~30~
字号:
package newgame;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Hero extends Base {

    int speedx, speedy; //速度
    Image img_hero; //玩家
    Image img_shadow;
    int frameWidth, frameHeight;

    boolean visble; //可见不可见
    int x, y; //坐标
    static int type; //类型
    Canvas1 mc;
    Tools tool;
    final int hero1 = 0;
    final int hero2 = 1;
    final int hero3 = 2;
    /**   switch(type){
          case hero1:break;
          case hero2:break;
          case hero3:break;
          }*/
    int frameCnt; //飛機同意張圖片序號(一般共5張)
    public Hero(Canvas1 mc, int type) {
        tool = new Tools(this.mc);
        this.mc = mc;
        this.type = type;
        frameCnt = 2;//(默認顯示中間即第二張)

        switch (type) {
        case hero1:
            img_hero = tool.getImage("/hero1.png");
            img_shadow = tool.getImage("/hero1s.png");
            this.setPosition(mc.SCx / 2, mc.SCy * 3 / 4);
            break;
        case hero2:
            img_hero = tool.getImage("/hero3.png");
            img_shadow = tool.getImage("/hero3s.png");
            this.setPosition(mc.SCx / 2, mc.SCy * 3 / 4);
            break;
        case hero3:
            img_hero = tool.getImage("/hero2.png");
            img_shadow = tool.getImage("/hero2s.png");
            this.setPosition(mc.SCx / 2, mc.SCy * 3 / 4);
            break;
        }

    }

    public void setPosition(int x, int y) {
        this.x = x;
        this.y = y;
        switch (type) {
        case hero1:
            speedx = 7;
            speedy =7;
            break;
        case hero2:
            speedx = 6;
            speedy = 6;
            break;
        case hero3:
            speedx = 5;
            speedy = 5;
            break;

        }
    }

    //this.x += speedx;
    // this.y += speedy;
    public void move() {

        if (mc.key2) {
          if(this.y>=0)  {this.y -= speedy;}
        }
        if (mc.key8) {
           if(this.y <=mc.SCy){ this.y += speedy;}
        }
        if (mc.key4) {
           if(this.x>=0){ this.x -= speedx;}
            if (frameCnt != 0) {
                frameCnt--;
            }
        }
        if (mc.key6) {
          if(this.x<=mc.SCx) { this.x += speedx;}
            if (frameCnt != 4) {
                frameCnt++;
            }
        }

        if (mc.key4 == false & frameCnt == 0) {
            frameCnt += 1;
        }
        if (mc.key4 == false & frameCnt == 1) {
            frameCnt += 1;
        }
        if (mc.key6 == false & frameCnt == 4) {
            frameCnt -= 1;
        }
        if (mc.key6 == false & frameCnt == 3) {
            frameCnt -= 1;
        }

    }


    public void paint(Graphics g) {
        if (type == 0) {
            mc.tool.drawPartImage(g, img_shadow, x - 12, y + 12, this.frameCnt * 26,
                                  0, 26, 23);
            mc.tool.drawPartImage(g, img_hero, x, y, this.frameCnt * 31, 0, 31, 27);
        }
        if (type == 1) {
            mc.tool.drawPartImage(g, img_shadow, x - 12, y + 12, this.frameCnt * 19,
                                  0, 19, 17);
            mc.tool.drawPartImage(g, img_hero, x, y, this.frameCnt * 23, 0, 23, 20);
        }
        if (type == 2) {
            mc.tool.drawPartImage(g, img_shadow, x - 12, y + 12, this.frameCnt * 23,
                                  0, 23, 19);
            mc.tool.drawPartImage(g, img_hero, x, y, this.frameCnt * 27, 0, 27, 22);
        }


    }

    public void setVisble(boolean flag) {
        this.visble = flag;
    }

    public boolean isVisble() {
        return this.visble;
    }
}

⌨️ 快捷键说明

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