repl.it
@SagaciousPan/

Text Adventure Version 0.1

Nodejs

A new unnamed text adventure game.

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Controller {
  constructor ({speed=75, fastMode=false}={}) {
    this.fastMode = fastMode;
    this.speed = speed;
  }
  async typeToConsole(text="No Text",newLine=true,pause=500,speed=this.speed) {
    if(this.fastMode==true){
      console.log(text);
      await sleep(500);
      return;
    }
    var textLength = text.length;
    var interval = setInterval(function(){
      textLength--;
      process.stdout.write(text[0]);
      text = text.slice(1, text.length);
      if(0 == textLength) {
        if(newLine)console.log();
        clearInterval(interval);
      }
    },speed);
    await sleep(text.length*mainController.speed+pause);
  }
}

function sleep(ms){
    return new Promise(resolve=>{
        setTimeout(resolve,ms)
    })
}

// instantiate mainController as Controller using default values
const mainController = new Controller({fastMode:true}); // {fastMode:false}

async function main(){
  process.stdout.write('\x1b[33m'); // yellow text
  await mainController.typeToConsole('Welcome to my unnamed text adventure.');
  await mainController.typeToConsole('Thoughout your adventure many encounters await you.');
  await mainController.typeToConsole('Some good...');
  await mainController.typeToConsole('and some...');
  await mainController.typeToConsole('well, ',false,1000);
  await mainController.typeToConsole('not so good.');
  console.log();
  await mainController.typeToConsole('I digress, ',false,1000);
  await mainController.typeToConsole('Good Luck and ',false,1000);
  process.stdout.write('\x1b[0m'); // reset color
  process.stdout.write('\x1b[31m'); // red text
  await mainController.typeToConsole('try your best not to die',false,100);
  process.stdout.write('\x1b[0m'); // reset color
  process.stdout.write('\x1b[33m.'); // yellow text
  await sleep(3000);
  var i=45;do{console.log();i--;}while(i>0);

}main();



// Everyone that has helped me so far:
//  mwilki7
//  theangryepicbanana
//  kurry Ƹ̵̡Ӝ̵̨̄Ʒ