我正在創建地下城遊戲。我希望玩家有機會發生變異,並隨着時間的推移造成健康損失。我的問題是,當序列被循環並且戰鬥階段結束時,突變再次被隨機化。意思是說,如果我發生突變並受到傷害並且敵人還活着,那麼它會重新執行「while」聲明並再次爲突變隨機化一個值。在循環中創建隨機值保持該值
if(input.equals("1")) {
int damageDealt = rand.nextInt(attackDamage);
int damageTaken = rand.nextInt(enemyAttackDamage);
int taken = rand.nextInt(mutation); //variable to see if a mutation occurs. Integer for mutation is 100.
if (taken < 10) {
// How do I tell the program to keep the number for taken?
// it will just re-loop and find a new rand.nextInt(mutation);
enemyHealth -= damageDealt; //damage taken for enemy
health -= damageTaken; //damage taken for player
health -= rand.nextInt(maxMutationDamage); //damage taken from mutation. maxMutationDamage is 15
JOptionPane.showMessageDialog(null, "You hit the " + enemy + " for " + damageDealt + " damage." + "\nYou receieved " + damageTaken + " damage." + "\nyour mutation did " + taken + " damage.");
}
else {
// I added this so that if the mutation didn't occur
// the calculations for the fight still renders.
enemyHealth -= damageDealt;
health -= damageTaken;
JOptionPane.showMessageDialog(null, "You hit the " + enemy + " for " + damageDealt + " damage." + "\nYou receieved " + damageTaken + " damage.");
}
if(health < 1) {
JOptionPane.showMessageDialog(null, "you have taken to much damage!");
break;
}
}
else if (input.equals("2")) {
if(numBandages > 0) {
health += bandagesHealAmount;
numBandages --;
JOptionPane.showMessageDialog(null, "You bandaged yourself for " + bandagesHealAmount + "."
+"\nYou now have " + health + " HP."
+"\nYou have " + numBandages + " bandages left.");
}
else {
JOptionPane.showMessageDialog(null, "You have no bandages in inventory to heal!");
}
}
else if (input.equals("3")) {
JOptionPane.showMessageDialog(null, "You ran away from the " + enemy + "!");
continue GAME; //it resets the sequence of the dungeon to a new fight
}
還,如果你們能幫助,我怎樣才能使它即使玩家跑掉,並且不癒合,他仍然得到的通過突變受傷?
只有在癒合完成後,突變纔會停止。
那麼你什麼時候需要指定一個新的隨機值?顯然不是每次執行輸入「1」的動作 - 但是何時? – laune
while循環在哪裏? – chenchuk
使用[編輯]進行編輯。 –