-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
119 lines (104 loc) · 2.26 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var Keys = {
ARROW_UP: 38,
ARROW_LEFT: 37,
ARROW_RIGHT: 39,
ARROW_DOWN: 40,
PAUSE: 80
};
/**
* Singleton game object
*/
var Game = {
//The base object
ground: null,
//The game status
paused: true,
//Earned points
points: 0,
//JQueryObject: the text with the points
$points: null,
//Game velocity
velocity: 100,
/**
* Increases the user points
*/
incPoints: function(){
this.points += 10;
this.$points.text(this.points);
},
/**
* Decreases the user points
*/
decPoints: function(){
this.points -= 50;
if(this.points < 0){
this.points = 0;
}
this.$points.text(this.points);
},
/**
* Increases the game velocity
*/
incVelocity: function(){
if(this.velocity >50){
this.velocity--;
}
},
/**
* Start the game
*/
start: function(context, canvas){
this.ground = new Ground(context, canvas);
this.ground.resize();
this.ground.init();
this.ground.centerSnake();
if(!this.$points){
this.$points = $('#points');
}
this.paused = false;
},
/**
* Pause the game
*/
pause: function(){
this.paused = true;
},
/**
* Stop the game
*/
stop: function(){
this.pauses = true;
},
/**
* Play / pause the game
*/
togglePause: function(){
if(this.paused){
this.start();
}else{
this.pause();
}
},
/**
* Event handler on key press
*/
sendKey: function(ev){
switch(ev.which){
case Keys.ARROW_LEFT:
this.ground.snake.setDirection(SnakeDirection.LEFT);
break;
case Keys.ARROW_UP:
this.ground.snake.setDirection(SnakeDirection.UP);
break;
case Keys.ARROW_DOWN:
this.ground.snake.setDirection(SnakeDirection.DOWN);
break;
case Keys.ARROW_RIGHT:
this.ground.snake.setDirection(SnakeDirection.RIGHT);
break;
case Keys.PAUSE:
this.togglePause();
break;
}
}
};