Guest User

Untitled

a guest
Oct 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. // Enemies our player must avoid
  2. var Enemy = function(x,y,speed) {
  3. // Variables applied to each of our instances go here,
  4. // we've provided one for you to get started
  5. this.x = x;
  6. this.y = y;
  7. this.speed = speed;
  8. // The image/sprite for our enemies, this uses
  9. // a helper we've provided to easily load images
  10. this.sprite = 'images/enemy-bug.png';
  11. };
  12.  
  13. // Update the enemy's position, required method for game
  14. // Parameter: dt, a time delta between ticks
  15. Enemy.prototype.update = function(dt) {
  16. // You should multiply any movement by the dt parameter
  17. // which will ensure the game runs at the same speed for
  18. // all computers.
  19. this.x += dt * this.speed;
  20.  
  21. if(this.x > 505){
  22. this.reset();
  23. }
  24. };
  25.  
  26. // Draw the enemy on the screen, required method for game
  27. Enemy.prototype.render = function() {
  28. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  29. };
  30.  
  31. //Function to reset the enemy when moves off screen
  32. Enemy.prototype.reset = function(){
  33. this.x = -50;
  34. this.speed = Math.floor(Math.random());
  35. };
  36.  
  37. // Now write your own player class
  38. // This class requires an update(), render() and
  39. // a handleInput() method.
  40.  
  41. var Player = function(x,y) {
  42.  
  43. this.x = x;
  44. this.y = y;
  45. this.sprite = 'images/char-cat-girl.png';
  46. };
  47.  
  48. Player.prototype.render = function() {
  49. ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
  50. };
  51.  
  52. Player.prototype.update = function(dt) {
  53. //prevent player from moving outside canvas
  54. if(this.y > 606){
  55. this.y = 606;
  56. }
  57. if(this.y < 1){
  58. this.y = 1;
  59. }
  60. if(this.x > 505){
  61. this.x = 505;
  62. }
  63.  
  64. if(this.x < 1){
  65. this.x = 0;
  66. }
  67.  
  68. //if player collides with enemy
  69. allEnemies.forEach(function(enemy) {
  70. if(this.x == enemy.x && this.y == enemy.y){
  71. this.reset();
  72. }
  73. });
  74. };
  75.  
  76.  
  77.  
  78. Player.prototype.handleInput = function(userInput) {
  79. switch (userInput) {
  80. case 'left':
  81. this.x -= this.speed + 50;
  82. break;
  83. case 'right':
  84. this.x += this.speed + 50;
  85. break;
  86. case 'up':
  87. this.y -= this.speed + 50;
  88. break;
  89. case 'down':
  90. this.y += this.speed + 50;
  91. break;
  92. }
  93. };
  94.  
  95. Player.prototype.reset = function() {
  96. this.x = 250;
  97. this.y = 1;
  98. };
  99.  
  100. // Now instantiate your objects.
  101. // Place all enemy objects in an array called allEnemies
  102. // Place the player object in a variable called player
  103. var allEnemies = [];
  104. var enemyStart = [50, 150, 200];
  105. var player = new Player(100, 300);
  106. var enemy;
  107.  
  108. enemyStart.forEach(function(startY) {
  109. enemy = new Enemy(0, startY, Math.floor(Math.random()));
  110. allEnemies.push(enemy);
  111. });
  112.  
  113.  
  114.  
  115.  
  116. // This listens for key presses and sends the keys to your
  117. // Player.handleInput() method. You don't need to modify this.
  118. document.addEventListener('keyup', function(e) {
  119. var allowedKeys = {
  120. 37: 'left',
  121. 38: 'up',
  122. 39: 'right',
  123. 40: 'down'
  124. };
  125.  
  126. player.handleInput(allowedKeys[e.keyCode]);
  127. });
Add Comment
Please, Sign In to add comment