-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (76 loc) · 2.05 KB
/
server.js
File metadata and controls
92 lines (76 loc) · 2.05 KB
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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const PORT = 3004;
const box = 20;
const players = {};
let food = createFood();
app.use(express.static(path.join(__dirname, 'public')));
function createFood() {
return {
x: Math.floor(Math.random() * 20) * box,
y: Math.floor(Math.random() * 20) * box,
};
}
function moveSnake(snake, direction) {
const head = { ...snake[0] };
if (direction === 'UP') head.y -= box;
if (direction === 'DOWN') head.y += box;
if (direction === 'LEFT') head.x -= box;
if (direction === 'RIGHT') head.x += box;
snake.unshift(head);
return snake;
}
function checkCollision(snake) {
const head = snake[0];
return (
head.x < 0 || head.x >= 400 ||
head.y < 0 || head.y >= 400 ||
snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)
);
}
io.on('connection', socket => {
console.log(`Player joined: ${socket.id}`);
players[socket.id] = {
snake: [{ x: 100, y: 100 }],
direction: 'RIGHT',
score: 0,
color: '#' + Math.floor(Math.random() * 16777215).toString(16),
};
socket.on('direction', dir => {
players[socket.id].direction = dir;
});
socket.on('disconnect', () => {
console.log(`Player left: ${socket.id}`);
delete players[socket.id];
});
});
setInterval(() => {
for (const id in players) {
const player = players[id];
player.snake = moveSnake(player.snake, player.direction);
const head = player.snake[0];
if (head.x === food.x && head.y === food.y) {
player.score += 10;
food = createFood();
} else {
player.snake.pop();
}
if (checkCollision(player.snake)) {
player.snake = [{ x: 100, y: 100 }];
player.direction = 'RIGHT';
player.score = 0;
}
}
io.emit('gameState', {
players,
food,
});
}, 160);
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});