Skip to content

Chapter 4: The Brains of the Operation

When the creator of Space Invaders first built his game in 1978, he accidentally made the alien ships speed up as the player destroyed them. It wasn’t planned — the computer simply had fewer graphics to draw, so it processed everything faster! Players loved the challenge, so the “bug” became an official feature.

You’ve designed a blueprint, created characters, and built levels. But without programming, those characters are just statues. Let’s bring your world to life.

Computers aren’t smart — they’re just fast and obedient. A computer only does exactly what you tell it. To make a game, you need to break big actions into tiny, logical steps.

The foundation of all programming is the if/then statementif/then statement: A logic rule that tells the computer: IF a condition is true, THEN do something. It's how programs make decisions. — it tells the computer what to do when something happens:

IF the player presses Jump → THEN move the character up

Games are usually more complex, so we add an Else branch. Think of Super Mario:

IF Mario gets hit AND has a Super Mushroom → THEN he shrinks
ELSE → he loses a life

A BooleanBoolean: A data type that can only be True or False. Named after mathematician George Boole. is a simple True or False value. Think of a zombie in Minecraft:

Condition Result
isDaytime = True Zombie catches fire 🔥
isDaytime = False Zombie keeps chasing you 🧟

You can combine Booleans with AND / OR operators to make more complex decisions.

Before writing code, developers often draw a flowchartflowchart: A diagram that maps out a program's decisions and actions step-by-step using shapes and arrows. to plan their logic visually.

When you need an action to repeat, you use looploop: A programming structure that repeats a block of code either a set number of times or while a condition is true.:

Loop Type What It Does Example
While Loop Runs while a condition is true While health > 0, keep playing battle music
For Loop Runs a specific number of times Run this code 5 times to spawn 5 enemies

A functionfunction: A reusable block of code that performs a specific task. You write it once, then call it whenever you need it. is a mini-program you write once and reuse whenever you want. Instead of typing the same damage math 100 times, you write a CalculateDamage function once and call it whenever needed.

While your game runs, the computer needs to remember a ton of information. That’s where data storage comes in.

Concept What It Is Example
variablevariable: A named storage location in a program whose value can change during execution. A box that stores info that can change playerHealth = 100
constantconstant: A named storage location whose value is set once and never changes during execution. A locked box that never changes MAX_LEVEL = 100

When you create these storage boxes, you choose a data typedata type: The category of data a variable can hold, such as whole numbers, decimals, or text.:

  1. Integers — Whole numbers. Perfect for counting: 5 coins, 3 lives, 1 sword.
  2. Floats — Numbers with decimals. Great for precision: a movement speed of 4.5.
  3. Strings — Words and text. When an NPC says "Welcome to my shop!", that’s a String.

An arrayarray: An ordered, numbered list that stores multiple values of the same type in a single variable. stores many similar items in a numbered list instead of creating hundreds of separate variables.

Professionals use Object-Oriented ProgrammingObject-Oriented Programming: A programming approach where you create reusable templates (classes) and stamp out individual copies (objects) from them. to stay organized. Think of it like a cookie cutter:

  1. Design one Goomba Class (the cookie cutter) with variables for health and speed.
  2. Stamp out 50 Goomba Objects into your game — each one independent.

Behind the scenes, the game loopgame loop: A continuous cycle that runs every frame: check input → update game state → render graphics. Typically runs 60 times per second. runs over and over — usually 60 times per second (60 FPS). Every cycle, it checks:

  1. Did the player press a button?
  2. Where is everything now?
  3. Should we play a sound?

You write code that responds to two kinds of input:

  • Hardware input — keyboard, mouse, joystick
  • GUI input — clicking an on-screen button like “Inventory”

The game needs to know when objects bump into each other. Every character has a hitboxhitbox: An invisible shape (usually a rectangle) drawn around a game object that the engine uses to detect collisions. — an invisible box drawn around them. When two hitboxes overlap, a collision happens, and your code decides what to do next.

To make gameplay rewarding, implement a feedback loopfeedback loop: A cycle where the game responds to player actions with visible or audible rewards (like score updates and sound effects), encouraging continued play.. Display the score on the GUI and trigger a satisfying sound effect every time it increases!

Modern game engines offer two approaches:

Approach How It Works Examples
Visual Programming Drag-and-drop blocks Scratch, Unreal Blueprints
Scripting Typing code line by line C# in Unity, Lua in Roblox Studio
Term Meaning Analogy
syntaxsyntax: The rules for how code must be written — spelling, punctuation, and structure that the computer requires. The spelling & grammar of your code Like following English grammar rules
semanticssemantics: The actual meaning or logic behind your code instructions — what you intend the code to do. The actual meaning of your instructions Like whether your sentence makes sense

Mistakes will happen. Here are the two main types:

  • Compile error — Stops the game from even starting. Usually a syntax mistake.
  • Runtime error — Happens while you’re playing. The code runs, but something goes wrong.

To fix bugs, you debugdebug: The process of finding and fixing errors (bugs) in your code..


  • I understand how if/then/else logic works
  • I can explain what a Boolean is
  • I know the difference between while loops and for loops
  • I can describe what a function does
  • I know the difference between variables and constants
  • I can name the three primitive data types
  • I understand what an array is and that it starts at index 0
  • I can explain Object-Oriented Programming with the cookie cutter analogy
  • I know what the game loop does each frame
  • I understand collision detection and hitboxes
  • I know the difference between syntax and semantics
  • I can describe two types of errors and how to debug them