旋转屏幕猫狗游戏java_用JAVA编写程序。有这么一款游戏,游戏中有刀跟剑,还有动物猫跟狗,现在要求当到攻击猫时,猫发出叫...

展开全部

public class Cat {

private final String SWORD = "1";

private final String KNIFE = "2";

private Integer life = 10;// 生命值

public void setLife() {

if (!32313133353236313431303231363533e58685e5aeb931333361303062isAlive()) {

System.out.println("The cat is die!!!");

}

life -= 2;

}

public void attack(String weapon) {

if (weapon.equals(SWORD)) {

atteckedBySword();

} else if (weapon.equals(KNIFE)) {

atteckedByKnife();

}

}

public boolean isAlive() {

return life > 0;

}

public void atteckedByKnife() {

if (isAlive()) {

setLife();

System.out.println("喵喵喵");

}

}

public void atteckedBySword() {

if (isAlive()) {

setLife();

System.out.println("喵哦哦");

}

}

}public class Dog {

private final String SWORD = "1";

private final String KNIFE = "2";

private Integer life = 15;// 生命值

/**

* 设置生命值

*/

public void setLife() {

if (!isAlive()) {

System.out.println("The cat is die!!!");

}

life -= 2;// 每次受到攻击,生命值-2

}

/**

* 是否还活着

*/

public boolean isAlive() {

return life > 0;

}

public void attack(String weapon) {

if (weapon.equals(SWORD)) {

atteckedBySword();

} else if (weapon.equals(KNIFE)) {

atteckedByKnife();

}

}

/**

* 被刀攻击

*/

public void atteckedByKnife() {

if (isAlive()) {

setLife();

System.out.println("汪汪汪");

}

}

/**

* 被剑攻击

*/

public void atteckedBySword() {

if (isAlive()) {

setLife();

System.out.println("汪哦哦");

}

}

}import java.util.Scanner;

public class Game {

private static Scanner input = new Scanner(System.in);

private static Cat tomcat = new Cat();

private static Dog tomdog = new Dog();

private final static String SWORD = "1";

private final static String KNIFE = "2";

/**

* 选取武器

*

* @return 攻击武器

*/

private static String takeWeapon() {

String weapon = "1";

do {

System.out.print("\nTake your weapon(1:knife  2:sword): ");

weapon = input.next();

} while (!weapon.equals(SWORD) && !weapon.equals(KNIFE));

return weapon;

}

/**

* 随机攻击

*

* @return

*/

private static int getRandomEnemy() {

return (int) (Math.random() * 2);

}

public static void main(String[] args) {

while (true) {

if (getRandomEnemy() == 0) {// 攻击猫

System.out.println("Attack the cat ");

String weapon = takeWeapon();

tomcat.attack(weapon);

} else {

System.out.println("Attack the cat ");

String weapon = takeWeapon();

tomdog.attack(weapon);

}

if (!tomcat.isAlive() && !tomdog.isAlive()) {

break;

}

}

System.out.println("end");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值