Ever wanted to create your own video game but thought it was too hard? The good news is you donโt need a game engineโjust JavaScript, HTML, and CSS!
In this beginner-friendly guide, weโll show you how to make a simple 2D game using JavaScript in just a few steps. By the end, youโll have a working game you can play in your browser! ๐
๐ What Youโll Learn:
โ HTML & CSS โ To create the game screen ๐จ
โ JavaScript โ To control game logic & movement ๐ฎ
โ Collision Detection โ To track when objects hit each other ๐
โ Game Loop โ To keep the game running smoothly โณ
Letโs get started! ๐
1๏ธโฃ Setting Up Your Project ๐
Create a new project folder, e.g., my-game/, and inside it, create these three files:
perl ----- my-game/ โโโ index.html (Game structure) โโโ style.css (Game design) โโโ script.js (Game logic)
Now open your code editor (VS Code, Sublime, etc.) and letโs begin! ๐ ๏ธ
2๏ธโฃ Create the Game Screen (HTML) ๐๏ธ
Inside index.html, add this:
html
-----
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple JS Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>My Simple JavaScript Game ๐ฎ</h1>
<canvas id="gameCanvas"></canvas>
<script src="script.js"></script>
</body>
</html>
โ๏ธ canvas is where the game will be drawn.
โ๏ธ script.js will handle game logic.
3๏ธโฃ Style the Game Screen (CSS) ๐จ
Inside style.css, add:
css
-----
body {
text-align: center;
font-family: Arial, sans-serif;
background-color: #222;
color: white;
}
canvas {
background: black;
display: block;
margin: 0 auto;
border: 2px solid white;
}
โ๏ธ Dark background to make the game look cool.
โ๏ธ White canvas border for a retro style.
๐น Now, open index.html in your browserโyou should see an empty canvas!
4๏ธโฃ Draw the Game Objects (JavaScript) ๐ฎ
Inside script.js, start by selecting the canvas and setting up the game loop:
js
-----
// Select the canvas
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Set canvas size
canvas.width = 500;
canvas.height = 300;
// Player (rectangle)
const player = {
x: 50,
y: 130,
width: 30,
height: 30,
color: "red",
speed: 5
};
// Enemy (moving object)
const enemy = {
x: 450,
y: 130,
width: 30,
height: 30,
color: "blue",
speed: 3
};
// Draw game objects
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear canvas
// Draw player
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
// Draw enemy
ctx.fillStyle = enemy.color;
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
}
// Update enemy position
function update() {
enemy.x -= enemy.speed; // Move enemy to the left
// Reset enemy when off-screen
if (enemy.x < -30) {
enemy.x = 500;
}
}
// Game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop); // Keeps the game running
}
// Start the game
gameLoop();
โ๏ธ Red player box stays still.
โ๏ธ Blue enemy box moves left and resets when off-screen.
Now refresh your browserโyou should see the enemy moving! ๐
5๏ธโฃ Move the Player with Keyboard Controls โจ๏ธ
Modify script.js to move the player with arrow keys:
js
-----
// Listen for key presses
document.addEventListener("keydown", movePlayer);
function movePlayer(event) {
if (event.key === "ArrowUp" && player.y > 0) {
player.y -= player.speed; // Move up
}
if (event.key === "ArrowDown" && player.y < canvas.height - player.height) {
player.y += player.speed; // Move down
}
}
โ๏ธ Arrow Up โฌ๏ธ moves the player up.
โ๏ธ Arrow Down โฌ๏ธ moves the player down.
๐น Test it! Press up/down arrows and see your red box move! ๐
6๏ธโฃ Add Collision Detection (Game Over!) ๐จ
Now, letโs detect when the player collides with the enemy and stop the game:
js
-----
// Check for collision
function checkCollision() {
if (
player.x < enemy.x + enemy.width &&
player.x + player.width > enemy.x &&
player.y < enemy.y + enemy.height &&
player.y + player.height > enemy.y
) {
alert("Game Over! ๐ต");
document.location.reload(); // Reload the game
}
}
// Modify game loop to check for collisions
function gameLoop() {
update();
draw();
checkCollision(); // Check if player hits enemy
requestAnimationFrame(gameLoop);
}
โ๏ธ If the player touches the enemy, the game ends with a “Game Over” alert.
7๏ธโฃ Winning Condition (Survive for 30 Seconds!) โณ
Letโs add a win condition:
js
-----
let timeSurvived = 0;
// Timer function
setInterval(() => {
timeSurvived++;
if (timeSurvived === 30) {
alert("You Win! ๐");
document.location.reload();
}
}, 1000);
โ๏ธ Survive for 30 seconds, and you win!
๐ฅ Bonus: Add Sound Effects & Background Music ๐ต
๐น Add a Jump Sound
1๏ธโฃ Download a jump sound (jump.mp3) and save it in your folder.
2๏ธโฃ Modify script.js:
js
-----
const jumpSound = new Audio("jump.mp3");
// Play sound when jumping
function movePlayer(event) {
if (event.key === "ArrowUp") {
jumpSound.play();
player.y -= player.speed;
}
}
โ๏ธ Now the game plays a sound when jumping! ๐ต
๐ Final Step: Host Your Game Online! ๐
To share your game with friends, deploy it using GitHub Pages (Free & Easy!).
๐น Steps to Host on GitHub Pages
1๏ธโฃ Upload your project to GitHub.
2๏ธโฃ Go to Settings โ Pages โ Select “main” branch โ Save.
3๏ธโฃ Your game is live at:
perl ----- https://yourusername.github.io/my-game/
๐ Now anyone can play your game online!
๐ฏ Final Thoughts: Youโre a Game Developer Now! ๐ฎ
You just built a working JavaScript game from scratch! ๐
๐ก What You Learned:
โ๏ธ Canvas API โ To draw objects on the screen.
โ๏ธ Game Loop โ To keep the game running smoothly.
โ๏ธ Keyboard Controls โ To move the player.
โ๏ธ Collision Detection โ To detect hits & game over.
โ๏ธ Hosting Your Game Online!
Now, try adding your own features: more enemies, different levels, or power-ups!

