How do I attack a monster in my Java text RPG?

0
0
Asked By CodeNinja42 On

Hey everyone! I'm just starting out with Java and have been working on a text-based RPG as my first project. The game has you follow a path where you encounter items and monsters, and I want to add a feature for attacking monsters. I've set up some basic variables like PlayerHP, SwordDmg, Slime1HP, and Slime1Dmg. I know I need a loop to manage the attack process, printing out messages about damage dealt and health remaining until the slime is defeated. However, I'm struggling with how to structure this loop properly. Any help would be greatly appreciated!

1 Answer

Answered By JavaWhiz On

Instead of a for loop, you might want to use a while loop here since you don't know how many attacks it will take to defeat the slime. For example, your loop would look something like this:

```java
while (slime1HP > 0) {
PlayerHP -= SlimeDmg;
Slime1HP -= SwordDmg;
System.out.println("You dealt " + SwordDmg + " damage to the Slime. It has " + Slime1HP + " HP left. The slime dealt " + SlimeDmg + " damage to you. You have " + PlayerHP + " HP left.");
}
```
This way, the loop continues until the slime's HP reaches zero!

NewbieCoder12 -

Thanks for the clarification! I wasn't sure if a while loop was the right choice, but it sounds like it is. I'll give it a try!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.