Advertisement
gaber-elsayed

snake game

Nov 3rd, 2021
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. const { MessageEmbed } = require("discord.js");
  2. const { MessageActionRow, MessageButton } = require('discord-buttons');
  3. const data = new Set();
  4.  
  5. const width = 15;
  6. const height = 10;
  7. const apple = { x: 0, y: 0 };
  8. const food = { x: 0, y: 0}
  9. const gameBoard = [];
  10.  
  11. class SnakeGame {
  12. constructor(options) {
  13. this.score = 0;
  14. this.inGame = false;
  15. this.snakeLength = 1;
  16. this.snake = [{ x: 0, y: 0 }];
  17. this.options = options;
  18. if (data.has(this.options.message.author.id)) return;
  19. data.add(this.options.message.author.id);
  20. for (let y = 0; y < height; y++) {
  21. for (let x = 0; x < width; x++) {
  22. gameBoard[y * width + x] = ":black_large_square:";
  23. }
  24. }
  25. }
  26.  
  27. gameBoardToString() {
  28. let str = '';
  29. for (let y = 0; y < height; y++) {
  30. for (let x = 0; x < width; x++) {
  31. if (x == apple.x && y == apple.y) {
  32. str += ":small_blue_diamond:";
  33. continue;
  34. }
  35. if (x == food.x && y == food.y) {
  36. str += ":small_orange_diamond:";
  37. continue;
  38. }
  39. let flag = true;
  40. for (let s = 0; s < this.snake.length; s++) {
  41. if (x == this.snake[s].x && y == this.snake[s].y) {
  42. str += ":green_circle:";
  43. flag = false;
  44. }
  45. }
  46. if (flag) {
  47. str += gameBoard[y * width + x];
  48. }
  49. }
  50. str += '\n';
  51. }
  52. return str;
  53. }
  54.  
  55. isLocInSnake(pos) {
  56. return this.snake.find((sPos) => sPos.x == pos.x && sPos.y == pos.y);
  57. }
  58.  
  59. newappleLoc() {
  60. let newapplePos = {
  61. x: 0,
  62. y: 0,
  63. };
  64. do {
  65. newapplePos = {
  66. x: parseInt(Math.random() * width),
  67. y: parseInt(Math.random() * height),
  68. };
  69. } while (this.isLocInSnake(newapplePos));
  70. apple.x = newapplePos.x;
  71. apple.y = newapplePos.y;
  72. }
  73.  
  74. newfoodLoc() {
  75. let newfoodPos = {
  76. x: 0,
  77. y: 0,
  78. };
  79. do {
  80. newfoodPos = {
  81. x: parseInt(Math.random() * width),
  82. y: parseInt(Math.random() * height),
  83. };
  84. } while (this.isLocInSnake(newfoodPos));
  85. food.x = newfoodPos.x;
  86. food.y = newfoodPos.y;
  87. }
  88.  
  89. step(msg){
  90. if (apple.x == this.snake[0].x && apple.y == this.snake[0].y) {
  91. this.score += 1;
  92. this.snakeLength++;
  93. this.newappleLoc();
  94. }
  95. if (food.x == this.snake[0].x && food.y == this.snake[0].y) {
  96. this.score += 2;
  97. this.snakeLength += 2;
  98. this.newfoodLoc();
  99. }
  100. const updateEmbed = new MessageEmbed()
  101. .setColor("BLUE")
  102. .setTitle("Snake Game")
  103. .setFooter("® RDDigital")
  104. .addField('Score:', this.score, true)
  105. .setDescription(this.gameBoardToString())
  106. .setTimestamp(Date.now());
  107. let disbtn1 = new MessageButton().setStyle('grey').setLabel('\u200b').setID('disablebtn1').setDisabled();
  108. let disbtn2 = new MessageButton().setStyle('grey').setLabel('\u200b').setID('disablebtn2').setDisabled();
  109.  
  110. let btnUP = new MessageButton().setStyle('blurple').setEmoji('⬆️').setID('btnUP')
  111. let btnDOWN = new MessageButton().setStyle('blurple').setEmoji('⬇️').setID('btnDOWN')
  112. let btnRIGHT = new MessageButton().setStyle('blurple').setEmoji('➡️').setID('btnRIGHT')
  113. let btnLEFT = new MessageButton().setStyle('blurple').setEmoji('⬅️').setID('btnLEFT')
  114. let rowbtnA = new MessageActionRow().addComponents([disbtn1, btnUP, disbtn2])
  115. let rowbtnB = new MessageActionRow().addComponents([btnLEFT, btnDOWN, btnRIGHT])
  116. let btnRow = [rowbtnA, rowbtnB]
  117.  
  118. msg.edit({
  119. embed: updateEmbed,
  120. components: btnRow
  121. })
  122. }
  123.  
  124. gameOver(m){
  125. this.inGame = false
  126. const gameoverembed = new MessageEmbed()
  127. .setColor("RED")
  128. .setTitle("GameOver")
  129. .setFooter("® RDDigital")
  130. .setDescription(`Your Score: ${this.score}`)
  131. .setTimestamp(Date.now());
  132. let disbtn1 = new MessageButton().setStyle('grey').setLabel('\u200b').setID('disablebtn1').setDisabled();
  133. let disbtn2 = new MessageButton().setStyle('grey').setLabel('\u200b').setID('disablebtn2').setDisabled();
  134.  
  135. let btnUP = new MessageButton().setStyle('blurple').setEmoji('⬆️').setID('btnUP').setDisabled()
  136. let btnDOWN = new MessageButton().setStyle('blurple').setEmoji('⬇️').setID('btnDOWN').setDisabled()
  137. let btnRIGHT = new MessageButton().setStyle('blurple').setEmoji('➡️').setID('btnRIGHT').setDisabled()
  138. let btnLEFT = new MessageButton().setStyle('blurple').setEmoji('⬅️').setID('btnLEFT').setDisabled()
  139. let rowbtnA = new MessageActionRow().addComponents([disbtn1, btnUP, disbtn2])
  140. let rowbtnB = new MessageActionRow().addComponents([btnLEFT, btnDOWN, btnRIGHT])
  141. let btnRow = [rowbtnA, rowbtnB]
  142.  
  143. m.edit({
  144. embed: gameoverembed,
  145. components: btnRow
  146. })
  147. }
  148.  
  149. newGame(){
  150. if (this.inGame) {
  151. return;
  152. }
  153. this.inGame = true;
  154. this.score = 0;
  155. this.snakeLength = 1;
  156. this.snake = [{ x: 5, y: 5 }];
  157. this.newappleLoc();
  158. this.newfoodLoc();
  159. const embed = new MessageEmbed()
  160. .setColor("BLUE")
  161. .setTitle("Snake Game")
  162. .setFooter("® RDDigital")
  163. .addField('Score:', this.score, true)
  164. .setDescription(this.gameBoardToString())
  165. .setTimestamp(Date.now());
  166. let disbtn1 = new MessageButton().setStyle('grey').setLabel('\u200b').setID('disablebtn1').setDisabled();
  167. let disbtn2 = new MessageButton().setStyle('grey').setLabel('\u200b').setID('disablebtn2').setDisabled();
  168.  
  169. let btnUP = new MessageButton().setStyle('blurple').setEmoji('⬆️').setID('btnUP')
  170. let btnDOWN = new MessageButton().setStyle('blurple').setEmoji('⬇️').setID('btnDOWN')
  171. let btnRIGHT = new MessageButton().setStyle('blurple').setEmoji('➡️').setID('btnRIGHT')
  172. let btnLEFT = new MessageButton().setStyle('blurple').setEmoji('⬅️').setID('btnLEFT')
  173. let rowbtnA = new MessageActionRow().addComponents([disbtn1, btnUP, disbtn2])
  174. let rowbtnB = new MessageActionRow().addComponents([btnLEFT, btnDOWN, btnRIGHT])
  175. let btnRow = [rowbtnA, rowbtnB]
  176.  
  177. this.options.message.channel.send({ embed }).then(async (m) => {
  178. m.edit({
  179. embed: embed,
  180. components: btnRow
  181. })
  182. const filter = (buttons) => buttons.clicker.id === this.options.message.author.id
  183. const collector = m.createButtonCollector(filter, {});
  184.  
  185. collector.on("collect", (button) => {
  186. button.reply.defer();
  187. const snakeHead = this.snake[0];
  188. const nextPos = {
  189. x: snakeHead.x,
  190. y: snakeHead.y,
  191. };
  192. if (button.id === "btnRIGHT") {
  193. let nextX = snakeHead.x + 1;
  194. if (nextX >= width) {
  195. nextX = 0;
  196. }
  197. nextPos.x = nextX;
  198. } else if (button.id === "btnUP") {
  199. let nextY = snakeHead.y - 1;
  200. if (nextY < 0) {
  201. nextY = height - 1;
  202. }
  203. nextPos.y = nextY;
  204. } else if (button.id === "btnDOWN") {
  205. let nextY = snakeHead.y + 1;
  206. if (nextY >= height) {
  207. nextY = 0;
  208. }
  209. nextPos.y = nextY;
  210. } else if (button.id === "btnLEFT") {
  211. let nextX = snakeHead.x - 1;
  212. if (nextX < 0) {
  213. nextX = width - 1;
  214. }
  215. nextPos.x = nextX;
  216. }
  217.  
  218.  
  219. if (this.isLocInSnake(nextPos)) {
  220. this.gameOver(m);
  221. collector.stop();
  222. data.delete(this.options.message.author.id);
  223. } else {
  224. this.snake.unshift(nextPos);
  225. if (this.snake.length > this.snakeLength) {
  226. this.snake.pop();
  227. }
  228. this.step(m);
  229. }
  230. })
  231. })
  232. }
  233. }
  234.  
  235. module.exports = SnakeGame;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement