Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"></link>
  7.     <script>
  8.         function randomIntWrapper(min, max) {
  9.             return function () {
  10.                 return Math.floor((Math.random() * max) + min)
  11.             }
  12.         }
  13.  
  14.         function Shipt(config) {
  15.             this.config = config;
  16.             const nave = this;
  17.             this.game = config.game;
  18.             this.row = config.row;
  19.             this.col = config.col;
  20.  
  21.             this.impactos = function () {
  22.                 let countImpactos = 0;
  23.                 config.game.bullets.forEach(function (b) {
  24.                     if (b.row === nave.row && b.col === nave.col) {
  25.                         if (countImpactos > 0) {
  26.                             nave.destroy();
  27.                         }
  28.                         // else if (Math.random() < 0.2) {
  29.                         //     nave.destroy();
  30.                         // }
  31.                         else {
  32.                             if (b.direction === 'up') {
  33.                                 nave.row++
  34.                             } else {
  35.                                 nave.row--
  36.                             }
  37.                             if (nave.row > 20) {
  38.                                 nave.row = 0
  39.                             }
  40.                             if (nave.row < 0) {
  41.                                 nave.row = 20
  42.                             }
  43.                         }
  44.                         countImpactos++
  45.                     }
  46.                 });
  47.             };
  48.             this.checkIfWin = function () {
  49.                 if (nave.game.running) {
  50.                     if (nave.col >= 20) {
  51.                         let gate = nave.game.config.tablero.gate;
  52.                         if (nave.row >= gate.start && nave.row <= gate.end) {
  53.                             nave.game.win()
  54.                         } else {
  55.                             nave.game.gameOver();
  56.                         }
  57.                     }
  58.                 }
  59.             };
  60.  
  61.             this.update = function () {
  62.                 "use strict";
  63.                 nave.impactos();
  64.                 nave.col++;
  65.                 nave.checkIfWin()
  66.             };
  67.             this.render = function () {
  68.                 var elem = document.querySelector("span[row='" + this.row + "'][column='" + this.col + "']")
  69.                 elem.innerHTML = '<i class="fa fa-space-shuttle" aria-hidden="true"></i>'
  70.             };
  71.             this.destroy = function () {
  72.                 nave.game.gameOver();
  73.             }
  74.         }
  75.  
  76.         function Disparo(config) {
  77.             "use strict";
  78.             this.config = config || {};
  79.             this.row = config.row;
  80.             this.col = config.col;
  81.             this.update = function () {
  82.  
  83.             };
  84.  
  85.             this.render = function () {
  86.                 var elem = document.querySelector("span[row='" + this.row + "'][column='" + this.col + "']")
  87.                 elem.innerHTML = '<i class="fa fa-bullseye" aria-hidden="true"></i>'
  88.             }
  89.         }
  90.  
  91.         function Game(config) {
  92.             const game = this;
  93.             const runnig = true;
  94.             const zeroOrOne = randomIntWrapper(1, 2);
  95.             const randTablero = randomIntWrapper(0, 20);
  96.             this.config = config || {};
  97.             this.config.tablero = this.config.tablero || {rows: 21, cols: 21, gate: {start: 5, end: 9}};
  98.             this.gameOver = function () {
  99.                 clearInterval(this.mainLoopIterval);
  100.                 game.running = false;
  101.                 document.write('Game Over!')
  102.             };
  103.             this.win = function () {
  104.                 "use strict";
  105.                 clearInterval(game.mainLoopIterval);
  106.                 game.running = false;
  107.                 document.write('You Win!')
  108.             };
  109.             this.nave = new Shipt({row: 10, col: 0, game: this});
  110.             this.bullets = [];
  111.  
  112.             this.render = function () {
  113.                 "use strict";
  114.                 let html = '';
  115.                 html += '<div id="tablero">';
  116.                 for (let i = 0; i < 21; i++) {
  117.                     for (let j = 0; j < 21; j++) {
  118.                         if (j === 0) {
  119.                             html += '</br>';
  120.                         }
  121.                         html += '<span row="' + i + '" column="' + j + '" ><i class="fa fa-square" aria-hidden="true"></i></span>';
  122.                     }
  123.                 }
  124.                 html += '</div>';
  125.                 document.getElementsByTagName('body')[0].innerHTML = html;
  126.                 game.bullets.forEach(function (e) {
  127.                     e.render()
  128.                 });
  129.  
  130.                 game.nave.render();
  131.  
  132.                 let gate = game.config.tablero.gate;
  133.                 var st = document.querySelector("span[row='" + gate.start + "'][column='20']");
  134.                 st.innerHTML = '<i class="fa fa-square-o" aria-hidden="true"></i>'
  135.                 var end = document.querySelector("span[row='" + gate.end + "'][column='20']")
  136.                 end.innerHTML = '<i class="fa fa-square-o" aria-hidden="true"></i>'
  137.             };
  138.             this.spawnBullet = function () {
  139.                 let start = randTablero();
  140.                 let row = zeroOrOne() ? 0 : 20;
  141.                 game.bullets.push(new Disparo({col: start, row: row, direction: row === 0 ? 'down' : 'up', game: game}));
  142.  
  143.             };
  144.  
  145.  
  146.             this.update = function () {
  147.                 game.nave.update();
  148.                 game.bullets.forEach(function (e) {
  149.                     e.update();
  150.                 });
  151.                 if (zeroOrOne()) {
  152.                     game.spawnBullet()
  153.                 }
  154.             };
  155.             this.start = function () {
  156.                 game.running = true;
  157.                 this.mainLoopIterval = setInterval(function () {
  158.                     game.update();
  159.                     game.render();
  160.                 }, this.config.interval || 200)
  161.             };
  162.         }
  163.  
  164.     </script>
  165.     <style>
  166.         span {
  167.             display: inline-block
  168.         }
  169.     </style>
  170. </head>
  171. <body>
  172. <script>
  173.     document.write('<div id="tablero"><button onclick="new Game().start()">Run</button>');
  174.     for (let i = 0; i < 21; i++) {
  175.         for (let j = 0; j < 21; j++) {
  176.             if (j === 0) {
  177.                 document.write('</br>')
  178.             }
  179.             document.write('<span row="' + i + '" column="' + j + '" >X</span>')
  180.         }
  181.     }
  182.     document.write('</div>');
  183. </script>
  184.  
  185.  
  186. </body>
  187. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement