Guest User

Untitled

a guest
Oct 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. // Enemies our player must avoid
  2. // declaring the enemy object
  3. var Enemy = function(x, y, speed) {
  4. // Variables applied to each of our instances go here,
  5. // we've provided one for you to get started
  6.  
  7. // The image/sprite for our enemies, this uses
  8. // a helper we've provided to easily load images
  9. this.x = x;
  10. this.y = y;
  11.  
  12. this.speed = speed;
  13. this.sprite = 'images/enemy-bug.png';
  14. };
  15.  
  16. // Update the enemy's position, required method for game
  17. // Parameter: dt, a time delta between ticks
  18. // setting enemy speed
  19. Enemy.prototype.update = function(dt) {
  20. // You should multiply any movement by the dt parameter
  21. // which will ensure the game runs at the same speed for
  22. // all computers.
  23.  
  24. if (this.x <= 500)
  25. {
  26. this.x = (this.speed * dt) + this.x;
  27. }
  28.  
  29. else
  30. {
  31. this.x = 0;
  32. }
  33.  
  34.  
  35. };
  36.  
  37. // Draw the enemy on the screen, required method for game
  38. // enemy image placed on screen
  39. Enemy.prototype.render = function() {
  40. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  41. };
  42.  
  43. // setting up multiple enemy characters
  44. var enemy1 = new Enemy(200,300, 30);
  45. var enemy2 = new Enemy(100,200, 60);
  46. var enemy3 = new Enemy(300,100, 90);
  47. var allEnemies = [enemy1,enemy2,enemy3];
  48.  
  49. // Now write your own player class
  50. // This class requires an update(), render() and
  51. // a handleInput() method.
  52. // declaring the player class
  53. var Player = function(x,y) {
  54. this.x = x;
  55. this.y = y;
  56.  
  57. this.sprite = 'images/char-boy.png'
  58.  
  59. };
  60.  
  61. //declaring new player variable
  62. var player = new Player (100,300);
  63.  
  64. Player.prototype.reset = function() {
  65. this.x = 100;
  66. this.y = 300;
  67.  
  68. };
  69.  
  70.  
  71. // updating parameters. If player leaves screen and if player reaches water
  72. Player.prototype.update = function() {
  73. for (var i = 0; i < 3; i++) {
  74. if ((this.x + 50 > allEnemies[i].x) && (this.x < allEnemies[i].x + 60) && (this.y + 50 > allEnemies[i].y) && (this.y < allEnemies[i].y + 60))
  75. {
  76. console.log("collision")
  77. Player.prototype.reset();
  78.  
  79. //please fix the logics on reset and update
  80.  
  81. }
  82. }
  83. };
  84.  
  85.  
  86.  
  87.  
  88. // reset postition of player on the event of a reset
  89.  
  90. Player.prototype.reset = function() {
  91.  
  92.  
  93. if (this.x >= 505)
  94. {
  95. this.x = 100;
  96. this.y = 300;
  97. }
  98.  
  99. if (this.y >= 606)
  100. {
  101. this.x = 100;
  102. this.y = 300;
  103. }
  104.  
  105. };
  106.  
  107.  
  108. // player image placed on screen
  109.  
  110. Player.prototype.render = function(){
  111. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  112. };
  113.  
  114. // setting up parameters for the event of user selected direction for player
  115.  
  116. Player.prototype.handleInput = function(direction){
  117.  
  118. if(direction === 'left' && this.x >= 100)
  119. {
  120. this.x = (this.x - 100);
  121. }
  122.  
  123. if(direction === 'up' && this.y >= 80)
  124. {
  125. this.y = (this.y - 100);
  126. }
  127. if(direction === 'right' && this.x <= 400)
  128. {
  129. this.x = (this.x + 100);
  130. }
  131. if(direction === 'down' && this.y <= 300)
  132. {
  133. this.y = (this.y + 100);
  134. }
  135.  
  136. if (this.y <= 100)
  137. {
  138. Player.prototype.reset();
  139. }
  140.  
  141. if (this.y >= 490)
  142. {
  143. Player.prototype.reset();
  144. }
  145.  
  146. };
  147.  
  148.  
  149. // Now instantiate your objects.
  150. // Place all enemy objects in an array called allEnemies
  151. // Place the player object in a variable called player
  152.  
  153.  
  154.  
  155. // This listens for key presses and sends the keys to your
  156. // Player.handleInput() method. You don't need to modify this.
  157. document.addEventListener('keyup', function(e) {
  158. var allowedKeys = {
  159. 37: 'left',
  160. 38: 'up',
  161. 39: 'right',
  162. 40: 'down'
  163. };
  164.  
  165. player.handleInput(allowedKeys[e.keyCode]);
  166. });
Add Comment
Please, Sign In to add comment