-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
65 lines (56 loc) · 1.5 KB
/
game.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
62
63
64
65
var Apple = require('./apple')
var Snake = require('./snake')
var Clanvas = require('./clanvas')
function Game (width, height) {
this.width = width
this.height = height
this.clanvas = new Clanvas(process.stdout, width, height, 'black')
this.restart()
}
Game.prototype.restart = function () {
this.snake = new Snake(this.clanvas)
this.apple = new Apple(this.clanvas)
this.points = 0
this.apple.relocate(this.snake)
}
Game.prototype.render = function () {
this.clanvas.clear()
this.snake.render()
this.apple.render()
this.renderHud()
}
Game.prototype.renderHud = function () {
var clanvas = this.clanvas
var charm = clanvas.charm
var middle = Math.round(this.width / 2)
var end = this.width
this.clanvas.fillLine(-1, 'white') // lazy hack, the whole area is shifted down to hold the hud
charm.foreground('black')
charm.position(middle - 3, 0).write('CLake')
charm.foreground(4).position(end - 12, 0).write('Score: ' + this.points)
}
Game.prototype.onKey = function (key) {
if (['left', 'right', 'up', 'down'].indexOf(key) !== -1) {
this.snake.turn(key)
}
if (key === 'g') {
this.snake.grow() // hmmmmmm
}
}
Game.prototype.update = function () {
var snake = this.snake
this.colliding = false
snake.move()
if (snake.contains(this.apple.position)) {
this.points++
this.apple.relocate(snake)
snake.grow()
}
if (snake.isCollidingWithSelf()) {
this.restart()
}
}
Game.prototype.exit = function () {
this.clanvas.dispose()
}
module.exports = Game