Use a javascript game engine
Green highlight

Create a simple browser game using a JavaScript game engine like Phaser, learning basic coding, sprites, keyboard controls, and simple game logic.

Orange shooting star
Download Guide
Collect Badge
grey blob
Challenge Image
Table of contents

Step-by-step guide to create a simple browser game using a JavaScript game engine

Here at SafeTube, we're on a mission to create a safer and more delightful internet. 😊

Easy JavaScript Game Tutorial

What you need
Adult supervision required, internet access, text editor or online code editor, two small sprite images named player.png and star.png or a simple drawing tool to make them

Step 1

Create a new project in your editor and make two files named index.html and game.js so your game has a web page and a script file.

Step 2

Open index.html and paste this script tag inside the head to add Phaser to your page:

Step 3

In index.html add this script tag just before the closing tag to load your game code:

Step 4

Open game.js and paste this game skeleton to create a Phaser game with empty scene functions: const config = { type: Phaser.AUTO, width: 800, height: 600, physics: { default: 'arcade', arcade: { gravity: { y: 0 } } }, scene: { preload: preload, create: create, update: update } }; let player; let stars; let cursors; let score = 0; let scoreText; new Phaser.Game(config);

Step 5

Inside game.js add this preload code to load your images by filename so Phaser can use them: function preload() { this.load.image('player','player.png'); this.load.image('star','star.png'); }

Step 6

In the create function add the player sprite and give it arcade physics so it can move and collide with world bounds: function create() { player = this.physics.add.sprite(400,300,'player'); player.setCollideWorldBounds(true); }

Step 7

Still in create add a group of collectible stars and make them bounce so they look fun: stars = this.physics.add.group({ key: 'star', repeat: 7, setXY: { x: 50, y: 50, stepX: 90 } }); stars.children.iterate(function(child){ child.setBounceY(Phaser.Math.FloatBetween(0.4,0.8)); });

Step 8

In create add keyboard controls so the player can move using arrow keys: cursors = this.input.keyboard.createCursorKeys();

Step 9

In create add a score text on the screen to show points: scoreText = this.add.text(16,16,'Score: 0',{ fontSize: '20px', fill: '#fff' });

Step 10

Still in create tell Phaser to check when the player overlaps a star and call a collect function: this.physics.add.overlap(player, stars, collectStar, null, this);

Step 11

Add the collectStar function to disable the collected star increase the score and update the score text so collecting works: function collectStar(player, star) { star.disableBody(true,true); score += 10; scoreText.setText('Score: ' + score); }

Step 12

Add the update function to read the arrow keys each frame and set the player's velocity so the sprite moves smoothly: function update() { player.setVelocity(0); if (cursors.left.isDown) { player.setVelocityX(-200); } else if (cursors.right.isDown) { player.setVelocityX(200); } if (cursors.up.isDown) { player.setVelocityY(-200); } else if (cursors.down.isDown) { player.setVelocityY(200); } }

Step 13

Run or preview index.html in your browser to test the game and fix small issues like image file names or sizes until the player moves and stars disappear when touched.

Step 14

Share a screenshot or short description of your finished game and what you learned by posting your project on DIY.org.

Final steps

You're almost there! Complete all the steps, bring your creation to life, post it, and conquer the challenge!

Complete & Share
Challenge badge placeholder

Help!?

What can I use if I can't access the Phaser CDN or don't have player.png and star.png?

Download phaser.min.js into your project and change the head tag to <script src='phaser.min.js'></script>, and either rename local image files to player.png and star.png or create temporary sprites with this.add.rectangle in the create function.

Why does the player sometimes not move or stars not disappear when I touch them?

Check the browser console for 404 or runtime errors, confirm preload lists 'player' and 'star' with the exact filenames you placed in the project, ensure create uses this.physics.add.sprite and this.physics.add.group so physics bodies exist, and verify update reads cursors (cursors = this.input.keyboard.createCursorKeys()) and the overlap uses collectStar.

How can I adapt the activity for younger or older children?

For younger kids, reduce stars (set repeat: 3), use larger player/star images and slower velocities (setVelocityX/Y to ±100), and for older kids add enemies, timers, higher speeds, more stars, or extend collectStar to track combos and levels.

What are simple ways to extend or personalize the finished game?

Add sound by using this.load.audio in preload and this.sound.play in collectStar, create player animations with this.load.spritesheet and this.anims.create, or change collectStar to respawn stars with star.enableBody(true, x, y, true, true) and tweak scoreText formatting.

Watch videos on how to create a simple browser game using a JavaScript game engine

Here at SafeTube, we're on a mission to create a safer and more delightful internet. 😊

The Easiest Javascript Game Ever

4 Videos

Facts about JavaScript game development for kids

⌨️ Keyboard controls use keydown and keyup events so characters can move smoothly while a key is held down.

⚡ Phaser is an open-source 2D HTML5 game framework first released in 2013 and loved for quick prototyping.

🕹️ JavaScript runs in every modern web browser, so players can play browser games without installing anything.

🧩 Sprites are packed images or frames; sprite sheets can hold dozens of animations in a single file to speed up games.

🎮 With a game engine, simple playable games like platformers or shooters can often be made in just a few hours.

How do I create a simple browser game using Phaser?

To create a simple browser game with Phaser, set up a basic HTML file and include Phaser via CDN or npm. Create a game configuration and Scene class, preload sprite images, then create sprites and add keyboard input listeners. Implement the update loop to move sprites and check simple collisions or scoring. Test in a browser and iterate—start with one player, one enemy, and a scoring system to keep it simple.

What materials and software do I need to build a Phaser game?

Materials you'll need include a computer with a modern web browser (Chrome/Firefox), a code editor like VS Code, and Phaser library (use CDN or npm). Get simple sprite images or draw them in free editors, and use a local web server (Live Server or python -m http.server) to run files. Optionally, install Node.js for build tools, Git for version control, and use online tutorials or starter templates.

What ages are suitable for making a simple browser game?

This activity suits kids aged about 8 and up. Ages 8–12 typically benefit from guided lessons and visual examples; older children and teens (13+) can work independently on more complex features. Younger kids (5–7) can participate with an adult pair-programming them to drag sprites, pick colors, and test ideas. Adjust complexity, pair programming, and time limits based on attention span and prior coding experience.

What are the benefits, safety tips, and variations for this activity?

Making a simple Phaser game builds problem-solving, sequencing, and creativity—kids learn sprites, events, and basic logic. It also introduces debugging and persistence. For safety, limit screen time, use trusted sprite sources or create original art, and supervise downloads or npm installs. Variation ideas: change genre (platformer, shooter, puzzle), add levels, or let kids design characters and rules to keep the activity fresh and age-appropriate.

Get 7 days of DIY for FREE!

Use a javascript game engine. Activities for Kids.