瀏覽代碼

Add Flying Ninja

michtang 3 年之前
父節點
當前提交
567a978ecf

+ 115 - 0
03-orange/09-flying-ninja.js

@@ -0,0 +1,115 @@
+/**
+This activity uses conditionals to creat movemtn code with the mouse. Timer and random chances are used to call functions that
+ create obstacles. The player must avoid the obstacles for as long as possible
+ */
+
+ //********************************Scene Object - Initialize When Scene Starts Event**********************//
+ if ($this.scene.state() == "PLAY") {
+    // set up the score variable
+    $this.score = 0;
+    // clone background image
+    var farBackgroundClone = farBackground.clone();
+    // place the cloned background image
+    farBackgroundClone.y(0);
+    farBackgroundClone.x(1000);
+    
+    // clone the background image
+    var closeBackgroundClone = closeBackground.clone();
+    // place the cloned background image
+    closeBackgroundClone.y(0);
+    closeBackgroundClone.x(1000);
+    
+    // every 2 seconds, spawn a new obstacle
+    createTimer(2000, function() {
+        var chance = random(100);
+        if (chance <= 25) {
+            $this.generateBottomObstacle();
+        } else if(chance <= 50) {
+            $this.generateTopObstacle();
+        } else {
+            $this.generateBottomObstacle();
+            $this.generateTopObstacle();
+        }
+        // increase score after every spawn
+        $this.score += 10; 
+        scoreLabel.text($this.score);
+    }); 
+}
+
+$this.generateBottomObstacle = function() {
+    // clone the mite object
+    var miteClone = mite.clone();
+    // position it with random y
+    miteClone.x(850);
+    miteClone.y(random(350, 500));
+    // activate the clone for collision
+    miteClone.active = true;
+};
+
+$this.generateTopObstacle = function() {
+    // clone the mite object
+    var miteClone = mite.clone();
+    // rotate it to flip its direction
+    miteClone.rotation(180);
+    // position it with random y
+    miteClone.x(850);
+    miteClone.y(random(100, 300));
+    // activate the clone for collision
+    miteClone.active = true;
+}
+
+ //*************************farBackground Object - Update Every Frame Event*******************************//
+ // always move the background
+$this.moveX();
+// move the image when it goes completely off the screen
+if ($this.x() <= -1000) {
+    $this.x(1000);
+}
+
+//****************************closeBackground Object - Update Every Frame Event*****************************//
+// always move the background
+$this.moveX(); // provide more immersive feeling with parallax motion
+/** objects close by seem to move faster than objects far away
+closeBackground has faster speed than farBackground so when they 
+ both move, it will give a sense of speed 
+*/
+if ($this.x() <= -1000) {
+    $this.x(1000);
+}
+
+//****************************mite Object - Update Every Frame Event**************************************//
+// always move the mite object
+$this.moveX();
+
+// if mite goes off screen, remove it
+if ($this.x() < -50) {
+    $this.remove();
+}
+
+// if the mite object is active and it's touching hotSpot
+if ($this.active && $this.isTouching(hotSpot)) {
+    //deactivate this mite object
+    $this.active = false;
+    // subtract points and update scoreLabel
+    $this.scene.score -= 5; 
+    scoreLabel.text($this.scene.score);
+}
+
+//****************************ninja Group - Update Every Frame Event**************************************//
+// calculate how far away the mouse is from the ninja
+var distanceToMouse = getMouseY() - $this.y();
+hotSpot.z(0); // hide hotSpot behind ninja sprite
+// if the mouse is above the ninja
+// distanceToMouse will be negative
+if (distanceToMouse < -5) {
+    // move ninja up
+    $this.moveY(-75);
+}
+
+if (distanceToMouse > 5) {
+    // move ninja down
+    $this.moveY(75);
+}
+
+// adjust the rotation of the ninja to tilt up and down
+$this.rotation(distanceToMouse * 0.1);

