java回合制游戏代码
时间: 2023-07-09 18:53:11 浏览: 71
以下是一个简单的 Java 回合制游戏的代码示例:
```java
import java.util.Scanner;
public class TurnBasedGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int playerHealth = 100;
int enemyHealth = 100;
boolean playerTurn = true;
boolean gameEnd = false;
System.out.println("Welcome to the turn-based game!");
System.out.println("You will fight against an enemy with 100 health points.");
System.out.println("Your health points are also 100.");
System.out.println("You can either attack or defend in your turn.");
System.out.println("The enemy will always attack in its turn.");
System.out.println("Let's begin!\n");
while (!gameEnd) {
if (playerTurn) {
System.out.println("Your turn:");
System.out.println("Enter 1 to attack, enter 2 to defend.");
int choice = scanner.nextInt();
if (choice == 1) {
int damage = (int) (Math.random() * 10) + 10;
System.out.println("You attacked the enemy and dealt " + damage + " damage.");
enemyHealth -= damage;
} else if (choice == 2) {
System.out.println("You defended and received only half of the enemy's attack.");
playerHealth -= 5;
} else {
System.out.println("Invalid input, turn skipped.");
}
playerTurn = false;
} else {
System.out.println("\nEnemy's turn:");
int damage = (int) (Math.random() * 10) + 10;
System.out.println("The enemy attacked you and dealt " + damage + " damage.");
playerHealth -= damage;
playerTurn = true;
}
System.out.println("\nCurrent status:");
System.out.println("Your health points: " + playerHealth);
System.out.println("Enemy's health points: " + enemyHealth + "\n");
if (playerHealth <= 0) {
System.out.println("You lost the game.");
gameEnd = true;
} else if (enemyHealth <= 0) {
System.out.println("You won the game!");
gameEnd = true;
}
}
}
}
```
这个游戏有两个角色,玩家和敌人,每个角色都有100点生命值。玩家可以选择攻击或防御,敌人则总是攻击。每个回合,一个角色执行一个动作,然后另一个角色执行一个动作。如果一个角色的生命值降至0或以下,游戏结束。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)