Advertisement
andrzuk

Snake Xenzia (code)

Jun 5th, 2018
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Snake Xenzia v1.0
  3.  *  Copyright (C) 2017 Andrzej Żukowski
  4.  *  http://angular-cms.pl
  5.  */
  6.  
  7. var canvas = document.getElementById("game-canvas");
  8. var ctx = canvas.getContext("2d");
  9.  
  10. var gameScore = 0;
  11. var gameTick = 0;
  12. var animationReady = false;
  13.  
  14. canvas.width = parseInt($("canvas#game-canvas").css("width"));
  15. canvas.height = parseInt($("canvas#game-canvas").css("height"));
  16.  
  17. var gameArea = {
  18.     cellSize: 15,
  19.     cellDist: 2,
  20.     colsCount: 60,
  21.     rowsCount: 28,
  22.     diamond: { x: 0, y: 0 },
  23.     init: function() {
  24.         ctx.clearRect(0, 0, canvas.width, canvas.height);
  25.         for (i = 0; i < this.colsCount; i++) {
  26.             for (j = 0; j < this.rowsCount; j++) {
  27.                 paintCell(i, j, "#cde");
  28.             }
  29.         }
  30.         gameScore = 0;
  31.         animationReady = true;
  32.         $("span#score").text(gameScore.toString());
  33.     },
  34.     setDiamond: function() {
  35.         var freePlace = false;
  36.         while (!freePlace) {
  37.             freePlace = true;
  38.             this.diamond.x = Math.floor(Math.random() * gameArea.colsCount);
  39.             this.diamond.y = Math.floor(Math.random() * gameArea.rowsCount);
  40.             for (i = 0; i < snake.length; i++) {
  41.                 if (snake.points[i].x == this.diamond.x && snake.points[i].y == this.diamond.y) {
  42.                     freePlace = false;
  43.                 }
  44.             }
  45.         }
  46.         paintCell(this.diamond.x, this.diamond.y, "#0c0");
  47.     },
  48.     saveScore: function() {
  49.         $.post("ajax/game/store_score.php", { player: player.name, score: gameScore }, function(data, status) {
  50.             var response = JSON.parse(data);
  51.             player.name = response.player;
  52.             player.score = response.score;
  53.             $("div#player-caption").text(player.name + " (" + player.score + ")");
  54.         });
  55.     },
  56.     getScores: function() {
  57.         $('div#scores-modal').modal('show');
  58.         $("table#scores-list tbody").empty();
  59.         var tableHeader = '<tr><th width="10%">Pozycja</th><th width="25%">Gracz</th><th width="25%">Adres IP</th><th width="25%" style="text-align: center;">Data</th><th width="15%" style="text-align: right;">Punkty</th></tr>';
  60.         $("table#scores-list tbody").append(tableHeader);
  61.         $.get("ajax/game/get_scores.php", function(data, status) {
  62.             var response = JSON.parse(data);
  63.             jQuery.each(response, function(index, item) {
  64.                 var tableRow = "<tr><td>" + (index + 1).toString() + "</td><td>" + item.player + "</td><td>" + item.ip + "</td><td class='scores-saved'>" + item.saved + "</td><td class='scores-score'>" + item.score + "</td></tr>";
  65.                 $("table#scores-list tbody").append(tableRow);
  66.             });
  67.         });
  68.     }
  69. };
  70.  
  71. var snake = {
  72.     x: 0,
  73.     y: 0,
  74.     direction: null,
  75.     length: 0,
  76.     growing: 3,
  77.     mode: 0,
  78.     speed: $("select#period").val(),
  79.     points: null,
  80.     init: function() {
  81.         this.points = [];
  82.         this.length = 10;
  83.         this.x = Math.floor(Math.random() * (gameArea.colsCount - 2 * this.length)) + this.length;
  84.         this.y = Math.floor(Math.random() * (gameArea.rowsCount - 2 * this.length)) + this.length;
  85.         this.points.push({ x: this.x, y: this.y });
  86.         var x = this.x, y = this.y;
  87.         var directions = ['left', 'right', 'up', 'down'];
  88.         this.direction = directions[Math.floor(Math.random() * 4)];
  89.         this.mode = 0;
  90.         for (i = 0; i < this.length - 1; i++) {
  91.             if (this.direction == 'left') {
  92.                 x++;
  93.             }
  94.             if (this.direction == 'right') {
  95.                 x--;
  96.             }
  97.             if (this.direction == 'up') {
  98.                 y++;
  99.             }
  100.             if (this.direction == 'down') {
  101.                 y--;
  102.             }
  103.             this.points.push({ x: x, y: y });
  104.         }
  105.         for (i = 0; i < this.length; i++) {
  106.             paintCell(this.points[i].x, this.points[i].y, "#c00");
  107.         }
  108.     },
  109.     checkCollision: function() {
  110.         for (i = 1; i < this.length; i++) {
  111.             if (this.x == this.points[i].x && this.y == this.points[i].y) {
  112.                 return false;
  113.             }
  114.         }
  115.         return true;
  116.     },
  117.     checkDiamondHit: function() {
  118.         if (this.x == gameArea.diamond.x && this.y == gameArea.diamond.y) {
  119.             return true;
  120.         }
  121.         return false;
  122.     },
  123.     growLength: function() {
  124.         for (i = 0; i < this.growing; i++) {
  125.             this.points.push({ x: this.points[this.length - 1].x, y: this.points[this.length - 1].y });
  126.             this.length++;
  127.         }
  128.     }
  129. };
  130.  
  131. var player = {
  132.     name: null,
  133.     score: 0,
  134.     showInput: function() {
  135.         $('div#player-modal').modal('show').on('shown.bs.modal', function() {
  136.             $('div#player-modal input#player-name').focus().keypress(function(event) {
  137.                 if (event.keyCode == 13) {
  138.                     $('button#save-player-name').click();
  139.                 }
  140.             });
  141.         });
  142.     },
  143.     getName: function() {
  144.         if (!this.name) {
  145.             this.name = 'Anonymous';
  146.         }
  147.         this.getScore();
  148.     },
  149.     getScore: function() {
  150.         $.post("ajax/game/get_score.php", { player: this.name }, function(data, status) {
  151.             var response = JSON.parse(data);
  152.             this.name = response.player;
  153.             this.score = response.score;
  154.             $("div#player-caption").text(this.name + " (" + this.score + ")");
  155.         });
  156.     }  
  157. };
  158.  
  159. function startAnimation() {
  160.     snake.mode = 1;
  161.     if (animationReady) {
  162.         animationStep();
  163.     }
  164. }
  165.  
  166. function stopAnimation() {
  167.     snake.mode = 0;
  168. }
  169.  
  170. function animationStep() {
  171.     animationReady = false;
  172.     setTimeout(function() {
  173.         paintCell(snake.points[snake.length - 1].x, snake.points[snake.length - 1].y, "#cde");
  174.         if (snake.direction == 'up') {
  175.             if (snake.y > 0) snake.y--;
  176.             else snake.y = gameArea.rowsCount - 1;
  177.         }
  178.         if (snake.direction == 'down') {
  179.             if (snake.y < gameArea.rowsCount - 1) snake.y++;
  180.             else snake.y = 0;
  181.         }
  182.         if (snake.direction == 'left') {
  183.             if (snake.x > 0) snake.x--;
  184.             else snake.x = gameArea.colsCount - 1;
  185.         }
  186.         if (snake.direction == 'right') {
  187.             if (snake.x < gameArea.colsCount - 1) snake.x++;
  188.             else snake.x = 0;
  189.         }
  190.         for (i = snake.length - 1; i > 0; i--) {
  191.             snake.points[i].x = snake.points[i - 1].x;
  192.             snake.points[i].y = snake.points[i - 1].y;
  193.         }
  194.         snake.points[0].x = snake.x;
  195.         snake.points[0].y = snake.y;
  196.         if (snake.checkCollision()) {
  197.             if (snake.checkDiamondHit()) {
  198.                 paintCell(snake.x, snake.y, "#600");
  199.                 gameScore += snake.growing;
  200.                 $("span#score").text(gameScore.toString());
  201.                 snake.growLength();
  202.                 gameArea.setDiamond();
  203.             }
  204.             else {
  205.                 paintCell(snake.x, snake.y, "#c00");
  206.                 gameTick++;
  207.                 if (gameTick % 1000 == 0) {
  208.                     gameTick = 0;
  209.                     stopAnimation();
  210.                 }
  211.                 animationReady = true;
  212.             }
  213.         }
  214.         else {
  215.             paintCell(snake.x, snake.y, "#000");
  216.             stopAnimation();
  217.             showExplosion();
  218.             snake.mode = 2;
  219.         }
  220.         if (snake.mode == 1) {
  221.             animationStep();
  222.         }
  223.     }, snake.speed);
  224. }
  225.  
  226. function showExplosion() {
  227.     var color_1 = "#cc0";
  228.     var color_2 = "#e90";
  229.     setTimeout(function() {
  230.         paintCell(snake.x - 1, snake.y - 1, color_1);
  231.         paintCell(snake.x, snake.y - 1, color_1);
  232.         paintCell(snake.x + 1, snake.y - 1, color_1);
  233.         paintCell(snake.x - 1, snake.y, color_1);
  234.         paintCell(snake.x + 1, snake.y, color_1);
  235.         paintCell(snake.x - 1, snake.y + 1, color_1);
  236.         paintCell(snake.x, snake.y + 1, color_1);
  237.         paintCell(snake.x + 1, snake.y + 1, color_1);
  238.         setTimeout(function() {
  239.             paintCell(snake.x - 2, snake.y - 2, color_2);
  240.             paintCell(snake.x - 1, snake.y - 2, color_2);
  241.             paintCell(snake.x, snake.y - 2, color_2);
  242.             paintCell(snake.x + 1, snake.y - 2, color_2);
  243.             paintCell(snake.x + 2, snake.y - 2, color_2);
  244.             paintCell(snake.x - 2, snake.y - 1, color_2);
  245.             paintCell(snake.x + 2, snake.y - 1, color_2);
  246.             paintCell(snake.x - 2, snake.y, color_2);
  247.             paintCell(snake.x + 2, snake.y, color_2);
  248.             paintCell(snake.x - 2, snake.y + 1, color_2);
  249.             paintCell(snake.x + 2, snake.y + 1, color_2);
  250.             paintCell(snake.x - 2, snake.y + 2, color_2);
  251.             paintCell(snake.x - 1, snake.y + 2, color_2);
  252.             paintCell(snake.x, snake.y + 2, color_2);
  253.             paintCell(snake.x + 1, snake.y + 2, color_2);
  254.             paintCell(snake.x + 2, snake.y + 2, color_2);
  255.             setTimeout(function() {
  256.                 gameArea.saveScore();
  257.                 gameArea.init();
  258.                 snake.init();
  259.                 gameArea.setDiamond();
  260.             }, 5000);
  261.         }, 100);
  262.     }, 100);
  263. }
  264.  
  265. function resetGame() {
  266.     gameArea.init();
  267.     snake.init();
  268.     gameArea.setDiamond();
  269. }
  270.  
  271. function changeDirection(direction) {
  272.     var speedIndex = $("select#period").prop('selectedIndex');
  273.     var speedCount = $("select#period").children('option').length;
  274.     if (direction == 'up') {
  275.         if (snake.direction == 'up') {
  276.             if (speedIndex > 0) {
  277.                 speedIndex--;
  278.                 $("select#period").prop('selectedIndex', speedIndex);
  279.                 $("select#period").change();
  280.             }
  281.             return;
  282.         }
  283.         if (snake.direction == 'down') {
  284.             if (speedIndex < speedCount - 1) {
  285.                 speedIndex++;
  286.                 $("select#period").prop('selectedIndex', speedIndex);
  287.                 $("select#period").change();
  288.             }
  289.             return;
  290.         }
  291.         snake.direction = 'up';
  292.     }
  293.     if (direction == 'down') {
  294.         if (snake.direction == 'down') {
  295.             if (speedIndex > 0) {
  296.                 speedIndex--;
  297.                 $("select#period").prop('selectedIndex', speedIndex);
  298.                 $("select#period").change();
  299.             }
  300.             return;
  301.         }
  302.         if (snake.direction == 'up') {
  303.             if (speedIndex < speedCount - 1) {
  304.                 speedIndex++;
  305.                 $("select#period").prop('selectedIndex', speedIndex);
  306.                 $("select#period").change();
  307.             }
  308.             return;
  309.         }
  310.         snake.direction = 'down';
  311.     }
  312.     if (direction == 'left') {
  313.         if (snake.direction == 'left') {
  314.             if (speedIndex > 0) {
  315.                 speedIndex--;
  316.                 $("select#period").prop('selectedIndex', speedIndex);
  317.                 $("select#period").change();
  318.             }
  319.             return;
  320.         }
  321.         if (snake.direction == 'right') {
  322.             if (speedIndex < speedCount - 1) {
  323.                 speedIndex++;
  324.                 $("select#period").prop('selectedIndex', speedIndex);
  325.                 $("select#period").change();
  326.             }
  327.             return;
  328.         }
  329.         snake.direction = 'left';
  330.     }
  331.     if (direction == 'right') {
  332.         if (snake.direction == 'right') {
  333.             if (speedIndex > 0) {
  334.                 speedIndex--;
  335.                 $("select#period").prop('selectedIndex', speedIndex);
  336.                 $("select#period").change();
  337.             }
  338.             return;
  339.         }
  340.         if (snake.direction == 'left') {
  341.             if (speedIndex < speedCount - 1) {
  342.                 speedIndex++;
  343.                 $("select#period").prop('selectedIndex', speedIndex);
  344.                 $("select#period").change();
  345.             }
  346.             return;
  347.         }
  348.         snake.direction = 'right';
  349.     }
  350.     gameTick = 0;
  351. }
  352.  
  353. $("button#play-start").on('click', function() {
  354.     startAnimation();
  355. });
  356.  
  357. $("button#play-pause").on('click', function() {
  358.     stopAnimation();
  359. });
  360.  
  361. $("button#move-up").on('click', function() {
  362.     changeDirection('up');
  363. });
  364.  
  365. $("button#move-down").on('click', function() {
  366.     changeDirection('down');
  367. });
  368.  
  369. $("button#move-left").on('click', function() {
  370.     changeDirection('left');
  371. });
  372.  
  373. $("button#move-right").on('click', function() {
  374.     changeDirection('right');
  375. });
  376.  
  377. $("select#period").on('change', function() {
  378.     snake.speed = $("select#period").val();
  379. });
  380.  
  381. $("button#save-player-name").on('click', function() {
  382.     player.name = $("#player-name").val().trim().substring(0, 20);
  383.     player.getName();
  384. });
  385.  
  386. $("a#show-scores").on('click', function() {
  387.     gameArea.getScores();
  388. });
  389.  
  390. document.addEventListener('keydown', function(event) {
  391.     /*
  392.     event.preventDefault();
  393.     */
  394.     var keyCode = event.keyCode;
  395.     if (keyCode == 37) changeDirection('left');
  396.     if (keyCode == 38) changeDirection('up');
  397.     if (keyCode == 39) changeDirection('right');
  398.     if (keyCode == 40) changeDirection('down');
  399.     if (keyCode == 13) startAnimation();
  400.     if (keyCode == 32) stopAnimation();
  401.     if (keyCode == 27) resetGame();
  402. });
  403.  
  404. function paintCell(x, y, color) {
  405.     ctx.fillStyle = color;
  406.     ctx.fillRect(x * gameArea.cellSize + gameArea.cellDist / 2, y * gameArea.cellSize + gameArea.cellDist / 2, gameArea.cellSize - gameArea.cellDist, gameArea.cellSize - gameArea.cellDist);
  407. }
  408.  
  409. gameArea.init();
  410. snake.init();
  411. gameArea.setDiamond();
  412. player.showInput();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement