Summary of "JavaScript Fighting Game Tutorial with HTML Canvas"
Video Tutorial Overview
The video tutorial, hosted by Chris, provides a comprehensive step-by-step guide on creating a 2D JavaScript fighting game using the HTML Canvas API. It covers fundamental game development concepts, mechanics, animations, and deployment.
Below is a detailed summary of the technological concepts, product features, and tutorial elements presented.
1. Project Setup and Basics
- Setting up the project folder structure with
index.html,index.js, and asset folders. - Creating an HTML
<canvas>element and linking JavaScript files. - Selecting the canvas and its 2D drawing context in JavaScript.
- Resizing the canvas to 1024x576 pixels (16:9 ratio) for optimal display.
- Drawing a background rectangle on the canvas to differentiate it visually.
- Using object-oriented programming (OOP) to create a
Spriteclass as a blueprint for game objects. - Instantiating player and enemy as rectangle sprites with position, width, height, color, and velocity.
- Implementing an animation loop using
requestAnimationFrameto update and render frames continuously. - Adding gravity and velocity to simulate falling and jumping mechanics.
- Clearing the canvas each frame to prevent paint-like effects.
2. Player and Enemy Movement
- Handling keyboard input with event listeners for
keydownandkeyup. - Using a
keysobject to track pressed keys and improve movement accuracy. - Managing simultaneous key presses and last key pressed to avoid movement conflicts.
- Implementing left/right movement (A/D for player, Arrow keys for enemy) and jumping (W for player, Arrow Up for enemy).
- Velocity-based position updates for smooth movement.
- Adjustable movement speed and gravity for better game feel.
3. Attack Mechanics and Collision Detection
- Creating an attack box as a rectangle offset relative to the player/enemy to represent attack range.
- Drawing the attack box only during attack animation frames.
- Detecting rectangular collision between attack box and opponent using a reusable
rectangularCollisionfunction. - Adding an
isAttackingflag andattack()method to manage attack timing with a short duration usingsetTimeout. - Activating attacks with keyboard inputs (Space for player, Arrow Down for enemy).
- Handling attack success and misses to correctly update health.
4. Health Bars and UI Interface
- Building health bars and timer UI elements using HTML
<div>s styled with inline CSS. - Using CSS Flexbox to layout health bars and timer horizontally and vertically centered.
- Applying padding, borders, and colors to health bars for visual clarity.
- Adding a timer that counts down from a set value (e.g., 10 or 60 seconds) using
setTimeoutloops. - Displaying game result text (“Tie”, “Player 1 Wins”, “Player 2 Wins”) overlayed on the canvas.
- Stopping the timer and disabling controls when a player dies or timer runs out.
- Using CSS
box-sizing: border-boxglobally to handle padding and width correctly. - Changing font to an 8-bit style using Google Fonts (“Press Start 2P”).
5. Sprites and Animation
- Organizing assets into folders (background, shop, characters).
- Creating a
Spriteclass for static and animated images with properties like position, scale, frames, and frame control. - Loading images dynamically and drawing them on the canvas with cropping for sprite sheets.
- Implementing frame-based animation with frame elapsed counters and frame hold to control animation speed.
- Refactoring the
Spriteclass and creating aFighterclass that extendsSpriteto handle player/enemy-specific logic (velocity, attack boxes, health, animations). - Handling multiple sprite states for characters: idle, run, jump, fall, attack, take hit, death.
- Switching between sprites based on player state (movement keys, velocity, attack status).
- Resetting animation frames on sprite switch to prevent flickering.
- Managing attack animation to only allow switching after animation completes.
- Implementing hit reaction animations and death animations that lock the player/enemy state.
- Preventing movement and input after death.
- Adding a semi-transparent white overlay on the canvas to improve player visibility over backgrounds.
- Using GSAP (GreenSock Animation Platform) library to animate health bar width changes smoothly.
6. Deployment
- Using Git and GitHub for version control and remote repository hosting.
- Creating a GitHub repository and pushing the project code via terminal commands.
- Deploying the game live on the web using Netlify by connecting the GitHub repository.
- Customizing the Netlify site URL for easier access.
- The game is now accessible online for players to play from anywhere.
Main Speakers and Sources
- Chris (Host/Instructor): Leads the entire tutorial, explains concepts, writes code live, and guides through setup, coding, animation, and deployment.
- Assets and Libraries:
- Game assets from itch.io (free game art packs by authors like Brew Love, Lewis Mello).
- Google Fonts for 8-bit font styling.
- GSAP (GreenSock Animation Platform) for smooth UI animations.
- Code snippets and examples provided live during the tutorial.
Key Takeaways
- Building a fighting game from scratch involves managing canvas rendering, game loops, physics (gravity/velocity), input handling, collision detection, and sprite animation.
- Object-oriented programming helps organize game entities with reusable classes and methods.
- Sprite sheet animations require careful frame management and cropping.
- UI elements like health bars and timers can be created with HTML/CSS and animated with JavaScript libraries.
- Deployment on GitHub and Netlify makes the game accessible online for free.
- The tutorial emphasizes clean, maintainable code with modularization (separating classes and utility functions).
This tutorial is an excellent resource for beginners to intermediate developers interested in 2D game development with JavaScript and HTML Canvas, covering both programming fundamentals and practical deployment.
Category
Technology