// Game Variables
let player = {
name: "Goku",
level: 1,
maxHealth: 100,
currentHealth: 100,
attack: 10,
defense: 5,
ki: 0,
experience: 0,
inventory: [],
};
let enemies = [
{ name: "Raditz", maxHealth: 150, attack: 15, defense: 8 },
{ name: "Nappa", maxHealth: 200, attack: 20, defense: 12 },
{ name: "Vegeta", maxHealth: 250, attack: 25, defense: 15 },
];
let currentEnemy = null;
// Game Functions
function startGame() {
console.log("Welcome to the Dragon Ball Z RPG!");
console.log("You are Goku, the Saiyan warrior.");
chooseAction();
}
function chooseAction() {
console.log("\nWhat would you like to do?");
console.log("1. Train");
console.log("2. Fight");
console.log("3. Inventory");
console.log("4. Status");
let choice = prompt("Enter a number: ");
switch (choice) {
case "1":
train();
break;
case "2":
fight();
break;
case "3":
showInventory();
break;
case "4":
showStatus();
break;
default:
console.log("Invalid choice.");
chooseAction();
}
}
function train() {
// Simple training logic
console.log("You train hard, increasing your strength and speed.");
player.level++;
player.attack += 2;
player.defense += 1;
player.maxHealth += 10;
player.currentHealth = player.maxHealth;
console.log(`Level: ${player.level}`);
console.log(`Attack: ${player.attack}`);
console.log(`Defense: ${player.defense}`);
console.log(`Max Health: ${player.maxHealth}`);
chooseAction();
}
function fight() {
if (currentEnemy) {
// Continue fighting existing enemy
battle();
} else {
// Choose enemy
console.log("\nChoose your opponent:");
for (let i = 0; i < enemies.length; i++) {
console.log(`${i + 1}. ${enemies[i].name}`);
}
let enemyIndex = prompt("Enter a number: ");
currentEnemy = enemies[enemyIndex - 1];
console.log(`You are now facing ${currentEnemy.name}!`);
battle();
}
}
function battle() {
while (player.currentHealth > 0 && currentEnemy.maxHealth > 0) {
// Player turn
let playerDamage = calculateDamage(player.attack, currentEnemy.defense);
currentEnemy.maxHealth -= playerDamage;
console.log(`You dealt ${playerDamage} damage to ${currentEnemy.name}.`);
console.log(`${currentEnemy.name} has ${currentEnemy.maxHealth} health left.`);
// Enemy turn
if (currentEnemy.maxHealth > 0) {
let enemyDamage = calculateDamage(currentEnemy.attack, player.defense);
player.currentHealth -= enemyDamage;
console.log(`${currentEnemy.name} dealt ${enemyDamage} damage to you.`);
console.log(`You have ${player.currentHealth} health left.`);
}
}
if (player.currentHealth <= 0) {
console.log("You have been defeated! Game Over.");
// Handle game over
} else {
console.log(`You defeated ${currentEnemy.name}!`);
// Handle victory
}
currentEnemy = null;
chooseAction();
}
function calculateDamage(attack, defense) {
return Math.max(attack - defense, 1);
}
function showInventory() {
console.log("\nYour inventory:");
// Display inventory items (future implementation)
chooseAction();
}
function showStatus() {
console.log("\nYour Status:");
console.log(`Level: ${player.level}`);
console.log(`Attack: ${player.attack}`);
console.log(`Defense: ${player.defense}`);
console.log(`Max Health: ${player.maxHealth}`);
console.log(`Current Health: ${player.currentHealth}`);
console.log(`Experience: ${player.experience}`);
chooseAction();
}
// Start the game
startGame();
```
Explanation:
* Variables:
* `player`: Stores player information like name, level, stats, and inventory.
* `enemies`: Array of enemy objects, each with their own stats.
* `currentEnemy`: Tracks the currently active enemy.
* Functions:
* `startGame()`: Displays the welcome message and starts the game loop.
* `chooseAction()`: Prompts the player to choose an action.
* `train()`: Simulates training, increasing player stats.
* `fight()`: Handles enemy selection and battle sequence.
* `battle()`: Implements the battle loop, alternating between player and enemy turns.
* `calculateDamage()`: Calculates the damage dealt based on attacker's attack and defender's defense.
* `showInventory()`: Displays the player's inventory (not implemented in this basic example).
* `showStatus()`: Displays the player's current stats.
Limitations:
* Basic mechanics: This is a very simple implementation. It lacks features like:
* Skill trees
* Item use and effects
* Dialogue and story elements
* Visuals and animations
* Saving and loading game progress
* Text-based: The game is text-based, making it less engaging.
* Limited enemy variety: Only a few basic enemies are included.
Possible Enhancements:
* Add more enemies: Include a wider variety of enemies with different abilities and stats.
* Implement skill trees: Allow players to learn new abilities and skills.
* Incorporate item use: Add items with different effects that can be used during battle.
* Develop a story: Create a storyline with dialogue and events.
* Add visuals and animations: Use HTML, CSS, and JavaScript to create a visual user interface.
* Save and load game progress: Use local storage or a database to save and load game data.
To make this game more like the old DBZ RPGs, you could:
* Add a turn-based battle system: Where players choose moves from a menu (like "Kamehameha", "Dragon Fist", etc.).
* Include "Ki" mechanics: Allow players to gather Ki to use powerful attacks.
* Implement "Super Saiyan" transformations: Granting temporary stat boosts and special attacks.
* Create a more complex world map: With locations to explore, towns to visit, and events to encounter.
Remember, this is just a basic framework. You can add your creativity and code to build a much richer and engaging DBZ RPG.