Explorar el Código

Add ball toss and dungeon escape

Michael Tang hace 3 años
padre
commit
66d0695a8f
Se han modificado 2 ficheros con 202 adiciones y 0 borrados
  1. 74 0
      03-orange/04-ball-toss.js
  2. 128 0
      03-orange/05-dungeon-escape.js

+ 74 - 0
03-orange/04-ball-toss.js

@@ -0,0 +1,74 @@
+//***************************Scene - Update Every Frame Event********************//
+// get the position of the mouse pointer
+var mousePosition = getPointerPos();
+
+// check to see if the space bar is pressed 
+var spaceIsPressed = isKeyPressed(Keys.space);
+
+// if the space bar is pressed 
+// and if the ball is not already launched 
+if( spaceIsPressed && !ball.isLaunched ) {
+    // update the message
+    message.text("Launched!");
+    
+    // give the ball x and y velocities
+    ball.velocityX = mousePosition.x - cannon.x();
+    ball.velocityY = mousePosition.y - cannon.y();
+    
+    // launch the ball
+    ball.isLaunched = true;
+}
+
+//*******************ball Object - Initialize When Scene Starts Event*************//
+// track if the ball has been launched 
+$this.isLaunched = false;
+
+// reset the ball's position
+// and set isLaunched to false
+$this.resetBall = function() {
+    $this.x(35);
+    $this.y(550);
+    $this.isLaunched = false;
+};
+
+//*******************ball Object - Update Every Frame Event***********************//
+// if the ball is launched 
+if ($this.isLaunched) {
+    // move the ball based on its velocity
+    $this.moveX($this.velocityX);
+    $this.moveY($this.velocityY);
+    
+    // change the y velocity due to gravity
+    $this.velocityY += 10;
+    
+    // if the ball goes off the screen
+    if ($this.x() > 800 || $this.y() > 600) {
+        //reset ball and update message
+        $this.resetBall();
+        message.text("Miss!");
+    }
+}
+
+// if the ball touches the basket
+if($this.isTouching(basket)) {
+    $this.resetBall()
+    message.text("Score!")
+}
+
+//********************basket Group - Update Every Frame Event*********************//
+// move the basket group
+$this.moveY();
+
+// if it reaches the top
+if($this.y() < 100) {
+    $this.speedY(50);
+}
+
+// if it reaches the bottom
+if($this.y() > 500) {
+    $this.speedY(-50);
+}
+
+//********************cannon Object - Update Evey Frame Event*********************//
+// point the cannon to the cursor
+$this.pointToCursor();

+ 128 - 0
03-orange/05-dungeon-escape.js

@@ -0,0 +1,128 @@
+/**
+ * Game Objects required: add Text object named "message"
+ */
+//*****************Scene Object - Initialize When Scene Starts Event***************//
+// world set up and randomization
+if ($this.scene.state() == "PLAY") {
+    // iterate 8 columns
+    for (var x = 1; x < 8; x++) {
+        // iterates 6 rows
+        for (var y = 1; y < 6; y++) {
+            // place wall at random locations
+            if(random(100) < 25) {
+                var clonedWall = wall.clone();
+                // use wall width and height to place properly
+                clonedWall.x(100 * x);
+                clonedWall.y(100 * y)
+            } else if (random(100) < 15) {
+                var clonedCoin = coin.clone();
+                clonedCoin.x(100 * x);
+                clonedCoin.y(100 * y);
+            }
+            
+        }
+    }
+}
+
+//********************wall Object - Update Every Frame Event***********************//
+// remove wall if touching stairs
+if ($this.isTouching(stairs)) {
+    $this.remove();
+}
+
+// if the wall is touching the ninja
+if ($this.isTouching(ninja)) {
+    // check the direction of movement
+    switch (ninja.moveDirection) {
+        case ("up"):
+            ninja.y(ninja.y() + 5);
+            break;
+        case("down"):
+            ninja.y(ninja.y() - 5 );
+            break;
+        case("left"):
+            ninja.x(ninja.x() + 5);
+            break;
+        case("right"):
+            ninja.x(ninja.x() - 5);
+            break;
+    }
+}
+
+//*********************coin Object - Update Every Frame Event**********************//
+// remove coin if touching stairs
+if($this.isTouching(stairs)) {
+    $this.remove();
+}
+
+// animate the coin
+$this.incrementAnimation();
+
+// if the ninja collects a coin
+// increment the ninja's coin total
+// and remove the coin
+if ($this.isTouching(ninja)) {
+    ninja.coins += 1;
+    $this.remove();
+}
+
+//*******************ninja Object - Update Every Frame Event***********************//
+// new method of moving sprite introduced
+// check to see what arrow keys are pressed
+var upPressed = isKeyPressed(Keys.upArrow) || isKeyPressed(Keys.w);
+var downPressed = isKeyPressed(Keys.downArrow) || isKeyPressed(Keys.s);
+var leftPressed = isKeyPressed(Keys.leftArrow) || isKeyPressed(Keys.a);
+var rightPressed = isKeyPressed(Keys.rightArrow) || isKeyPressed(Keys.d);
+
+// store the ninja's direction as a string
+$this.moveDirection = "";
+
+// determine which key is pressed
+// and update the moveDirection variable
+if (upPressed && $this.y() > 0) {
+    $this.moveDirection = "up";
+}
+
+if (downPressed && $this.y() < 520) {
+    $this.moveDirection = "down";
+}
+
+if (leftPressed && $this.x() >0) {
+    $this.moveDirection = "left";
+}
+
+if (rightPressed && $this.x() < 720) {
+    $this.moveDirection = "right";
+}
+
+// switch on moveDirection variable
+switch ($this.moveDirection) {
+    // move the ninja down
+    case "down":
+        $this.moveY(100);
+        break;
+    // move the ninja up
+    case "up":
+        $this.moveY(-100);
+        break;
+    // move the ninja left
+    case "left":
+        $this.moveX(-100);
+        break;
+    // move the ninja right
+    case "right":
+        $this.moveX(100);
+        break;
+}
+
+//******************ninja Object - Initialize When Scene Starts Event************//
+// set up variable to track coins
+$this.coins = 0;
+
+//*******************stairs Object - Update Every Frame Event********************//
+// if the ninja reaches the stairs 
+if ($this.isTouching(ninja)) {
+    message.text("You reached the exit with " + ninja.coins + " coins.");
+    message.z(10);
+    ninja.remove();
+}