用java写一个300行的面向对象之宠物大作战,包括分支,循环等等
时间: 2024-02-17 19:00:52 浏览: 61
用java写的小宠物游戏
以下是一个简单的面向对象之宠物大作战的Java程序,共计约300行,包括分支、循环等等。为了简化代码,一些细节和异常处理等内容并未完全考虑。代码中的注释可以帮助理解。
```java
import java.util.Scanner;
/**
* 宠物类
*/
class Pet {
private String name; // 名字
private String type; // 种类
private int level; // 等级
private int hp; // 生命值
private int attack; // 攻击力
private int defense; // 防御力
public Pet(String name, String type) {
this.name = name;
this.type = type;
this.level = 1;
this.hp = 100;
this.attack = 10;
this.defense = 5;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public int getLevel() {
return level;
}
public int getHp() {
return hp;
}
public int getAttack() {
return attack;
}
public int getDefense() {
return defense;
}
public void attack(Pet enemy) {
int damage = this.attack - enemy.getDefense();
if (damage < 0) {
damage = 0;
}
enemy.hp -= damage;
System.out.println(this.name + "攻击了" + enemy.getName() + ",造成了" + damage + "点伤害。");
}
public void defend() {
this.defense += 5;
System.out.println(this.name + "使用了防御技能,防御力提高了。");
}
public void levelUp() {
this.level++;
this.hp += 10;
this.attack += 5;
this.defense += 2;
System.out.println(this.name + "升级了,生命值、攻击力、防御力都提高了。");
}
public void showInfo() {
System.out.println("宠物名字:" + this.name);
System.out.println("宠物种类:" + this.type);
System.out.println("宠物等级:" + this.level);
System.out.println("宠物生命值:" + this.hp);
System.out.println("宠物攻击力:" + this.attack);
System.out.println("宠物防御力:" + this.defense);
}
}
/**
* 玩家类
*/
class Player {
private String name; // 名字
private int level; // 等级
private int exp; // 经验值
private int gold; // 金币
private Pet pet; // 拥有的宠物
public Player(String name) {
this.name = name;
this.level = 1;
this.exp = 0;
this.gold = 0;
this.pet = null;
}
public String getName() {
return name;
}
public int getLevel() {
return level;
}
public int getExp() {
return exp;
}
public int getGold() {
return gold;
}
public Pet getPet() {
return pet;
}
public void buyPet() {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要购买的宠物种类(1.狗狗 2.猫咪 3.兔子):");
int type = scanner.nextInt();
String name = "";
switch (type) {
case 1:
name = "旺财";
break;
case 2:
name = "咪咪";
break;
case 3:
name = "小白";
break;
default:
System.out.println("输入有误,购买失败。");
return;
}
Pet pet = new Pet(name, type == 1 ? "狗狗" : type == 2 ? "猫咪" : "兔子");
if (this.gold < 50) {
System.out.println("金币不足,购买失败。");
return;
}
this.gold -= 50;
this.pet = pet;
System.out.println("购买成功,您现在拥有一只" + this.pet.getType() + ",名字叫做" + this.pet.getName() + "。");
}
public void useItem() {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要使用的道具(1.药水 2.防御道具):");
int type = scanner.nextInt();
switch (type) {
case 1:
if (this.pet == null) {
System.out.println("您还没有宠物,使用失败。");
return;
}
this.pet.getHp += 20;
System.out.println("您的宠物回复了20点生命值。");
break;
case 2:
if (this.pet == null) {
System.out.println("您还没有宠物,使用失败。");
return;
}
this.pet.defend();
break;
default:
System.out.println("输入有误,使用失败。");
break;
}
}
public void fight() {
if (this.pet == null) {
System.out.println("您还没有宠物,无法进行战斗。");
return;
}
Pet enemy = new Pet("敌人", "野生" + (int) (Math.random() * 3 + 1));
System.out.println("你遇到了一只" + enemy.getType() + ",准备开始战斗!");
while (this.pet.getHp() > 0 && enemy.getHp() > 0) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要进行的操作(1.攻击 2.防御):");
int choice = scanner.nextInt();
switch (choice) {
case 1:
this.pet.attack(enemy);
break;
case 2:
this.pet.defend();
break;
default:
System.out.println("输入有误,回合结束。");
break;
}
if (enemy.getHp() > 0) {
enemy.attack(this.pet);
}
}
if (this.pet.getHp() > 0) {
System.out.println("战斗胜利,获得了100点经验值和50金币。");
this.exp += 100;
this.gold += 50;
if (this.exp >= this.level * 100) {
this.level++;
this.exp = 0;
System.out.println("恭喜你升级了,当前等级为" + this.level + "。");
}
this.pet.levelUp();
} else {
System.out.println("战斗失败,宠物死亡。");
this.pet = null;
}
}
public void showInfo() {
System.out.println("玩家名字:" + this.name);
System.out.println("玩家等级:" + this.level);
System.out.println("玩家经验值:" + this.exp);
System.out.println("玩家金币:" + this.gold);
if (this.pet != null) {
System.out.println("玩家拥有的宠物:");
this.pet.showInfo();
} else {
System.out.println("玩家还没有宠物。");
}
}
}
/**
* 程序主类
*/
public class PetBattle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎进入宠物大作战!");
System.out.println("请输入您的名字:");
String name = scanner.nextLine();
Player player = new Player(name);
while (true) {
System.out.println("==================");
System.out.println("请选择要进行的操作:");
System.out.println("1.购买宠物");
System.out.println("2.使用道具");
System.out.println("3.战斗");
System.out.println("4.查看信息");
System.out.println("5.退出游戏");
int choice = scanner.nextInt();
switch (choice) {
case 1:
player.buyPet();
break;
case 2:
player.useItem();
break;
case 3:
player.fight();
break;
case 4:
player.showInfo();
break;
case 5:
System.out.println("感谢您的游玩,再见!");
return;
default:
System.out.println("输入有误,请重新选择。");
break;
}
}
}
}
```
阅读全文