怎么用java写一个简单的rouge游戏
时间: 2023-12-01 09:02:34 浏览: 70
Rouge游戏是一个基于文本的角色扮演游戏,玩家需要探索地下城,打败敌人并收集宝藏。以下是一个简单的Java程序,可以创建一个基本的Rouge游戏:
```java
import java.util.Scanner;
import java.util.Random;
public class RougeGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int playerHealth = 100;
int playerAttack = 10;
int playerDefense = 5;
int playerGold = 0;
int level = 1;
boolean gameOver = false;
System.out.println("Welcome to Rouge Game!");
System.out.println("You are currently in level " + level);
while (!gameOver) {
int enemyHealth = rand.nextInt(50) + (level * 10);
int enemyAttack = rand.nextInt(10) + (level * 2);
int enemyDefense = rand.nextInt(5) + (level * 2);
System.out.println("An enemy appeared! Defeat it to move to the next level.");
while (enemyHealth > 0) {
System.out.println("Player health: " + playerHealth);
System.out.println("Enemy health: " + enemyHealth);
System.out.println("Choose your action:");
System.out.println("1. Attack");
System.out.println("2. Defend");
int choice = input.nextInt();
if (choice == 1) {
int damage = playerAttack - enemyDefense;
if (damage > 0) {
enemyHealth -= damage;
System.out.println("You dealt " + damage + " damage to the enemy!");
} else {
System.out.println("Your attack had no effect on the enemy.");
}
} else if (choice == 2) {
int defense = playerDefense * 2;
System.out.println("You defended and increased your defense by " + defense + ".");
playerDefense += defense;
} else {
System.out.println("Invalid choice! Please choose again.");
}
if (enemyHealth > 0) {
int damage = enemyAttack - playerDefense;
if (damage > 0) {
playerHealth -= damage;
System.out.println("The enemy dealt " + damage + " damage to you!");
} else {
System.out.println("The enemy's attack had no effect on you.");
}
}
if (playerHealth <= 0) {
System.out.println("Game over! You died.");
gameOver = true;
break;
}
}
if (!gameOver) {
int gold = rand.nextInt(10) + (level * 5);
System.out.println("You defeated the enemy and earned " + gold + " gold!");
playerGold += gold;
System.out.println("Current gold: " + playerGold);
level++;
System.out.println("You advanced to level " + level + "!");
}
}
}
}
```
这个程序使用了Scanner类和Random类,用于获取玩家输入和产生随机数。游戏主要包括玩家和敌人的属性和战斗过程。每次战斗结束后,玩家可以收集金币并进入下一级别。游戏在玩家死亡或退出时结束。这只是一个简单的Rouge游戏,可以根据需要进行修改和扩展。
阅读全文