Last active
October 1, 2018 20:06
-
-
Save joshbeckman/3bef200aed6d912c1b93e7fd75c74f7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var readline = require('readline'); | |
var jumpEmpty = '▖▘▝'.split(''); | |
var block = '\x1b[31m▖\x1b[0m'; | |
var space = ' '; | |
var Game = function(options){ | |
if(!(this instanceof Game)) return new Game(options) | |
options = options || {}; | |
this.width = options.width || 50; | |
this.world = []; | |
var wdx = 1; | |
while (wdx < this.width) { | |
this.world.push(space); | |
wdx++; | |
} | |
this.blocks = options.blocks || generateBlocks(200); | |
this.blockIndex = 0; | |
this.stream = process.stdout; | |
this.position = options.position || 20; | |
this.delay = 1000 / (options.speed || 10); | |
this.state = { | |
cursor: 0, | |
jumping: false, | |
jumpPosition: 0, | |
jumpSequence: jumpEmpty, | |
}; | |
this.rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
this.rl.on('close', () => { | |
process.exit(0); | |
}); | |
}; | |
Game.prototype.onTick = function () { | |
this.clearLine(this.stream); | |
var world = this.world.concat([]); | |
world.splice(this.position, 1, this.getPlayer()); | |
this.state.jumping = false; | |
this.stream.write(world.join('')); | |
}; | |
Game.prototype.getPlayer = function() { | |
var pos = this.state.jumpPosition; | |
if (this.state.jumping || pos) { | |
this.state.jumpPosition++; | |
} | |
if (this.state.jumpPosition > 2) { | |
this.state.jumpPosition = 0; | |
} | |
return this.state.jumpSequence[pos]; | |
}; | |
Game.prototype.lose = function() { | |
console.log('Boom!'); | |
console.log(`Score: ${this.state.cursor}`); | |
this.stop(); | |
process.exit(0); | |
}; | |
Game.prototype.colliding = function() { | |
return this.world[this.position] !== space | |
&& this.state.jumpPosition == 0; | |
}; | |
Game.prototype.start = function() { | |
var self = this; | |
var iteration = function() { | |
self.world.shift(); | |
if (self.colliding()) { | |
return self.lose(); | |
} | |
var land = self.blocks[self.blockIndex]; | |
if (land == self.state.cursor) { | |
self.world.push(block); | |
self.blockIndex++; | |
} else { | |
self.world.push(space); | |
} | |
self.onTick(); | |
self.state.cursor++; | |
}; | |
readline.emitKeypressEvents(process.stdin); | |
if (process.stdin.isTTY) | |
process.stdin.setRawMode(true); | |
process.stdin.on('keypress', this.handleKey.bind(this)); | |
iteration(); | |
this.id = setInterval(iteration, this.delay); | |
return this; | |
}; | |
Game.prototype.handleKey = function(a, key) { | |
if (key.name !== 'up' | |
&& key.name !== 'w') return; | |
if (this.state.jumping || this.state.jumpPosition) return; | |
this.state.jumping = true; | |
} | |
Game.prototype.stop = function(clear) { | |
clearInterval(this.id); | |
this.id = undefined; | |
if (clear) { | |
this.clearLine(this.stream); | |
} | |
return this; | |
}; | |
Game.prototype.clearLine = function(stream) { | |
readline.clearLine(stream, 0); | |
readline.cursorTo(stream, 0); | |
return this; | |
}; | |
function generateBlocks(len) { | |
var blocks = []; | |
var start = 1; | |
var used = 0; | |
for (var i = 1; i < len; i++) { | |
if (Math.random() > 0.75 && !used) { | |
blocks.push(i); | |
used = 3; | |
} else { | |
used = Math.max(0, used - 1); | |
} | |
} | |
return blocks; | |
} | |
(new Game({ speed: 11 })).start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment