Create HTML5 Games With Canvas_ Comprehensive Guide
Create HTML5 Games With Canvas_ Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Game Mechanics 4
1. Keyboard Input 4
2. Collision Detection 4
3. Paddle Movement 4
Complete Game Example: Paddle and Ball Game 5
Exercises 7
Exercise 1: Add Score Tracking 7
Solution: 7
Multiple-Choice Questions 8
Question 1: 8
Question 2: 8
Question 3: 8
Best Practices for Canvas Games 9
The HTML5 <canvas> element provides a powerful way to create 2D and 3D games directly in
the browser. This guide will help you build interactive HTML5 games using the Canvas API, with
detailed examples, exercises, and quiz questions.
The <canvas> element is a container for graphics that allows you to draw shapes, text, images,
and animations using JavaScript.
1. Drawing Shapes:
○ Rectangles: fillRect, strokeRect, clearRect
○ Paths: beginPath, moveTo, lineTo, arc
2. Drawing Text:
○ fillText, strokeText
3. Working with Images:
○ drawImage
4. Animation:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
○ requestAnimationFrame
1. HTML Structure
2. JavaScript Code
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
ballSpeedY = -ballSpeedY;
}
requestAnimationFrame(draw); // Call the next frame
}
// Start the game
draw();
Game Mechanics
1. Keyboard Input
document.addEventListener("keydown", handleKeyDown);
function handleKeyDown(event) {
if (event.key === "ArrowUp") {
console.log("Move Up");
} else if (event.key === "ArrowDown") {
console.log("Move Down");
}
}
2. Collision Detection
3. Paddle Movement
let paddleX = canvas.width / 2 - 50;
const paddleWidth = 100;
const paddleHeight = 10;
document.addEventListener("mousemove", (event) => {
const rect = canvas.getBoundingClientRect();
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
paddleX = event.clientX - rect.left - paddleWidth / 2;
});
function drawPaddle() {
ctx.fillStyle = "black";
ctx.fillRect(paddleX, canvas.height - paddleHeight, paddleWidth,
paddleHeight);
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
ctx.beginPath();
ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = "blue";
ctx.fill();
ctx.closePath();
}
// Main game loop
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
// Move the ball
ballX += ballSpeedX;
ballY += ballSpeedY;
// Ball collision with walls
if (ballX + ballRadius > canvas.width || ballX - ballRadius < 0) {
ballSpeedX = -ballSpeedX;
}
if (ballY - ballRadius < 0) {
ballSpeedY = -ballSpeedY;
}
// Ball collision with paddle
if (
ballY + ballRadius > canvas.height - paddleHeight &&
ballX > paddleX &&
ballX < paddleX + paddleWidth
) {
ballSpeedY = -ballSpeedY;
}
// Game over
if (ballY + ballRadius > canvas.height) {
alert("Game Over!");
document.location.reload();
}
requestAnimationFrame(draw);
}
draw();
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
</script>
Exercises
Modify the paddle and ball game to track and display the player's score.
Solution:
let score = 0;
function updateScore() {
score++;
}
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "black";
ctx.fillText("Score: " + score, 8, 20);
}
// Update the draw function
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
drawScore();
// Ball collision with paddle
if (
ballY + ballRadius > canvas.height - paddleHeight &&
ballX > paddleX &&
ballX < paddleX + paddleWidth
) {
ballSpeedY = -ballSpeedY;
updateScore();
}
// Ball collision with walls
if (ballX + ballRadius > canvas.width || ballX - ballRadius < 0) {
ballSpeedX = -ballSpeedX;
}
if (ballY - ballRadius < 0) {
ballSpeedY = -ballSpeedY;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
}
if (ballY + ballRadius > canvas.height) {
alert("Game Over! Your Score: " + score);
document.location.reload();
}
ballX += ballSpeedX;
ballY += ballSpeedY;
requestAnimationFrame(draw);
}
Multiple-Choice Questions
Question 1:
1. fillRectangle
2. drawRect
3. fillRect
4. rectFill
Answer: 3. fillRect
Question 2:
Question 3:
1. setInterval
2. setTimeout
3. requestAnimationFrame
4. drawLoop
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
8
Answer: 3. requestAnimationFrame
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis