Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. const WINSCORE = 2048;
  2. const FIELDSIZE = 5;
  3. //Can't move
  4. var gameField = CreateMatrix(FIELDSIZE,FIELDSIZE);
  5. //var flying = [[x, y, 1024]];
  6. var keyboard;
  7. var SCORE = 0;
  8. var EmptyX;
  9. var EmptyY;
  10. var canvas = document.getElementById("matrix");
  11. var context = canvas.getContext("2d");
  12. canvas.width = 500;
  13. canvas.height = 500;
  14.  
  15. function RandomDegree() {
  16. return Math.floor(Math.random()*2) + 1;
  17. }
  18.  
  19. function Random(min, max) {
  20. return Math.floor(Math.random() * (max - min + 1)) + min;
  21. }
  22.  
  23. function drawBackground() {
  24. context.fillStyle = "#FAFAD2";
  25. context.fillRect(0, 0, canvas.width, canvas.height);
  26. for (let x = 100; x < 500; x += 100) {
  27. context.moveTo(x, 0);
  28. context.lineTo(x, 800);
  29. }
  30. for (let y = 100; y < 500; y += 100) {
  31. context.moveTo(0, y);
  32. context.lineTo(500, y);
  33. }
  34. context.strokeStyle = "#808080";
  35. context.stroke();
  36. context.strokeStyle = "#F00";
  37. context.font = "30px AR DELANEY";
  38. }
  39.  
  40. function DrawNumber(number, posY, posX, clear = true) {
  41. if (clear) {
  42. context.fillStyle = "#FAFAD2";
  43. context.fillRect(posX * 100 + 1, posY * 100 + 1, 98, 98);
  44. }
  45. context.textAlign = 'center';
  46. context.strokeText(number, (posX+1)*100-50, (posY+1)*100-50);
  47. }
  48.  
  49. function IsEmpty(Y, X) {
  50. return(gameField[Y][X] === 0);
  51. }
  52.  
  53. function ShowMatrix(rows){
  54. for(let i=0; i<rows; i++){
  55. console.log(gameField[i]);
  56. }
  57. console.log('');
  58. }
  59.  
  60. function CheckEmptyCells() {
  61. let flag = false;
  62. for (let i = 0; i < FIELDSIZE; i++) {
  63. for (let j = 0; j < FIELDSIZE; j++){
  64. if(IsEmpty(i, j)){
  65. EmptyX = j;
  66. EmptyY = i;
  67. flag = true;
  68. }
  69. }
  70. }
  71. return flag;
  72. }
  73.  
  74. function AddNewCell() {
  75. EndGame();
  76. var X = Random(0, FIELDSIZE-1);
  77. var Y = Random(0, FIELDSIZE-1);
  78. console.log(X, Y);
  79. if(IsEmpty(Y, X)) {
  80. gameField[Y][X] = Math.pow(2, RandomDegree());
  81. DrawNumber(gameField[Y][X], Y, X);
  82. }else{
  83. EndGame();
  84. gameField[EmptyY][EmptyX] = Math.pow(2, RandomDegree());
  85. DrawNumber(gameField[EmptyY][EmptyX], EmptyY, EmptyX);
  86. }
  87. }
  88.  
  89. function EndGame() {
  90. if(!CheckEmptyCells()){
  91. // матрица поля
  92. }
  93. }
  94.  
  95. function AddFirstCell() {
  96. AddNewCell();
  97. AddNewCell();
  98. }
  99.  
  100. function CreateMatrix(rows, columns){
  101. gameField = new Array();
  102. for(let i=0; i<rows; i++){
  103. gameField[i] = new Array();
  104. for(let j=0; j< columns; j++){
  105. gameField[i][j] = 0;
  106. }
  107. }
  108. return gameField;
  109. }
  110.  
  111. function Move(direction){
  112. let moves = [];
  113. switch(direction){
  114. case "left": // если нажата клавиша влево
  115. for(var i=0; i < FIELDSIZE; i++) {
  116. for (var j = 1; j < FIELDSIZE; j++) {
  117. if (gameField[i][j] === 0) continue;
  118. let Curr = 1;
  119. while (j - Curr > 0 && gameField[i][j - Curr] === 0) {
  120. Curr++;
  121. }
  122. if (gameField[i][j - Curr] === 0 || gameField[i][j - Curr] === gameField[i][j]) {
  123. gameField[i][j - Curr] += gameField[i][j];
  124. moves.push([i, j, i, j - Curr]);
  125. SCORE += gameField[i][j - Curr];//check
  126. } else {
  127. if (Curr === 1) continue;
  128. gameField[i][j - Curr + 1] += gameField[i][j];
  129. moves.push([i, j, i, j - Curr + 1]);
  130. }
  131. gameField[i][j] = 0;
  132. }
  133. }
  134. break;
  135. case "up": // если нажата клавиша вверх
  136. for(var i=1; i<FIELDSIZE; i++) {
  137. for (var j = 0; j < FIELDSIZE; j++) {
  138. if (gameField[i][j] === 0) continue;
  139. let Curr = 1;
  140. while (i - Curr > 0 && gameField[i - Curr][j] === 0) {
  141. Curr++;
  142. }
  143. if (gameField[i - Curr][j] === 0 || gameField[i- Curr][j] === gameField[i][j]) {
  144. gameField[i - Curr][j] += gameField[i][j];
  145. moves.push([i, j, i- Curr, j]);
  146. SCORE += gameField[i- Curr][j];
  147. } else {
  148. if (Curr === 1) continue;
  149. gameField[i - Curr + 1][j] += gameField[i][j];
  150. moves.push([i, j, i - Curr + 1, j]);
  151. }
  152. gameField[i][j] = 0;
  153. }
  154. }
  155. break;
  156. case "right": // если нажата клавиша вправо
  157. for(var i=0; i<FIELDSIZE; i++) {
  158. for (var j = FIELDSIZE-2; j >= 0; j--) {
  159. if (gameField[i][j] === 0) continue;
  160. let Curr = 1;
  161. while (j + Curr < FIELDSIZE-1 && gameField[i][j + Curr] === 0 ) {
  162. Curr++;
  163. }
  164. if (gameField[i][j + Curr] === 0 || gameField[i][j + Curr] === gameField[i][j]) {
  165. gameField[i][j + Curr] += gameField[i][j];
  166. moves.push([i, j, i, j + Curr]);
  167. SCORE += gameField[i][j + Curr];
  168. } else {
  169. if (Curr === 1) continue;
  170. gameField[i][j + Curr - 1] += gameField[i][j];
  171. moves.push([i, j, i, j + Curr - 1]);
  172. }
  173. gameField[i][j] = 0;
  174. }
  175. }
  176. break;
  177. case "down": // если нажата клавиша вниз
  178. for(var i=FIELDSIZE-2; i>=0; i--) {
  179. for (var j = 0; j < FIELDSIZE; j++) {
  180. if (gameField[i][j] === 0) continue;
  181. let Curr = 1;
  182. while (i + Curr < FIELDSIZE - 1 && gameField[i + Curr][j] === 0) {
  183. Curr++;
  184. }
  185. if (gameField[i + Curr][j] === 0 || gameField[i + Curr][j] === gameField[i][j]) {
  186. gameField[i + Curr][j] += gameField[i][j];
  187. moves.push([i, j, i+ Curr, j]);
  188. SCORE += gameField[i + Curr][j];
  189. } else {
  190. if (Curr === 1) continue;
  191. gameField[i + Curr - 1][j] += gameField[i][j];
  192. moves.push([i, j, i+ Curr - 1, j]);
  193. }
  194. gameField[i][j] = 0;
  195. }
  196. }
  197. break;
  198. }
  199. AddNewCell();
  200. for (m of moves) {
  201. //console.log(moves);
  202. let y1 = m[0], x1 = m[1], y2 = m[2], x2 = m[3];
  203. let x = x1, y = y1;
  204. let dx = (x2 - x1) / 10;
  205. let dy = (y2 - y1) / 10;
  206. let frame = 0;
  207. var timerId = setInterval(function () {
  208. DrawNumber('', Math.floor(y), Math.floor(x));
  209. DrawNumber('', Math.ceil(y), Math.ceil(x));
  210. y += dy;
  211. x += dx;
  212. DrawNumber(gameField[y2][x2], y, x, false);
  213. frame++;
  214. if (frame === 10)
  215. clearInterval(timerId);
  216.  
  217. }, 2000);
  218. }
  219. //ShowMatrix(FIELDSIZE);
  220. }
  221.  
  222. function Direction(key){
  223. var direction;
  224. switch(key.keyCode){
  225. case 37: // если нажата клавиша влево
  226. // что-то делаем
  227. direction = 'left';
  228. break;
  229. case 38: // если нажата клавиша вверх
  230. direction = 'up';
  231. break;
  232. case 39: // если нажата клавиша вправо
  233. direction = 'right';
  234. break;
  235. case 40: // если нажата клавиша вниз
  236. direction = 'down';
  237. break;
  238. }
  239. return Move(direction);
  240. }
  241.  
  242. function init() {
  243. drawBackground();
  244. AddFirstCell();
  245. addEventListener("keydown", Direction);
  246. }
  247.  
  248. init();
  249.  
  250. function show(state){
  251. document.getElementById('window').style.display = state;
  252. document.getElementById('wrap').style.display = state;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement