Advertisement
Guest User

Basic Anysite Game By Samuel Walls

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     var ball = undefined;
  3.     var data = {
  4.         'x': 50,
  5.         'y': 50,
  6.         'dirX': 90,
  7.         'dirY': 90
  8.     };
  9.     var currentDirection = [0, 3];
  10.  
  11.     const changeDirection = function() {
  12.         currentDirection[0] = Math.floor(Math.random() * 2);
  13.         currentDirection[1] = Math.floor(Math.random() * 2 + 2);
  14.     }
  15.  
  16.     const createBall = function() {
  17.         var elem = document.createElement('div');
  18.         elem.id = 'gameBallMovable';
  19.         elem.style.display = 'block';
  20.         elem.style.position = 'absolute';
  21.         elem.style.background = 'radial-gradient(circle at 100px 100px, #00ffff, #000)';
  22.         elem.style.width = elem.style.height = '50px';
  23.         elem.style.borderRadius = '50%';
  24.         elem.style.top = elem.style.left = '0';
  25.         elem.style.userSelect = 'none';
  26.         document.body.appendChild(elem);
  27.         ball = document.getElementById('gameBallMovable');
  28.     };
  29.  
  30.     const touchingElement = function(obj) {
  31.         const x = obj.x,
  32.                     y = obj.y,
  33.                     elems = document.getElementsByTagName('*'),
  34.                     ignore = ['div', 'span', 'body', 'head', 'html', 'style', 'script', 'link', 'meta', 'title', 'option', 'optgroup', 'link'];
  35.         for (let i = 0; i < elems.length; i++) {
  36.             info = elems[i].getBoundingClientRect();
  37.             if (elems[i].tagName && ignore.join(',').indexOf(elems[i].tagName.toLocaleLowerCase()) == -1 && elems[i].id != 'gameBallMovable' && elems[i].style.visibility !='hidden' && elems[i].style.display != 'none' && elems[i].style.opacity != '0' && (info.width > 5 || info.height > 5) && x > info.x && x < info.x + info.width && y > info.y && y < info.y + info.height) {
  38.                 return [true, elems[i]];
  39.             }
  40.         }
  41.         return [false, undefined];
  42.     }
  43.  
  44.     const gameLoop = function() {
  45.         ball.style.left = (data.x - 25) + 'px';
  46.         ball.style.top = (data.y - 25) + 'px';
  47.         ball.style.transform = `rotate(${data.angle}deg)`;
  48.         if (data.x > window.innerWidth || data.y > window.innerHeight || data.x < 0 || data.y < 0) {
  49.             changeDirection();
  50.         }
  51.         for (let i = 0; i < currentDirection.length; i++) {
  52.             switch (currentDirection[i]) {
  53.                 case 0:
  54.                     data.x += 10;
  55.                     break;
  56.                 case 1:
  57.                     data.x -= 10
  58.                     break;
  59.                 case 2:
  60.                     data.y += 10;
  61.                     break;
  62.                 case 3:
  63.                     data.y -= 10;
  64.                     break;
  65.             }
  66.         }
  67.  
  68.         data.x < 25 && (data.x = 25, changeDirection());
  69.         data.x > window.innerWidth - 25 && (data.x = window.innerWidth - 25, changeDirection());
  70.         data.y < 25 && (data.y = 25, changeDirection());
  71.         data.y > window.innerHeight - 25 && (data.y = window.innerHeight - 25, changeDirection());
  72.  
  73.         const touchingObj = touchingElement(data);
  74.         if (touchingObj[0]) {
  75.             touchingObj[1].style.opacity = '0';
  76.             changeDirection();
  77.         }
  78.         window.requestAnimationFrame(gameLoop);
  79.     }
  80.  
  81.     const initiate = function() {
  82.         createBall();
  83.         gameLoop();
  84.     }
  85.     initiate();
  86. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement