Advertisement
GoToBread

JS Monster Spawn

Oct 30th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html>
  3.     <head>
  4.         <title>Monsters</title>
  5.         <style>
  6.             #main {background-color: black;}
  7.         </style>
  8.     </head>
  9.     <body onload="run();">
  10.         <canvas id="main" width="640" height="480"></canvas>
  11.         <script src="keyHandling.js"></script>
  12.         <script>
  13.             /*
  14.             You are a box on the screen, moving with the arrow keys.
  15.             There are other boxes around you in random places.
  16.            
  17.             There are a random amount of monsters on the screen per
  18.             page refresh, all in random positions.
  19.             */
  20.             var canvas = document.getElementById("main");
  21.             var context = canvas.getContext("2d");
  22.            
  23.             // generic entity object
  24.             var entity = {
  25.                 x: 0,
  26.                 y: 0,
  27.                 width:  50,
  28.                 height: 50,
  29.                 color: "rgb(0, 0, 0)"
  30.             };
  31.            
  32.             var player = Object.create(entity);
  33.             var floorTile = Object.create(entity);
  34.            
  35.             var initMonsterSwarm = function(numMonsters) {
  36.                 var swarm = [];
  37.                 for(var m = 0; m < numMonsters; m++) {
  38.                     var monster = Object.create(entity);
  39.                     monster.color = "rgb(255, 0, 0)";
  40.                     swarm.push(monster);
  41.                 }
  42.                 return swarm;
  43.             };
  44.            
  45.             var initMonsterPosition = function(swarm) {
  46.                 if(swarm !== []) {
  47.                     for(var i = 0, j = swarm.length; i < j; i++) {
  48.                         swarm[i].x = Math.random()*canvas.width;
  49.                         swarm[i].y = Math.random()*canvas.height;
  50.                     }
  51.                 }
  52.             };
  53.            
  54.             var spawnMonsterSwarm = function(swarm) {
  55.                 if(swarm !== []) {
  56.                     for(var s = 0, t = swarm.length; s < t; s++) {
  57.                         context.fillStyle = swarm[s].color;
  58.                         context.fillRect(swarm[s].x,
  59.                                 swarm[s].y,
  60.                                 swarm[s].width,
  61.                                 swarm[s].height);
  62.                     }
  63.                 }
  64.             };
  65.            
  66.             var initFloor = function() {
  67.                 floorTile.color = "rgb(0, 0, 100)";
  68.             };
  69.             var drawFloor = function() {
  70.                 context.fillStyle = floorTile.color;
  71.                 context.fillRect(0, 0, canvas.width, canvas.height);
  72.             };
  73.            
  74.             var initPlayer = function() {
  75.                 player.color = "rgb(255, 255, 255)"
  76.                 context.fillStyle = player.color;
  77.             };
  78.             var drawPlayer = function() {
  79.                 context.fillStyle = player.color;
  80.                 context.fillRect(player.x, player.y, player.width, player.height);
  81.             };
  82.            
  83.             var update = function() {
  84.                 var s = 3;
  85.                 if(key.left)  {player.x -= s;}
  86.                 if(key.up)    {player.y -= s;}
  87.                 if(key.right) {player.x += s;}
  88.                 if(key.down)  {player.y += s;}
  89.             };
  90.            
  91.             var numMonsters = Math.random()*10;
  92.            
  93.             var initGame = function() {
  94.                 swarm = initMonsterSwarm(numMonsters);
  95.                 initMonsterPosition(swarm);
  96.                 initFloor();
  97.                 initPlayer();
  98.             };
  99.            
  100.             var main = function() {
  101.                 drawFloor();
  102.                 drawPlayer();
  103.                 spawnMonsterSwarm(swarm);
  104.                 update();
  105.             };
  106.            
  107.             var run = function() {
  108.                 var FPS = 1000 / 60;
  109.                 initGame();
  110.                 setInterval(main, FPS);
  111.             };
  112.         </script>
  113.     </body>
  114. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement