Home โ€บ Module 2
โšก Module 02 ยท Intermediate

Give Your Game a Brain ๐Ÿง 

What if you could teach a computer how to think? Let's build a simple "brain" for a game character using a powerful idea called a State Machine. This is how you make characters that can patrol, chase, and attack all on their own! Ready? Let's go!

PAN'S SAFETY CHECK ๐Ÿ›ก๏ธ: When you build cool stuff, you're like a superhero creating a secret base. Never use your real name, address, or passwords in your code! Keep your personal info out of your projects to keep your digital world safe. Awesome!
๐Ÿ‘พ

What is a State Machine?

Think about a traffic light! It has three states: ๐ŸŸข Green, ๐ŸŸก Yellow, and ๐Ÿ”ด Red. It can only be in one state at a time, and it changes based on a rule (like a timer). That's a state machine! Your game characters work the same way.

An enemy goblin doesn't just randomly attack. It exists in one State at a time: maybe it's just Patrolling. If it sees the player, it changes its state to Chasing. If it gets close enough, it changes again to Attacking.

Patrolling ๐Ÿšถ
โ†’ Sees Player โ†’
Chasing ๐Ÿƒ
โ†’ Gets Close โ†’
Attacking โš”๏ธ
๐Ÿ“บ Game Maker's Toolkit โ€” "What is a State Machine?" โ€” A brilliant explanation of how game characters make decisions. A must-watch for any future game designer!

Psst! This video breaks it down perfectly. Watch it to see how the pros think about character brains, then get ready to build your own! โœจ

So, Why Bother?

You could just write a bunch of `if/else` statements, right? `if the player is close, then attack, else if the player is far, then patrol...` It gets messy fast! We call that spaghetti code ๐Ÿ because it's a tangled mess. State machines keep your logic clean and organized, making it way easier to build (and fix!) complex behaviors.

๐Ÿ Spaghetti Code

Hard to follow!

๐Ÿง  State Machine

Clean & Organized!

โšก Quick Quiz!

A sleepy dragon is in the SLEEPING state. What event would change its state to ANGRY?

Someone gives it a pillow
A knight steals its treasure
It dreams about flying

Challenge: Design a Guard's Brain! ๐Ÿง 

Your mission, builder! Design a guard's brain that has no dead ends. A guard should always be able to get back to the IDLE state eventually. Describe the rules for IDLE, INVESTIGATING, and ALERT. Make sure you include a rule for how the guard calms down from ALERT!

AI Feedback will appear here...
๐Ÿš€ Power-Up Challenge: Add a New State

Nice work! Real game characters have even more states. What if the player runs away after the guard is in the ALERT state?

Try adding a fourth state, like RETURNING_TO_POST, to your logic in the box above. Think about the rules:

  • What makes the guard go from ALERT to RETURNING_TO_POST?
  • What happens when the guard gets back to its post?

Update your logic and run the simulation again to see what the AI thinks!

Awesome! Your logic is the blueprint. The section below shows how a programmer would start turning that blueprint into real code. See if you can spot where your "rules" would fit!

curious? see what this looks like in python code...
class Guard:
    def __init__(self):
        self.state = "IDLE"
        print("Guard created. Initial state: IDLE")

    def update(self, sees_player, hears_sound):
        print(f"\n... sees_player={sees_player}, hears_sound={hears_sound} ...")
        if self.state == "IDLE":
            if hears_sound:
                self.state = "INVESTIGATING"
        elif self.state == "INVESTIGATING":
            if sees_player:
                self.state = "ALERT"
        # In a real game, you'd add more logic here!
        print(f"New state is: {self.state}")

# Let's run a simulation!
guard = Guard()
guard.update(sees_player=False, hears_sound=True)  # Hears a noise!
guard.update(sees_player=True, hears_sound=False)   # Sees the player!

๐Ÿ“š Learn More

๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง Parent Corner

Your young developer is learning core computer science concepts! State machines are the first step to creating the "smart" AI characters you see in modern games. If they're excited to build more, we recommend starting with GDevelop. It's a free, no-code game engine that's perfect for this skill level. As they grow, Godot Engine is another fantastic, free option.

Talk about it: Ask your child what 'states' their favorite game character has (e.g., "Is Mario small, big, or does he have a fire flower?"). This is a great way to see state machines in the wild! For a fun screen-free activity, try designing a simple board game together! Thinking about the rules and what a player can do is a great way to practice state machine logic.

โ† Previous