用java 写一个250行包含玩家类的 面向对象之宠物大作战
时间: 2024-01-03 20:03:15 浏览: 71
好的,我会为您编写一个基于 Java 的宠物大作战游戏,包含玩家类。
首先,我们需要定义宠物类和玩家类。宠物类包含宠物的基本属性和行为,玩家类则包含玩家的基本属性和行为。
以下是宠物类的代码实现:
```java
public class Pet {
private String name;
private int health;
private int power;
private int speed;
public Pet(String name, int health, int power, int speed) {
this.name = name;
this.health = health;
this.power = power;
this.speed = speed;
}
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public int getPower() {
return power;
}
public int getSpeed() {
return speed;
}
public void setHealth(int health) {
this.health = health;
}
public void attack(Pet enemy) {
int damage = this.power - enemy.getSpeed();
if (damage > 0) {
enemy.setHealth(enemy.getHealth() - damage);
}
}
}
```
以上代码定义了一个宠物类,包括宠物的姓名、健康值、攻击力和速度。构造函数用于初始化宠物属性。getName、getHealth、getPower和getSpeed方法用于获取宠物属性。setHealth方法用于设置宠物的健康值。attack方法用于让宠物攻击敌人。
接下来是玩家类的代码实现:
```java
public class Player {
private String name;
private Pet pet;
private int level;
private int experience;
private int coins;
public Player(String name, Pet pet) {
this.name = name;
this.pet = pet;
this.level = 1;
this.experience = 0;
this.coins = 0;
}
public String getName() {
return name;
}
public Pet getPet() {
return pet;
}
public int getLevel() {
return level;
}
public int getExperience() {
return experience;
}
public int getCoins() {
return coins;
}
public void setPet(Pet pet) {
this.pet = pet;
}
public void gainExperience(int experience) {
this.experience += experience;
if (this.experience >= 100) {
levelUp();
}
}
private void levelUp() {
this.level++;
this.experience -= 100;
this.pet.setHealth(pet.getHealth() + 10);
this.pet.setPower(pet.getPower() + 5);
this.pet.setSpeed(pet.getSpeed() + 2);
}
public void earnCoins(int coins) {
this.coins += coins;
}
public void buyFood(int coins) {
if (this.coins >= coins) {
this.coins -= coins;
this.pet.setHealth(pet.getHealth() + 20);
}
}
public void fight(Player enemy) {
Pet myPet = this.pet;
Pet enemyPet = enemy.getPet();
while (myPet.getHealth() > 0 && enemyPet.getHealth() > 0) {
myPet.attack(enemyPet);
enemyPet.attack(myPet);
}
if (myPet.getHealth() > 0) {
this.gainExperience(50);
this.earnCoins(20);
} else {
System.out.println("You lost the battle");
}
}
}
```
以上代码定义了一个玩家类,包括玩家的姓名、宠物、等级、经验和金币。构造函数用于初始化玩家属性。getName、getPet、getLevel、getExperience和getCoins方法用于获取玩家属性。setPet方法用于更换宠物。gainExperience方法用于让玩家获得经验值,当经验值达到100时,会升级,提升宠物的属性。earnCoins方法用于让玩家获得金币。buyFood方法用于让玩家购买食物,当玩家的金币足够时,可以让宠物恢复健康。fight方法用于让玩家与敌人进行战斗,比较宠物的健康值,直到一方的宠物健康值小于等于0时,结束战斗。
最后是游戏主函数的实现:
```java
public class PetBattle {
public static void main(String[] args) {
Pet pet1 = new Pet("Pet1", 100, 30, 20);
Pet pet2 = new Pet("Pet2", 80, 25, 30);
Player player1 = new Player("Player1", pet1);
Player player2 = new Player("Player2", pet2);
System.out.println("Welcome to Pet Battle!");
System.out.println("Player1: " + player1.getName() + "'s pet " + player1.getPet().getName());
System.out.println("Player2: " + player2.getName() + "'s pet " + player2.getPet().getName());
System.out.println("Let the battle begin!");
player1.fight(player2);
}
}
```
以上代码初始化了两只宠物和两个玩家。打印出欢迎语和对战的双方宠物信息,并开始战斗。
这是一个简单的宠物大作战游戏,您可以根据需求进行扩展。
阅读全文