This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake-game.js
171 lines (148 loc) · 5.2 KB
/
snake-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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const Discord = require('discord.js');
const WIDTH = 15;
const HEIGHT = 10;
const gameBoard = [];
const apple = { x: 1, y: 1 };
class SnakeGame {
constructor() {
this.snake = [{ x: 5, y: 5 }];
this.snakeLength = 1;
this.score = 0;
this.gameEmbed = null;
this.inGame = false;
for (let y = 0; y < HEIGHT; y++) {
for (let x = 0; x < WIDTH; x++) {
gameBoard[y * WIDTH + x] = "🟦";
}
}
}
gameBoardToString() {
let str = ""
for (let y = 0; y < HEIGHT; y++) {
for (let x = 0; x < WIDTH; x++) {
if (x == apple.x && y == apple.y) {
str += "🍎";
continue;
}
let flag = true;
for (let s = 0; s < this.snake.length; s++) {
if (x == this.snake[s].x && y == this.snake[s].y) {
str += "🟩";
flag = false;
}
}
if (flag)
str += gameBoard[y * WIDTH + x];
}
str += "\n";
}
return str;
}
isLocInSnake(pos) {
return this.snake.find(sPos => sPos.x == pos.x && sPos.y == pos.y);
};
newAppleLoc() {
let newApplePos = { x: 0, y: 0 };
do {
newApplePos = { x: parseInt(Math.random() * WIDTH), y: parseInt(Math.random() * HEIGHT) };
} while (this.isLocInSnake(newApplePos))
apple.x = newApplePos.x;
apple.y = newApplePos.y;
}
newGame(msg) {
if (this.inGame)
return;
this.inGame = true;
this.score = 0;
this.snakeLength = 1;
this.snake = [{ x: 5, y: 5 }];
this.newAppleLoc();
const embed = new Discord.MessageEmbed()
.setColor('#03ad03')
.setTitle('Snake Game')
.setDescription(this.gameBoardToString())
.setTimestamp();
msg.channel.send(embed).then(emsg => {
this.gameEmbed = emsg;
this.gameEmbed.react('⬅️');
this.gameEmbed.react('⬆️');
this.gameEmbed.react('⬇️');
this.gameEmbed.react('➡️');
this.waitForReaction();
});
}
step() {
if (apple.x == this.snake[0].x && apple.y == this.snake[0].y) {
this.score += 1;
this.snakeLength++;
this.newAppleLoc();
}
const editEmbed = new Discord.MessageEmbed()
.setColor('#03ad03')
.setTitle('Snake Game')
.setDescription(this.gameBoardToString())
.setTimestamp();
this.gameEmbed.edit(editEmbed);
this.waitForReaction();
}
gameOver() {
this.inGame = false;
const editEmbed = new Discord.MessageEmbed()
.setColor('#03ad03')
.setTitle('Snake Game')
.setDescription("GAME OVER!\nSCORE: " + this.score)
.setTimestamp();
this.gameEmbed.edit(editEmbed);
this.gameEmbed.reactions.removeAll()
}
filter(reaction, user) {
return ['⬅️', '⬆️', '⬇️', '➡️'].includes(reaction.emoji.name) && user.id !== this.gameEmbed.author.id;
}
waitForReaction() {
this.gameEmbed.awaitReactions((reaction, user) => this.filter(reaction, user), { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
const snakeHead = this.snake[0];
const nextPos = { x: snakeHead.x, y: snakeHead.y };
if (reaction.emoji.name === '⬅️') {
let nextX = snakeHead.x - 1;
if (nextX < 0)
nextX = WIDTH - 1;
nextPos.x = nextX;
}
else if (reaction.emoji.name === '⬆️') {
let nextY = snakeHead.y - 1;
if (nextY < 0)
nextY = HEIGHT - 1;
nextPos.y = nextY;
}
else if (reaction.emoji.name === '⬇️') {
let nextY = snakeHead.y + 1;
if (nextY >= HEIGHT)
nextY = 0;
nextPos.y = nextY;
}
else if (reaction.emoji.name === '➡️') {
let nextX = snakeHead.x + 1;
if (nextX >= WIDTH)
nextX = 0;
nextPos.x = nextX;
}
reaction.users.remove(reaction.users.cache.filter(user => user.id !== this.gameEmbed.author.id).first().id).then(() => {
if (this.isLocInSnake(nextPos)) {
this.gameOver();
}
else {
this.snake.unshift(nextPos);
if (this.snake.length > this.snakeLength)
this.snake.pop();
this.step();
}
});
})
.catch(collected => {
this.gameOver();
});
}
}
module.exports = SnakeGame;