简单的回合制小游戏
设计需求:
(1)怪物的共有的属性有:名称、血量、攻击力、防御力;共有的方法有:显示属性、判定是否死亡、和攻击
(2)设置两种怪物:独眼巨人,树怪,英雄
(3)独眼巨人特有的属性有:武器(狼牙棒或钉锤);独眼巨人具有的攻击方法是:使用武器(狼牙棒或钉锤)攻击
(4)树怪特有的属性:高度;树怪的攻击方式是使用树枝缠绕攻击
(5)英雄攻击方式为王之藐视
(5)创建2只独眼巨人和1只树怪的对象和1名英雄,放入集合中,并轮流攻击集合中的下一个对象(0攻击1,1攻击2,2攻击0),直到只剩下一只怪物存活
(6)伤害值计算公式为:攻击者攻击力-被攻击者防御力,被攻击者的血量会要扣减调伤害值
(7)死亡判定规则为:血量小于等于0
怪兽父类:
public class Monster { public String name; public int hp; public int power; public int defence; public Monster(String name, int hp, int power, int defence) { super(); this.name = name; this.hp = hp; this.power = power; this.defence = defence; } public void show() { System.out.print("怪物名称:"+name); System.out.print(" ;血量:"+hp); System.out.print(" ;攻击力:"+power); System.out.print(" ;防御力: "+defence); } public boolean dead(Monster monster) { return hp<=0; } public void attacMode(Monster monster) { int harm = this.power-monster.defence; monster.hp -=harm; System.out.println("进行攻击,对"+monster.name+"造成" +harm+"点伤害"); } }
独眼巨人类:
public class Monser1 extends Monster{ String arm; public Monser1(String name, int hp, int power, int defence,String arm) { super(name, hp, power, defence); this.arm = arm; } public void attacMode(Monster monster) { System.out.print(name+"使用 "+arm+" "); super.attacMode(monster); } public void show() { super.show(); System.out.println(" ;武器:"+arm); } }
树精类:
public class Monser2 extends Monster{ double Height; public Monser2(String name, int hp, int power, int defence,double Height) { super(name, hp, power, defence); this.Height = Height; } public void attacMode(Monster monster) { System.out.print(name+"使用 树枝缠绕 "); } public void show() { super.show(); System.out.println(" ;高度:"+Height); } }
英雄类:
public class Hero extends Monster { public Hero(String name, int hp, int power, int defence) { super(name, hp, power, defence); } public void attacMode(Monster monster) { System.out.print(name+"使用 王之藐视 "); super.attacMode(monster); } }
测试类:
import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { List<Monster> list = new ArrayList<Monster>(); list.add(new Monser1("独眼巨人凯特", 50, 10, 5, "咖喱棒")); list.add(new Monser2("树怪盖亚", 75, 8, 6, 2.8)); list.add(new Monser1("独眼巨人卡姆", 50, 11, 4, "狼牙棒")); list.add(new Hero("英雄王子", 500, 20, 5)); for (int i = 0; i < list.size(); i++) { list.get(i).show(); } int i = 0; while(list.size()>1) { Monster attacter = list.get(i); Monster hamer = list.get((i+1)%list.size()); attacter.attacMode(hamer); if(hamer.dead(hamer)){ System.out.println(hamer.name+"被击败"); list.remove(hamer); } i = (i+1)%list.size(); } System.out.println(list.get(0).name+"获得胜利"); list.get(0).show(); } }