Advertisement
Tacidus

Code.org | Unscramble

Jan 8th, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://studio.code.org/projects/applab/42eLJ_-8kwTPIvlSoOLlZ05Bvb82ZefN0_lCJXn9QVQ
  2.  
  3. // mainScreenIcon image was provided by code.org, labelled as icon://fa-keyboard-o
  4.  
  5. var wordList = [ // List of words separated into different difficuties.
  6.  
  7.   // EASY DIFFICULTY
  8.  
  9.   {word: "CAT", difficulty: "EASY"},
  10.   {word: "DOG", difficulty: "EASY"},
  11.   {word: "BALL", difficulty: "EASY"},
  12.   {word: "FISH", difficulty: "EASY"},
  13.   {word: "TREE", difficulty: "EASY"},
  14.   {word: "BOOK", difficulty: "EASY"},
  15.   {word: "MOON", difficulty: "EASY"},
  16.   {word: "STAR", difficulty: "EASY"},
  17.   {word: "CAKE", difficulty: "EASY"},
  18.   {word: "BIRD", difficulty: "EASY"},
  19.   {word: "SNOW", difficulty: "EASY"},
  20.   {word: "FROG", difficulty: "EASY"},
  21.   {word: "SAND", difficulty: "EASY"},
  22.   {word: "CAR", difficulty: "EASY"},
  23.   {word: "BEE", difficulty: "EASY"},
  24.   {word: "BEAR", difficulty: "EASY"},
  25.   {word: "SHIP", difficulty: "EASY"},
  26.   {word: "LAND", difficulty: "EASY"},
  27.   {word: "BOAT", difficulty: "EASY"},
  28.   {word: "APPLE", difficulty: "EASY"},
  29.   {word: "MILK", difficulty: "EASY"},
  30.   {word: "HAND", difficulty: "EASY"},
  31.   {word: "FOOT", difficulty: "EASY"},
  32.   {word: "GAME", difficulty: "EASY"},
  33.   {word: "KEY", difficulty: "EASY"},
  34.   {word: "DOOR", difficulty: "EASY"},
  35.   {word: "FARM", difficulty: "EASY"},
  36.   {word: "RAIN", difficulty: "EASY"},
  37.   {word: "POND", difficulty: "EASY"},
  38.   {word: "BACK", difficulty: "EASY"},
  39.   {word: "BARK", difficulty: "EASY"},
  40.   {word: "SALT", difficulty: "EASY"},
  41.   {word: "WIND", difficulty: "EASY"},
  42.   {word: "WORM", difficulty: "EASY"},
  43.   {word: "KING", difficulty: "EASY"},
  44.   {word: "QUEEN", difficulty: "EASY"},
  45.   {word: "SPADE", difficulty: "EASY"},
  46.   {word: "JOKE", difficulty: "EASY"},
  47.  
  48.   // MEDIUM DIFFICULTY
  49.  
  50.   {word: "TIGER", difficulty: "MEDIUM"},
  51.   {word: "DANCE", difficulty: "MEDIUM"},
  52.   {word: "BREAD", difficulty: "MEDIUM"},
  53.   {word: "GRASS", difficulty: "MEDIUM"},
  54.   {word: "CHAIR", difficulty: "MEDIUM"},
  55.   {word: "OCEAN", difficulty: "MEDIUM"},
  56.   {word: "MUSIC", difficulty: "MEDIUM"},
  57.   {word: "CANDY", difficulty: "MEDIUM"},
  58.   {word: "WATER", difficulty: "MEDIUM"},
  59.   {word: "CLOUD", difficulty: "MEDIUM"},
  60.   {word: "TABLE", difficulty: "MEDIUM"},
  61.   {word: "BRUSH", difficulty: "MEDIUM"},
  62.   {word: "EAGLE", difficulty: "MEDIUM"},
  63.   {word: "TRAIN", difficulty: "MEDIUM"},
  64.   {word: "PAPER", difficulty: "MEDIUM"},
  65.   {word: "WATCH", difficulty: "MEDIUM"},
  66.   {word: "SUGAR", difficulty: "MEDIUM"},
  67.   {word: "SLIME", difficulty: "MEDIUM"},
  68.   {word: "SMILE", difficulty: "MEDIUM"},
  69.   {word: "MONEY", difficulty: "MEDIUM"},
  70.   {word: "GREEN", difficulty: "MEDIUM"},
  71.   {word: "BLUE", difficulty: "MEDIUM"},
  72.   {word: "FIELD", difficulty: "MEDIUM"},
  73.   {word: "CLOCK", difficulty: "MEDIUM"},
  74.   {word: "SHOES", difficulty: "MEDIUM"},
  75.   {word: "STORM", difficulty: "MEDIUM"},
  76.   {word: "ISLAND", difficulty: "MEDIUM"},
  77.   {word: "TRAVEL", difficulty: "MEDIUM"},
  78.   {word: "TICKET", difficulty: "MEDIUM"},
  79.   {word: "JADE", difficulty: "MEDIUM"},
  80.   {word: "ZEBRA", difficulty: "MEDIUM"},
  81.  
  82.   // HARD DIFFICULTY
  83.  
  84.   {word: "ELEPHANT", difficulty: "HARD"},
  85.   {word: "FREEDOM", difficulty: "HARD"},
  86.   {word: "UNIVERSE", difficulty: "HARD"},
  87.   {word: "WORLD", difficulty: "HARD"},
  88.   {word: "FOREST", difficulty: "HARD"},
  89.   {word: "SYMPHONY", difficulty: "HARD"},
  90.   {word: "HOSPITAL", difficulty: "HARD"},
  91.   {word: "PICTURE", difficulty: "HARD"},
  92.   {word: "EXERCISE", difficulty: "HARD"},
  93.   {word: "WISDOM", difficulty: "HARD"},
  94.   {word: "HOLOGRAM", difficulty: "HARD"},
  95.  
  96. ];
  97.  
  98. var messages = [ // List of messages meant to be displayed once players reach a certain score.
  99.  
  100.   {text: "This is a good start!", amount: 1},
  101.   {text: "Three right-answer-teers!", amount: 3},
  102.   {text: "You are on a roll!", amount: 5},
  103.   {text: "This is impressive!", amount: 8},
  104.   {text: "You're really good at this..!", amount: 10},
  105.   {text: "This is my highest score ever", amount: 14},
  106.   {text: "A whole 20 words with no misses??", amount: 20},
  107.   {text: "How are you doing this?", amount: 22},
  108.   {text: "Okay this is starting to get worrying", amount: 25},
  109.   {text: "You have to be cheating.", amount: 30}
  110.  
  111.  
  112.   ];
  113.  
  114. var currentWord = ""; // Placeholder variabe for the current word chosen
  115. var scrambledWord = ""; // PLaceholder variable for the current word scrambled
  116. var hintUsed = false; // Bool value meant to hold whether or not a hint has been used
  117. var playerScore = 0; // Holds player current score
  118. var playerHighscore = 0; // Holds player highest score
  119.  
  120. // Main Screen Handlers
  121.  
  122. onEvent("mainScreenBegin", "click", function(){ // Functions runs when begin button is pressed
  123.  
  124.   setScreen("gameScreen"); // Sets screen to the game screen
  125.   updateScreen(); // Calls updatescreen() function
  126.  
  127. });
  128.  
  129. // Game Screen Handlers
  130.  
  131. function updateScreen(){ // function ran when UI update is needed
  132.  
  133.   var randomPick = Math.floor(Math.random() * wordList.length); // Picks a random word from the list of words
  134.   currentWord = wordList[randomPick].word; // Updates currentWord vaue to chosen word
  135.   scrambledWord = scrambleWord(currentWord); // Runs scrambleWord(word) function with currentWord as sent parameter.
  136.  
  137.   setText("gameScreenScrambleBox", scrambledWord); // Updates the textbox to the scrambled word
  138.   setText("gameScreenDifficulty", wordList[randomPick].difficulty); // Updates textbox to match current word difficulty
  139.  
  140.   // Series of if-statements ran in order to update the text color of word difficulty text box
  141.  
  142.   if (wordList[randomPick].difficulty == "EASY") {
  143.    
  144.     setProperty("gameScreenDifficulty", "text-color", "#17b300");
  145.    
  146.   } else if (wordList[randomPick].difficulty == "MEDIUM") {
  147.    
  148.     setProperty("gameScreenDifficulty", "text-color", "#ffcb4c");
  149.    
  150.   } else {
  151.    
  152.     setProperty("gameScreenDifficulty", "text-color", "#fb7070");
  153.    
  154.   }
  155.  
  156.   // Updates player score and player highscore text boxes
  157.  
  158.   setText("gameScreenScore", "Score: " + playerScore);
  159.   setText("gameScreenHScore", "Highscore: " + playerHighscore);
  160.  
  161.   hintUsed = false; // Resets hintUsed to false
  162.   setText("gameScreenHDisplay", ""); // Resets textbox to blank
  163.  
  164.   setText("gameScreenTextInput", ""); // Resets user input text box to blank
  165.  
  166. }
  167.  
  168. // Parameter word holds the current randomly chosen word.
  169. function scrambleWord(word){ // Function ran when sent word needs to be scrambled
  170.  
  171.   var letters = word.split(""); // Separates characters into individuals and assigns them to the letters list
  172.  
  173.   for (var i = letters.length - 1; i > 0; i--){ // Loops through all elements in letters list
  174.    
  175.     var tempIndex = Math.floor(Math.random() * (i + 1)); // Creates a temporary index bet
  176.    
  177.     var tempVariable = letters[i]; // Temporary variable meant to hold current chosen letter
  178.     letters[i] = letters[tempIndex]; // Changes the value of the letters[i] element to randomly chosen index
  179.     letters[tempIndex] = tempVariable; // Changes the value of letters[tempIndex] element to formerly updated variable
  180.    
  181.   }
  182.  
  183.   return letters.join(""); // Joins the individual characters into one chunk of characters (scrambled word)
  184.  
  185. }
  186.  
  187.  
  188. // Parameter bool holds true/false value, true used when added score, false used when removed score.
  189. function updateScore(bool){ // Function ran when score needs to be updated
  190.  
  191.   if (bool){ // Function ran when bool parameter is true
  192.    
  193.     playerScore++; // Increases player score by 1
  194.    
  195.     if (playerScore > playerHighscore){ // Updates player high score if player score is greater than current highest score
  196.      
  197.       playerHighscore = playerScore;
  198.      
  199.     }
  200.    
  201.     for (var i = 0; i < messages.length - 1; i++){ // Loops through messages list
  202.      
  203.       if (messages[i].amount == playerScore){ // Continues function if the amount in messages list is equal to the player score
  204.        
  205.         setText("gameMessageDisplay", messages[i].text); // Updates text of the message displaying text box
  206.        
  207.       }
  208.      
  209.     }
  210.    
  211.   } else {
  212.    
  213.     playerScore = 0; // Resets score to 0 if player anwers incorrectly
  214.    
  215.     setText("gameMessageDisplay", ""); // Resets message displaying text box to 0
  216.    
  217.   }
  218.  
  219. }
  220.  
  221. onEvent("gameScreenBack", "click", function(){ // Event ran when the back button is pressed
  222.  
  223.   setScreen("mainScreen"); // Sets screen to main/home screen
  224.  
  225.  
  226. });
  227.  
  228. onEvent("gameScreenSubmit", "click", function(){ // Event ran when the submit button is pressed
  229.  
  230.   if (getText("gameScreenTextInput").toUpperCase() == currentWord || getText("gameScreenTextInput").toUpperCase() == currentWord + " " ){ // Checks if user inputted text is equal to the current word
  231.    
  232.     updateScore(true); // Runs update score function with bool set to true
  233.    
  234.     setProperty("gameScreenTextInput", "border-color", "green");
  235.    
  236.     // Functions as a one second pause in the middle of the event
  237.    
  238.     var currTime = getTime();
  239.     while (getTime() - currTime < 1000){}
  240.    
  241.     // Visual and auditory aspects ran
  242.    
  243.     setProperty("gameScreenTextInput", "border-color", rgb(153, 153, 153));
  244.    
  245.     playSound("sound://category_notifications/game_notification_83.mp3"); // Sound provided and owned by code.org.
  246.    
  247.   } else{
  248.    
  249.     updateScore(false); // Runs update score function with bool set to false
  250.    
  251.     setProperty("gameScreenTextInput", "border-color", "red");
  252.    
  253.     // Functions as a one second pause in the middle of the event
  254.    
  255.     var currTime0 = getTime();
  256.     while (getTime() - currTime0 < 1000){}
  257.    
  258.     // Visual and auditory aspects ran
  259.    
  260.     setProperty("gameScreenTextInput", "border-color", rgb(153, 153, 153));
  261.    
  262.     playSound("sound://category_alerts/comedy_game_over_1.mp3"); // Sound provided and owned by code.org.
  263.    
  264.   }
  265.  
  266.   updateScreen(); // Calls the updatescreen() function
  267.  
  268. });
  269.  
  270. onEvent("gameScreenHint", "click", function(){ // Function ran when the hint button is pressed
  271.  
  272.   var firstLetter = currentWord[0]; // Gathers the first letter of the current word
  273.  
  274.   // Checks whether the hint has already been used on this word or not.
  275.  
  276.   if (!hintUsed){ // If hint hasnt been used provides hint and updates hint text box while setting hintUsed variable to true
  277.    
  278.     setText("gameScreenHDisplay", "THE FIRST LETTER OF THE WORD IS: " + firstLetter);
  279.    
  280.     hintUsed = true;
  281.    
  282.   } else { // If hint has been used provides warning and then updates hint text box with current hint
  283.    
  284.     setText("gameScreenHDisplay", "HINT ALREADY USED!");
  285.    
  286.     // Functions as a one second pause in the middle of the event
  287.    
  288.     var currTime = getTime();
  289.     while (getTime() - currTime < 1000){}
  290.    
  291.     setText("gameScreenHDisplay", "THE FIRST LETTER OF THE WORD IS: " + firstLetter);
  292.    
  293.   }
  294.  
  295. });
  296.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement