05-dungeon-escape.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Game Objects required: add Text object named "message"
  3. */
  4. //*****************Scene Object - Initialize When Scene Starts Event***************//
  5. // world set up and randomization
  6. if ($this.scene.state() == "PLAY") {
  7. // iterate 8 columns
  8. for (var x = 1; x < 8; x++) {
  9. // iterates 6 rows
  10. for (var y = 1; y < 6; y++) {
  11. // place wall at random locations
  12. if(random(100) < 25) {
  13. var clonedWall = wall.clone();
  14. // use wall width and height to place properly
  15. clonedWall.x(100 * x);
  16. clonedWall.y(100 * y)
  17. } else if (random(100) < 15) {
  18. var clonedCoin = coin.clone();
  19. clonedCoin.x(100 * x);
  20. clonedCoin.y(100 * y);
  21. }
  22. }
  23. }
  24. }
  25. //********************wall Object - Update Every Frame Event***********************//
  26. // remove wall if touching stairs
  27. if ($this.isTouching(stairs)) {
  28. $this.remove();
  29. }
  30. // if the wall is touching the ninja
  31. if ($this.isTouching(ninja)) {
  32. // check the direction of movement
  33. switch (ninja.moveDirection) {
  34. case ("up"):
  35. ninja.y(ninja.y() + 5);
  36. break;
  37. case("down"):
  38. ninja.y(ninja.y() - 5 );
  39. break;
  40. case("left"):
  41. ninja.x(ninja.x() + 5);
  42. break;
  43. case("right"):
  44. ninja.x(ninja.x() - 5);
  45. break;
  46. }
  47. }
  48. //*********************coin Object - Update Every Frame Event**********************//
  49. // remove coin if touching stairs
  50. if($this.isTouching(stairs)) {
  51. $this.remove();
  52. }
  53. // animate the coin
  54. $this.incrementAnimation();
  55. // if the ninja collects a coin
  56. // increment the ninja's coin total
  57. // and remove the coin
  58. if ($this.isTouching(ninja)) {
  59. ninja.coins += 1;
  60. $this.remove();
  61. }
  62. //*******************ninja Object - Update Every Frame Event***********************//
  63. // new method of moving sprite introduced
  64. // check to see what arrow keys are pressed
  65. var upPressed = isKeyPressed(Keys.upArrow) || isKeyPressed(Keys.w);
  66. var downPressed = isKeyPressed(Keys.downArrow) || isKeyPressed(Keys.s);
  67. var leftPressed = isKeyPressed(Keys.leftArrow) || isKeyPressed(Keys.a);
  68. var rightPressed = isKeyPressed(Keys.rightArrow) || isKeyPressed(Keys.d);
  69. // store the ninja's direction as a string
  70. $this.moveDirection = "";
  71. // determine which key is pressed
  72. // and update the moveDirection variable
  73. if (upPressed && $this.y() > 0) {
  74. $this.moveDirection = "up";
  75. }
  76. if (downPressed && $this.y() < 520) {
  77. $this.moveDirection = "down";
  78. }
  79. if (leftPressed && $this.x() >0) {
  80. $this.moveDirection = "left";
  81. }
  82. if (rightPressed && $this.x() < 720) {
  83. $this.moveDirection = "right";
  84. }
  85. // switch on moveDirection variable
  86. switch ($this.moveDirection) {
  87. // move the ninja down
  88. case "down":
  89. $this.moveY(100);
  90. break;
  91. // move the ninja up
  92. case "up":
  93. $this.moveY(-100);
  94. break;
  95. // move the ninja left
  96. case "left":
  97. $this.moveX(-100);
  98. break;
  99. // move the ninja right
  100. case "right":
  101. $this.moveX(100);
  102. break;
  103. }
  104. //******************ninja Object - Initialize When Scene Starts Event************//
  105. // set up variable to track coins
  106. $this.coins = 0;
  107. //*******************stairs Object - Update Every Frame Event********************//
  108. // if the ninja reaches the stairs
  109. if ($this.isTouching(ninja)) {
  110. message.text("You reached the exit with " + ninja.coins + " coins.");
  111. message.z(10);
  112. ninja.remove();
  113. }