+ 0 - 0
03-orange/10-shut-the-box.js


+ 0 - 0
03-orange/11-PY-the-sky-is-falling.js


+ 126 - 0
03-orange/12-PY-endless-run.js

@@ -0,0 +1,126 @@
+/**
+
+Decomposition: 
+* Ninja jumps in y direction when space is pressed
+* Ninja is animated every frame
+
+* rock moves from right to left
+* lose a point if the rock touches the ninja
+
+* shuriken moves from right to left 
+* shuriken y position is randomized
+* lose a point if shuriken touches ninja
+
+* Scene has ninja jump code
+* Scene has rock spawn code
+* Scene has shuriken spawn code
+* Updates the score
+
+ */
+
+//****************Scene Object - Initialize When Scene Starts Event*****************//
+if($this.scene.state() == "PLAY") {
+    // set up score variables
+    $this.score = 0;
+    // timer for 2 seconds, spawn a new obstacle
+    createTimer(2000, function() {
+        var chance = random(100);
+        if (chance <= 25) {
+            $this.generateStone()
+        } else if(chance <= 75) {
+            $this.generateShuriken();
+        } else {
+            $this.generateStone();
+            $this.generateShuriken();
+        }
+        // increase score after every spawn
+        $this.score += 10;
+        scoreLabel.text($this.score);
+    });
+}
+
+$this.generateStone = function() {
+    // clone the stone object
+    var stoneClone = stone.clone();
+    // position it off the screen
+    stoneClone.x(850);
+    // activate the clone for collision
+    stoneClone.active = true; 
+};
+
+$this.generateShuriken = function() {
+    //clone the shuriken object
+    var shurikenClone = shuriken.clone();
+    // position it with a random y
+    shurikenClone.x(850);
+    shurikenClone.y(random(150,400));
+    // randomize the speedX
+    shurikenClone.speedX(random(-200,-100));
+    // activate the clone for collision
+    shurikenClone.active = true; 
+}; 
+
+//*********************Stone Object - Update Every Frame Event********************//
+// always be moving
+$this.moveX();
+
+// if object goes off the screen, remove it
+if ($this.x() < -150) {
+    $this.remove();
+}
+
+// if the object is active and it touches ninja
+if ($this.active && $this.isTouching(ninja)) {
+    //deactivate object
+    $this.active = false;
+    //subtract points and update the UI
+    $this.scene.score -= 5;
+    scoreLabel.text($this.scene.score);
+}
+
+//*********************Shuriken Object - Update Every Frame***********************//
+// always be moving
+$this.moveX();
+
+// if the object goes off screen, remove it
+if ($this.x() < 150) {
+    $this.remove();
+}
+
+// if the object is active and touchs the ninja
+if ($this.active && $this.isTouching(ninja)) {
+    // deactivate this object
+    $this.active = false;
+    // subtract points and update the UI
+    $this.scene -= 5;
+    scoreLabel.text($this.scene.score);
+}
+
+//***********************Scene - Update Every Frame*********************************//
+//check to see if spacebar is pressed
+var spaceIsPressed = isKeyPressed(Keys.space);
+
+// if the spacebar is pressed
+// and ninja is not already jumping
+if (spaceIsPressed && !ninja.isJumping) {
+    ninja.velocityY = -450;
+    ninja.isJumping = true; 
+}
+
+//********************ninja Object - Initialize When Scene Starts*******************//
+$this.incrementAnimation();
+
+// if the ninja is jumping
+if (this.isJumping) {
+    // move the ninja based on its velocity
+    $this.moveY($this.velocityY);
+
+    //change the y velocity due to gravity
+    $this.velocityY += 10;
+}
+
+// if the ninja touches the ground
+if ($this.y() > 500) {
+    $this.isJumping = false;
+}
+

+ 0 - 0
03-orange/13-PY-circus-bounce.js


+ 0 - 0
03-orange/14-PY-word-scramble.js