11-PY-the-sky-is-falling.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //****************** Scene Object - Initialize When Scene Starts Event*********//
  2. $this.resetShuriken = function () {
  3. // deactivate and hide the shuriken
  4. shuriken.active = false;
  5. shuriken.visible(false);
  6. };
  7. $this.generateCloud = function() {
  8. // clone the cloud object
  9. var cloudClone = cloud.clone();
  10. // position it off the screen
  11. // with a random x
  12. cloudClone.x(random(100, 700));
  13. cloudClone.y(0);
  14. };
  15. $this.throwShuriken = function() {
  16. // move the shuriken to the ninja
  17. shuriken.x(ninja.x());
  18. shuriken.y(ninja.y());
  19. // activate it and make it visible
  20. shuriken.active = true;
  21. shuriken.visible(true);
  22. };
  23. if ($this.scene.state() == "PLAY") {
  24. // set up score varible
  25. $this.score = 0;
  26. // initialize the shuriken
  27. $this.resetShuriken();
  28. // every 3 seconds, generate new cloud
  29. createTimer(3000, function() {
  30. $this.generateCloud();
  31. });
  32. }
  33. //********************* ninja Object - Update Every Frame Event*******************//
  34. $this.incrementAnimation();
  35. // move the ninja left
  36. if (isKeyPressed(Keys.leftArrow)) {
  37. $this.moveX(-100);
  38. }
  39. // move the ninja right
  40. if (isKeyPressed(Keys.rightArrow)) {
  41. $this.moveX(100);
  42. }
  43. // if the spacebar is pressed
  44. // and if the shuriken is not active
  45. if (isKeyPressed(Keys.space) && !shuriken.active) {
  46. $this.scene.throwShuriken();
  47. }
  48. //*********************shuriken Object - Update Every Frame Event*****************//
  49. // move the shuriken if it's active
  50. if ($this.active) {
  51. $this.moveY();
  52. }
  53. // if it flies off the screen
  54. // reset it
  55. if ($this.y() <= 0) {
  56. $this.scene.resetShuriken();
  57. }
  58. //**********************cloud Object - Update Every Frame Event ******************//\
  59. // always move the cloud
  60. $this.moveY();
  61. // remove the cloud if it reaches the top of the screen
  62. if ($this.y() < 0) {
  63. // increase the score after every cleared cloud
  64. $this.scene.score += 10;
  65. scoreLabel.text($this.scene.score);
  66. $this.remove();
  67. }
  68. // end game if the cloud reaches the bottom of the screen
  69. if (this.y() >= 500) {
  70. $this.scene.stopCode();
  71. }
  72. // if the cloud touches an active shuriken
  73. if (shuriken.active && $this.isTouching(shuriken)) {
  74. // push cloud up
  75. $this.y($this.y() - 200);
  76. // reset the shuriken
  77. $this.scene.resetShuriken();
  78. }