Advertisement
andrzuk

Tetris (code)

Mar 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Tetris 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. canvas.width = parseInt($("canvas#game-canvas").css("width"));
  11. canvas.height = parseInt($("canvas#game-canvas").css("height"));
  12.  
  13. var shapes = [
  14.     {
  15.         map: [[0, 0, 0], [1, 1, 1], [0, 0, 0]],
  16.         color: '#cc0000'
  17.     },
  18.     {
  19.         map: [[1, 1, 0], [1, 1, 0], [0, 0, 0]],
  20.         color: '#0099ff'
  21.     },
  22.     {
  23.         map: [[1, 0, 0], [1, 1, 0], [0, 1, 0]],
  24.         color: '#00cc00'
  25.     },
  26.     {
  27.         map: [[0, 0, 1], [0, 1, 1], [0, 1, 0]],
  28.         color: '#cc00cc'
  29.     },
  30.     {
  31.         map: [[0, 1, 0], [0, 1, 0], [0, 1, 1]],
  32.         color: '#ffcc33'
  33.     },
  34.     {
  35.         map: [[0, 1, 0], [0, 1, 0], [1, 1, 0]],
  36.         color: '#ff9933'
  37.     },
  38.     {
  39.         map: [[0, 1, 0], [1, 1, 1], [0, 0, 0]],
  40.         color: '#9966cc'
  41.     },
  42.     {
  43.         map: [[0, 0, 0], [1, 1, 1], [1, 0, 1]],
  44.         color: '#cc6600'
  45.     },
  46. ];
  47. var playGame = false, stepTime, playTime = 500, localStorageMaps = 'Tetris_Maps', localStorageStats = 'Tetris_Stats';
  48.  
  49. var gameArea = {
  50.     cellSize: 20,
  51.     cellDist: 2,
  52.     colsCount: 20,
  53.     rowsCount: 25,
  54.     cellColor: "#cde",
  55.     blockColor: "#666",
  56.     cellStatus: [],
  57.     startTime: null,
  58.     level: 0,
  59.     scores: 0,
  60.     record: 0,
  61.     achieved: '2000-01-01 01:01:01',
  62.     init: function() {
  63.         ctx.clearRect(0, 0, canvas.width, canvas.height);
  64.         ctx.fillStyle = "#def";
  65.         ctx.fillRect(0, 0, canvas.width, canvas.height);
  66.         this.cellStatus = [];
  67.         for (i = 0; i < this.colsCount; i++) {
  68.             var row = [];
  69.             for (j = 0; j < this.rowsCount; j++) {
  70.                 this.paintCell(i, j, this.cellColor);
  71.                 row.push(0);
  72.             }
  73.             this.cellStatus.push(row);
  74.         }
  75.         this.startTime = new Date();
  76.         this.level = 0;
  77.         this.scores = 0;
  78.         gameFigure.counter = 0;
  79.         $('div#game-message').text('Gra w toku...');
  80.         $('div#game-button').css({ display: 'none' });
  81.     },
  82.     paintCell: function(x, y, color) {
  83.         ctx.fillStyle = color;
  84.         ctx.fillRect(x * this.cellSize + this.cellDist / 2, y * this.cellSize + this.cellDist / 2, this.cellSize - this.cellDist, this.cellSize - this.cellDist);
  85.     },
  86.     clearFigure: function(figure) {
  87.         for (i = 0; i < figure.size; i++) {
  88.             for (j = 0; j < figure.size; j++) {
  89.                 if (figure.shape.map[i][j]) {
  90.                     this.paintCell(figure.position.x + i, figure.position.y + j, this.cellColor);
  91.                 }
  92.             }
  93.         }
  94.     },
  95.     paintFigure: function(figure) {
  96.         for (i = 0; i < figure.size; i++) {
  97.             for (j = 0; j < figure.size; j++) {
  98.                 if (figure.shape.map[i][j]) {
  99.                     this.paintCell(figure.position.x + i, figure.position.y + j, figure.shape.color);
  100.                 }
  101.             }
  102.         }
  103.     },
  104.     checkCollision: function(figure) {
  105.         for (i = 0; i < figure.size; i++) {
  106.             for (j = 0; j < figure.size; j++) {
  107.                 if (figure.shape.map[i][j]) {
  108.                     if (figure.position.x + i < 0) return true;
  109.                     if (figure.position.x + i > this.colsCount - 1) return true;
  110.                     /*
  111.                     if (figure.position.y + j < 0) return true;
  112.                     */
  113.                     if (figure.position.y + j > this.rowsCount - 1) return true;
  114.                     if (gameArea.cellStatus[figure.position.x + i][figure.position.y + j]) return true;
  115.                 }
  116.             }
  117.         }
  118.         return false;
  119.     },
  120.     checkCompletion: function() {
  121.         var completed = 0;
  122.         for (i = 0; i < this.colsCount; i++) {
  123.             completed += gameArea.cellStatus[i][this.rowsCount - 1] ? 1 : 0;
  124.         }
  125.         return completed == this.colsCount;
  126.     },
  127.     collapse: function() {
  128.         for (j = this.rowsCount - 1; j > 0; j--) {
  129.             for (i = 0; i < this.colsCount; i++) {
  130.                 gameArea.cellStatus[i][j] = gameArea.cellStatus[i][j - 1];
  131.             }
  132.         }
  133.         for (i = 0; i < this.colsCount; i++) {
  134.             gameArea.cellStatus[i][0] = 0;
  135.         }
  136.         for (j = this.rowsCount - 1; j > 0; j--) {
  137.             for (i = 0; i < this.colsCount; i++) {
  138.                 if (this.cellStatus[i][j]) {
  139.                     if (this.cellStatus[i][j] <= shapes.length) {
  140.                         this.paintCell(i, j, shapes[this.cellStatus[i][j] - 1].color);
  141.                     }
  142.                     else {
  143.                         this.paintCell(i, j, this.blockColor);
  144.                     }
  145.                 }
  146.                 else {
  147.                     this.paintCell(i, j, this.cellColor);
  148.                 }
  149.             }
  150.         }
  151.         for (i = 0; i < this.colsCount; i++) {
  152.             this.paintCell(i, 0, this.cellColor);
  153.         }
  154.         this.level++;
  155.         this.scores += this.colsCount;
  156.         if (playTime > 20) playTime -= 10;
  157.         gameFigure.init();
  158.     },
  159.     saveStatistics: function() {
  160.         if (gameArea.scores > gameArea.record) {
  161.             gameArea.record = gameArea.scores;
  162.             gameArea.achieved = gameArea.getDate();
  163.         }
  164.         var stats = {
  165.             'blocks': gameFigure.counter,
  166.             'maps': shapes.length,
  167.             'level': gameArea.level,
  168.             'scores': gameArea.scores,
  169.             'record': gameArea.record,
  170.             'achieved': gameArea.achieved,
  171.             'play_time': gameArea.getTime(),
  172.             'finished': gameArea.getDate()
  173.         };
  174.         localStorage.setItem(localStorageStats, JSON.stringify(stats));
  175.         $.post("ajax/game/store_stats.php", stats, function(data, status) {
  176.             var response = JSON.parse(data);
  177.             gameArea.record = response.record;
  178.             gameArea.achieved = response.achieved;
  179.             $('#record').text('Rekord: ' + gameArea.record);
  180.             $('#achieved').text('(' + gameArea.achieved + ')');
  181.         });
  182.     },
  183.     getDate: function() {
  184.         var currTime = new Date();
  185.         var day = currTime.getDate();
  186.         var month = currTime.getMonth() + 1;
  187.         var year = currTime.getFullYear();
  188.         var hours = currTime.getHours();
  189.         var minutes = currTime.getMinutes();
  190.         var seconds = currTime.getSeconds();       
  191.         return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
  192.     },
  193.     getTime: function() {
  194.         var currTime = new Date();
  195.         var minutes = Math.floor((currTime - this.startTime) / 1000 / 60);
  196.         var seconds = Math.floor((currTime - this.startTime) / 1000 % 60);
  197.         var minString = minutes < 10 ? "0" + minutes.toString() : minutes.toString();
  198.         var secString = seconds < 10 ? "0" + seconds.toString() : seconds.toString();
  199.         return minString + ":" + secString;
  200.     },
  201. };
  202.  
  203. var gameFigure = {
  204.     size: 0,
  205.     index: 0,
  206.     position: { x: 0, y: 0 },
  207.     shape: {},
  208.     drop: false,
  209.     counter: 0,
  210.     init: function() {
  211.         var idx = Math.floor(Math.random() * shapes.length);
  212.         this.shape = shapes[idx];
  213.         this.index = idx + 1;
  214.         this.size = this.shape.map.length;
  215.         this.position = { x: gameArea.colsCount / 2 - 2, y: -this.size };
  216.         var prepare = Math.floor(Math.random() * 4);
  217.         switch (prepare) {
  218.             case 1:
  219.                 this.rotate('left');
  220.                 break;
  221.             case 2:
  222.                 this.rotate('right');
  223.                 break;
  224.             case 3:
  225.                 for (i = 0; i < 2; i++) {
  226.                     this.rotate('left');
  227.                 }
  228.                 break;
  229.         }
  230.         this.drop = false;
  231.         stepTime = playTime;
  232.         this.counter++;
  233.         gameArea.scores += gameArea.level;
  234.         playGame = true;
  235.         $('#blocks').text('Bloczek: ' + gameFigure.counter.toString());
  236.         $('#level').text('Poziom: ' + gameArea.level.toString());
  237.         $('#scores').text('Punktów: ' + gameArea.scores.toString());
  238.         $('#record').text('Rekord: ' + gameArea.record.toString());
  239.         $('#achieved').text('(' + gameArea.achieved + ')');
  240.         if (gameArea.checkCollision(this)) {
  241.             playGame = false;
  242.             gameArea.saveStatistics();
  243.             $('div#game-message').text('Gra zakończona.');
  244.             $('div#game-button').css({ display: 'block' });
  245.         }
  246.         else {
  247.             gameFigure.animate();
  248.         }
  249.     },
  250.     animate: function() {
  251.         if (!playGame) return;
  252.         var collision = false;
  253.         gameArea.clearFigure(this);
  254.         this.position.y++;
  255.         if (gameArea.checkCollision(this)) {
  256.             this.position.y--;
  257.             collision = true;
  258.         }
  259.         gameArea.paintFigure(this);
  260.         $('#position').text('Pozycja: (' + this.position.x.toString() + ', ' + this.position.y.toString() + ')');
  261.         $('#time').text('Czas: ' + gameArea.getTime());
  262.         if (collision) {
  263.             this.settle();
  264.         }
  265.         else {
  266.             setTimeout(function() {
  267.                 gameFigure.animate();
  268.             }, stepTime);
  269.         }
  270.     },
  271.     move: function(direction) {
  272.         if (direction == 'left') {
  273.             gameArea.clearFigure(this);
  274.             this.position.x--;
  275.             if (gameArea.checkCollision(this)) this.position.x++;
  276.             gameArea.paintFigure(this);
  277.         }
  278.         if (direction == 'right') {
  279.             gameArea.clearFigure(this);
  280.             this.position.x++;
  281.             if (gameArea.checkCollision(this)) this.position.x--;
  282.             gameArea.paintFigure(this);
  283.         }
  284.         if (direction == 'up') {
  285.             /*
  286.             gameArea.clearFigure(this);
  287.             this.position.y--;
  288.             if (gameArea.checkCollision(this)) this.position.y++;
  289.             gameArea.paintFigure(this);
  290.             */
  291.         }
  292.         if (direction == 'down') {
  293.             gameArea.clearFigure(this);
  294.             this.position.y++;
  295.             if (gameArea.checkCollision(this)) this.position.y--;
  296.             gameArea.paintFigure(this);
  297.         }
  298.     },
  299.     rotate: function(direction) {
  300.         var map = [], row = [];
  301.         for (i = 0; i < this.size; i++) {
  302.             row = [];
  303.             for (j = 0; j < this.size; j++) {
  304.                 row.push(this.shape.map[i][j]);
  305.             }
  306.             map.push(row);
  307.         }
  308.         if (direction == 'left') {
  309.             gameArea.clearFigure(this);
  310.             for (i = 0; i < this.size; i++) {
  311.                 for (j = 0; j < this.size; j++) {
  312.                     this.shape.map[i][j] = map[this.size - j - 1][i];
  313.                 }
  314.             }
  315.             if (gameArea.checkCollision(this)) this.shape.map = map;
  316.             gameArea.paintFigure(this);
  317.         }
  318.         if (direction == 'right') {
  319.             gameArea.clearFigure(this);
  320.             for (i = 0; i < this.size; i++) {
  321.                 for (j = 0; j < this.size; j++) {
  322.                     this.shape.map[i][j] = map[j][this.size - i - 1];
  323.                 }
  324.             }
  325.             if (gameArea.checkCollision(this)) this.shape.map = map;
  326.             gameArea.paintFigure(this);
  327.         }
  328.     },
  329.     settle: function() {
  330.         for (i = 0; i < this.size; i++) {
  331.             for (j = 0; j < this.size; j++) {
  332.                 if (this.shape.map[i][j]) {
  333.                     gameArea.cellStatus[this.position.x + i][this.position.y + j] = this.index;
  334.                 }
  335.             }
  336.         }
  337.         if (gameArea.checkCompletion()) {
  338.             setTimeout(function() {
  339.                 gameArea.collapse();
  340.             }, playTime);
  341.         }
  342.         else {
  343.             this.init();
  344.         }
  345.     },
  346. };
  347.  
  348. var mapEditor = {
  349.     customMaps: [],
  350.     customMap: [],
  351.     customColors: [],
  352.     customColor: '#336699',
  353.     loadMaps: function() {
  354.         this.customMaps = [];
  355.         for (item = 0; item < shapes.length; item++) {
  356.             this.customMap = [];
  357.             for (j = 0; j < shapes[item].map.length; j++) {
  358.                 var row = [];
  359.                 for (i = 0; i < shapes[item].map.length; i++) {
  360.                     row.push(shapes[item].map[i][j]);
  361.                 }
  362.                 this.customMap.push(row);
  363.             }
  364.             this.customMaps.push(this.customMap);
  365.         }
  366.         var size = this.customMaps[this.customMaps.length - 1].length;
  367.         this.customMap = [];
  368.         for (j = 0; j < size; j++) {
  369.             var row = [];
  370.             for (i = 0; i < size; i++) {
  371.                 row.push(0);
  372.             }
  373.             this.customMap.push(row);
  374.         }
  375.         var mapItems = '';
  376.         mapItems += '<div id="maps-container">';
  377.         for (item = 0; item < this.customMaps.length; item++) {
  378.             size = this.customMaps[item].length;
  379.             this.customColors[item] = shapes[item].color;
  380.             var mapItem = '';
  381.             mapItem += '<div class="item-container">';
  382.             mapItem += '<div class="lp">' + (item + 1).toString() + '.' + '</div>';
  383.             mapItem += '<div class="map">';
  384.             mapItem += '<table align="center">';
  385.             for (j = 0; j < this.customMaps[item].length; j++) {
  386.                 mapItem += '<tr>';
  387.                 for (i = 0; i < this.customMaps[item].length; i++) {
  388.                     cellStyle = this.customMaps[item][i][j] ? 'background: ' + shapes[item].color : '';
  389.                     mapItem += '<td><button id="map-' + item + '-' + i + '-' + j + '" class="btn btn-default map-element" style="' + cellStyle + '"></button></td>';
  390.                 }
  391.                 mapItem += '</tr>';
  392.             }
  393.             mapItem += '</table>';
  394.             mapItem += '</div>';
  395.             mapItem += '<div class="color"><input type="color" id="colorpicker-' + item + '" onchange="mapEditor.setColor(this.id, this.value)" value="' + shapes[item].color + '"></div>';
  396.             mapItem += '<div class="actions"><button id="save-' + item + '" class="btn btn-xs btn-primary update-map">Zapisz</button>&nbsp;<button id="delete-' + item + '" class="btn btn-xs btn-warning delete-map">Usuń</button></div>';
  397.             mapItem += '</div>';
  398.             mapItems += mapItem;
  399.         }
  400.         var newItem = '';
  401.         newItem += '<div class="item-container">';
  402.         newItem += '<div class="lp">-</div>';
  403.         newItem += '<div class="map">';
  404.         newItem += '<table align="center">';
  405.         for (j = 0; j < size; j++) {
  406.             newItem += '<tr>';
  407.             for (i = 0; i < size; i++) {
  408.                 newItem += '<td><button id="map-new-' + i + '-' + j + '" class="btn btn-default map-element"></button></td>';
  409.             }
  410.             newItem += '</tr>';
  411.         }
  412.         newItem += '</table>';
  413.         newItem += '</div>';
  414.         newItem += '<div class="color"><input type="color" id="colorpicker-new" onchange="mapEditor.setColor(this.id, this.value)" value="' + mapEditor.customColor + '"></div>';
  415.         newItem += '<div class="actions"><button id="add-map" class="btn btn-xs btn-success disabled">Dodaj do kolekcji</button></div>';
  416.         newItem += '</div>';
  417.         mapItems += newItem;
  418.         mapItems += '</div>';
  419.         $('div#maps-table').html(mapItems);
  420.         $('button.map-element').bind('click', function() {
  421.             var id = $(this).attr('id');
  422.             var parts = id.split('-');
  423.             var item = parts[1], x = parts[2], y = parts[3];
  424.             if (item == 'new') {
  425.                 mapEditor.customMap[x][y] = 1 - mapEditor.customMap[x][y];
  426.                 var cellColor = mapEditor.customMap[x][y] ? mapEditor.customColor : '';
  427.                 $('button#map-new-' + x + '-' + y).css({ 'background': cellColor });
  428.                 var filled = false;
  429.                 for (i = 0; i < mapEditor.customMap.length; i++) {
  430.                     for (j = 0; j < mapEditor.customMap.length; j++) {
  431.                         if (mapEditor.customMap[i][j]) {
  432.                             filled = true;
  433.                         }
  434.                     }
  435.                 }
  436.                 if (filled) {
  437.                     $('button#add-map').removeClass('disabled');
  438.                 }
  439.                 else {
  440.                     $('button#add-map').addClass('disabled');
  441.                 }
  442.             }
  443.             else {
  444.                 mapEditor.customMaps[item][x][y] = 1 - mapEditor.customMaps[item][x][y];
  445.                 var cellColor = mapEditor.customMaps[item][x][y] ? shapes[item].color : '';
  446.                 $('button#map-' + item + '-' + x + '-' + y).css({ 'background': cellColor });
  447.                 var filled = false;
  448.                 for (i = 0; i < mapEditor.customMaps[item].length; i++) {
  449.                     for (j = 0; j < mapEditor.customMaps[item].length; j++) {
  450.                         if (mapEditor.customMaps[item][i][j]) {
  451.                             filled = true;
  452.                         }
  453.                     }
  454.                 }
  455.                 if (filled) {
  456.                     $('button#save-' + item).attr('disabled', false);
  457.                 }
  458.                 else {
  459.                     $('button#save-' + item).attr('disabled', true);
  460.                 }
  461.             }
  462.         });
  463.         $('button#add-map').bind('click', function() {
  464.             var newMap = [];
  465.             for (j = 0; j < mapEditor.customMap.length; j++) {
  466.                 var row = [];
  467.                 for (i = 0; i < mapEditor.customMap.length; i++) {
  468.                     row.push(mapEditor.customMap[i][j]);
  469.                 }
  470.                 newMap.push(row);
  471.             }
  472.             if (mapEditor.checkUnique(newMap)) {
  473.                 shapes.push({ map: newMap, color: mapEditor.customColor });
  474.                 mapEditor.loadMaps();
  475.                 mapEditor.setMessage('Mapa została pomyślnie dodana do kolekcji.', 'success');
  476.             }
  477.             else {
  478.                 mapEditor.setMessage('Taka mapa już istnieje w kolekcji.', 'error');
  479.             }
  480.         });
  481.         $('button.update-map').bind('click', function() {
  482.             var object = $(this).attr('id');
  483.             var parts = object.split('-');
  484.             var id = parts[1];
  485.             if (mapEditor.checkOthers(mapEditor.customMaps[id], id)) {
  486.                 for (j = 0; j < shapes[id].map.length; j++) {
  487.                     for (i = 0; i < shapes[id].map.length; i++) {
  488.                         shapes[id].map[i][j] = mapEditor.customMaps[id][j][i];
  489.                     }
  490.                 }
  491.                 shapes[id].color = mapEditor.customColors[id];
  492.                 mapEditor.loadMaps();
  493.                 mapEditor.setMessage('Mapa została pomyślnie zapisana.', 'success');
  494.             }
  495.             else {
  496.                 mapEditor.setMessage('Taka mapa już istnieje w kolekcji.', 'error');
  497.             }
  498.         });
  499.         $('button.delete-map').bind('click', function() {
  500.             var object = $(this).attr('id');
  501.             var parts = object.split('-');
  502.             var id = parts[1];
  503.             if (shapes.length > 1) {
  504.                 shapes.splice(id, 1);
  505.                 mapEditor.loadMaps();
  506.                 mapEditor.setMessage('Mapa została pomyślnie usunięta z kolekcji.', 'success');
  507.             }
  508.             else {
  509.                 mapEditor.setMessage('Kolekcja musi zawierać przynajmniej jedną mapę.', 'error');
  510.             }
  511.         });
  512.     },
  513.     checkUnique: function(map) {
  514.         for (q = 0; q < 4; q++) {
  515.             map = this.rotateQuarter(map);
  516.             for (item = 0; item < shapes.length; item++) {
  517.                 var difference = true;
  518.                 for (j = 0; j < shapes[item].map.length; j++) {
  519.                     for (i = 0; i < shapes[item].map.length; i++) {
  520.                         if (map[i][j] != shapes[item].map[i][j]) {
  521.                             difference = false;
  522.                         }
  523.                     }
  524.                 }
  525.                 if (difference) {
  526.                     return false;
  527.                 }
  528.             }
  529.         }
  530.         return true;
  531.     },
  532.     checkOthers: function(map, id) {
  533.         for (q = 0; q < 4; q++) {
  534.             map = this.rotateQuarter(map);
  535.             for (item = 0; item < shapes.length; item++) {
  536.                 if (item == id) continue;
  537.                 var difference = true;
  538.                 for (j = 0; j < shapes[item].map.length; j++) {
  539.                     for (i = 0; i < shapes[item].map.length; i++) {
  540.                         if (map[j][i] != shapes[item].map[i][j]) {
  541.                             difference = false;
  542.                         }
  543.                     }
  544.                 }
  545.                 if (difference) {
  546.                     return false;
  547.                 }
  548.             }
  549.         }
  550.         return true;
  551.     },
  552.     rotateQuarter: function(map) {
  553.         var row = [], rotated = [];
  554.         for (i = 0; i < map.length; i++) {
  555.             row = [];
  556.             for (j = 0; j < map.length; j++) {
  557.                 row.push(map[i][j]);
  558.             }
  559.             rotated.push(row);
  560.         }
  561.         for (i = 0; i < map.length; i++) {
  562.             for (j = 0; j < map.length; j++) {
  563.                 rotated[i][j] = map[j][map.length - i - 1];
  564.             }
  565.         }
  566.         return rotated;
  567.     },
  568.     setColor: function(object, color) {
  569.         var parts = object.split('-');
  570.         var id = parts[1];
  571.         if (id == 'new') {
  572.             this.customColor = color;
  573.         }
  574.         else {
  575.             this.customColors[id] = color;
  576.         }
  577.     },
  578.     setMessage: function(message, type) {
  579.         switch (type) {
  580.             case 'success':
  581.                 $('div#alert-info').text(message);
  582.                 $('div#alert-info').css({ display: 'block' });
  583.                 $('div#alert-danger').css({ display: 'none' });
  584.                 break;
  585.             case 'error':
  586.                 $('div#alert-danger').text(message);
  587.                 $('div#alert-info').css({ display: 'none' });
  588.                 $('div#alert-danger').css({ display: 'block' });
  589.                 break;
  590.             default:
  591.                 $('div#alert-info').css({ display: 'none' });
  592.                 $('div#alert-danger').css({ display: 'none' });
  593.                 break;
  594.         }
  595.     }
  596. };
  597.  
  598. $(document).ready(function() {
  599.     var data = localStorage.getItem(localStorageMaps);
  600.     if (data !== null) {
  601.         var object = JSON.parse(data);
  602.         shapes = [];
  603.         for (i = 0; i < object.maps.length; i++) {
  604.             shapes.push(object.maps[i]);
  605.         }
  606.     }
  607.     mapEditor.loadMaps();
  608.     var stats = localStorage.getItem(localStorageStats);
  609.     if (stats !== null) {
  610.         var object = JSON.parse(stats);
  611.         gameArea.record = object.record;
  612.         gameArea.achieved = object.achieved;
  613.     }
  614. });
  615.  
  616. if (typeof eventsListenerRegistered == 'undefined') {
  617.    
  618.     document.addEventListener('keydown', function(event) {
  619.         if (event.keyCode == 37) {
  620.             gameFigure.move('left');
  621.         }
  622.         if (event.keyCode == 39) {
  623.             gameFigure.move('right');
  624.         }
  625.         if (event.keyCode == 38) {
  626.             gameFigure.move('up');
  627.         }
  628.         if (event.keyCode == 40) {
  629.             gameFigure.move('down');
  630.         }
  631.         if (event.keyCode == 33) {
  632.             gameFigure.rotate('left');
  633.         }
  634.         if (event.keyCode == 34) {
  635.             gameFigure.rotate('right');
  636.         }
  637.         if (event.keyCode == 32) {
  638.             if (!gameFigure.drop) {
  639.                 playTime = stepTime;
  640.                 stepTime = 20;
  641.                 gameFigure.drop = true;
  642.             }
  643.         }
  644.     });
  645.    
  646.     eventsListenerRegistered = true;
  647. }
  648.  
  649. $('button#start').on('click', function() {
  650.     gameArea.init();
  651.     gameFigure.init();
  652. });
  653.  
  654. $('button#rotate-left').on('click', function() {
  655.     gameFigure.rotate('left');
  656. });
  657.  
  658. $('button#rotate-right').on('click', function() {
  659.     gameFigure.rotate('right');
  660. });
  661.  
  662. $('button#move-left').on('click', function() {
  663.     gameFigure.move('left');
  664. });
  665.  
  666. $('button#move-right').on('click', function() {
  667.     gameFigure.move('right');
  668. });
  669.  
  670. $('button.drop-down').on('click', function() {
  671.     if (!gameFigure.drop) {
  672.         playTime = stepTime;
  673.         stepTime = 20;
  674.         gameFigure.drop = true;
  675.     }
  676. });
  677.  
  678. $('button#map-editor').on('click', function() {
  679.     $('div#game-play').css({ display: 'none' });
  680.     $('div#game-map').css({ display: 'block' });
  681.     mapEditor.setMessage(null, null);
  682. });
  683.  
  684. $('button#close-editor').on('click', function() {
  685.     $('div#game-play').css({ display: 'block' });
  686.     $('div#game-map').css({ display: 'none' });
  687.     mapEditor.setMessage(null, null);
  688. });
  689.  
  690. $('button#save-maps').on('click', function() {
  691.     var customMaps = { 'maps': shapes };
  692.     localStorage.setItem(localStorageMaps, JSON.stringify(customMaps));
  693.     $('button#close-editor').click();
  694. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement