소스 검색

Add Word Scramble and Sky is Falling

Michael Tang 3 년 전
부모
커밋
5819f06446
3개의 변경된 파일185개의 추가작업 그리고 0개의 파일을 삭제
  1. BIN
      .DS_Store
  2. 92 0
      03-orange/11-PY-the-sky-is-falling.js
  3. 93 0
      03-orange/14-PY-word-scramble.js

BIN
.DS_Store


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

@@ -0,0 +1,92 @@
+//****************** Scene Object - Initialize When Scene Starts Event*********//
+$this.resetShuriken = function () {
+    // deactivate and hide the shuriken
+    shuriken.active  = false;
+    shuriken.visible(false);
+    
+};
+
+$this.generateCloud = function() {
+    // clone the cloud object
+    var cloudClone = cloud.clone();
+    // position it off the screen
+    // with a random x
+    cloudClone.x(random(100, 700));
+    cloudClone.y(0);
+};
+
+$this.throwShuriken = function() {
+    // move the shuriken to the ninja
+    shuriken.x(ninja.x());
+    shuriken.y(ninja.y());
+    // activate it and make it visible
+    shuriken.active = true;
+    shuriken.visible(true);
+};
+
+if ($this.scene.state() == "PLAY") {
+    // set up score varible
+    $this.score = 0; 
+    // initialize the shuriken
+    $this.resetShuriken();
+    // every 3 seconds, generate new cloud
+    createTimer(3000, function() {
+        $this.generateCloud();
+    });
+}
+
+//********************* ninja Object - Update Every Frame Event*******************//
+$this.incrementAnimation();
+
+// move the ninja left
+if (isKeyPressed(Keys.leftArrow)) {
+    $this.moveX(-100);
+}
+
+// move the ninja right
+if (isKeyPressed(Keys.rightArrow)) {
+    $this.moveX(100);
+}
+
+// if the spacebar is pressed
+// and if the shuriken is not active
+if (isKeyPressed(Keys.space) && !shuriken.active) {
+    $this.scene.throwShuriken();
+}
+
+//*********************shuriken Object - Update Every Frame Event*****************//
+// move the shuriken if it's active
+if ($this.active) {
+    $this.moveY();
+}
+
+// if it flies off the screen 
+// reset it
+if ($this.y() <= 0) {
+    $this.scene.resetShuriken();
+}
+
+//**********************cloud Object - Update Every Frame Event ******************//\
+// always move the cloud
+$this.moveY();
+
+// remove the cloud if it reaches the top of the screen
+if ($this.y() < 0) {
+    // increase the score after every cleared cloud
+    $this.scene.score += 10;
+    scoreLabel.text($this.scene.score);
+    $this.remove();
+}
+
+// end game if the cloud reaches the bottom of the screen
+if (this.y() >= 500) {
+    $this.scene.stopCode();
+}
+
+// if the cloud touches an active shuriken
+if (shuriken.active && $this.isTouching(shuriken)) {
+    // push cloud up
+    $this.y($this.y() - 200);
+    // reset the shuriken
+    $this.scene.resetShuriken();
+}

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

@@ -0,0 +1,93 @@
+//***************** Scene Object - Initialize When Scene Starts Event *************//
+// this function takes an array
+// shuffles it randomly and returns it
+$this.shuffle = function(arr){
+    arr.sort(() => random(-10, 5));
+    return arr;
+};
+
+$this.words = [
+  "analog",
+  "array",
+  "binary",
+  "browser",
+  "computer",
+  "condition",
+  "conditional",
+  "database",
+  "download",
+  "function",
+  "group",
+  "hardware",
+  "interface",
+  "javascript",
+  "keywords",
+  "layers",
+  "megabyte",
+  "network",
+  "object",
+  "program",
+  "random",
+  "routine",
+  "scene",
+  "script",
+  "software",
+  "sprite",
+  "string",
+  "syntax",
+  "toggle",
+  "variable",
+  "visible",
+  "website",
+  "window",
+];
+
+$this.getNewWord = function() {
+    // clear out the guess word
+    guess.text("");
+    message.text("Unscramble the word: Click here for a new one.");
+    
+    // select a word at a random
+    $this.selectedWord = $this.words[random($this.words.length - 1)];
+    
+    // create an empty array for the ordered letters
+    var orderedLetters = [];
+    
+    // put each letter in the array
+    for (var l = 0; l < $this.selectedWord.length; l++) {
+        orderedLetters.push($this.selectedWord[l]);
+    }
+    
+    // shuffle the letters in a new array
+    var shuffledLetters = $this.shuffle(orderedLetters);
+    // updated the shuffledLabel text
+    shuffledLabel.text(shuffledLetters);
+};
+
+$this.checkGuess = function() {
+    // only check if the guess is the correct length
+    var originalWord = $this.selectedWord;
+    var guessedWord = guess.text();
+    if(originalWord.length === guessedWord.length) {
+        message.text("Good job! Click here to play again!");
+        // loop through letter in the guess
+        for (var l = 0; l < originalWord.length; l++) {
+            if (originalWord[l] != guessedWord[l]) {
+                message.text("Wrong! Click here to try again!");
+            }
+        }
+    }
+}
+
+if ($this.scene.state() == "PLAY") {
+    $this.getNewWord();
+}
+
+//********************* resetButton Object - Mouse Click Event ********************//
+guess.text("");
+
+//******************** submitButton Object - Mouse Click Event ********************//
+$this.scene.checkGuess();
+
+//******************** message Object - Mouse Click Event ************************//
+$this.scene.getNewWord();