基于对象文本的 Java 游戏帮助(初学者)



所以我必须创建一个Java游戏。我对Java很陌生,我整天都在研究这个问题。但是我无法弄清楚什么是不正确的。这是我玩游戏的主要课程。我还有一个角色类、一个武器类和一个护甲类。我现在的问题是我不希望任何玩家的HP低于0。如果它低于 0,它应该更改为 0,然后该敌人将不再被选中与之作战。我想我不知何故选择惠普是错误的,但我真的不明白如何。谁能帮忙?


import Character.Character;
import java.util.Random;
public class Main {
public static void main(String[] args) {    
Scanner scanner = new Scanner(System.in);
System.out.println("> Welcome to The Game Project <");
System.out.println("n >> Input Main Character Name: ");
String main_name = scanner.nextLine();
System.out.println(">> Input Main Character Power: ");
int main_power=scanner.nextInt();
System.out.println(">> Input Main Character Hp: ");
int main_hp=scanner.nextInt();
System.out.println("----------------------------------------------------");
Character main_ch=new Character (main_hp,main_power,main_name);
show_status(main_ch);
check_bag(main_ch);
Character enemies[]=new Character [10];
enemies[0]= new Character("Werewolf");
enemies[1]= new Character("Vampire");
enemies[2]= new Character("Alien");
enemies[3]= new Character("Witch");
enemies[4]= new Character("Ghost");
enemies[5]= new Character("Skeleton");
enemies[6]= new Character("Zombie");
enemies[7]= new Character("Demon");
enemies[8]= new Character("Mummy");
enemies[9]= new Character("Dragon");
boolean check = false;
GAME:   
while(true) {
Random rnd=new Random();
int selected = rnd.nextInt(enemies.length); //random enemy selected
enemies[selected].damage(main_ch.hit_point());
System.out.println();
System.out.println(">>>>> An enemy has appeared and wants to fight! <<<<<");
show_status(enemies[selected]);
System.out.println();
System.out.println(">> What do you want to do?");
System.out.println("t1. Fight!");
System.out.println("t2. Run!");
int input = scanner.nextInt();
if(input==1) {
int damageDealt = rnd.nextInt(main_ch.hit_point());
int damageTaken = rnd.nextInt(main_ch.damage(main_ch.hit_point()));
enemies[selected].hp -= damageDealt;
main_ch.hp -= damageTaken;
System.out.println("You caused "+ damageDealt +" damage to the enemy!");
System.out.println("You received "+ damageTaken +" damage from the enemy!");
//        show_status(enemies[selected]);
if(main_ch.hp ==0) {
System.out.println();
System.out.println("t Oh no! You died! Better luck next time. Thanks for playing!");
System.out.println();
break;
}
int dead_count=0;
for(int i=0; i<enemies.length;i++) {
if(enemies[i].getHp()==0) {
dead_count=dead_count+1;
}
}
if(dead_count==enemies.length) {
check=true;
}
if(check) {
System.out.println(">>>>> You won! Congratulations, you defeated all of your enemies! <<<<<");
break;
}

}
else if(input==2) {
System.out.println();
System.out.println("-----You ran away to safety! .... But, uh oh... What is that over there?-----");
continue GAME;
}
else {
System.out.println(">>>>>>>>>> Your entry is invalid. <<<<<<<<<<");
}                        
}
}
public static void show_status(Character character) {
System.out.println("----------------- Character Status -----------------");
System.out.println("tCharacter Name:tt"+character.getName());
System.out.println("tCharacter HP:tt"+character.getHp());
System.out.println("tCharacter Power:t"+character.getPower());
System.out.println("tCharacter Defense:t"+character.getDefense());
System.out.println("tCharacter MP:tt"+character.getMp());
System.out.println("tCharacter Level:t"+character.getLevel());
System.out.println("tWeapon Name:tt"+character.getWeapon().getName());
System.out.println("tWeapon Power:tt"+character.getWeapon().getPower());
System.out.println("tArmor Name:tt"+character.getArmor().getName());
System.out.println("tArmor Defense:tt"+character.getArmor().getDefense());
System.out.println("----------------------------------------------------");
}
public static void check_bag(Character character) {
System.out.println("-------------------- Bag Status --------------------");
System.out.println("tMoney:ttt$"+ character.getBag().getMoney());
for(int i = 0; i<4; i++) {
System.out.println("tWeapon Name/Power:t"+ character.getBag().getWeaponArray()[i].getName()+" // "+character.getBag().getWeaponArray()[i].getPower());
}
for(int i = 0; i<4; i++) {
System.out.println("tArmor  Name/Defense:t"+ character.getBag().getArmorArray()[i].getName()+" // "+character.getBag().getArmorArray()[i].getDefense());
}
System.out.println("----------------------------------------------------");
}```

This is the code for the Character package:
```package Character;
import java.util.Random;
import Equipment.*;
public class Character {
private Armor armor = new Armor();
private Weapon weapon = new Weapon();
private Bag bag = new Bag();
public int hp, power, defense, mp, level;
private String name;
Random rnd=new Random(); 
public Character(String name) {
this.name=name;
Random rnd=new Random();
this.hp=rnd.nextInt(1000)+1;
this.power=rnd.nextInt(100)+1;
this.defense=rnd.nextInt(10)+1;
this.mp=rnd.nextInt(100)+1;
this.level=1;
}
public Character(int hp, int power, String name) {
this.hp=hp;
this.power=power;
this.name=name;
this.defense=rnd.nextInt(10)+1;
this.mp=rnd.nextInt(100)+1;
this.level=1;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getMp() {
return mp;
}
public void setMp(int mp) {
this.mp = mp;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
if(this.hp==0) {
this.level = this.level + 1;
}
}
public String getName() {
return name;
}
public int damage(int enemy_power) {
int damage = enemy_power - this.defense;
if(damage<0){ //avoid healing by damage
damage=0;
}
this.hp=this.hp - damage;
if(this.hp<0) { //avoid negative hp
this.hp = 0;
}
return damage;
}
public Armor getArmor() {
return armor;
}
public void setArmor(Armor armor) {
this.armor = armor;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public int hit_point() {
int total_power = this.power + this.weapon.getPower();
return total_power;
}
public Bag getBag() {
return bag;
}
public void setBag(Bag bag) {
this.bag = bag;
}
public class Bag{
Weapon weaponArray[] = new Weapon[4];
Armor armorArray[] = new Armor[4];
int money = 150;
public Bag(){
for(int i=0; i<weaponArray.length; i++) {
weaponArray[i] = new Weapon();
armorArray[i] = new Armor();
}
}
public Weapon[] getWeaponArray() {
return weaponArray;
}
public void setWeaponArray(int yourWeaponIndex, Weapon enemyWeapon) {
this.weaponArray[yourWeaponIndex] = enemyWeapon;
}
public Armor[] getArmorArray() {
return armorArray;
}
public void setArmorArray(Armor[] armorArray) {
this.armorArray = armorArray;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
}```
I will also have to figure out a few more things like how to increase the main character's level whenever they kill an enemy. increase the hit_point and decreased the mp if a skill is used (by the way, does anyone know what mp stands for?), and switch out weapons and armor with a dead enemy if theirs is better than mine. 

这是你的问题:

if(main_ch.hp ==0)

对角色施加伤害后,检查角色的HP是否正好为零。如果角色受到命中,留下 1 点或更多生命值,则战斗可以继续。如果角色受到的命中,生命值低于0,战斗仍然可以继续。只有当角色受到一定程度的伤害将HP精确到0时,战斗才会停止。

幸运的是,您已经描述了修复程序:

如果它低于 0,则应将其更改为 0

这可以像这样完成:

条件从正好为零更改为小于或等于零

if(main_ch.hp <= 0) {
// Added code to reset health
main_ch.setHp(0);
System.out.println();
System.out.println("t Oh no! You died! Better luck next time. Thanks for playing!");
System.out.println();
break;
}

在不相关的说明中,您可能需要重新考虑在此代码中使用循环的方式。您可以通过避免所有标记的继续和中断语句来使您的代码更优雅、更易于阅读,而只使用嵌套的 while 循环来执行您想要的操作,如下所示:

while(playerWantsToPlay)
{
// Oh noes! An encounter. Fight or run away bravely?
while(combatInProgress)
{
if(characterIsAlive && targetIsAlive)
{
// bash each other in the face
}
}
}

我认为您可以猜测如何根据游戏条件和玩家选择更改条件来控制游戏流程,而无需continuebreak来回跳跃。

最新更新