-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
152 lines (136 loc) · 3.56 KB
/
app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
//启动后端express服务器
server.listen(process.env.PORT || 3000, process.env.IP || "127.0.0.1", function () {
var addr = server.address();
console.log("Server listening at", addr.address + ":" + addr.port);
});
//设置路由
app.get('/', function (req, res) {
res.sendfile(__dirname + '/online_snake_battle.html');
});
app.get('/main.js', function (req, res) {
res.sendfile(__dirname + '/main.js');
});
app.get('/style.css', function (req, res) {
res.sendfile(__dirname + '/style.css');
});
var w = 50, h = 50;
var snakes = [];
var foods = [];
//生成一条蛇,默认长度为2,向左游动
var generateSnake = function(id){
var x = parseInt(Math.random() * w-3, 10);
var y = parseInt(Math.random() * h, 10);
var result = [];
result.push({x: x, y: y});
result.push({x: x+1, y: y});
var snake = {
id: id,
dir: 3,
body: result
};
snakes.push(snake);
return snakes[snakes.length - 1];
};
//生成一个食物
var generateFood = function(){
foods.push({
x: parseInt(Math.random() * w, 10), y: parseInt(Math.random() * h, 10)
});
};
//判断蛇游动的下一个坐标点
var nextStep = function (snake) {
if (snake.body[0]) {
switch (snake.dir) {
case 1:
return {x: snake.body[0].x, y: (snake.body[0].y - 1 >= 0) ? (snake.body[0].y - 1) : (h - 1)};
break;
case 2:
return {x: snake.body[0].x, y: (snake.body[0].y + 1 < h) ? (snake.body[0].y + 1) : (0)};
break;
case 3:
return {x: (snake.body[0].x - 1 >= 0) ? (snake.body[0].x - 1) : (w - 1), y: snake.body[0].y};
break;
case 4:
return {x: (snake.body[0].x + 1 < w) ? (snake.body[0].x + 1) : (0), y: snake.body[0].y};
break;
}
}
else {
return false;
}
};
//寻找当前点是否是食物
var find = function (point) {
for (var i = 0; i < foods.length; i++) {
if (point.x === foods[i].x && point.y === foods[i].y)
return i;
}
return -1;
};
var eat = function(i, next){
snakes[i].body.unshift(next);
var j = find(next);
if (j !== -1) {
foods.splice(j, 1);
generateFood();
} else {
snakes[i].body.pop();
}
};
//当蛇碰撞的时候游戏结束
var conflict = function(next){
for(var i = 0 ; i < snakes.length; i++){
if(snakes[i]) {
for (var j = 0; j < snakes[i].body.length; j++){
if(snakes[i].body[j].x === next.x && snakes[i].body[j].y === next.y){
return true;
}
}
}
}
return false;
};
var running = false;
var interval;
generateFood();
io.on('connection', function (socket) {
var snake = generateSnake(socket.id);
if(!running) {
running = true;
interval = setInterval(function () {
io.sockets.emit('data', {
snakes: snakes,
foods: foods
});
for (var i = 0; i < snakes.length; i++) {
var next = nextStep(snakes[i]);
if (next) {
if (conflict(next)) {
io.sockets.emit('over');
if(interval) {
clearInterval(interval);
}
}
eat(i, next);
}
}
}, 100);
}
socket.on('dir', function (data) {
if(data.dir + snake.dir !== 3 && data.dir + snake.dir !== 7){//不能反向移动
snake.dir = data.dir;
}
});
socket.on('disconnect', function(){//客户端退出的时候删除蛇
for(var i = 0; i < snakes.length ; i++){
if(snakes[i].id = socket.id){
snakes.splice(i, 1);
break;
}
}
})
});