CS50 2D - Lecture 5 - Legend of Zelda (live, unedited)
Overview
CS50's "Legend of Zelda" lecture explores 2D game development concepts inspired by the classic franchise, covering dungeon generation, hitboxes/hurtboxes, screen scrolling, and stenciling. David Malan and Colton Ogden demonstrate implementing these features in Lua using the Love2D framework, showcasing data-driven design and coroutines for complex algorithms like dungeon generation.
Key takeaways
- Data-driven design, using external definition tables for entities and animations, significantly speeds up game development iteration.
- Coroutines in Lua provide a powerful mechanism for debugging complex algorithms like dungeon generation by allowing step-by-step execution.
- Stenciling, implemented via `love.graphics.setStencilState` and `setColorMask`, enables advanced graphical effects like masking pixels to create illusions of depth and corridors.
- The distinction between hitboxes (can be struck) and hurtboxes (inflict damage) is crucial for fine-tuning combat mechanics and game balance.
- Screen scrolling in Zelda-like games often involves a clever illusion: loading the next room off-screen and resetting the player/camera to (0,0) after a tweened transition.
Chapters
- CS50's first live stream focusing on The Legend of Zelda.
- Discussion of favorite Nintendo consoles: Nintendo 64, Super Nintendo, and Wii.
- Zelda's gold cartridge and open-world exploration were groundbreaking for its time.
- The original Zelda featured discrete screens and an early open-world concept.
- Link to the Past expanded the formula with multiple worlds and items like the boomerang.
- Modern Zelda titles like Tears of the Kingdom (2023) and Breath of the Wild (2017) remain top sellers.
- Side-scrollers (like Mario) primarily use the Y-axis for gravity-based jumping.
- Zelda's top-down (bird's-eye) view allows full movement across X and Y axes.
- This lecture focuses on dungeon generation, hitboxes/hurtboxes, screen scrolling, and stenciling.
- Audience volunteer Damon demonstrates "Legend of 50" gameplay.
- Features include character movement, sword swings, and collecting the Triforce.
- Initial demo shows a dungeon without doorways, requiring a restart.
- Damon navigates a generated dungeon with switches and doorways.
- Pressing 'M' reveals a dungeon layout map.
- The demo concludes with a stylized "Game Over" screen after losing all health.
- Classic screen scrolling, strikable enemies with hurtboxes, and AABB collision detection are discussed.
- Game objects like switches and doorways, along with game states and player animations, are highlighted.
- Player entity states include walking, idle, and sword-swinging.
- The initial "Zelda Zero" update focuses on top-down rendering and player movement.
- Includes basic character animations for different directions and a health display (hearts).
- Core 2D movement from a bird's-eye perspective is established.
- Top-down perspectives can be isometric or orthographic.
- Tiles simulate lighting and perspective with different highlight colors.
- Tiles have unique textures and indices, similar to Mario's rendering approach.
- Sprite sheets contain numerous frames for character actions (walking, picking up objects).
- Problem set involves implementing throwable pots.
- Sword-wielding sprites are larger (32x32) than walking sprites (16x16 or 16x24).
- Entities are containers for behavior and attributes, subclassable for specific types (player, enemy).
- Data-driven design uses definition tables (e.g., entity defs) to define animations and attributes externally.
- This approach separates programmer and designer tasks, allowing for easier iteration.
- Entities have attributes like health, managed via hearts (2 health per heart).
- Player invulnerability mechanic (phasing in/out) prevents rapid damage accumulation.
- This mechanic provides a buffer for players to escape or defeat enemies.
- Entity definitions (e.g., `entitydefs.lua`) store animation data, intervals, and texture names.
- This allows designers to modify game elements without touching core code.
- The system supports defining various creature types and their behaviors.
- Sprite sheets are split into uniform sizes (e.g., 16x32) for programmatic loading.
- This simplifies animation management by using indexable frames (e.g., `gframes.character_walk`).
- Data-driven animation definition reduces manual coding.
- Player uses state machines (walk, idle) similar to Mario's implementation.
- `love.graphics.push` and `love.graphics.pop` manage graphics states for transformations.
- This allows UI elements (like hearts) to remain fixed while the game world scrolls.
- Demonstrates player movement with directional animations (up, down, left, right).
- Idle and walk states are managed via state machines.
- The system allows designers to influence game behavior through data.
- Introduces rendering dungeon tiles, building on Mario's tile-based approach.
- No tile-based collision implemented; boundary checks handle room limits.
- Tile sets can be complex, with variants for aesthetic variety (e.g., floor, wall tiles).
- Tiles are typically 16x16 pixels, but larger objects like doorways use multiple tiles.
- A Python script (using Pillow) or Love2D can number tiles for easy reference.
- Variants of tiles (e.g., different floor patterns) prevent visual homogeneity.
- The `Room` object manages tiles, doorways, switches, and entities.
- The `generate_walls_and_floors` function iterates through a grid, placing tiles based on constants.
- Random selection from tile variants (e.g., 3-4 wall types) enhances visual variety.
- Player's walk state update handles boundary checks against screen limits.
- This prevents the player from moving outside the defined room boundaries.
- Static invocation (`EntityWalkState.update(self, dt)`) calls parent class methods.
- Introduces the concept of a dungeon composed of multiple interconnected rooms.
- Dungeon generator creates a structure of contiguous rooms.
- The `Dungeon` object manages a `rooms` table and the `current_room`.
- The `Dungeon` object holds a 2D grid of rooms and tracks the `current_room`.
- A `next_room` variable is crucial for managing transitions between rooms.
- Player starting position (`start_x`, `start_y`) can be customized.
- Hitboxes are areas that can be struck; hurtboxes are areas that inflict damage.
- Previously, these were often treated as one; now, they are distinct for finer control.
- Examples from Street Fighter and Minecraft illustrate hitbox/hurtbox usage.
- The player gains a `swing_sword` state, activating a hitbox for the sword.
- Hitbox dimensions and positions are adjusted based on player direction.
- Debug rectangles visualize hitboxes and hurtboxes for balance tuning.
- The `swing_sword` state changes player animation based on direction.
- Hitbox coordinates (`hitbox_x`, `hitbox_y`, `width`, `height`) are calculated dynamically.
- The hitbox is compared against entities' hurtboxes for damage detection.
- Adds various creatures (skeletons, slimes, bats, ghosts, spiders) to the dungeon.
- Entities have simple AI: random movement, stopping at walls, idle/walk states.
- Player collision with enemies triggers damage and invulnerability frames.
- The `generate_entities` function places creatures randomly within the room.
- Entities have health (1 HP) and are removed when defeated.
- Player takes damage (`damage(1)`) and enters invulnerability state upon collision.
- Introduces triggers (switches) that activate events, like opening doorways.
- Doorways are represented by four tiles and can be larger than standard tiles.
- The `Doorway` class manages its open/closed state and directionality.
- Game objects (like switches) have `on_collide` functions for interaction.
- Data-driven design allows defining object properties (type, texture, states) in configuration files.
- Switches trigger opening doorways and playing sound effects upon player collision.
- Implements the characteristic Zelda screen-to-screen scrolling effect.
- Uses `love.timer.tween` for smooth camera and player movement between rooms.
- The illusion involves loading the next room off-screen and resetting the player's position to (0,0).
- Utilizes `love.event.on` and `love.event.dispatch` for managing game events.
- When colliding with a doorway, a 'shift' event is dispatched.
- The `Dungeon` constructor sets up event handlers for directional shifts.
- Transition logic is triggered within the player's `walk_state` after detecting wall collision (`self.bumped`).
- Player's position is centered with the doorway before dispatching the 'shift' event.
- This ensures the player aligns correctly when entering the next room.
- The `begin_shifting` function calculates the next room's position and sets its doorways to open.
- It determines the player's final position in the next room (opposite entry point).
- Tweening animates the camera and player movement over a specified duration (e.g., 1 second).
- The `finish_shifting` callback resets room/dungeon states and camera positions to zero.
- It swaps `current_room` and `next_room` pointers.
- Switches are reset, and doors are closed upon returning to a previous room.
- Implements stenciling (masking) to create the illusion of moving through corridors.
- The stencil buffer prevents pixels from being drawn in specific areas (e.g., behind archways).
- Demonstrates both Love 11 (`love.graphics.stencil`) and Love 12 (`love.graphics.setStencilState`) APIs.
- `love.graphics.setStencilState` uses action, mode, and value parameters.
- Actions include replace, increment, decrement; modes include always, equal, less than.
- `love.graphics.setColorMask` prevents drawing to the screen while modifying the stencil buffer.
- The `render` function in `room.lua` sets the color mask to false and uses `setStencilState`.
- Rectangles are drawn to define the stencil mask areas (e.g., archways).
- A stencil test (`less than one`) determines which pixels pass through.
- A debug flag (key 'S') draws semi-transparent red rectangles to visualize stencil areas.
- This aids in verifying the placement and alignment of masking operations.
- The Love 12 version requires `setColorMask` to prevent drawing while modifying the stencil buffer.
- The dungeon generator uses a queue (breadth-first approach) to create contiguous rooms.
- A weighted chance (40% skip) introduces randomness in room placement.
- A `visited` array prevents redundant room generation and ensures contiguousness.
- Generation proceeds through phases: rooms, doorways, and finalization.
- The `Dungeon` generator visualizes the process step-by-step using coroutines (`coroutine.wrap`).
- Coroutines allow pausing and resuming generation, making complex algorithms easier to debug.
- Emphasizes designing game behavior through data (e.g., entity attributes like flammable, aggressive).
- Entity Component Systems (ECS) are well-suited for complex, dynamic games.
- This approach facilitates emergent gameplay and rapid design iteration.
- Contrasts modern high-level frameworks like Love2D with low-level development (NES, SNES).
- NES games were written in assembly (6502), requiring direct hardware manipulation.
- Exploring historical paradigms highlights programming advancements.
- Extends player abilities: lifting/throwing pots as projectiles, damaging enemies.
- Introduces HP recovery via hearts dropped by enemies or found in pots.
- Task includes implementing treasure chests containing items like the boomerang.
- Player can throw a boomerang, which returns after a certain distance.
- The boomerang damages enemies, bounces off walls, and cannot be thrown while active.
- An inventory icon displays the boomerang's availability.
- Covered top-down and side-scrolling 2D game development fundamentals.
- Explored concepts like dungeon generation, scrolling, stenciling, and data-driven design.
- Future topics include physics (Box2D) and turn-based systems (Pokemon).
- `love.graphics.setStencilState` configures stencil buffer operations (action, mode, value).
- `love.graphics.setColorMask` prevents drawing to the screen, allowing only stencil buffer modification.
- These functions enable masking effects like drawing behind terrain.
- Demonstrates stenciling in Zelda 7, showing the character moving under archways.
- Pressing 'S' toggles debug rectangles that visualize the stencil mask areas.
- This debugging practice helps verify correct stencil buffer setup.
- The `render` function uses `setColorMask(false, false, false, false)` to disable screen drawing.
- `setStencilState` with 'replace' action and 'one' value writes to the stencil buffer.
- A stencil test (`less than one`) controls pixel rendering based on stencil buffer values.
- The dungeon generator uses a queue for breadth-first room placement.
- Coroutines (`coroutine.wrap`) allow step-by-step visualization and debugging of the generation process.
- The generator ensures contiguous rooms and includes a doorway placement phase.
Transcript excerpt
5:00 Hello world. This is CS50, the first of two live streams today. I am David Mailen and I'm here with Colton Ogden and today we're going to be focusing on The Legend of Zelda, which was one of my favorite games growing up on the original Nintendo Entertainment System. Um, as always, we're going to be fielding questions via the chat. I'll raise my hand to surface any to Colton as well. Please forgive if we start and stop to have to fix things along the way. Uh but otherwise, we're just about good to go. Uh we have a tradition of fun facts here, and I didn't think of one in advance, so I'm stalling for just a moment so as to think of a really good one, and I've come up with this question. Of all of this Nintendo systems over the years, do you have a favorite console? I think Nintendo 64. Interesting. It's nostalgic. It was what I played mostly in elementary school, but Super Nintendo probably it's that's
5:47 they're tied for you. Well, for me it was the original Nintendo, but I'm thinking to myself, the N64 I think I only first played in graduate school. You played Wii quite a bit. The Wii is what got me back into gaming, actually, because it was so much simpler than like the traditional controllers of the Xbox and PlayStation, right? Yeah. But the Switch is pretty good, too, I think. I would agree. All right. Well, we'll be back in a moment. Hello world. This is CS52D lecture 5.
7:27 Today we're going to be looking at The Legend of Zelda, which is a favorite franchise of mine amidst Mario. I like Mario quite a bit as well, but this was one of the first series that I have
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, CS50.