【发布时间】:2014-06-05 00:00:16
【问题描述】:
我正在尝试创建一个有两个战士的游戏。战士对角斗士。 两个战士都有1000的生命值。战斗开始时,玩家 1 可以在三个选项中进行选择:1 - 刺击 2 - 切片 3 - 喝药水......
假设玩家 1 选择推力并攻击 55(使用随机),然后我的代码打印玩家 2 的新生命值,现在是 945。然后轮到玩家 2,假设他做了完全相同的事情并从玩家身上拿走一些生命值1.
现在它会回到玩家 1 的回合。这就是复杂的地方。现在玩家 1 进行第二次攻击,假设他造成 80 点伤害。它将打印出玩家二有 920 生命值。它没有减去之前的攻击。我希望他们战斗,直到其中一个生命值降至 0 并死亡。
那么我该如何修复我的代码,以便在每次攻击后健康状况不会在 1000 处重新启动,并且它会从之前的攻击中已经降低的新健康状况中减去。像这样:1000 hp - 55 = 945 hp - 80 = 865 hp 等等。我是编码新手。一月开始。任何帮助都会很棒!
import java.util.Random;
public class Duel
{
Random hit = new Random();
Random hit1 = new Random();
int newHealth, newHealth1;
int outcome, outcome1, outcome2, outcome3;
int attack, attack1;
int defense, defense1;
int health, health1;
void calculateWinner()
{
do
{
outcome = hit1.nextInt(100) - hit1.nextInt(15);
newHealth1 = health1 - outcome;
System.out.println("Your attack does " + outcome + " damage!");
System.out.println("");
System.out.print("Warrior Health: " + newHealth1 + "\n");
break;
}
while(newHealth1 == 0);
//System.out.println("Gladiator is the winner!");
}
void calculateWinner1()
{
do
{
outcome1 = hit.nextInt(100) - hit.nextInt(15);
newHealth = health - outcome1;
System.out.println("Your attack does " + outcome1 + " damage!");
System.out.println("");
System.out.print("Gladiator Health: " + newHealth + "\n\n");
break;
}
while(newHealth == 0);
//System.out.println("Warrior is the winner!");
}
void calculateHealth()
{
do
{
newHealth = health + hit.nextInt(35);
System.out.println("You drink the potion.");
System.out.println("Your health is now at " + newHealth + "!");
break;
}
while(newHealth1 > 0 && newHealth > 0);
}
void calculateHealth1()
{
do
{
newHealth1 = health1 + hit.nextInt(35);
System.out.println("You drink the potion.");
System.out.println("Your health is now at " + newHealth1 + "!");
break;
}
while(newHealth1 > 0 && newHealth > 0);
}
}
import java.util.Random;
import java.util.Scanner;
public class DuelMain
{
public static void main(String[] args)
{
// Random + Scanner
Random hit = new Random();
Random hit1 = new Random();
Scanner input = new Scanner(System.in);
// String + Int
String player1name = "";
String player2name = "";
int restart;
int player1option;
int player2option;
int fight = 0;
Duel warrior = new Duel();
Duel gladiator = new Duel();
gladiator.attack = hit.nextInt(100);
gladiator.defense = hit.nextInt(15);
gladiator.health = 1000;
warrior.attack1 = hit1.nextInt(100);
warrior.defense1 = hit1.nextInt(15);;
warrior.health1 = 1000;
// Printing Names
System.out.print("Gladiator Enter Name: ");
player1name = input.nextLine();
System.out.print("Warrior Enter Name: ");
player2name = input.nextLine();
while(fight == 0)
{
// Choose Moves (Player1)
System.out.printf("%n%s, Choose Your Move! \n", player1name);
System.out.println("1: Thrust 2: Slice 3: Drink Potion");
player1option = input.nextInt();
if(player1option == 1)
{
System.out.printf("You Thrust Your Sword At %s! \n", player2name);
warrior.attack1 = hit1.nextInt(100);
warrior.defense1 = hit1.nextInt(15);
warrior.health1 = 1000;
warrior.calculateWinner();
}
if(player1option == 2)
{
System.out.printf("You Slice Your Sword At %s! \n", player2name);
warrior.attack1 = hit1.nextInt(100);
warrior.defense1 = hit1.nextInt(15);
warrior.health1 = 1000;
warrior.calculateWinner();
}
if(player1option == 3)
{
warrior.calculateHealth();
}
// Choose Moves (Player2)
System.out.printf("%n%s, Choose Your Move! \n", player2name);
System.out.println("1: Thrust 2: Slice 3: Drink Potion");
player2option = input.nextInt();
if(player2option == 1)
{
System.out.printf("You Thrust Your Sword At %s! \n", player1name);
gladiator.attack = hit.nextInt(100);
gladiator.defense = hit.nextInt(15);
gladiator.health = 1000;
gladiator.calculateWinner1();
}
if(player2option == 2)
{
System.out.printf("You Slice Your Sword At %s! \n", player1name);
gladiator.attack = hit.nextInt(100);
gladiator.defense = hit.nextInt(15);
gladiator.health = 1000;
gladiator.calculateWinner1();;
}
if(player2option == 3)
{
gladiator.calculateHealth1();
}
}
}
}
【问题讨论】:
-
删除
security标签考虑到这个问题绝对没有任何关系。
标签: java