p5.js v1.9
🎮 Game Starters
game.js Saved
CANVAS VIEWPORT
X: 0, Y: 0 60 FPS
Canvas Pos:
x: 0, y: 0
⚠️
Oops! Something went wrong
ReferenceError: ...
💡 Hint: Check spelling of functions like ellipse(), rect(), or background()

Create 2D Web Games with JavaScript and p5.js

Code 2D Web Games lets you create and modify browser games using JavaScript and p5.js. Start with a template, edit its code, and test the results in a playable preview.

Games use a repeating loop to read player input, update positions, check collisions, and draw each frame. Changing these instructions helps you explore movement, scoring, difficulty, and game rules.

How to Use This App

  • Choose a starter template from the templates panel and confirm that it can replace your current code.
  • Edit the JavaScript to change colors, movement, scoring, or game rules.
  • Enable Live Feedback to rerun the preview after edits, or press Ctrl+Enter to run your code.
  • Test your game in the preview using the keyboard or mouse controls defined in its code.
  • Insert code snippets or sound effects from the reference and sound panels.
  • Check console messages and error hints to troubleshoot problems.
  • Export your game as an HTML file or download its JavaScript source.

Examples and Use Cases

  • Adjust a brick breaker game: Open Neon Brick Breaker and change the paddle width or ball speed. Play each version to compare how these settings affect difficulty.
  • Experiment with jumping physics: Use Pixel Coin Runner to modify gravity and jump strength. Test whether the player can still clear pits and collect coins.
  • Customize an arcade challenge: Edit Starship Asteroid Dodger to change asteroid movement or scoring. Test the ship with WASD or arrow keys and fire lasers with Space.
  • Explore interactive animation: Load Neon Particle Trails and change particle colors, sizes, or lifetimes. Move the mouse and click to observe the resulting trails and bursts.
  • Share a coding project: Customize Cyber Snake 2D, then export the game as HTML. Send the file to someone to open in a browser with internet access for the required libraries.

Helpful Details

Understand Movement and Gravity

A simple movement rule is position = position + velocity. For jumping, vertical velocity changes with gravity on each update. In the usual p5.js canvas coordinates, positive vertical movement goes downward, so a jump typically starts with a negative vertical velocity.

Change one value at a time and test the result. Stronger gravity shortens airtime, while a larger upward starting speed can increase jump height.

Debug Small Changes

  • JavaScript names are case-sensitive. A misspelled variable or function can stop the game.
  • Check matching parentheses, brackets, and quotation marks when a syntax error appears.
  • Live Feedback reruns the project after edits, which resets in-memory gameplay progress. Disable it when making several related changes before testing.
  • Insert snippets inside the appropriate function or condition. A sound call placed directly in the drawing loop may repeat every frame.

Keep a Downloaded Backup

Browser saves belong to the current browser and site. Clearing site data or switching browsers can make saved code unavailable. Download your JavaScript before replacing a template or making major changes.

An exported HTML game still loads p5.js from an external source, so it is not a fully offline package. Keep internet access available when opening or sharing it.

Frequently Asked Questions

Do I need coding experience to use this app?

Basic JavaScript knowledge helps, but you can begin by modifying a starter template. Try changing colors or speed values, then use the built-in reference to explore p5.js functions.

What kinds of games can I create?

You can build 2D browser games and interactive animations using JavaScript and p5.js. Starter templates include an asteroid dodger, brick breaker, coin runner, Snake, particle trails, and a bouncing physics sandbox.

Why does my game restart when I edit the code?

Live Feedback reruns your project after code changes, resetting its current gameplay state. Disable Live Feedback and run the code manually when you are ready to test several changes together.

Is my code saved between visits?

The app supports saving code in your current browser’s local storage. These saves do not sync across browsers or devices and can be lost if you clear site data. Download your JavaScript source as a backup.

Can I download and share my game?

Yes. Export your game as an HTML file to share or download its JavaScript source for further editing. The exported HTML loads p5.js externally, so internet access is needed to load that library.

How can I troubleshoot a game that does not run?

Check the console and error hints for clues. Look for misspelled names, missing brackets, or misplaced code snippets. JavaScript is case-sensitive, so variable and function names must match exactly.