typescript-nodeprojects-adventure-game
Version:
This code is for a text-based fighting game written in TypeScript. Players choose an opponent from a list and engage in a turn-based battle. Each turn, the player can attack, drink a health potion, or run away. The outcome of an attack is random, with a 5
237 lines (236 loc) • 8.77 kB
JavaScript
import chalk from "chalk";
import inquirer from "inquirer";
// classes Player & Opponent
class Player {
name;
fuel = 100;
constructor(name) {
this.name = name;
}
fuelDecrease() {
let fuel = this.fuel - 25;
this.fuel = fuel;
}
fuelIncrease() {
this.fuel = 100;
}
}
class Opponent {
name;
fuel = 100;
constructor(name) {
this.name = name;
}
fuelDecrease() {
let fuel = this.fuel - 25;
this.fuel = fuel;
}
}
// Player Name & Opponent Select
let player = await inquirer.prompt({
type: "input",
name: "name",
message: "Please Enter Your Name:"
});
let opponent = await inquirer.prompt({
type: "list",
name: "select",
message: "Select Your Opponent",
choices: ["Lucifer", "Harley Quinn", "Pablo Escobar", "Kratos", "Ra's al Ghul"]
});
// Gather Data
let p1 = new Player(player.name);
let o1 = new Opponent(opponent.select);
do {
// Lucifer
if (opponent.select == "Lucifer") {
let ask = await inquirer.prompt({
type: "list",
name: "opt",
message: "Select Your Opponent",
choices: ["Attack", "Drink Portion", "Run for your Life.."],
});
if (ask.opt == "Attack") {
let num = Math.floor(Math.random() * 2);
if (num > 0) {
p1.fuelDecrease();
console.log(chalk.bold.red(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.green(`${o1.name} fuel is ${o1.fuel}`));
if (p1.fuel <= 0) {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
if (num <= 0) {
o1.fuelDecrease();
console.log(chalk.bold.green(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.red(`${o1.name} fuel is ${o1.fuel}`));
if (o1.fuel <= 0) {
console.log(chalk.green.bold.italic("You Win"));
process.exit();
}
}
}
if (ask.opt == "Drink Portion") {
p1.fuelIncrease();
console.log(chalk.bold.italic.green(`You Drink Health Portion Your Fuel Is ${p1.fuel}`));
}
if (ask.opt == "Run For Your Life..") {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
// Harley Quinn
if (opponent.select == "Harley Quinn") {
let ask = await inquirer.prompt({
type: "list",
name: "opt",
message: "Select Your Opponent",
choices: ["Attack", "Drink Portion", "Run for your Life.."],
});
if (ask.opt == "Attack") {
let num = Math.floor(Math.random() * 2);
if (num > 0) {
p1.fuelDecrease();
console.log(chalk.bold.red(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.green(`${o1.name} fuel is ${o1.fuel}`));
if (p1.fuel <= 0) {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
if (num <= 0) {
o1.fuelDecrease();
console.log(chalk.bold.green(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.red(`${o1.name} fuel is ${o1.fuel}`));
if (o1.fuel <= 0) {
console.log(chalk.green.bold.italic("You Win"));
process.exit();
}
}
}
if (ask.opt == "Drink Portion") {
p1.fuelIncrease();
console.log(chalk.bold.italic.green(`You Drink Health Portion Your Fuel Is ${p1.fuel}`));
}
if (ask.opt == "Run For Your Life..") {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
// Pablo Escobar
if (opponent.select == "Pablo Escobar") {
let ask = await inquirer.prompt({
type: "list",
name: "opt",
message: "Select Your Opponent",
choices: ["Attack", "Drink Portion", "Run for your Life.."],
});
if (ask.opt == "Attack") {
let num = Math.floor(Math.random() * 2);
if (num > 0) {
p1.fuelDecrease();
console.log(chalk.bold.red(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.green(`${o1.name} fuel is ${o1.fuel}`));
if (p1.fuel <= 0) {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
if (num <= 0) {
o1.fuelDecrease();
console.log(chalk.bold.green(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.red(`${o1.name} fuel is ${o1.fuel}`));
if (o1.fuel <= 0) {
console.log(chalk.green.bold.italic("You Win"));
process.exit();
}
}
}
if (ask.opt == "Drink Portion") {
p1.fuelIncrease();
console.log(chalk.bold.italic.green(`You Drink Health Portion Your Fuel Is ${p1.fuel}`));
}
if (ask.opt == "Run For Your Life..") {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
// Kratos
if (opponent.select == "Kratos") {
let ask = await inquirer.prompt({
type: "list",
name: "opt",
message: "Select Your Opponent",
choices: ["Attack", "Drink Portion", "Run for your Life.."],
});
if (ask.opt == "Attack") {
let num = Math.floor(Math.random() * 2);
if (num > 0) {
p1.fuelDecrease();
console.log(chalk.bold.red(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.green(`${o1.name} fuel is ${o1.fuel}`));
if (p1.fuel <= 0) {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
if (num <= 0) {
o1.fuelDecrease();
console.log(chalk.bold.green(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.red(`${o1.name} fuel is ${o1.fuel}`));
if (o1.fuel <= 0) {
console.log(chalk.green.bold.italic("You Win"));
process.exit();
}
}
}
if (ask.opt == "Drink Portion") {
p1.fuelIncrease();
console.log(chalk.bold.italic.green(`You Drink Health Portion Your Fuel Is ${p1.fuel}`));
}
if (ask.opt == "Run For Your Life..") {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
// Ra's al Ghul
if (opponent.select == "Ra's al Ghul") {
let ask = await inquirer.prompt({
type: "list",
name: "opt",
message: "Select Your Opponent",
choices: ["Attack", "Drink Portion", "Run for your Life.."],
});
if (ask.opt == "Attack") {
let num = Math.floor(Math.random() * 2);
if (num > 0) {
p1.fuelDecrease();
console.log(chalk.bold.red(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.green(`${o1.name} fuel is ${o1.fuel}`));
if (p1.fuel <= 0) {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
if (num <= 0) {
o1.fuelDecrease();
console.log(chalk.bold.green(`${p1.name} fuel is ${p1.fuel}`));
console.log(chalk.bold.red(`${o1.name} fuel is ${o1.fuel}`));
if (o1.fuel <= 0) {
console.log(chalk.green.bold.italic("You Win"));
process.exit();
}
}
}
if (ask.opt == "Drink Portion") {
p1.fuelIncrease();
console.log(chalk.bold.italic.green(`You Drink Health Portion Your Fuel Is ${p1.fuel}`));
}
if (ask.opt == "Run For Your Life..") {
console.log(chalk.red.bold.italic("You Loose, Better Luck Next Time"));
process.exit();
}
}
} while (true);