12-PY-endless-run.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. Decomposition:
  3. * Ninja jumps in y direction when space is pressed
  4. * Ninja is animated every frame
  5. * rock moves from right to left
  6. * lose a point if the rock touches the ninja
  7. * shuriken moves from right to left
  8. * shuriken y position is randomized
  9. * lose a point if shuriken touches ninja
  10. * Scene has ninja jump code
  11. * Scene has rock spawn code
  12. * Scene has shuriken spawn code
  13. * Updates the score
  14. */
  15. //****************Scene Object - Initialize When Scene Starts Event*****************//
  16. if($this.scene.state() == "PLAY") {
  17. // set up score variables
  18. $this.score = 0;
  19. // timer for 2 seconds, spawn a new obstacle
  20. createTimer(2000, function() {
  21. var chance = random(100);
  22. if (chance <= 25) {
  23. $this.generateStone()
  24. } else if(chance <= 75) {
  25. $this.generateShuriken();
  26. } else {
  27. $this.generateStone();
  28. $this.generateShuriken();
  29. }
  30. // increase score after every spawn
  31. $this.score += 10;
  32. scoreLabel.text($this.score);
  33. });
  34. }
  35. $this.generateStone = function() {
  36. // clone the stone object
  37. var stoneClone = stone.clone();
  38. // position it off the screen
  39. stoneClone.x(850);
  40. // activate the clone for collision
  41. stoneClone.active = true;
  42. };
  43. $this.generateShuriken = function() {
  44. //clone the shuriken object
  45. var shurikenClone = shuriken.clone();
  46. // position it with a random y
  47. shurikenClone.x(850);
  48. shurikenClone.y(random(150,400));
  49. // randomize the speedX
  50. shurikenClone.speedX(random(-200,-100));
  51. // activate the clone for collision
  52. shurikenClone.active = true;
  53. };
  54. //*********************Stone Object - Update Every Frame Event********************//
  55. // always be moving
  56. $this.moveX();
  57. // if object goes off the screen, remove it
  58. if ($this.x() < -150) {
  59. $this.remove();
  60. }
  61. // if the object is active and it touches ninja
  62. if ($this.active && $this.isTouching(ninja)) {
  63. //deactivate object
  64. $this.active = false;
  65. //subtract points and update the UI
  66. $this.scene.score -= 5;
  67. scoreLabel.text($this.scene.score);
  68. }
  69. //*********************Shuriken Object - Update Every Frame***********************//
  70. // always be moving
  71. $this.moveX();
  72. // if the object goes off screen, remove it
  73. if ($this.x() < 150) {
  74. $this.remove();
  75. }
  76. // if the object is active and touchs the ninja
  77. if ($this.active && $this.isTouching(ninja)) {
  78. // deactivate this object
  79. $this.active = false;
  80. // subtract points and update the UI
  81. $this.scene -= 5;
  82. scoreLabel.text($this.scene.score);
  83. }
  84. //***********************Scene - Update Every Frame*********************************//
  85. //check to see if spacebar is pressed
  86. var spaceIsPressed = isKeyPressed(Keys.space);
  87. // if the spacebar is pressed
  88. // and ninja is not already jumping
  89. if (spaceIsPressed && !ninja.isJumping) {
  90. ninja.velocityY = -450;
  91. ninja.isJumping = true;
  92. }
  93. //*****************ninja Object - Initialize When Scene Starts Event*****************//
  94. $this.velocityY = 0;
  95. $this.isJumping = false;
  96. //********************ninja Object - Update Every Frame Event***********************//
  97. $this.incrementAnimation();
  98. // if the ninja is jumping
  99. if (this.isJumping) {
  100. // move the ninja based on its velocity
  101. $this.moveY($this.velocityY);
  102. //change the y velocity due to gravity
  103. $this.velocityY += 10;
  104. }
  105. // if the ninja touches the ground
  106. if ($this.y() > 500) {
  107. $this.isJumping = false;
  108. }