Guest User

Untitled

a guest
Nov 21st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. // Enemies our player must avoid
  2. var Enemy = function(x, y, speed) {
  3. this.x = x;
  4. this.y = y;
  5. this.speed = speed;
  6. // The sprite continuously crops and displays the image.
  7. this.sprite = 'images/enemy-bug.png';
  8. };
  9.  
  10. var Enemy_Rock = function(x, y, speed) {
  11. this.x = x;
  12. this.y = y;
  13. this.speed = speed;
  14. // The sprite continuously crops and displays the image.
  15. this.sprite = 'images/Rock.png';
  16. };
  17.  
  18.  
  19.  
  20. Enemy_Rock.prototype.update = function(dt) {
  21. this.x += this.speed * dt;
  22. if (this.x >= 505) {
  23. this.x = 0;
  24. }
  25. checkCollision(this);
  26. };
  27.  
  28. Enemy_Rock.prototype.render = function() {
  29. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  30. };
  31.  
  32. // Update the enemy's position, required method for game
  33. // Parameter: dt, a time delta between ticks
  34. Enemy.prototype.update = function(dt) {
  35. // You should multiply any movement by the dt parameter
  36. // which will ensure the game runs at the same speed for
  37. // all computers.
  38. this.x += this.speed * dt;
  39.  
  40. // 505 defines the width of the Canvas.
  41. // The code that initializes the left end again when the enemy is struck at the right end.
  42. if (this.x >= 505) {
  43. this.x = 0;
  44. }
  45. checkCollision(this);
  46. };
  47.  
  48. // * Draw the enemy on the screen, required method for game
  49. Enemy.prototype.render = function() {
  50. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  51. };
  52.  
  53. // Definition Player
  54. var Player = function(x, y, speed) {
  55. this.x = x;
  56. this.y = y;
  57. this.speed = speed;
  58. this.sprite = 'images/char-boy.png';
  59. };
  60.  
  61. Player.prototype.update = function() {}
  62. Player.prototype.render = function() {
  63. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  64. displayScoreLevel(score, gameLevel);
  65. };
  66.  
  67. // KeyPress Settings
  68. Player.prototype.handleInput = function(keyPress) {
  69. if (keyPress == 'left') {
  70. player.x -= player.speed;
  71. }
  72. if (keyPress == 'up') {
  73. player.y -= player.speed - 20;
  74. }
  75. if (keyPress == 'right') {
  76. player.x += player.speed;
  77. }
  78. if (keyPress == 'down') {
  79. player.y += player.speed - 20;
  80. }
  81. console.log('keyPress is: ' + keyPress);
  82. };
  83.  
  84. // Display Score
  85. var displayScoreLevel = function(aScore, aLevel) {
  86. var canvas = document.getElementsByTagName('canvas');
  87. var firstCanvasTag = canvas[0];
  88. scoreLevelDiv.innerHTML = 'Score : ' + aScore + ' / ' + 'Level : ' + aLevel;
  89. document.body.insertBefore(scoreLevelDiv, firstCanvasTag[0]);
  90. };
  91.  
  92. // Check Collision
  93. var checkCollision = function(anEnemy) {
  94. // check for collision between enemy and player
  95. if (
  96. player.y + 131 >= anEnemy.y + 90 &&
  97. player.x + 25 <= anEnemy.x + 88 &&
  98. player.y + 73 <= anEnemy.y + 135 &&
  99. player.x + 76 >= anEnemy.x + 11) {
  100. console.log('collided');
  101. // Position player after Collision
  102. player.x = 202.5;
  103. player.y = 383;
  104. }
  105.  
  106. // check for player reaching top of canvas and winning the game
  107. if (player.y + 63 <= 0) {
  108. // if player wins, add 1 to the score and level
  109. player.x = 202.5;
  110. player.y = 383;
  111. console.log('you made it!');
  112.  
  113. ctx.fillStyle = 'white';
  114. ctx.fillRect(0, 0, 505, 350);
  115. // pass score as an argument to the increaseDifficulty function
  116. score += 1;
  117. gameLevel += 1;
  118. console.log('current score: ' + score + ', current level: ' + gameLevel);
  119. increaseDifficulty(score);
  120. }
  121.  
  122. // check if player runs into left, bottom, or right canvas walls
  123. // prevent player from moving beyond canvas wall boundaries
  124. if (player.y > 383) {
  125. player.y = 383;
  126. }
  127. if (player.x > 402.5) {
  128. player.x = 402.5;
  129. }
  130. if (player.x < 2.5) {
  131. player.x = 2.5;
  132. }
  133. };
  134.  
  135. // Increase number of enemies on screen based on player's score
  136. var increaseDifficulty = function(numEnemies) {
  137. // remove all previous enemies on canvas
  138. allEnemies.length = 0;
  139.  
  140. // load new set of enemies
  141. for (var i = 0; i <= numEnemies; i++) {
  142. var enemy = new Enemy(0, Math.random() * 260 + 50, Math.random() * 256);
  143. var enemy_rock = new Enemy_Rock(0, Math.random() * 260 + 50, Math.random() * 256);
  144.  
  145. allEnemies.push(enemy, enemy_rock);
  146. }
  147. };
  148.  
  149. // Initial setting
  150. var allEnemies = [];
  151. var player = new Player(202.5, 383, 100);
  152. var score = 0;
  153. var gameLevel = 1;
  154. var scoreLevelDiv = document.createElement('div');
  155. var enemy = new Enemy(0, Math.random() * 184 + 50, Math.random() * 256);
  156. var enemy_rock = new Enemy_Rock(0, Math.random() * 184 + 50, Math.random() * 256);
  157.  
  158. allEnemies.push(enemy, enemy_rock);
  159.  
  160.  
  161. // This listens for key presses and sends the keys to your
  162. // Player.handleInput() method. You don't need to modify this.
  163. document.addEventListener('keyup', function(e) {
  164. var allowedKeys = {
  165. 37: 'left',
  166. 38: 'up',
  167. 39: 'right',
  168. 40: 'down'
  169. };
  170.  
  171. player.handleInput(allowedKeys[e.keyCode]);
  172. });
Add Comment
Please, Sign In to add comment