SHARE
TWEET

Untitled

a guest Oct 10th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const gameContainer = document.getElementById("gameContainer");
  2.  
  3. let player = {
  4.     "position": {
  5.         "x": 5,
  6.         "y": 5
  7.     }
  8. };
  9.  
  10. function rysuj() {
  11.     let html = '<table>';
  12.     for(let x=0;x<tableWIDTH;x++) {
  13.         html += '<tr>';
  14.         for(let y=0;y<tableHEIGHT;y++) {
  15.             html += '<td>';
  16.             let img = '<img src="2.png">';
  17.             for(let i=0;i<table.length;i++) {
  18.                 if(table[i].x == x&&table[i].y == y) {
  19.                     img = '<img src="1.png">';
  20.                 }
  21.             }
  22.            
  23.             if(player.position.x == x&&player.position.y == y) {
  24.                 img = '<img src="3.png">';
  25.             }
  26.            
  27.             html += img+'</td>';
  28.         }
  29.         html += '</tr>';
  30.     }
  31.     html += '</table>';
  32.     gameContainer.innerHTML = html;
  33. }
  34.  
  35. setInterval(render, 20);
  36.  
  37. function render() {
  38.     rysuj();
  39. }
  40.  
  41.  
  42. function checkCollision(x, y) {
  43.     for(let i=0;i<table.length;i++) {
  44.         if(table[i].x == x&&table[i].y == y) {
  45.             return false;
  46.         }
  47.     }
  48.    
  49.     if(x>=tableWIDTH||y>=tableHEIGHT||x<0||y<0) {
  50.         return false;
  51.     }
  52.    
  53.     return true;
  54. }
  55.  
  56. document.addEventListener("keydown", (e) => {
  57.     switch(e.keyCode) {
  58.         case 65: // Left
  59.             if(checkCollision(player.position.x,player.position.y-1)) {
  60.                 player.position.y -= 1;
  61.             }
  62.         break;
  63.  
  64.         case 87: // Up
  65.             if(checkCollision(player.position.x-1,player.position.y)) {
  66.                 player.position.x -= 1;
  67.             }
  68.         break;
  69.  
  70.         case 68: // Right
  71.             if(checkCollision(player.position.x,player.position.y+1)) {
  72.                 player.position.y += 1;
  73.             }
  74.         break;
  75.  
  76.         case 83: // Down
  77.             if(checkCollision(player.position.x+1,player.position.y)) {
  78.                 player.position.x += 1;
  79.             }
  80.         break;
  81.     }
  82. });
  83.  
  84. rysuj();
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top