Guest User

Untitled

a guest
Oct 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 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. // reset postition of player on the event of a reset
  65. Player.prototype.reset = function() {
  66. this.x = 100;
  67. this.y = 300;
  68.  
  69. if (this.x >= 505)
  70. {
  71. this.x = 100;
  72. this.y = 300;
  73. }
  74.  
  75. if (this.y >= 606)
  76. {
  77. this.x = 100;
  78. this.y = 300;
  79. }
  80.  
  81.  
  82. };
  83.  
  84.  
  85. // updating parameters. If player leaves screen and if player reaches water
  86. Player.prototype.update = function() {
  87. for (var i = 0; i < 3; i++) {
  88. 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))
  89. {
  90. console.log("collision")
  91. player.reset();
  92.  
  93. //please fix the logics on reset and update
  94.  
  95. }
  96. }
  97. };
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. // player image placed on screen
  106.  
  107. Player.prototype.render = function(){
  108. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  109. };
  110.  
  111. // setting up parameters for the event of user selected direction for player
  112.  
  113. Player.prototype.handleInput = function(direction){
  114.  
  115. if(direction === 'left' && this.x >= 100)
  116. {
  117. this.x = (this.x - 100);
  118. }
  119.  
  120. if(direction === 'up' && this.y >= 80)
  121. {
  122. this.y = (this.y - 100);
  123. }
  124. if(direction === 'right' && this.x <= 400)
  125. {
  126. this.x = (this.x + 100);
  127. }
  128. if(direction === 'down' && this.y <= 300)
  129. {
  130. this.y = (this.y + 100);
  131. }
  132.  
  133. if (this.y <= 100)
  134. {
  135. player.reset();
  136. }
  137.  
  138. if (this.y >= 490)
  139. {
  140. player.reset();
  141. }
  142.  
  143. };
  144.  
  145.  
  146. // Now instantiate your objects.
  147. // Place all enemy objects in an array called allEnemies
  148. // Place the player object in a variable called player
  149.  
  150.  
  151.  
  152. // This listens for key presses and sends the keys to your
  153. // Player.handleInput() method. You don't need to modify this.
  154. document.addEventListener('keyup', function(e) {
  155. var allowedKeys = {
  156. 37: 'left',
  157. 38: 'up',
  158. 39: 'right',
  159. 40: 'down'
  160. };
  161.  
  162. player.handleInput(allowedKeys[e.keyCode]);
  163. });
Add Comment
Please, Sign In to add comment