Skip to content

Instantly share code, notes, and snippets.

@luisangelma
Created August 4, 2015 00:38
Show Gist options
  • Select an option

  • Save luisangelma/dcd1662edb55856fe563 to your computer and use it in GitHub Desktop.

Select an option

Save luisangelma/dcd1662edb55856fe563 to your computer and use it in GitHub Desktop.
RPG Game
var readlineSync = require('readline-sync');
var name = readlineSync.question('Hey stranger, what is your name?: ');
var wildAdventure = function() {
var self = this;
var enemies = ['White Walker', 'White Orc', 'Wild Dragon'];
var enemy;
var randomEnemy = function() {
return Math.floor(Math.random() * ((enemies.length - 1) - 0 + 1)) + 0;
};
var enemy = enemies[randomEnemy()];
this.user = name;
this.userIsActive = true;
this.userHp = 50;
this.enemyHp = 50;
this.enemyIsActive = null;
this.enemyCount = 1;
this.userAction = function() {
var action = readlineSync.question('What do you want to do?, enter "r" to run or "a" to attack: ').toLowerCase();
var userAttackPower = Math.floor(Math.random() * (50 - 25 + 1) + 25);
switch(action) {
case 'r':
self.userIsActive = false;
console.log('You\'re a chicken for running away and not killing the ' + enemy + '\nThe Lord of Light has sent you to Mordor');
setTimeout(function(){
console.log(':)');
}, 4000);
setTimeout(function(){
console.log(':|');
}, 5000);
setTimeout(function(){
console.log(':o');
}, 6000);
setTimeout(function(){
console.log(':(');
}, 7000);
setTimeout(function(){
console.log('A hungry Orc has eaten you!!!');
}, 8000);
break;
case 'a':
self.enemyHp-=userAttackPower;
console.log('You just attacked ' + enemy + ' for ' + userAttackPower + ' attack power');
break;
default:
console.log('Please enter a valid key');
}
};
this.enemyAction = function() {
if(self.userIsActive === true && self.enemyHp > 0) {
var enemyAttackPower = Math.floor(Math.random() * (50 - 50 + 1) + 50);
self.userHp-=enemyAttackPower;
console.log(enemy + ' just attacked you for ' + enemyAttackPower + ' attack power');
} else if (self.enemyHp <= 0) {
self.enemyIsActive = false;
self.enemyCount++;
console.log(self.user + ' has killed ' + enemy);
} else if(self.userHp <= 0) {
self.userIsActive = false;
}
};
this.processAttack = function() {
if(self.enemyIsActive) {
while(self.enemyHp > 0 && self.userIsActive === true) {
self.userAction();
self.enemyAction();
}
} else if(self.userIsActive === false && self.enemyHp > 0) {
console.log(self.enemy + ' has killed you');
}
};
this.initialize = function() {
self.enemyIsActive = true;
readlineSync.keyIn('Press any key to walk: ');
console.log('+++++' + randomEnemy());
console.log('Walking...');
console.log('Holy smokes, A ' + enemy + ' has appeared');
self.processAttack();
while(self.enemyIsActive === false && self.enemyCount <= 3) {
console.log('==============================================');
readlineSync.keyIn('Press any key to walk: ');
randomEnemy();
console.log('+++++' + randomEnemy());
console.log('Walking...');
console.log('Holy smokes, A ' + enemy + ' has appeared');
self.enemyIsActive = true;
self.enemyHp = 50;
self.processAttack();
}
if (self.enemyIsActive === false) {
console.log(self.user + ' is the master of wild adventures!');
}
};
this.initialize();
};
wildAdventure()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment