快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

怪物类java代码 java怪物猎人

java游戏编程-怪物跟着人移动问题

只需要让怪物得到当前人的坐标,然后向这个方向移动就行了。比如说你可以用一个方向向量来代表怪物的运行状态,如:

我们提供的服务有:网站设计、成都网站制作、微信公众号开发、网站优化、网站认证、壶关ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的壶关网站制作公司

class MoveDirection{

int dx;

int dy;

}

根据你人物和怪物的相对位置,计算出dx,dy。比如说人物坐标是(300,300),怪物坐标是(400,200),怪物每次移动20。那么dx = -20, dy = 20。

然后你的怪物每次运动的时候增加方向向量就可以了。

class Monster{

public void move(MoveDirection direction){

this.x += direction.dx;

this.y += direction.dy;

}

}

如果你的人和怪物是在复杂的地图当中也是一样的。只不过要先判定是否可以向某个方向移动就行了。

求这个过程Java代码,急急急

public class Dmytest{

public static void main(String args[]){

Hero yx1 = new Hero("英雄1",300);

Hero yx2 = new Hero("英雄2",200);

yx1.atkHero(yx2);

}

}

class Hero{

private String name;

private int blood;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getBlood() {

return blood;

}

public void setBlood(int blood) {

this.blood = blood;

}

public Hero(){}

public Hero(String name,int blood){

this.name = name;

this.blood = blood;

}

public void showInfo(){

System.out.println("英雄姓名:"+name+" 血量为:"+blood);

}

public void atkHero(Hero h){

int sBlood = this.blood-h.getBlood();

System.out.println("英雄的血量只剩了:"+sBlood);

}

}

