feat: snake game

feat: unit.random
feat: ui.center
This commit is contained in:
Mahdi Dibaiee
2015-12-12 09:44:22 +03:30
parent f34f78112e
commit 6ef4f8890f
13 changed files with 468 additions and 19 deletions

View File

@ -45,10 +45,15 @@ var Interface = (function () {
this.cursor = (0, _ansi2.default)(this.output).hide();
this.columns = this.output.columns;
this.rows = this.output.rows;
this.input.addListener('data', function (data) {
var always = listeners.filter(function (listener) {
return listener.key === '';
});
always.forEach(function (listener) {
return listener.fn();
});
var key = Object.keys(keys).find(function (value, i) {
return keys[value] === data;
});
@ -80,8 +85,30 @@ var Interface = (function () {
}, {
key: 'onKey',
value: function onKey(key, fn) {
if (typeof key === 'function') {
fn = key;
key = '';
}
listeners.push({ key: key, fn: fn });
}
}, {
key: 'columns',
get: function get() {
return this.output.columns;
}
}, {
key: 'rows',
get: function get() {
return this.output.rows;
}
}, {
key: 'center',
get: function get() {
return {
x: this.output.columns / 2,
y: this.output.rows / 2
};
}
}]);
return Interface;

View File

@ -54,6 +54,12 @@ var Unit = (function () {
this.x = x;
this.y = y;
}
}, {
key: 'random',
value: function random() {
this.x = Math.max(1, Math.floor(Math.random() * this.output.columns));
this.y = Math.max(1, Math.floor(Math.random() * this.output.rows));
}
}, {
key: 'speed',
value: function speed() {