To Do This Week:
With ChatGPT, explore ideas for your HTML5 Canvas game.
Canvas Project DUE March 26 (this Wednesday)
HTML Game Project DUE Wednesday April 9th
Module Notes
View Canvas-Game List
HTML5 Games
Explore the W3schools HTML5 Games Demo to see how JavaScript can build a game using the Canvas element.
Here is the example refactored for ES6: Dodging Obstacles Game
Understand how objects are generated and then animated with speed and gravity properties. Experiment with values.
Previous Student Canvas Games:
Other Resources:
- Web Storage
- Drag and Drop
- JavaScript Progress Bar
- Custom Range Sliders
- @Frankslaboratory – Canvas Game Tutorials
Game Assets for Knight Runner:
- UI Elements: OwlishMedia
- Background Layers: Edermuniz
- Player/Enemy Sprites:
JS Files for Knight Runner:
In-Class Exercise:
Review the HTML5 Game in ES6
Explore this games with falling balls using keyboard functionality. Experiment with values—what can you change?
Generate quick Canvas game templates with ChatGPT.
Adding Images and Sound Effects
To add images, first load the image in the Image() object, then draw it with drawImage()
.
const playerImg = new Image(); playerImg.src = 'player.png'; ctx.drawImage(playerImg, this.x, this.y, this.width, this.height);
To add sound effects, load the audio file in the Audio() object and then use play()
when needed. Setting current time to zero helps when do fast repetitions like shooting.
const shootSound = new Audio('shoot.wav'); shootSound.currentTime = 0; // rewind to start shootSound.play();
Popular Games refactored to HTML5/ES6 with classes
Pong Logic Steps:
Set up the canvas: Get access to the canvas element and its drawing context so we can draw shapes like paddles and the ball.
Create Paddle
class:
-
Each paddle has a position (
x
,y
), size (width
,height
), and speed. -
Responds to move up and down based on player input (
w
,s
, arrow keys) , and draws itself on the screen with each frame.
Create Ball
class:
-
The ball has a position, size, and speed in both X and Y directions.
-
It knows how to move, bounce off the top/bottom, detect paddle collisions, and reset after a score. When bouncing off a side, the X direction is reversed (i.e.
ballSpeedX *= -1
). Y stays the same.
Track game state:
-
Keep score for Player 1 and Player 2.
-
Listen for key presses (
w
,s
, arrow keys) to move the paddles. -
Use booleans to store which keys are currently held down.
Game loop (updateGame
):
-
Clear the canvas each frame.
-
Update paddle positions based on key input.
-
Move the ball and check for collisions or scoring.
-
Draw paddles, ball, and score.
-
Use
requestAnimationFrame()
to keep the game running smoothly.
Asteroids Logic Steps:
Set up the canvas: Same as Pong – get the drawing surface ready so we can show the ship, bullets, and asteroids.
Create Ship
class:
-
The ship has a position, direction (angle), thrust (speed), and can rotate.
-
It knows how to move with physics (inertia), apply thrust, rotate, shoot bullets, and draw itself as a triangle. The ship’s direction is controlled by an angle that tells us which way it’s pointing. When you rotate the ship with the arrow keys, you’re changing that angle – for thrust forward and shooting. The interia is set with key up:
thrust.x *= 0.99
which will incrementally decrease thrust with each frame. -
The ship also wraps around the screen if it goes off the edges. If the ship or asteroid goes off the left edge (
x < 0
), we set itsx
position to the right edge (canvas.width
). If it goes off the right edge (x > canvas.width
), we move it tox = 0
. We do the same for the top and bottom using they
value andcanvas.height
Create Bullet
class:
-
Each bullet starts at the ship’s position and moves in the direction the ship was pointing.
-
It has a limited lifespan and disappears after a while or when it leaves the screen.
-
It knows how to move and draw itself.
Create Asteroid
class:
-
Asteroids have random positions and velocities.
-
They move continuously and wrap around the screen.
-
They know how to check if they’ve been hit by a bullet.
Create Game
class:
-
The game creates the ship and an initial batch of asteroids.
-
It tracks which keys are being pressed.
-
It runs the game loop (update and draw functions).
-
Handle player input: arrow keys rotate the ship and apply thrust and shooting.
-
Update ship, bullets, and asteroids.
-
Check for collisions between bullets and asteroids.
-
Remove bullets and asteroids when collisions occur.
-
In a loop: draw everything on the screen and call
requestAnimationFrame()
to keep looping the game smoothly.
HTML5 Canvas Game: 15%
DUE: Wednesday April 9th
For this project, create an interactive HTML5 canvas game using JavaScript for engaging gameplay. Consult ChatGPT for brainstorming, planning, coding assistance and troubleshooting. Follow these guidelines:
Your Steps:
- Game Concept Brainstorm: What kind of game is it? (e.g., avoid falling objects, jump over obstacles, collect coins)
- Game Design: Define basic mechanics, scoring system etc.
- Basic Features: List the key features and write them in simple, human terms: objective, rules, controls, levels
- Consult with ChatGPT: Use ChatGPT for refining your design and solving technical challenges.
- Logic Steps: work out the basic steps in human language for the game set up, functionality and scoring.
- Script the Game: Begin coding or work with ChatGPT for the coding in ES6 JavaScript.
- Testing and Debugging: Ensure functionality and address balance and interaction issues.
- CSS Styling and UI Design: Style your game’s interface to enhance usability and aesthetics.
- Add AI Media: Generate sound effects, images, or graphic elements.
- Integration Statement: Document logic steps and how you used ChatGPT and add this as a commented section in the <script>.
- Final Submission: Upload files to a folder named “game.” Validate and test functionality on multiple browsers.
Suggested Game Ideas:
- Platformer with Dynamic Environments: Procedurally generate levels for a unique experience each time.
- Educational Games: Teach concepts like math or history interactively.
- Adventure Game with Expressive Characters: Create animated, expressive interactions.
- Physics-Based Puzzle Game: Manipulate objects with gravity and friction to solve challenges.
- Dodging Objects Game: Navigate through increasing challenges while dodging obstacles.
- Maze Runner Game: Design complex, dynamically generated mazes.
- Physics-Based Destruction Game: Destroy structures strategically, using p5.js for realistic effects.
- Escape Game with Environmental Puzzles: Solve puzzles to escape rooms with interactive elements.
Grading Criteria:
- Effort and execution of JavaScript steps
- Process of creative discovery
- Game usability and instructions
- UX design and presentation
Project Requirements:
- Title and author name visible
- Well-commented code
- Statement in the <script> section with logic steps, resources and ChatGPT usage
- Uploaded to a server as “index.html” inside a folder named “game”