Advertisement
sutterseba

Simple Snake Game

May 31st, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************************************
  2.  * Simple Snake Game / WBA Aufgabe 4
  3.  * Sebastian Sutter 2022
  4.  ************************************/
  5.  
  6. let board, context, scoreboard, highscoreboard;
  7. let size = 25, rows = 21, cols = 21;
  8.  
  9. let sx = size * 5;
  10. let sy = size * 5;
  11. let body = [];
  12.  
  13. let vx = 0, vy = 0;
  14. let fx, fy;
  15.  
  16. let highscore = 0;
  17. let score = 0;
  18. let gameover = false;
  19.  
  20. window.onload = function () {
  21.  
  22.     scoreboard = document.getElementById("score");
  23.     highscoreboard = document.getElementById("highscore");
  24.  
  25.     board = document.getElementById("board");
  26.     board.height = rows * size;
  27.     board.width = cols * size;
  28.  
  29.     context = board.getContext("2d");
  30.  
  31.     spawnFruit();
  32.     document.addEventListener("keyup", changeDirection);
  33.    
  34.     setInterval(update, 1000/10);
  35. }
  36.  
  37. function update () {
  38.  
  39.     if (gameover) {
  40.         reset();
  41.         return;
  42.     }
  43.  
  44.     // CANVAS
  45.     context.fillStyle = "#000";
  46.     context.fillRect(0, 0, board.width, board.height);
  47.  
  48.     // FRUIT
  49.     context.fillStyle = "lime";
  50.     context.fillRect(fx, fy, size, size);
  51.  
  52.     if (sx == fx && sy == fy) {
  53.         score++;
  54.         body.push([fx, fy]);
  55.         spawnFruit();
  56.     }
  57.  
  58.     for (let i = body.length - 1; i > 0; i--) {
  59.         body[i] = body[i-1];
  60.     }
  61.  
  62.     if (body.length) {
  63.         body[0] = [sx, sy];
  64.     }
  65.  
  66.     // SNAKE
  67.     context.fillStyle = "#fff";
  68.     sx += vx * size;
  69.     sy += vy * size;
  70.     context.fillRect(sx, sy, size, size);
  71.  
  72.     for (let i = 0; i < body.length; i++) {
  73.         context.fillRect(body[i][0], body[i][1], size, size);
  74.     }
  75.  
  76.     // SCORE
  77.     scoreboard.innerHTML = score;
  78.     highscoreboard.innerHTML = highscore;
  79.    
  80.     // GAME OVER
  81.     if (sx < 0 || sx > cols * size || sy < 0 || sy > rows * size) {
  82.         gameover = true;
  83.     }
  84.  
  85.     for (let i = 0; i < body.length; i++) {
  86.  
  87.         if (sx == body[i][0] && sy == body[i][1]) {
  88.             gameover = true;
  89.         }
  90.     }
  91. }
  92.  
  93. function changeDirection (event) {
  94.  
  95.     switch (event.code) {
  96.         case "ArrowUp":
  97.             vx = 0;
  98.             if (vy != 1) vy = -1;
  99.             break;
  100.         case "ArrowDown":
  101.             vx = 0;
  102.             if (vy != -1) vy = 1;
  103.             break;
  104.         case "ArrowRight":
  105.             if (vx != -1) vx = 1;
  106.             vy = 0;
  107.             break;  
  108.         case "ArrowLeft":
  109.             if (vx != 1) vx = -1;
  110.             vy = 0;
  111.             break;  
  112.     }
  113. }
  114.  
  115. function spawnFruit () {
  116.     fx = Math.floor(Math.random() * cols) * size;
  117.     fy = Math.floor(Math.random() * rows) * size;
  118. }
  119.  
  120. function reset () {
  121.  
  122.     context.fillStyle = "lime";
  123.     context.fillRect(0, 0, board.width, board.height);
  124.  
  125.     spawnFruit();
  126.    
  127.     sx = size * 5;
  128.     sy = size * 5;
  129.     vx = 0;
  130.     vy = 0;
  131.  
  132.     if (score > highscore) {
  133.         highscore = score;
  134.     }
  135.  
  136.     body = []
  137.     score = 0;
  138.     gameover = false;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement