diff --git a/build/classes/interface.js b/build/classes/interface.js new file mode 100644 index 0000000..9dbdb7a --- /dev/null +++ b/build/classes/interface.js @@ -0,0 +1,152 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _ansi = require('ansi'); + +var _ansi2 = _interopRequireDefault(_ansi); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var _process = process, + stdout = _process.stdout, + stdin = _process.stdin; + + +var listeners = []; + +var prefix = '\x1B'; +var keys = { + right: prefix + '[C', + up: prefix + '[A', + left: prefix + '[D', + down: prefix + '[B', + exit: '\x03', + space: ' ' +}; + +var Interface = function () { + function Interface() { + var _this = this; + + var output = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : stdout; + var input = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : stdin; + + _classCallCheck(this, Interface); + + this.output = output; + this.input = input; + + this.input.setRawMode(true); + this.input.setEncoding('utf8'); + + this.cursor = (0, _ansi2.default)(this.output).hide(); + + 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; + }); + + if (key === 'exit') { + _this.output.write('\x1B[2J\x1B[0;0H'); + process.exit(); + } + + var match = listeners.filter(function (listener) { + return listener.key === key || listener.key === data; + }); + + match.forEach(function (listener) { + return listener.fn(); + }); + }); + } + + _createClass(Interface, [{ + key: 'clear', + value: function clear() { + this.output.write('\x1B[2J\x1B[0;0H'); + } + }, { + key: 'write', + value: function write() { + var _cursor; + + (_cursor = this.cursor).write.apply(_cursor, arguments); + } + }, { + key: 'onKey', + value: function onKey(key, fn) { + if (typeof key === 'function') { + fn = key; + key = ''; + } + listeners.push({ key: key, fn: fn }); + } + }, { + key: 'line', + value: function line(from, to) { + var delta = { + x: to.x - from.x, + y: to.y - from.y + }; + + var error = 0; + + var deltaerr = Math.abs(delta.y / delta.x); + + var y = from.y; + + + for (var x = from.x; x < to.x; x++) { + this.cursor.goto(x, y); + this.write('.'); + error += deltaerr; + + while (error >= 0.5) { + this.cursor.goto(x, y); + this.write('.'); + y += Math.sign(delta.y); + + error -= 1; + } + } + } + }, { + 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; +}(); + +exports.default = Interface; diff --git a/build/classes/tank.js b/build/classes/tank.js new file mode 100644 index 0000000..9a7e3c9 --- /dev/null +++ b/build/classes/tank.js @@ -0,0 +1,162 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Bullet = undefined; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _unit = require('./unit'); + +var _unit2 = _interopRequireDefault(_unit); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var BULLET_SPEED = 10; + +var Tank = function (_Unit) { + _inherits(Tank, _Unit); + + function Tank(ui) { + _classCallCheck(this, Tank); + + var _this = _possibleConstructorReturn(this, (Tank.__proto__ || Object.getPrototypeOf(Tank)).call(this, ui)); + + _this._angle = 0; + _this.bullets = []; + _this.health = 100; + + _this.size = { x: 5, y: 3 }; + return _this; + } + + _createClass(Tank, [{ + key: 'shoot', + value: function shoot() { + var bullet = new Bullet(this.output); + bullet.go(this.x + 2, this.y - 2); + bullet.velocity = this.normalize(); + + this.bullets.push(bullet); + } + }, { + key: 'draw', + value: function draw() { + if (this.dead) return; + + var x = this.x, + y = this.y; + + if (this.color && this.color[0] === '#') { + this.output.cursor.hex(this.color); + } else if (this.color) { + this.output.cursor[this.color](); + } + if (this.bold) this.output.cursor.bold(); + + x = Math.round(x) + 2; + y = Math.round(y) - 2; + + var cannon = void 0; + if (this.angle < 35) cannon = '_';else if (this.angle < 70) cannon = '/';else if (this.angle < 115) cannon = '|';else if (this.angle < 160) cannon = '\\';else cannon = '_'; + + this.output.cursor.goto(x + 2, y - 2); + this.output.write(cannon); + this.output.cursor.goto(x, y - 1); + this.output.write('.===.'); + this.output.cursor.goto(x, y); + this.output.write('o===o'); + this.output.cursor.reset(); + } + }, { + key: 'normalize', + value: function normalize() { + return [Math.cos(this._angle), Math.sin(this._angle) * -1]; + } + }, { + key: 'angle', + set: function set(deg) { + if (deg < 0) deg = 0; + if (deg > 180) deg = 180; + + this._angle = _radian(deg); + }, + get: function get() { + return _degree(this._angle); + } + }], [{ + key: 'radian', + value: function radian(deg) { + return _radian(deg); + } + }, { + key: 'degree', + value: function degree(rad) { + return _degree(rad); + } + }]); + + return Tank; +}(_unit2.default); + +exports.default = Tank; + +var Bullet = exports.Bullet = function (_Unit2) { + _inherits(Bullet, _Unit2); + + function Bullet(ui) { + _classCallCheck(this, Bullet); + + var _this2 = _possibleConstructorReturn(this, (Bullet.__proto__ || Object.getPrototypeOf(Bullet)).call(this, ui)); + + _this2.velocity = [0, 0]; + + _this2.shape = '•'; + _this2.color = 'red'; + + _this2.dieOnExit = true; + + _this2.gravity = [0.8, 0.8]; + _this2.acceleration = [0, 0.015]; + return _this2; + } + + _createClass(Bullet, [{ + key: 'speed', + value: function speed() { + this.velocity[0] += this.acceleration[0]; + this.velocity[1] += this.acceleration[1]; + + var x = this.velocity[0] * this.gravity[0]; + var y = this.velocity[1] * this.gravity[1]; + return [x, y]; + } + }, { + key: 'collides', + value: function collides(target) { + var targetX = Math.round(target.x); + var targetY = Math.round(target.y); + var x = Math.round(this.x); + var y = Math.round(this.y); + + return x > targetX - target.size.x && x < targetX + target.size.x && y > targetY - target.size.y && y < targetY + target.size.y; + } + }]); + + return Bullet; +}(_unit2.default); + +function _radian(deg) { + return deg * Math.PI / 180; +} + +function _degree(rad) { + return rad * 180 / Math.PI; +} diff --git a/build/classes/unit.js b/build/classes/unit.js new file mode 100644 index 0000000..a72f4bc --- /dev/null +++ b/build/classes/unit.js @@ -0,0 +1,128 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var Unit = function () { + function Unit(output) { + _classCallCheck(this, Unit); + + this._health = 0; + this.dead = false; + this.output = output; + + this.dieOnExit = false; + } + + _createClass(Unit, [{ + key: 'move', + value: function move(x, y) { + if (!x && !y) return this.move.apply(this, _toConsumableArray(this.speed())); + this.x += x; + this.y += y; + } + }, { + key: 'draw', + value: function draw() { + if (this.dead) return; + + var x = this.x, + y = this.y, + shape = this.shape; + + if (this.color && this.color[0] === '#') { + this.output.cursor.hex(this.color); + } else if (this.color) { + this.output.cursor[this.color](); + } + if (this.bold) this.output.cursor.bold(); + + this.output.cursor.goto(Math.round(x), Math.round(y)); + this.output.write(shape); + this.output.cursor.reset(); + } + }, { + key: 'go', + value: function go(x, y) { + 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() { + var signs = [Math.random() > 0.5 ? -1 : 1, Math.random() > 0.5 ? -1 : 1]; + return [Math.random() * signs[0], Math.random() * signs[1]]; + } + }, { + key: 'collides', + value: function collides(target) { + var _this = this; + + if (Array.isArray(target)) { + return target.find(function (t) { + return _this.collides(t); + }); + } + + var targetX = Math.round(target.x); + var targetY = Math.round(target.y); + var x = Math.round(this.x); + var y = Math.round(this.y); + + return x === targetX && y == targetY; + } + }, { + key: 'health', + set: function set(value) { + this._health = Math.max(0, value); + + if (this.health === 0) { + this.dead = true; + } + }, + get: function get() { + return this._health; + } + }, { + key: 'x', + set: function set(value) { + this._x = value; + + if (this.dieOnExit && this._x > this.output.columns) { + this.dead = true; + } + }, + get: function get() { + return this._x; + } + }, { + key: 'y', + set: function set(value) { + this._y = value; + + if (this.dieOnExit && this._y > this.output.rows) { + this.dead = true; + } + }, + get: function get() { + return this._y; + } + }]); + + return Unit; +}(); + +exports.default = Unit; diff --git a/build/locales/en.json b/build/locales/en.json new file mode 100644 index 0000000..f3be16f --- /dev/null +++ b/build/locales/en.json @@ -0,0 +1,17 @@ +{ + "snake": { + "score": "Score", + "gameOver": "Game Over!", + "anyKey": "Press any key to play again" + }, + "tanks": { + "playerWon": "Player %{num} won!", + "player": "Player %{number}", + "health": "Health %{value}", + "angle": "Angle %{value}" + }, + "spacecraft": { + "score": "Score" + } +} + diff --git a/build/locales/es.json b/build/locales/es.json new file mode 100644 index 0000000..04d9a79 --- /dev/null +++ b/build/locales/es.json @@ -0,0 +1,16 @@ +{ + "snake": { + "score": "Puntaje", + "gameOver": "¡Se acabó el juego!", + "anyKey": "Presioná cualquier tecla para jugar nuevamente" + }, + "tanks": { + "playerWon": "¡Ha ganado el jugador %{name}!", + "player": "Jugador %{number}", + "health": "Salud %{value}", + "angle": "Ángulo %{value}" + }, + "spacecraft": { + "score": "Puntaje" + } +} diff --git a/build/snake.js b/build/snake.js new file mode 100644 index 0000000..f0f119c --- /dev/null +++ b/build/snake.js @@ -0,0 +1,188 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); + +exports.default = function (i18n) { + loop(i18n); + + ui.onKey(function () { + if (!stop) return; + + stop = false; + snake = []; + head = createPart(); + head.color = '#71da29'; + createPart(); + createPart(); + + score = 0; + + point.random(); + + loop(i18n); + }); +}; + +var _unit = require('./classes/unit'); + +var _unit2 = _interopRequireDefault(_unit); + +var _interface = require('./classes/interface'); + +var _interface2 = _interopRequireDefault(_interface); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var FRAME = 100; +var ui = new _interface2.default(); + +var snake = []; +var UP = 0; +var DOWN = 1; +var LEFT = 2; +var RIGHT = 3; +var head = createPart(); +head.color = '#71da29'; +head.dieOnExit = true; +createPart(); +createPart(); + +var point = new _unit2.default(ui); +point.shape = '+'; +point.color = '#f3ff6a'; +point.random(); + +var score = 0; + +var stop = false; +function loop(i18n) { + if (stop) return; + ui.clear(); + + point.draw(); + + snake.forEach(function (part, i) { + part.draw(); + + if (i > 0) part.findWay(i); + + part.move(); + }); + + if (head.collides(point)) { + point.random(); + createPart(); + score++; + + FRAME -= 5; + } + + if (head.collides(snake.slice(2))) { + gameover(i18n); + } + + ui.cursor.goto(0, 0).yellow().write(i18n.t('snake.score') + ': ' + score); + ui.cursor.reset(); + + setTimeout(loop, FRAME, i18n); +} + +ui.onKey('right', function () { + changeDirection(RIGHT); +}); +ui.onKey('down', function () { + changeDirection(DOWN); +}); +ui.onKey('up', function () { + changeDirection(UP); +}); +ui.onKey('left', function () { + changeDirection(LEFT); +}); + +function changeDirection(dir) { + if (head.direction === UP && dir === DOWN || head.direction === DOWN && dir === UP || head.direction === LEFT && dir === RIGHT || head.direction === RIGHT && dir === LEFT) return; + head.direction = dir; +} + +function createPart() { + var part = new _unit2.default(ui); + var last = snake[snake.length - 1]; + + var direction = void 0; + if (!last) { + direction = UP; + } else { + direction = last.direction; + } + + part.shape = '•'; + part.color = '#bdfe91'; + part.direction = direction; + part.changeTo = null; + + part.findWay = function (i) { + var ahead = snake[i - 1]; + + if (this.changeTo !== null) { + this.direction = this.changeTo; + this.changeTo = null; + } + if (this.direction !== ahead.direction) { + this.changeTo = ahead.direction; + } + }; + + part.speed = function () { + var multiplier = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; + var direction = part.direction; + + var x = direction === LEFT ? -1 : direction === RIGHT ? 1 : 0; + var y = direction === UP ? -1 : direction === DOWN ? 1 : 0; + + return [x * multiplier, y * multiplier]; + }; + + var _part$speed = part.speed(), + _part$speed2 = _slicedToArray(_part$speed, 2), + dX = _part$speed2[0], + dY = _part$speed2[1]; + + dX *= -1; + dY *= -1; + + var x = last ? last.x + dX : ui.center.x; + var y = last ? last.y + dY : ui.center.y; + + part.go(x, y); + + snake.push(part); + return part; +} + +function gameover(i18n) { + var MSG = i18n.t('snake.gameOver'); + ui.cursor.goto(ui.center.x - MSG.length / 2, ui.center.y); + ui.cursor.red(); + ui.cursor.bold(); + ui.write(MSG); + + ui.cursor.reset(); + ui.cursor.hex('#f65590'); + var RETRY = i18n.t('snake.anyKey'); + ui.cursor.goto(ui.center.x - RETRY.length / 2, ui.center.y + 2); + ui.write(RETRY); + + FRAME = 100; + + stop = true; +} + +process.on('exit', function () { + ui.cursor.horizontalAbsolute(0).eraseLine(); + ui.cursor.show(); +}); diff --git a/build/spacecraft.js b/build/spacecraft.js new file mode 100644 index 0000000..fdad993 --- /dev/null +++ b/build/spacecraft.js @@ -0,0 +1,131 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +exports.default = function (i18n) { + setInterval(function (i18n) { + ui.clear(); + + player.draw(); + + missles.forEach(function (missle, i) { + missle.move(1, 0); + missle.draw(); + + var enemy = missle.collides(enemies); + if (enemy) { + enemy.killed = 1; + enemy.color = 'red'; + enemy.shape = '*'; + missle.dead = true; + + ENEMY_SPAWN_RATE -= 5; + + score++; + } + + if (missle.dead) { + missles.splice(i, 1); + } + }); + + enemies.forEach(function (enemy, i) { + // move with speed + enemy.move(); + enemy.draw(); + + if (enemy.dead) { + enemies.splice(i, 1); + } + + if (enemy.killed === 3) enemy.dead = true; + if (enemy.killed < 3) enemy.killed++; + }); + + ui.cursor.goto(0, 0).yellow().write(i18n.t('spacecraft.score') + ': ' + score); + ui.cursor.reset(); + }, FRAME, i18n); +}; + +var _unit = require('./classes/unit'); + +var _unit2 = _interopRequireDefault(_unit); + +var _interface = require('./classes/interface'); + +var _interface2 = _interopRequireDefault(_interface); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var FRAME = 20; +var ENEMY_SPAWN_RATE = 1000; +var RELOAD_TIME = 200; + +var ui = new _interface2.default(); + +var player = new _unit2.default(ui); +player.go(1, ui.center.y); +player.shape = '=>'; +player.color = '#77d6ff'; +player.bold = true; +player.canShoot = true; + +var explosion = new _unit2.default(ui); +explosion.dead = true; + +var missles = []; +var enemies = []; +var score = 0; + +ui.onKey('right', function () { + player.move(1, 0); +}); +ui.onKey('down', function () { + player.move(0, 1); +}); +ui.onKey('up', function () { + player.move(0, -1); +}); +ui.onKey('left', function () { + player.move(-1, 0); +}); + +ui.onKey('space', function () { + if (!player.canShoot) return; + + player.canShoot = false; + + var missle = new _unit2.default(ui); + missle.go(player.x, player.y); + missle.shape = '+'; + missle.dieOnExit = true; + + missles.push(missle); + + setTimeout(function () { + player.canShoot = true; + }, RELOAD_TIME); +}); + +(function loop() { + var enemy = new _unit2.default(ui); + enemy.go(Math.random() * ui.output.columns, 0); + enemy.shape = 'o'; + enemy.color = '#f7c71e'; + enemy.dieOnExit = true; + + enemy.speed = function () { + return [Math.random() > 0.9 ? 0.4 : 0, 0.06]; + }; + + enemies.push(enemy); + + setTimeout(loop, ENEMY_SPAWN_RATE); +})(); + +process.on('exit', function () { + ui.cursor.horizontalAbsolute(0).eraseLine(); + ui.cursor.show(); +}); diff --git a/build/tanks.js b/build/tanks.js new file mode 100644 index 0000000..e19a3a5 --- /dev/null +++ b/build/tanks.js @@ -0,0 +1,158 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +exports.default = function (i18n) { + loop(i18n); +}; + +var _tank = require('./classes/tank'); + +var _tank2 = _interopRequireDefault(_tank); + +var _interface = require('./classes/interface'); + +var _interface2 = _interopRequireDefault(_interface); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var FRAME = 50; +var ui = new _interface2.default(); +var immoblize = false; + +var one = new _tank2.default(ui); +one.go(10, ui.rows); + +var two = new _tank2.default(ui); +two.go(ui.columns - 10, ui.rows); + +var stop = false; + +function loop(i18n) { + if (stop) return; + + ui.clear(); + + if (one.dead || two.dead) { + var num = one.dead ? '2' : '1'; + var msg = i18n.t('tanks.playerWon', { num: num }); + ui.cursor.red(); + ui.cursor.bold(); + + ui.cursor.goto(ui.center.x - msg.length / 2, ui.center.y); + ui.write(msg); + + stop = true; + return; + } + + one.draw(); + one.bullets.forEach(function (bullet, i) { + if (bullet.dead) { + immoblize = false; + one.bullets.splice(i, 1); + return; + } + bullet.move(); + bullet.draw(); + + if (bullet.collides(two)) { + two.health -= 10; + bullet.dead = true; + } + }); + + ui.cursor.goto(0, 1); + if (turn() === one) ui.cursor.hex('#54ffff'); + ui.write(i18n.t('tanks.player', { number: "1" })); + ui.cursor.reset(); + ui.cursor.goto(0, 2); + ui.write(i18n.t('tanks.health', { value: one.health })); + ui.cursor.goto(0, 3); + ui.write(i18n.t('tanks.angle', { value: parseInt(one.angle) })); + + two.draw(); + two.bullets.forEach(function (bullet, i) { + if (bullet.dead) { + immoblize = false; + two.bullets.splice(i, 1); + return; + } + bullet.move(); + bullet.draw(); + + if (bullet.collides(one)) { + one.health -= 10; + bullet.dead = true; + } + }); + + ui.cursor.goto(ui.output.columns - 10, 1); + if (turn() === two) ui.cursor.hex('#54ffff'); + ui.write(i18n.t('tanks.player', { number: "2" })); + ui.cursor.reset(); + ui.cursor.goto(ui.output.columns - 10, 2); + ui.write(i18n.t('tanks.health', { value: two.health })); + ui.cursor.goto(ui.output.columns - 10, 3); + ui.write(i18n.t('tanks.angle', { value: parseInt(two.angle) })); + + setTimeout(loop, FRAME, i18n); +} + +ui.onKey('down', function () { + if (immoblize) return; + + turn().angle -= 1; +}); + +ui.onKey('up', function () { + if (immoblize) return; + + turn().angle += 1; +}); + +ui.onKey('left', function () { + if (immoblize) return; + + turn().x -= 1; +}); + +ui.onKey('right', function () { + if (immoblize) return; + + turn().x += 1; +}); + +ui.onKey('space', function () { + if (immoblize) return; + + turn().shoot(); + + immoblize = true; + TURN = !TURN; +}); + +ui.onKey(function () { + if (one.dead || two.dead) { + one.go(10, ui.rows); + two.go(ui.columns - 10, ui.rows); + + one.health = 100; + two.health = 100; + + one.dead = false; + two.dead = false; + + stop = false; + + loop(); + } +}); + +var TURN = true; +function turn() { + if (TURN) return one; + return two; +}