Advertisement
Guest User

Experiment with objects in JavaScript.

a guest
Jun 9th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Create the canvas
  2. var canvas = document.createElement("canvas");
  3. var ctx = canvas.getContext("2d");
  4. canvas.width = 512;
  5. canvas.height = 480;
  6. document.body.appendChild(canvas);
  7.  
  8. // Background image
  9. var bgReady = false;
  10. var bgImage = new Image();
  11. bgImage.onload = function () {
  12.     bgReady = true;
  13. };
  14. bgImage.src = "images/background.png";
  15.  
  16. // Hero image
  17. var heroReady = false;
  18. var heroImage = new Image();
  19. heroImage.onload = function () {
  20.     heroReady = true;
  21. };
  22. heroImage.src = "images/hero.png";
  23.  
  24. // Monster image
  25. var monsterReady = false;
  26. var monsterImage = new Image();
  27. monsterImage.onload = function () {
  28.     monsterReady = true;
  29. };
  30. monsterImage.src = "images/monster.png";
  31.  
  32. // Kill sound
  33. var snd = new Audio("audio/kill.wav")
  34.  
  35. /* Game objects */
  36.  
  37. // Hero object
  38. var hero = {
  39.     speed: 250 // movement in pixels per second
  40. };
  41.  
  42. // Monster object
  43. var monster = {
  44.     speed: 200,
  45.     directions: [null, null]
  46. };
  47. var monstersCaught = 0;
  48.  
  49. // Wave counter
  50. var currentWave = 0;
  51.  
  52. // Handle keyboard controls
  53. var keysDown = {};
  54.  
  55. addEventListener("keydown", function (e) {
  56.     keysDown[e.keyCode] = true;
  57. }, false);
  58.  
  59. addEventListener("keyup", function (e) {
  60.     delete keysDown[e.keyCode];
  61. }, false);
  62.  
  63. // Reset the game when the player catches a monster
  64. var reset = function () {
  65.     hero.x = canvas.width / 2;
  66.     hero.y = canvas.height / 2;
  67.  
  68.     // Change wave number and change speed accordingly
  69.     if (monstersCaught % 10 == 0){
  70.         currentWave++;
  71.         hero.speed += 25;
  72.         monster.speed += 50;
  73.     }
  74.  
  75.     // Reset speed on waves that are multiples of five
  76.     if (currentWave % 5 == 1){
  77.         hero.speed = 250;
  78.         monster.speed = 250;
  79.     }
  80.  
  81.     placeMonster();
  82. };
  83.  
  84. var placeMonster = function () {
  85.     // Throw the monster somewhere on the screen randomly
  86.     monster.x = 32 + (Math.random() * (canvas.width - 96));
  87.     monster.y = 32 + (Math.random() * (canvas.height - 96));
  88. };
  89.  
  90. var moveEntity = function (entity, modifier, direction) {
  91.     switch (direction) {
  92.     case 38:
  93.     case 'up':
  94.         if (entity.y >= 32) { // Check collision with top wall
  95.             entity.y -= entity.speed * modifier;
  96.         }
  97.         break;
  98.     case 40:
  99.     case 'down':
  100.         if (entity.y <= canvas.height - 64) { // Check collision with bottom wall
  101.             entity.y += entity.speed * modifier;
  102.         }
  103.         break;
  104.     case 37:
  105.     case 'left':
  106.         if (entity.x >= 32) { // Check collision with left wall
  107.             entity.x -= entity.speed * modifier;
  108.         }
  109.         break;
  110.     case 39:
  111.     case 'right':
  112.         if (entity.x <= canvas.width - 64) {
  113.             entity.x += entity.speed * modifier;
  114.         }
  115.         break;
  116.     }
  117. };
  118.  
  119. var keyToDirection = {
  120.     38: 'up',
  121.     40: 'down',
  122.     37: 'left',
  123.     39: 'right'
  124. };
  125.  
  126. var directions = [37, 38, 39, 40];
  127.  
  128. // Update game objects
  129. var update = function (modifier) {
  130.     for (key in keysDown) {
  131.         var direction = keyToDirection[key];
  132.         if (direction) moveEntity(hero, modifier, keyToDirection[key]);
  133.     }
  134.  
  135.     for (var i = 0; i < 2; i++) {
  136.         moveEntity(monster, modifier, monster.directions[i]);
  137.     }
  138.  
  139.     // Are they touching?
  140.     if (
  141.         hero.x <= (monster.x + 32)
  142.         && monster.x <= (hero.x + 32)
  143.         && hero.y <= (monster.y + 32)
  144.         && monster.y <= (hero.y + 32)
  145.     ) {
  146.         ++monstersCaught;
  147.  
  148.         // Resets sound if it is still playing
  149.         if(snd.currentTime > 0) {
  150.             snd.currentTime = 0;
  151.         }
  152.         snd.play();
  153.  
  154.         placeMonster();
  155.     }
  156. };
  157.  
  158. var changeMonsterDirections = function() {
  159.     for (var i = 0; i < 2; i++) {
  160.         monster.directions[i] = directions[Math.floor(Math.random() * directions.length)];
  161.     }
  162.     // If both directions are the same, null out one of them so that
  163.     // the monster doesn't move twice as fast
  164.     if (monster.directions[0] == monster.directions[1]) {
  165.         monster.directions[1] = null;
  166.     }
  167. }
  168.  
  169. // Draw everything
  170. var render = function () {
  171.     if (bgReady) {
  172.         ctx.drawImage(bgImage, 0, 0);
  173.     }
  174.  
  175.     if (heroReady) {
  176.         ctx.drawImage(heroImage, hero.x, hero.y);
  177.     }
  178.  
  179.     if (monsterReady) {
  180.         ctx.drawImage(monsterImage, monster.x, monster.y);
  181.     }
  182.  
  183.     // Score
  184.     ctx.fillStyle = "rgb(250, 250, 250)";
  185.     ctx.font = "14px Helvetica";
  186.     ctx.textAlign = "left";
  187.     ctx.textBaseline = "top";
  188.     ctx.fillText("Goblins caught: " + monstersCaught, 32, 32);
  189.     ctx.fillText("Current wave: " + currentWave, 32, 46);
  190. };
  191.  
  192. // The main game loop
  193. var main = function () {
  194.     var now = Date.now();
  195.     var delta = now - then;
  196.  
  197.     update(delta / 1000);
  198.     render();
  199.  
  200.     then = now;
  201. };
  202.  
  203. // Let's play this game!
  204. reset();
  205. var then = Date.now();
  206. setInterval(main, 1); // Execute as fast as possible
  207. setInterval(changeMonsterDirections, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement