Operators
Operators - CS111 Review
| Back | Index | Next |
|---|---|---|
| Data Types | Index | Input/Output |
Operators
Click a category to see operators from Heist.exe.
Operators in Heist.exe
Operators are what actually do things with data — transform it, combine it, or evaluate it. In Heist.exe, they drive the Guard’s bounce physics, build sprite paths, and gate every key decision in the game logic.
flowchart TD
A[Game Data] --> B[➕ Math Operators<br>Transform values]
A --> C[📝 String Operators<br>Build text & paths]
A --> D[✔️ Boolean Expressions<br>Evaluate conditions]
B --> E[Velocity · Position<br>· Score]
C --> F[Sprite paths · Console<br>logs · API URLs]
D --> G[Collision · Collection<br>· API errors]
The Three Categories
Mathematical Operators — transform numbers through calculations
Exactly what it is: Math operators perform calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), and assignment operations (+=, -=, *=, /=). They’re essential for physics, movement, and score tracking.
From Heist.exe:
// Guard.js — line 44: Bounce mechanic (the entire physics!)
this.velocity.y *= -1; // Multiply by -1 to reverse direction
// Before: velocity.y = -3 (moving up)
// After: velocity.y = 3 (moving down)
// Guard.js — line 26: Apply velocity each frame
this.position.y += this.velocity.y; // Add velocity to position
// Every frame, Guard moves by its velocity: y decreases (up) or increases (down)
// Guard.js — horizontal boundary stop
this.velocity.x = 0; // Assignment: set to zero (stop moving)
// Gem.js — collect() method: Accumulate score
this.gameEnv.stats.coinsCollected = (this.gameEnv.stats.coinsCollected || 0) + this.value;
// ||: Use 0 if coinsCollected doesn't exist yet
// +: Add current gem value (5) to total
The bounce mechanic is literally one operator: velocity.y *= -1 flips the sign, keeping magnitude the same. Perfect example of an operator doing complex work.
String Operators — combine and transform text
Exactly what it is: String operators let you combine multiple pieces of text into one, embed live values into templates, or modify how text behaves. Concatenation (+) joins strings. Template literals (backticks) let you embed variables directly into strings.
From Heist.exe:
// Gem.js — lines 54-55: Template literal with embedded values
console.log(`Gem collected! +${this.value} | Total: ${this.gameEnv?.stats?.coinsCollected}`);
// Outputs: "Gem collected! +5 | Total: 12"
// The ${} syntax embeds live variable values
// HeistL2.js — lines 20, 50, 88: Concatenation builds file paths
src: path + "/images/projects/heist-exe/heist-guard.png"
// path might be: "https://moopa01.opencodingsociety.com"
// Result: "https://moopa01.opencodingsociety.com/images/projects/heist-exe/heist-guard.png"
// heistMusic.js: Build iTunes API endpoint with encoded search term
`https://itunes.apple.com/search?term=${encodeURIComponent(picked)}&entity=song&limit=25`
// encodeURIComponent: Convert "indie acoustic" → "indie%20acoustic" (URL-safe)
// Template literal ${} embeds the encoded term into the full URL
Template literals are cleaner than concatenation: \Total: ${score}` vs “Total: “ + score`.
Boolean Expressions — evaluate true/false conditions to gate logic
Exactly what it is: Boolean operators combine and invert true/false values. && (AND) means “both must be true”. || (OR) means “at least one must be true”. ! (NOT) inverts the value: true becomes false, false becomes true. instanceof checks if an object is an instance of a specific class.
From Heist.exe:
// Guard.js — line 21: Two conditions must both be true
if (!this.playerDestroyed && this.collisionChecks()) {
// Run collision event only if:
// 1. Player hasn't been destroyed already (!this.playerDestroyed)
// AND
// 2. Collision is currently detected (this.collisionChecks())
}
// Guard.js — line 56: Find Player object by type
var player = this.gameEnv.gameObjects.find(obj => obj instanceof Player);
// instanceof Player: Is obj an instance of the Player class?
// Returns the first object that matches, or null if none found
// heistMusic.js: Check failed API response
if (!response.ok) {
throw new Error('API request failed (' + response.status + ')');
}
// !: Invert response.ok
// response.ok = true → !response.ok = false (don't throw)
// response.ok = false → !response.ok = true (throw error)
// Gem.js — line 22: Short-circuit on already-collected flag
if (this.permanentlyCollected) return;
// If permanentlyCollected is true: exit immediately
// If permanentlyCollected is false: continue with collection logic
// Gem.js — Safe default with ||
(this.gameEnv.stats.coinsCollected || 0)
// If coinsCollected exists and is truthy: use it
// Otherwise (undefined, null, 0, false): use 0
&& is the most critical operator in Heist.exe: it prevents the Guard from destroying the player twice. ! inverts states. instanceof ensures you’re colliding with the right type of object.
| Category | Operators | Used for |
|---|---|---|
| Math | *= += + \|\| |
Bounce, movement, score accumulation |
| String | + ` `` ` encodeURIComponent |
Paths, logs, API URLs |
| Boolean | && ! instanceof |
Collision gating, error handling, state checks |
Tip:
*= -1is one of the most useful patterns in game physics — it’s the entire Guard bounce in a single operator. Flip the sign, keep the magnitude.