class Monster{

private String name;

private int blood;

private String type;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getBlood() {

return blood;

}

public void setBlood(int blood) {

this.blood = blood;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public Monster(){}

public Monster(String name,int blood,String type){

this.name = name;

this.blood = blood;

this.type = type;

}

public void showInfo(){

System.out.println("怪物姓名:"+name+" 血量为:"+blood+" 怪物类型为:"+type);

}

}

class Weapon{

private String name;

private String ATK;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getATK() {

return ATK;

}

public void setATK(String aTK) {

ATK = aTK;

}

public Weapon(){}

public Weapon(String name,String atk){

this.name = name;

this.ATK = atk;

}

public void showInfo(){

System.out.println("武器名称:"+name+" 攻击力为:"+ATK);

}

}

所有代码在这,应该能帮到你!

1道JAVA面向对象的练习题,最后不懂!求助!

//防御方法通过输入参数接收攻击力,如果攻击力小于防御力,生命值不受影响,反之伤害值=攻击力-防御力

public void recovery(int attack){

recovery(15) 是你自己本身定义的方法,用来判断攻击力和防御能力的大小从而去改变他们的生命值。

比如就你那参数 15来说。 攻击是15 防御才10 所以对象会受到15-10=5的伤害

java当中怪物类如何引用武器类打怪兽类

这是个挺有趣的问题,我们可以一起来分析下:

1.这里面只有两个类,武器类和怪兽类;而动作只有一个,打。

2.面向对象的思想是为类分配职责,那么打这个动作到底分配给谁?

很明显应该分配给怪兽类,因为打这个动作涉及的大部分数据都是怪兽类包含的,从常识上来讲分配给武器类感觉怪怪的...

3.从上面可以想象到会出现这样的情况monster.hit(otherMonster),这里monster是打,otherMonster是被打。hit方法里面需要将otherMonster的信息获取出来,然后计算打的过程...这样会把otherMonster的数据封装破坏掉,如果扩展otherMonster,显然你必需要在hit方法里判断otherMonster是哪种类型。但是monster.hit(otherMonster)又是符合对象的思维的。那么问题出在哪里?

问题出在打是一个动词,打这个方法分配给了一个对象,但作用的是另一个对象,因此而破坏了数据封装。如果打分配给一个对象,同时作用的是这个对象,那么问题就解决了。

怎样把打分配给一个对象,同时作用的是这个对象呢?答案是把打转换成被打,于是就成了otherMonster.hit(),在monster需要去打otherMonster时,调用otherMonster.hit()就可以了。

4.武器类还没有被引入进来,明显武器不会自动攻击,他必须装备在monster上,我们可以把weapon作为monster的成员。

5.武器类可以被使用在打这个动作上,于是我们为hit方法增加武器参数,于是就变成otherMonster.hit(weapon),这个方法是在monster内部调用的,weapon也是monster的成员,没有破坏封装性。

6.至此,你提的这个问题就是这样了...

class Monster{

private Weapon weapon;

public void hit(Weapon weapon){

//dosometing.

}

public Weapon getWeapon(){

return weapon;

}

public void setWeapon(Weapon weapon){

this.weapon = weapon;

}

}

是不是感觉怪怪的...我们可以继续来探讨这个问题:

为什么感觉怪怪的?是因为这个问题太简化了,简化到我们并不知道Monster被打之后到底发生什么。

感觉这个很像个游戏,大部分游戏都是基于hitpoint(血量)的,为了使这个问题带感一些,我们给Monster一个int类型的hitPoint。

同时给武器类赋予一个attackPoint,在Monster被打的时候扣除attackPoint数量的hitPoint。

那么问题来了,hit方法里需要获取Weapon中的attackPoint,这又会把Weapon的数据封装破坏掉...

为此我们需要给Monster一个直接扣除hitPoint的方法damage(int attackPoint),让Weapon能够调用damage方法把自身的attackPoint传递进来。

Weapon本身我们可以分配一个attack(Monster monster)方法来给Monster把自身传递进来,于是程序就变成了:

class Monster{

private Weapon weapon;

private int hitPoint;

public void hit(Weapon weapon){

weapon.attack(this);

}

public void damage(int attackPoint){

this.hitPoint -= attackPoint;

}

public Weapon getWeapon(){

return weapon;

}

public void setWeapon(Weapon weapon){

this.weapon = weapon;

}

}

class Weapon{

private int attackPoint;

public void attack(Monster monster){

monster.damage(attackPoint);

}

}

也许有人会问,hit里面调用weapon.attack,attack里面又调用monster.damage,那还不如在hit里直接获取weapon的attackPoint,然后直接扣除hitPoint。

为什么要这么麻烦呢?实际上这里Weapon是一个策略(Strategy模式),由策略来决定对对象到底采取什么样的作用。这里感觉麻烦也是因为问题太简单了。

再带感一点,我们参考网游的做法,Monster本身有一定的躲避和格挡率,同时某些Weapon可能会有暴击率:

此时我们需要在hit方法里先计算是否躲避或者格挡,如果都不成功,才调用weapon.attack(this).在attack方法里,需要先计算是否暴击,如果暴击,则把attackPoint乘以2。于是又变成了

class Monster{

private Weapon weapon;

private int hitPoint;

private int dodge;//100%比例

private int block;//100%比例

public void hit(Weapon weapon){

if (isDodge()){

return;

}

if (isBlock){

return;

}

weapon.attack(this);

}

public void damage(int attackPoint){

this.hitPoint -= attackPoint;

}

public boolean isDodge(){

//COMPUTE IS DODGE?

}

public boolean isBlock(){

//COMPUTE IS BLOCK?

}

public Weapon getWeapon(){

return weapon;

}

public void setWeapon(Weapon weapon){

this.weapon = weapon;

}

}

class Weapon{

private int attackPoint;

private int critical;//100%比例

public void attack(Monster monster){

int actualAttackPoint = isCritical()?attackPoint*2:attackPoint;

monster.damage(actualAttackPoint);

}

public boolean isCritical(){

//COMPUTE IS CRITICAL

}

}

这样就不觉得麻烦了...

最后应该把monster抽象成接口,因为可能不只是monster可以被武器打哦

你还可以后面再加入怎么判断Monster挂了,通过观察者模式来通知打人者,还有其他各种各样带感的东西。

祝你好运!

java 创建一个LOL中的英雄类和怪物类 初学者,求大神指点

static class Hero{ //英雄类

private String name;//英雄的名字

private Integer experience;//经验值

private Integer level; //英雄等级

private Integer attack; //攻击力

public Hero(String name){ //新建英雄的构造方法

this.name = name;//输入名字

this.experience = 0;//初始经验值为0

this.level = 1;//初始等级为1

this.attack = 100;//初始攻击力为100

}

public void AttackMonster(Monster monster){ //攻击怪物的方法

System.out.println("英雄"+this.name+"攻击"+monster.getName()+"造成"+this.attack+"点伤害");

Integer hp = monster.getHealthPoint();//怪物被攻击前血量

if (hp  this.attack) { //判断 如果怪物的血量大于攻击力

hp = hp - this.attack;//怪物的血量 就减少攻击力的点数

} else { //如果怪物的血量 等于或者小于攻击力

hp = 0; //怪物血量为0 死亡

}

monster.setHealthPoint(hp); //改变怪物被攻击后的血量

System.out.println("怪物剩余血量"+hp);

if (hp == 0  this.level 18){ //如果怪物死亡 且英雄等级没有达到最高的18级

System.out.println("英雄"+this.name+"的经验值增加"+monster.getExperience()+"点");

this.experience = this.experience + monster.getExperience(); //英雄的经验值 增加

if (this.experience = level * 100){ //设定 英雄升级所需经验值为 等级*100  如果英雄增加后的经验值达到升级条件

this.experience = this.experience - level*100; //经验值减去当前升级需要的经验值 溢出的进入下一个等级经验

this.level = this.level + 1;//英雄等级 +1

this.attack = this.attack + 20; //设定 每升一级英雄攻击力 +20

System.out.println("英雄"+this.name+"升级了,等级+1,攻击力+20,当前等级"+this.level+",当前攻击力"+this.attack);

}

}

}

//获取英雄名字的方法

public String getName() {

return name;

}

//改变英雄名字的方法

public void setName(String name) {

this.name = name;

}

//获取英雄当前经验值的方法

public Integer getExperience() {

return experience;

}

//改变英雄当前经验值的方法

public void setExperience(Integer experience) {

this.experience = experience;

}

//获取英雄当前等级的方法

public Integer getLevel() {

return level;

}

//改变英雄当前等级的方法

public void setLevel(Integer level) {

this.level = level;

}

//获取英雄当前攻击力的方法

public Integer getAttack() {

return attack;

}

//改变英雄当前攻击力的方法

public void setAttack(Integer attack) {

this.attack = attack;

}

}

static class Monster{ //怪物类

private String name; //怪物的名字

private Integer healthPoint;//生命值

private Integer level;//怪物等级

private Integer experience; //怪物的经验值

public Monster(String name,Integer level){ //创建怪物的构造方法 参数 名字,等级

this.name = name; //初始化怪物的名字

this.level = level; //初始化怪物的等级

this.healthPoint = level * 1000; //初始化怪物的血量

this.experience = level * 10;//初始化怪物的经验,英雄杀死后可获得,设定怪物经验值为等级*10

}

public Integer getHealthPoint() {

return healthPoint;

}

public void setHealthPoint(Integer healthPoint) {

this.healthPoint = healthPoint;

}

public Integer getLevel() {

return level;

}

public void setLevel(Integer level) {

this.level = level;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getExperience() {

return experience;

}

public void setExperience(Integer experience) {

this.experience = experience;

}

}

public static void main(String[] args) throws Exception {

Hero hero = new Hero("德玛西亚之力"); //新建一个英雄

Monster monster = new Monster("小兵",1);//新建一个小兵

do {

hero.AttackMonster(monster); //攻击怪物

}while (monster.getHealthPoint() != 0);  //如果怪物没死 一直攻击

}


文章名称:怪物类java代码 java怪物猎人
文章出自:http://6mz.cn/article/dodedid.html

其他资讯