Guest User

Untitled

a guest
Nov 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. // Check Collision
  2. var checkCollision = function(anEnemy) {
  3. // check for collision between enemy and player
  4. if (
  5. player.y + 131 >= anEnemy.y + 90 &&
  6. player.x + 25 <= anEnemy.x + 88 &&
  7. player.y + 73 <= anEnemy.y + 135 &&
  8. player.x + 76 >= anEnemy.x + 11) {
  9. console.log('collided');
  10. // Position player after Collision
  11. player.x = 202.5;
  12. player.y = 383;
  13. }
  14.  
  15. // check for player reaching top of canvas and winning the game
  16. if (player.y + 63 <= 0) {
  17. // if player wins, add 1 to the score and level
  18. player.x = 202.5;
  19. player.y = 383;
  20. console.log('you made it!');
  21.  
  22. ctx.fillStyle = 'white';
  23. ctx.fillRect(0, 0, 505, 350);
  24. // pass score as an argument to the increaseDifficulty function
  25. score += 1;
  26. gameLevel += 1;
  27. console.log('current score: ' + score + ', current level: ' + gameLevel);
  28. increaseDifficulty(score);
  29. }
  30.  
  31. // check if player runs into left, bottom, or right canvas walls
  32. // prevent player from moving beyond canvas wall boundaries
  33. if (player.y > 383) {
  34. player.y = 383;
  35. }
  36. if (player.x > 402.5) {
  37. player.x = 402.5;
  38. }
  39. if (player.x < 2.5) {
  40. player.x = 2.5;
  41. }
  42. };
  43.  
  44. // Increase number of enemies on screen based on player's score
  45. var increaseDifficulty = function(numEnemies) {
  46. // remove all previous enemies on canvas
  47. allEnemies.length = 0;
  48.  
  49. // load new set of enemies
  50. for (var i = 0; i <= numEnemies; i++) {
  51. var enemy = new Enemy(0, Math.random() * 260 + 50, Math.random() * 256);
  52. var enemy_rock = new Enemy_Rock(0, Math.random() * 260 + 50, Math.random() * 256);
  53.  
  54. allEnemies.push(enemy, enemy_rock);
  55. }
  56. };
Add Comment
Please, Sign In to add comment