欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单的回合制小游戏

程序员文章站 2022-04-20 17:39:38
设计需求: (1)怪物的共有的属性有:名称、血量、攻击力、防御力;共有的方法有:显示属性、判定是否死亡、和攻击 (2)设置两种怪物:独眼巨人,树怪 (3)独眼巨人特有的属性有:武器(狼牙棒或钉锤);独眼巨人具有的攻击方法是:使用武器(狼牙棒或钉锤)攻击 (4)树怪特有的属性:高度;树怪的攻击方式是使 ......

设计需求:

(1)怪物的共有的属性有:名称、血量、攻击力、防御力;共有的方法有:显示属性、判定是否死亡、和攻击

(2)设置两种怪物:独眼巨人,树怪

(3)独眼巨人特有的属性有:武器(狼牙棒或钉锤);独眼巨人具有的攻击方法是:使用武器(狼牙棒或钉锤)攻击

(4)树怪特有的属性:高度;树怪的攻击方式是使用树枝缠绕攻击

(5)创建2只独眼巨人和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 boolean live = true;
	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.println("怪物名称:"+name);
		System.out.println("血量:"+hp);
		System.out.println("攻击力:"+power);
		System.out.println("防御力:"+defence);
	}
	public void attacMode(Monster monster) {
		
	}
}

 独眼巨人类:

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) {
		monster.hp  = monster.hp - (this.power-monster.defence);
		if(monster.hp>0) {
			System.out.println(this.name+"使用"+this.arm+"进行攻击,对"+monster.name+"造成"
					+(this.power-monster.defence)+"点伤害");
		}
		else {
			monster.hp = 0;
			System.out.println(monster.name+"死亡");
			monster.live = false;
		}
	}
	public void show() {
		System.out.print("怪物名称:"+name);
		System.out.print("  血量:"+hp);
		System.out.print("  攻击力:"+power);
		System.out.print("  防御力:"+defence);
		System.out.println("  武器:"+arm);
	}
}

树精类:

public class Monser2 extends Monster{

	String Height;
	public Monser2(String name, int hp, int power, int defence,String Height) {
		super(name, hp, power, defence);
		this.Height = Height;
	}
	
	public void attacMode(Monster monster) {
		monster.hp  = monster.hp - (this.power-monster.defence);
		if(monster.hp>0) {
			System.out.println(this.name+"使用树枝缠绕进行攻击,对"+monster.name+"造成"
					+(this.power-monster.defence)+"点伤害");
		}
		else {
			monster.hp = 0;
			System.out.println(monster.name+"死亡");
			monster.live = false;
		}
	}
	
	public void show() {
		System.out.print("怪物名称:"+name);
		System.out.print("  血量:"+hp);
		System.out.print("  攻击力:"+power);
		System.out.print("  防御力:"+defence);
		System.out.println("  高度:"+Height);
	}

}

测试类:

import java.util.ArrayList;
import java.util.List;

public class Demo {

	public static void main(String[] args) {
		Monser1 kt = new Monser1("独眼巨人凯特",100,25,10,"钉锤");
		Monser1 km = new Monser1("独眼巨人卡姆",100,30,7,"狼牙棒");
		Monser2 gy = new Monser2("树怪盖亚",80,35,12,"10.5");
		List<Monster> list = new ArrayList<Monster>();
		list.add(kt);
		list.add(km);
		list.add(gy);
		for(int i=0;i<list.size();i++) {
			list.get(i).show();
		}
		int size = 0;
		while(true) {
			if(list.size()==1) {
				System.out.println(list.get(0).name+"获得胜利");
				break;
			}
			if(size<list.size()) {
				list.get(size).attacMode(list.get(size+1));
				if(list.get(size+1).hp==0)
					list.remove(size+1);
				size++;
			}
			if(size==list.size()-1) {
				list.get(size).attacMode(list.get(0));
				if(list.get(0).hp==0)
					list.remove(0);
				size = 0;
			}
		}
	}
}