CS111 Evidence: Operators
Operators Evidence
๐น Operators: Mathematical
- ๐ Requirement: Use math operators to calculate positions, adjust game dimensions, and update scores.
- ๐ Assessment Method: Code review of math formulas, shortcut operators, and variable updates.
- What it is: Symbols used to do math in code, like addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), and shorthand updates (`+=`, `-=`).
- How it works: They change numbers in real time, like changing a character's X/Y pixels to make them move or dropping a countdown timer by 1 every second.
- Why it helps: It makes the game dynamic. Math runs everything from smooth physics and movement speeds to calculating accurate hitboxes and scores.
// EVIDENCE: OPERATORS - MATHEMATICAL
// Source: GameLevelRedRidingHood1.js & GameLevelRedRidingHood3.js
// Part 1: Gravity and Fall Physics Vectors
class GameLevelRedRidingHood1 {
update() {
const player = this.gameEnv.gameObjects.find(obj => obj.id === 'player');
if (player) {
// Mathematical Assignment Operators (+=): Adding forces to numeric tracking values
this.vy += this.gravity; // Increases vertical velocity downwards by gravity constant
player.position.y += this.vy; // Shifts physical coordinate pixel value by updated velocity
// Mathematical Arithmetic Operators (-): Calculating the bottom screen floor landing boundary
const floor = this.gameEnv.innerHeight - player.height;
}
}
}
// Part 2: Centered Spatial Offsets and Hitbox Shrinking Formulas
class GameLevelRedRidingHood3 {
update() {
const enemy = this.gameEnv.gameObjects.find(obj => obj instanceof Enemy);
if (!enemy) return;
// Complex Arithmetic Expressions (*, /): Offsetting and sizing dimensions directly
const wolfBox = {
x: enemy.x + enemy.width * 0.15, // Shrinks collision boundary space using multiplication
y: enemy.y + enemy.height * 0.15,
width: enemy.width * 0.7, // Restricts valid hitting space to 70% of asset size
height: enemy.height * 0.7
};
}
}
๐น Operators: String
- ๐ Requirement: Use string operators to join text elements and update text displays dynamically.
- ๐ Assessment Method: Code review of concatenation operators and compound assignment text updates.
- What it is: String operators manipulate text. The primary operators are the concatenation operator (`+`) and the concatenation assignment operator (`+=`).
- How it works: The `+` operator links separate strings into a single text sequence, while `+=` appends new chunks of text directly onto the end of an existing string variable.
- Why it helps: It handles text building efficiently. It allows the game engine to construct file paths dynamically or continuously append new lines of dialogue to an onscreen status box.
// EVIDENCE: OPERATORS - STRING
// Source: GameLevelRedRidingHood1.js & GameLevelRedRidingHood4.js
class GameLevelRedRidingHood1 {
constructor(gameEnv, game) {
// String Concatenation (+): Combining explicit text literals with directory references
const image_data_wood = {
name: 'woods',
src: gameEnv.path + "/images/projects/red-riding/woods.png",
pixels: { height: 580, width: 1038 }
};
}
update() {
const currentScore = this.gameEnv.stats?.coinsCollected || 0;
// Dynamic Text Update (+): Merging a label sequence with a changing numeric value
if (this.scoreElement) {
this.scoreElement.innerHTML = "Cookies Collected: " + currentScore;
}
}
}
class GameLevelRedRidingHood4 {
constructor(gameEnv) {
// Template Literals (``): Embedding raw numeric tracking values inside an operational text block
this.timerDisplay.textContent = `Time: ${this.timeLeft}`;
this.secretBanner.textContent = 'SECRET LEVEL 4: WOLF SHOOTING MINIGAME PRESS Q TO SHOOT';
}
}
๐น Operators: Boolean
- ๐ Requirement: Implement logical operators to check multiple conditions and control game logic gates.
- ๐ Assessment Method: Code review of logical AND, OR, and NOT operations within decision flows.
- What it is: Logical operators combine or flip true/false statements. This includes AND (`&&`), OR (`||`), and NOT (`!`).
- How it works: `&&` requires all conditions to be true, `||` requires at least one condition to be true, and `!` reverses a boolean value from true to false (or vice versa).
- Why it helps: It creates smart guardrails. It lets the engine verify complex rules simultaneouslyโlike checking if a player has hit an item *and* has room in their inventory before picking it up.
// EVIDENCE: OPERATORS - BOOLEAN
// Source: Red Riding Hood Game Architecture
class GameLevelRedRidingHood4 {
constructor(gameEnv) {
this.gameEnv = gameEnv;
}
evaluateGameRules(player, enemy) {
// Logical OR Operator (||): True if at least one condition is met
// Blocks updates if the game is over OR if the level is currently paused
if (this.gameOver || this.isPaused) {
return "Execution halted.";
}
// Logical AND Operator (&&): True only if ALL conditions are met
// Trigger damage if player touches enemy AND player's temporary invincibility is turned off
if (player.collidesWith(enemy) && !player.isInvincible) {
player.health -= 10;
player.isInvincible = true; // Set flag to true to prevent instant multi-hits
}
// Logical NOT Operator (!): Inverts a boolean value (true becomes false, false becomes true)
// If the player is NOT jumping, allow them to safely initiate a new jump cycle
if (!player.isJumping) {
player.canJump = true;
}
}
}