How to Make Your Own Simple Video Game in JavaScript ๐ŸŽฎ๐Ÿš€

Taylor Emma
7 Min Read
Disclosure: This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!

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!

Share This Article
A senior editor for The Mars that left the company to join the team of SenseCentral as a news editor and content creator. An artist by nature who enjoys video games, guitars, action figures, cooking, painting, drawing and good music.