-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
94 lines (94 loc) · 2.86 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
(function(){
let that;
function Game(map){
this.speed = 200;
this.map = map;
this.score = 0;
this.mapWidth = Math.ceil(map.clientWidth/10);
this.mapHeight = Math.ceil(map.clientHeight/10);
this.x = Tools.random(0, this.mapWidth-1)*10;
this.y = Tools.random(0, this.mapHeight-1)*10;
this.snake = new Snake(map); // 生成一个蛇对象
this.food = new Food(map, {x:this.x, y:this.y}); // 生成一个食物对象
that = this;
}
Game.prototype.start = function(){ // 开始游戏
this.food.render();
this.snake.render();
}
Game.prototype.control = function(keyCode){ // 控制小蛇移动方向
clearInterval(this.startMove);
switch(keyCode){
case 37:
this.startMove = setInterval(()=>{
this.snake.move(3);
limit();
eat();
}, 200);
break;
case 38:
this.startMove = setInterval(()=>{
this.snake.move(0);
limit();
eat();
}, 200);
break;
case 39:
this.startMove = setInterval(()=>{
limit();
this.snake.move(1);
eat();
}, 200);
break;
case 40:
this.startMove = setInterval(()=>{
this.snake.move(2);
limit();
eat();
}, 200);
break;
}
}
function limit() { // 地图界限
console.log(that.mapWidth, that.mapHeight);
if(that.snake.bodys[0].x+10 > that.mapWidth*10 || that.snake.bodys[0].x < 0){
clearInterval(that.startMove);
alert('你挂了!得分:' + that.score);
reStart();
return;
}else if(that.snake.bodys[0].y+10 > that.mapHeight*10 || that.snake.bodys[0].y < 0){
clearInterval(that.startMove);
alert('你挂了!得分:' + that.score);
reStart();
return;
}
that.snake.bodys.forEach((item, index)=>{
if(index == 0) return true;
if(that.snake.bodys[0].x == item.x && that.snake.bodys[0].y == item.y){
clearInterval(that.startMove);
alert('你挂了!得分:' + that.score);
reStart();
return;
}
})
}
function eat(){ // 吃食物
if(that.snake.bodys[0].x == that.food.obj.x && that.snake.bodys[0].y == that.food.obj.y){
let x = that.snake.bodys[that.snake.bodys.length-1].x;
let y = that.snake.bodys[that.snake.bodys.length-1].y;
that.snake.bodys.push({x, y})
let foodX = Tools.random(0, that.mapWidth-1)*10;
this.foodY = Tools.random(0, that.mapHeight-1)*10;
that.food.render({x: foodX, y: foodY});
that.snake.render();
that.score++;
}
}
function reStart(){ // 重新开始游戏
that.snake.remove();
that.snake = new Snake(that.map);
that.snake.render();
that.score = 0;
}
window.Game = Game;
})()