daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Oct 21st, 2018 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Creating variables
  2.  
  3. var sizeX = 50, sizeY = 50;
  4.  
  5. // -1 -> nishto
  6. // x  -> sleda player x
  7. var grid = [];
  8. for(var i = 0;i < sizeX;i ++) {
  9.     grid[i] = [];
  10.     for(var j = 0;j < sizeY;j ++) {
  11.         grid[i][j] = -1;
  12.     }
  13. }
  14.  
  15. var drawSize = 10;
  16.  
  17. var playerX = [], playerY = [], playerDirection = [], playerAlive = [], playerColor = [];
  18.  
  19.  
  20. var moveX = [], moveY = [];
  21.  
  22. moveX[0] = 0;
  23. moveX[1] = 0;
  24. moveX[2] = 1;
  25. moveX[3] = -1;
  26.  
  27. moveY[0] = -1;
  28. moveY[1] = 1;
  29. moveY[2] = 0;
  30. moveY[3] = 0;
  31.  
  32. // 0 - nagore
  33. // 1 - nadoly
  34. // 2 - nadqsno
  35. // 3 - nalqvo
  36. // 999 - nishto
  37.  
  38. var reverseDir = [];
  39. reverseDir[0] = 1;
  40. reverseDir[1] = 0;
  41. reverseDir[2] = 3;
  42. reverseDir[3] = 2;
  43.  
  44. for(var i = 0;i < 5;i ++) {
  45.     playerX[i] = Math.floor(Math.random() * sizeX);
  46.     playerY[i] = Math.floor(Math.random() * sizeX);
  47.     playerDirection[i] = Math.floor(Math.random() * moveX.length);
  48.     playerAlive[i] = true;
  49.     playerColor[i] = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")";;
  50. }
  51.  
  52. var keyToDirection = [];
  53. for(var i = 0;i < 200;i ++) {
  54.     keyToDirection[i] = 999;
  55. }
  56.  
  57. keyToDirection[87] = 0;
  58. keyToDirection[83] = 1;
  59. keyToDirection[68] = 2;
  60. keyToDirection[65] = 3;
  61.  
  62.  
  63.  
  64. var updateCount = 0;
  65.  
  66. function update() {
  67.     updateCount ++;
  68.    
  69.     if(updateCount % 5 == 0) {
  70.         for(var cp = 1;cp < playerX.length;cp ++) {
  71.             if(playerAlive[cp]) {
  72.                 var newX = playerX[cp] + moveX[playerDirection[cp]];
  73.                 var newY = playerY[cp] + moveY[playerDirection[cp]];
  74.                 var shteUmremLi = false;
  75.                 // proverka za izlizane ot ekrana
  76.                 if(newX < 0 || newX >= sizeX || newY < 0 || newY > sizeY) {
  77.                     shteUmremLi = true;
  78.                 } else {
  79.                     if(grid[newX][newY] != -1) {
  80.                         shteUmremLi = true;
  81.                     }
  82.                 }
  83.                 if(shteUmremLi || (Math.random() > 0.9)) {
  84.                     var newDir = Math.floor(Math.random() * moveX.length);
  85.                     while(newDir == playerDirection[cp] || newDir == reverseDir[playerDirection[cp]]) {
  86.                         newDir = Math.floor(Math.random() * moveX.length);
  87.                     }
  88.                     playerDirection[cp] = newDir;
  89.                 }
  90.             }
  91.         }
  92.         for(var cp = 0;cp < playerX.length;cp ++) {
  93.             if(playerAlive[cp]) {
  94.                 var newX = playerX[cp] + moveX[playerDirection[cp]];
  95.                 var newY = playerY[cp] + moveY[playerDirection[cp]];
  96.  
  97.                 // proverka za izlizane ot ekrana
  98.                 if(newX < 0 || newX >= sizeX || newY < 0 || newY > sizeY) {
  99.                     playerAlive[cp] = false;
  100.                 } else {
  101.                     if(grid[newX][newY] != -1) {
  102.                         playerAlive[cp] = false;
  103.                     } else {
  104.                         grid[playerX[cp]][playerY[cp]] = cp;
  105.                         playerX[cp] = newX;
  106.                         playerY[cp] = newY;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112. }
  113.  
  114. function draw() {
  115.     // This is how you draw a rectangle
  116.     for(var x = 0;x < sizeX;x ++) {
  117.         for(var y = 0;y < sizeY;y ++) {
  118.             if(grid[x][y] != -1) {
  119.                 context.fillStyle = playerColor[grid[x][y]];
  120.                 context.fillRect(x * drawSize, y * drawSize, drawSize, drawSize);
  121.             }
  122.         }
  123.     }
  124.     context.strokeStyle = "red";
  125.     context.lineWidth = 5;
  126.     for(var cp = 0;cp < playerX.length;cp ++) {
  127.         context.strokeRect(playerX[cp] * drawSize, playerY[cp] * drawSize,
  128.                         drawSize, drawSize);
  129.         if(playerAlive[cp]) { context.fillStyle = "black"; }
  130.         else { context.fillStyle = "red"; }
  131.         context.fillRect(playerX[cp] * drawSize, playerY[cp] * drawSize,
  132.                         drawSize, drawSize);
  133.     }
  134. };
  135.  
  136. function keyup(key) {
  137.     if(keyToDirection[key] != 999) {
  138.         playerDirection[0] = keyToDirection[key];
  139.     }
  140.     console.log(playerDirection[0]);
  141.     // Show the pressed keycode in the console
  142.     console.log("Pressed", key);
  143. };
  144.  
  145. function mouseup() {
  146.     // Show coordinates of mouse on click
  147.     console.log("Mouse clicked at", mouseX, mouseY);
  148. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top