Learn to Code with Replit: A Beginner’s Guide to Creating a Simple Game in JavaScript using p5.js

code space invaders game in JS

Introduction

Replit is an online integrated development environment (IDE) that allows users to code, build, and run programs directly in their web browsers. It supports a variety of programming languages, including JavaScript, and offers numerous libraries, such as p5.js, for creating interactive applications. In this tutorial, we will walk you through the process of using Replit to learn coding and create a simple game in JavaScript using the p5.js library.

Getting Started with Replit

  1. Navigate to https://replit.com/ and sign up for a free account, or log in if you already have one.
  2. Once logged in, click on the “+ New Repl” button in the top right corner of the screen.
  3. In the “Select a Language” dropdown, choose “JavaScript”. You can leave the default name for your Repl or change it to something more descriptive, like “SimpleGame”. Click “Create Repl” to proceed.

Creating a Simple Game with p5.js in Replit

For this tutorial, we will create a simple game in which a player-controlled circle moves around the screen, trying to avoid randomly placed obstacles. We will use the p5.js library to handle graphics and user input.

  1. Add the p5.js library to your Repl by clicking on the “Add Package” button in the left sidebar (it looks like a cube). Type “p5” in the search bar and click on the “p5” package to install it.
  2. In the index.html file, add the following code between the <head> tags to link the p5.js library:
” style=”color:#d8dee9ff;display:none” aria-label=”Copy” class=”code-block-pro-copy-button”>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

Now, switch to the script.js file to start coding. First, let’s create the basic structure of a p5.js sketch with the setup() and draw() functions:

function setup() {
  createCanvas(800, 600);
}

function draw() {
  background(220);
}

The setup() function is called once when the program starts, and the draw() function is called repeatedly to update the screen. In this example, we create an 800×600 canvas and set the background color to a light gray.

  1. Next, let’s add the player-controlled circle. We’ll use the ellipse() function to draw the circle and the keyIsDown() function to handle user input:
let playerX = 400;
let playerY = 300;
let playerSpeed = 5;

function setup() {
  createCanvas(800, 600);
}

function draw() {
  background(220);

  // Move the player based on user input
  if (keyIsDown(LEFT_ARROW)) playerX -= playerSpeed;
  if (keyIsDown(RIGHT_ARROW)) playerX += playerSpeed;
  if (keyIsDown(UP_ARROW)) playerY -= playerSpeed;
  if (keyIsDown(DOWN_ARROW)) playerY += playerSpeed;

  // Draw the player circle
  ellipse(playerX, playerY, 50, 50);
}

Now, when you press the arrow keys, the circle should move around the screen.

  1. Finally, let’s add some randomly placed obstacles. We’ll create an array of obstacle objects and draw them using the rect() function:
let playerX = 400;
let playerY = 300;
let playerSpeed = 5;

let obstacles = [];

function setup() {
  createCanvas(800, 600);

// Generate random obstacles
for (let i = 0; i < 10; i++) {
obstacles.push({
x: random(width),
y: random(height),
size: 30
});
}
}

function draw() {
background(220);

// Move the player based on user input
if (keyIsDown(LEFT_ARROW)) playerX -= playerSpeed;
if (keyIsDown(RIGHT_ARROW)) playerX += playerSpeed;
if (keyIsDown(UP_ARROW)) playerY -= playerSpeed;
if (keyIsDown(DOWN_ARROW)) playerY += playerSpeed;

// Draw the player circle
ellipse(playerX, playerY, 50, 50);

// Draw the obstacles
for (let i = 0; i < obstacles.length; i++) {
rect(obstacles[i].x, obstacles[i].y, obstacles[i].size, obstacles[i].size);
}
}

In this code, we generate an array of 10 obstacle objects with random positions on the canvas. Then, we draw them as rectangles in the `draw()` function. Conclusion You’ve now created a simple game using JavaScript and the p5.js library in Replit! This tutorial serves as a starting point for learning to code and creating more complex projects. You can build on this example by adding features such as collision detection, scoring, or more advanced game mechanics. Replit’s easy-to-use interface and extensive support for various programming languages make it an excellent tool for learning to code and experimenting with new ideas. Happy coding!

SHARE WITH FRIENDS >

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Top Live Virtual Math Enrichment Classes and Small Groups for Minneapolis Teens (STEM Track & Competition Prep)

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

How to choose an Ivy League or top‑STEM‑university live virtual math tutor in Minneapolis

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Evening and Weekend Live Virtual Math Tutoring: After‑School Scheduling Tips for Minneapolis Parents

Live Virtual Math Tutoring & Enrichment, Minneapolis

20 Apr 2026

Can live virtual tutoring improve AP Calculus & AP Statistics scores? A Minneapolis parent’s guide

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

Affordable Virtual Math Tutoring Options for Busy Tampa Families (Evenings & Weekends)

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

Summer Virtual Coding Camps for Tampa Bay Kids: Dates, Ages, and What They Build

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

How Live Virtual Small-Group Classes Improve Math Confidence: Evidence, Metrics, and Tampa Parent Stories

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

What Tampa Parents Should Ask About Instructor Credentials, Class Size, and Outcomes

Live Virtual Coding & Math Enrichment (K–12), Tampa

20 Apr 2026

Project-Based Coding for Kids: Portfolio Projects That Help Tampa Students Stand Out for High School & College