What is a Boolean ?

Booleans: - Represent true or false values.
  • Used for logical operations and decision-making.

  • Simplify condition checking in programming.

Booleans are essential in game development as they represent true or false conditions. They ensure the game functions correctly, especially when it comes to actions like exiting a game level. Here’s a sample of how booleans are used in a game’s code:

%%js

//Play around with the variables to see what happens
let keyInserted = true;
let correctKey = false;
let lockPick = false;

if (lockPick) {
    console.log("Door opened.")
}
else {
    //Check if the key you have was inserted and is the correct key
    if (keyInserted){
        console.log("Inserted key into door.")
    }
    else {
        console.log("Insert key into door.")
    }
    if (keyInserted && correctKey){
        console.log("Door opened.")
    }
    else {
        console.log("Incorrect key, door did not open.")
    }
}


Lock Pick Check:

  • If lockPick is true, the code logs “Door opened.” and stops further checks.
    • Key Insertion Check:
    • If lockPick is false, the code checks if keyInserted is true.
    • If keyInserted is true, it logs “Inserted key into door.”
    • If keyInserted is false, it logs “Insert key into door.”
  • Correct Key Check:
    • After checking if the key is inserted, it checks if both keyInserted and correctKey are true.
    • If both are true, it logs “Door opened.”
    • If either is false, it logs “Incorrect key, door did not open.”

What are Arrays?

Arrays: - Ordered collections of elements. - Store multiple values in a single variable. - Allow efficient data manipulation and traversal.

How can we use Arrays in game: Because They are helpful when it comes to multiple values in a single variable, making it easy to manage large sets of data.

case "finishline":
                console.log("finish line checks")
                console.log(GameEnv.gameObjects)
                var collectedCoin
                if (collectedCoin == false){
                    for (let obj of GameEnv.gameObjects) {
                        console.log(obj.jsonifiedElement.id)
                        if (obj.jsonifiedElement.id === "coin") {
                            collectedCoin = false
                            console.log("coin not collected not advancing to next lvl")
                            return;
                        }
                }
  • Finish Line Case:
    • Logs a message indicating the finish line checks are being performed.
    • Outputs the array of game objects for inspection.
  • Collected Coin Check:
    • Initializes a variable to track whether the coin has been collected.
    • Iteration Over Game Objects:
    • Loops through each game object in the array.
    • Logs the ID of each game object for debugging purposes.
  • Coin Check:
    • If an object with the ID “coin” is found, marks the coin as not collected.
    • Logs a message indicating the coin is not collected and prevents advancing to the next level.

What is a String?

  • Strings:
    • Represent sequences of characters.
    • Used for text manipulation and storage.
    • Enable operations like concatenation, searching, and formatting.

Strings are like greetings in a game where for example in one of our games we have to interact with a NPC when we press button and it greets us.

let greeting = "Hi I am Java Nomad, the Java mascot. I am very happy to spend some linux shell time with you!";

Declaration and Assignment: The let keyword declares a new variable named greeting. The string “Hi I am Java Nomad, the Java mascot. I am very happy to spend some linux shell time with you!” is assigned to this variable. This means the variable greeting now holds this entire sequence of characters as its value.

Conclusion

Arrays, booleans, and strings are crucial in programming because they enable effective data storage, decision-making, and text processing.

  • Arrays organize multiple elements.
  • Booleans drive logic through true/false values.
  • Strings handle and manipulate text.

Together, they form the foundation for dynamic and efficient code.