📄 hero.java
字号:
package perGame;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
public class Hero {
int hp, mp, attack, defence, speed,speedX,speedY,maxHP,maxMP;
int face; //脸的朝向
int frameWidth;//碰撞寬度
int frameHeight;//碰撞高度
int x, y;//英雄橫縱坐標
int level;//等级
int experience;//升级所需经验值
int currentExperience;//当前经验值
int count;//人物行走指针
int lastX,lastY;
int row,col;
String name; //名字
String skill; //技能
MyCanvas mc;
Image img;//英雄圖片
final static int face_up = 0;
final static int face_left = 1;
final static int face_right = 2;
final static int face_down = 3;
/**
* 构造函数:生成一个英雄对象
* @param hp int--生命
* @param mp int--魔法力
* @param attack int--攻击力
* @param defence int--防御力
* @param speed int--速度
* @param name String--名字
* @param skill String--技能
* @param mc MyCanvas--画布对象
*/
public Hero(int hp, int mp,int speed, String name,int level,
String skill, MyCanvas mc, Image img, int frameWidth,
int frameHeight) {
this.level=level;
this.speed = speed;
this.name = name;
this.skill = skill;
this.mc = mc;
this.img = img;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.x = 100;
this.y = 100;
this.face = 2;
this.speedX = this.speed;
this.speedY = 0;
this.level=1;
this.currentExperience=0;
this.experience=20*level;
this.attack = this.level*10;
this.defence = this.level*5;
this.hp = hp;
this.mp = mp;
maxHP=hp;
maxMP=mp;
}
/**
* 方法:移动
*/
public void move() {
/**
* 英雄的坐标变换
*/
this.x += this.speedX;
this.y += this.speedY;
this.lastX = this.speedX;
this.lastY = this.speedY;
}
public void backMove()
{
this.x -=lastX;
this.y -=lastY;
}
/**
* 方法:向上走
*/
public void walk_UP() {
if (this.face != this.face_up) {
this.face = this.face_up;
this.speedX = 0;
this.speedY = -this.speed;
}
this.move();
}
/**
* 方法:向左走
*/
public void walk_LEFT() {
if (this.face != this.face_left) {
this.face = this.face_left;
this.speedX = -this.speed;
this.speedY = 0;
}
this.move();
}
/**
* 方法:向右走
*/
public void walk_RIGHT() {
if (this.face != this.face_right) {
this.face = this.face_right;
this.speedX = this.speed;
this.speedY = 0;
}
this.move();
}
/**
* 方法:向下走
*/
public void walk_DOWN() {
if (this.face != this.face_down) {
this.face = this.face_down;
this.speedX = 0;
this.speedY = this.speed;
}
this.move();
}
/**
* 方法:画英雄
*/
public void paintHero(Graphics g) {
int clipX = 16;
int clipY = 24;
switch (this.face) {
case face_up:
clipX += 16 * count;
clipY=0;
break;
case face_left:
clipX += 16 * count;
clipY=72;
break;
case face_right:
clipX += 16 * count;
clipY=24;
break;
case face_down:
clipX += 16 * count; clipY=48;
break;
}
count++;
if(count>=2){count=0;}
mc.tools.drawPartImage(g, this.img, this.x-mc.viewX,
this.y-mc.viewY, clipX, clipY,
16, 24
);
}
public boolean collidsMap() {
int xx = 0;
int yy = 0;
switch (face) {
case face_up:
xx = x + this.frameWidth/2;
yy = y +8;
break;
case face_down:
xx = x + this.frameWidth/2;
yy = y + this.frameHeight;
break;
case face_left:
xx = x;
yy = y + 16;
break;
case face_right:
xx = x + this.frameWidth;
yy = y + 16;
break;
}
row = yy/mc.map.tileHeight;
col = xx/mc.map.tileWidth;
if(row<0)
{
row = 0;
}
if(col <0)
{
col = 0;
}
if(this.col>mc.map.Data_Map_Block[0].length-1)
{
col= mc.map.Data_Map_Block[0].length-1;
}
if(row>mc.map.Data_Map_Block.length-1)
{
row=mc.map.Data_Map_Block.length-1;
}
if(mc.map.Data_Map_Block[row][col] != 0)
{
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -