- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
Creating games with JavaScript is a great way to learn and practice programming. Below is an example of a simple Snake Game implemented in JavaScript.
Example: Snake Game
// Initialize the canvas and contextconst canvas = document.getElementById("gameCanvas");const ctx = canvas.getContext("2d");// Set up the game variableslet snake = [{ x: 200, y: 200 }];let direction = "RIGHT";let food = { x: Math.floor(Math.random() * 20) * 20, y: Math.floor(Math.random() * 20) * 20 };let score = 0;// Function to draw the snakefunction drawSnake() {ctx.clearRect(0, 0, canvas.width, canvas.height);snake.forEach(part => {ctx.fillStyle = "green";ctx.fillRect(part.x, part.y, 20, 20);});}// Function to move the snakefunction moveSnake() {const head = { x: snake[0].x, y: snake[0].y };if (direction === "RIGHT") head.x += 20;if (direction === "LEFT") head.x -= 20;if (direction === "UP") head.y -= 20;if (direction === "DOWN") head.y += 20;snake.unshift(head);if (head.x === food.x && head.y === food.y) {score += 10;food = { x: Math.floor(Math.random() * 20) * 20, y: Math.floor(Math.random() * 20) * 20 };} else {snake.pop();}}// Function to draw the foodfunction drawFood() {ctx.fillStyle = "red";ctx.fillRect(food.x, food.y, 20, 20);}// Function to update the game statefunction updateGame() {moveSnake();drawSnake();drawFood();}// Event listener for key pressesdocument.addEventListener("keydown", event => {if (event.key === "ArrowRight" && direction !== "LEFT") direction = "RIGHT";if (event.key === "ArrowLeft" && direction !== "RIGHT") direction = "LEFT";if (event.key === "ArrowUp" && direction !== "DOWN") direction = "UP";if (event.key === "ArrowDown" && direction !== "UP") direction = "DOWN";});// Game loopsetInterval(updateGame, 100);Copied!✕Copy 35+ Free JavaScript Games (Code+ Demo)
Jan 4, 2024 · Learn how to create various fun and simple Javascript games with code and demo examples. Explore puzzles, card games, arcade games, and more with HTML, CSS, and Phaser …
HTML Game Example - W3Schools
Learn how to make games, using nothing but HTML and JavaScript. Push the buttons to move the red square: With our online editor, you can edit the code, and click on a button to view the result.
36 JavaScript Games | FreeFrontend
5 days ago · A collection of fun and interactive JavaScript games. From classic recreations to unique, custom-made mini-games, all built with pure HTML, CSS, and JavaScript.
javascript-games · GitHub Topics · GitHub
Here are 102 public repositories matching this topic... Build 100+ Projects In 100 Days Using HTML, CSS and JAVASCRIPT. A curated list of awesome JavaScript Games 🎮. Provides a well …
15+ Games Using JavaScript [Demo + Free Code]
Mar 23, 2024 · Whether you are a beginner or a professional web developer these JavaScript game source codes will always help you. There are many JavaScript games like Simple Games, …
- People also ask
Javascript Mini Games
A collection of fun mini games built with JavaScript, complete with code examples and documentation.
2D breakout game using pure JavaScript - MDN
Aug 2, 2025 · In this step-by-step tutorial we create an MDN Breakout game written entirely in pure JavaScript and rendered on HTML <canvas>. Every step has editable, live samples available to …
JavaScript Snake game tutorial: Build a simple, interactive game
May 5, 2025 · This tutorial will guide you in building a simple, interactive Snake game using HTML, CSS, and JavaScript. You’ll have a fun game and a better grasp of core web development …
How to Build a Simple Game with JavaScript - itch.io
Jun 17, 2025 · JavaScript can do more than build websites—it can also power fun games! In this guide, you’ll learn how to build a simple browser-based game using plain JavaScript, HTML, and …