Booleans in My Game
How booleans are used in my JavaScript ocean game.
Booleans in My Game
What Are Booleans?
Booleans are values that can only be:
true
or
false
Booleans are used to control logic and decision-making in programs.
Games use booleans to track:
- collisions
- enemy states
- cooldowns
- player conditions
- game events
My game uses booleans throughout many gameplay systems.
Why Booleans Are Important
Without booleans, the game could not:
- check conditions
- control enemy behavior
- activate cooldown systems
- prevent repeated actions
- manage game states
Booleans help the game make decisions.
Example 1: Collision Cooldown
My game uses a boolean to prevent repeated enemy damage.
gameEnv.elonHitCooldown = false;
Explanation
This boolean tracks whether:
- the player can currently take damage
- cooldown is active or inactive
If the value is:
true→ cooldown is activefalse→ player can take damage again
Example 2: Checking Boolean Conditions
The game checks booleans using conditionals.
if (!this.gameEnv.elonHitCooldown)
Explanation
This means:
if cooldown is false
The game then:
- allows damage
- deducts points
- activates cooldown
Example 3: Enemy State Tracking
Enemies use booleans to control behavior.
if (this.isKilling) return;
Explanation
The boolean isKilling determines:
- whether enemy actions should stop
- whether updates should continue
If isKilling is:
true→ stop updatingfalse→ continue behavior
Example 4: Collision Detection
Booleans are produced by comparisons.
if (dist < 40)
Explanation
The expression:
dist < 40
returns:
trueif objects are close enoughfalseif they are too far apart
This controls collision logic.
Example 5: Object Existence Checks
Booleans help prevent errors.
if (fish)
Explanation
This checks whether:
- the fish object exists
- the object is valid
- the game can safely interact with it
The condition evaluates to either true or false.
Example 6: Score Protection
Booleans are used in score validation.
if (this.score < 0)
Explanation
This comparison returns:
trueif score is negativefalseotherwise
The game uses this to prevent invalid values.
Example 7: Multiple Boolean Checks
My game combines booleans with nested logic.
if (dist < 40) {
if (!this.gameEnv.elonHitCooldown) {
this.gameEnv.gameScorer.deductPoints(10);
}
}
Explanation
This uses multiple boolean conditions to:
- detect collisions
- check cooldown state
- decide whether damage should happen
Booleans make complex game logic possible.
Why Booleans Help My Game
Booleans improved my project by:
- controlling gameplay logic
- managing enemy and player states
- preventing repeated actions
- supporting collision systems
- enabling decision-making
Without booleans, the game could not properly react to gameplay events.