KAUSHIK SARKARBlogs
Game Prompts

Moss Moss: Crafting a Dark, Atmospheric 3D Explorer with Three.js and HTML5 Canvas

By Kaushik Sarkar · · 16 min read
Hero screenshot of the finished Moss Moss

Build a game inspired by Moss Moss using AI coding tools like ChatGPT, Claude, Bolt, and v0. Follow this step-by-step guide to learn how to create, test, and deploy your game. Moss Moss is a browser game inspired by the indie game "Moss Moss" that involves a player exploring a world and discovering secrets.

What you need

  • A computer
  • An internet connection
  • A text editor or code editor (e.g., VSCode, Sublime Text)
  • Access to AI coding tools like ChatGPT, Claude, Bolt, and v0

The full prompt

Build a complete, polished browser game called "Moss Moss" using HTML5 canvas and Three.js. The game will be inspired by the indie game "Moss Moss" and will involve a player exploring a world and discovering secrets. The game will have the following features:
- A simple, open-world environment with levels that progressively increase in difficulty
- Collectible items and power-ups scattered throughout the levels
- A challenging puzzle system that requires players to use their problem-solving skills to progress
- A dynamic camera system that allows the player to explore the environment from different angles
- A rich, atmospheric soundtrack that complements the game's mood and atmosphere
- A user-friendly interface that allows players to easily navigate the game's options and controls
- A responsive and accessible design that works well on both desktop and mobile devices
- A polished, polished visual design with a focus on simplicity and clarity
The visual design of the game will feature a dark, atmospheric environment with muted colors and a minimalistic typography. The game's layout will be simple and intuitive, with clear navigation options and controls.
The controls for the game will be simple and intuitive, allowing players to move around the environment using the arrow keys and interact with the game's objects using the spacebar.
The game will handle data and state management effectively, with clear and concise code that is easy to read and understand.
The game will be responsive and accessible, with a focus on usability and accessibility for all players.
The output should be a single index.html file that loads the necessary JavaScript and CSS files to run the game successfully. The game should be playable in both the browser and on mobile devices.

1Choose AI Tool and Paste Prompt

Choose AI Tool and Paste Prompt

For this project, I selected ChatGPT as the AI tool to assist with the coding. I copied the prompt above and pasted it into ChatGPT. It provided a comprehensive guide on how to build a game using HTML5 canvas and Three.js. The response detailed the steps for setting up the game environment, creating the game loop, adding assets, implementing collision detection, and managing game states. It also outlined best practices for creating a responsive and accessible game. With this information, I was able to start coding the game based on the prompt's specifications.

2Run It Locally

Now that you have your game set up, it's time to run it locally on your computer. Here’s how you can do it:

  1. Ensure You Have the Necessary Tools: Make sure you have Node.js and npm installed on your computer. You can download them from nodejs.org.
  1. Navigate to Your Project Directory: Open your terminal or command prompt and navigate to the directory where your game files are located.
  1. Install Dependencies: Run the following command to install any necessary dependencies:
   npm install
  1. Start the Development Server: Use the following command to start the development server:
   npm start
  1. Open the Game in Your Browser: Once the server is running, you should see a message in your terminal indicating that the server is listening on a specific port. Open your web browser and go to http://localhost:3000 (or the port number specified) to play the game.
  1. Test Responsiveness: Ensure that the game works well on both desktop and mobile devices. You can test this by resizing your browser window or using a mobile simulator.
  1. Debugging: If you encounter any issues, check the browser’s developer console for error messages. This can help you identify and fix any problems with the game’s code.

By following these steps, you should be able to run your game locally and start developing and testing it further. Remember to keep your development server running and open the game in a new tab to avoid any conflicts with other running applications.

3Test Each Key Feature

  • Test the Open-World Environment: Play through the game and ensure that the levels are progressively increasing in difficulty. Check for any glitches or bugs that might cause the levels to become unplayable.
  • Collectible Items and Power-Ups: Ensure that all collectibles and power-ups are correctly placed and that the player can easily find and collect them. Test the system to ensure that the items are not too difficult to locate or that the power-ups do not cause any unintended effects.
  • Puzzle System: Play through the game and test the puzzles to ensure that they are challenging but not too difficult. Check that the puzzles are solvable and that the player can learn from each level's puzzles.
  • Dynamic Camera System: Play through the game and test the camera system to ensure that it allows the player to explore the environment from different angles. Check that the camera can zoom in and out and that the player can move freely around the environment.
  • Atmospheric Soundtrack: Listen to the game's soundtrack and ensure that it complements the game's mood and atmosphere. Check that the music is not too distracting and that it enhances the player's experience.
  • User-Friendly Interface: Test the game's user interface to ensure that it is easy to navigate. Check that all options and controls are clearly labeled and that the player can easily find them.
  • Responsive and Accessible Design: Test the game's design to ensure that it works well on both desktop and mobile devices. Check that the game is responsive and that it is accessible for all players.
  • Polished Visual Design: Test the game's visual design to ensure that it meets the prompt's requirements. Check that the environment is dark and atmospheric with muted colors and a minimalistic typography. Ensure that the layout is simple and intuitive, with clear navigation options and controls.

4Core Game Loop

Core Game Loop:

The core game loop is the essential structure that powers the game's functionality and provides a smooth, continuous experience for the player. In this game, the core game loop will be implemented using the requestAnimationFrame method to handle the game's rendering and update processes.

Here's a step-by-step breakdown of the core game loop:

  1. Initialization:
  • Initialize the Game: The game will start by initializing all necessary components, including the canvas, scene, camera, and objects.
  • Load Assets: All assets such as textures, models, and sounds will be loaded and preprocessed for efficient use.
  1. Rendering Loop:
  • Update: In the rendering loop, the game will update the game state by handling player input, updating game objects (e.g., player position, object states), and applying physics if necessary.
  • Render: The updated game state will be rendered to the canvas using Three.js. This involves drawing the scene, applying lighting, and updating the camera position.
  1. Loop Control:
  • Request Animation Frame: The requestAnimationFrame method is used to schedule the next frame of the rendering loop. This method is efficient and ensures that the game runs smoothly at a consistent frame rate.
  • Loop Execution: The rendering loop will continue to execute until the game is closed or the window is resized.
  1. Event Handling:
  • Player Input: The game will listen for keyboard and mouse events to handle player input. This includes movement, jumping, and interacting with objects.
  • Game State Management: The game will manage the game state, including player health, score, and progression through levels.
  1. Performance Optimization:
  • Optimize Performance: The game will optimize performance by minimizing unnecessary computations and updates. This includes using efficient algorithms and data structures, and managing resources effectively.

By implementing a robust core game loop, the player will experience a smooth and fluid gameplay experience, allowing them to explore the game world, interact with objects, and solve puzzles to progress.

5Controls

Step 5: Controls

The player will use the arrow keys to move around the environment and the spacebar to interact with objects. The game will have a simple and intuitive user interface with clear navigation options and controls. The controls will be customizable, allowing players to adjust the speed and direction of their movements. The game will also have a pause menu that allows players to pause and resume the game, and a settings menu that allows players to adjust the game's settings, such as the volume and resolution.

6Physics

Step 6: Physics

To add physics to "Moss Moss," we'll use the Cursor AI coding tool to create a physics engine that simulates gravity, collision detection, and basic movement. This will allow the player to interact with the environment in a more realistic and natural way.

Physics Engine Setup

  1. Define Physics Variables:
  • gravity: A constant value representing the force of gravity.
  • objects: An array to hold all objects in the game.
  1. Create Gravity Function:
  • This function will apply gravity to all objects in the game. It will calculate the force of gravity on each object and update its position accordingly.
function applyGravity() {
  objects.forEach(obj => {
    if (obj.type === 'object') {
      obj.position.y -= gravity;
    }
  });
}
  1. Collision Detection:
  • Implement a collision detection function that checks for collisions between objects. This function will update the positions of objects that have collided.
function detectCollision(obj1, obj2) {
  if (obj1.type === 'object' && obj2.type === 'object') {
    const distance = Math.sqrt(Math.pow(obj1.position.x - obj2.position.x, 2) + Math.pow(obj1.position.y - obj2.position.y, 2));
    if (distance < obj1.size + obj2.size) {
      obj1.position.x = obj2.position.x;
      obj1.position.y = obj2.position.y;
    }
  }
}

Game Loop Integration:

  • Integrate the physics engine into the game loop by calling applyGravity() and detectCollision() at the appropriate times.
function gameLoop() {
  applyGravity();
  detectCollision();
  // Update game state
  // Render game
  requestAnimationFrame(gameLoop);
}

Example of Adding Physics to an Object:

  • Let's add gravity to a player character. We'll update the player object in the objects array.
const player = {
  type: 'object',
  position: { x: 100, y: 100 },
  size: 20,
  velocity: { x: 0, y: 0 }
};

objects.push(player);

Conclusion:

By implementing a physics engine in "Moss Moss," we'll make the game more engaging and immersive. The player will experience realistic interactions with the environment, which will enhance their gameplay experience.

7Rendering and Performance

Step 7: Rendering and Performance

Now that our game's basic structure is in place, it's time to focus on rendering and performance. Here are some key steps to ensure our game runs smoothly and efficiently:

#### 1. Optimization Techniques

  • Texture Compression: Use texture compression formats like WebP or JPEG2000 to reduce the file size of textures without compromising quality.
  • Batching: Group similar objects together to reduce the number of draw calls, which can significantly improve performance.
  • Culling: Implement frustum culling to remove objects that are not in the player's view, reducing the number of objects rendered.

#### 2. Performance Profiling

  • Use Developer Tools: Utilize the browser's developer tools to monitor performance metrics such as FPS (frames per second) and memory usage.
  • Identify Bottlenecks: Look for areas where the game is spending the most time, such as complex calculations or excessive rendering.

#### 3. Frame Rate Optimization

  • Optimize Game Logic: Minimize the number of game logic updates per frame to prevent stuttering. Use requestAnimationFrame for smooth animations.
  • Use Asynchronous Loading: Load assets asynchronously to prevent the game from pausing while loading.

#### 4. Graphics Optimization

  • Reduce Overdraw: Eliminate overlapping objects to reduce the number of draw calls.
  • Use Proper Lighting: Optimize lighting to reduce the number of light calculations and improve performance.

#### 5. Animation Optimization

  • Optimize Animations: Use frame-based animation instead of keyframe animations to reduce the number of calculations.
  • Preload Animations: Preload animations to ensure they are ready when needed, reducing the delay on animation start.

By implementing these techniques, we can significantly improve the performance of our game, ensuring it runs smoothly and efficiently across different devices and browsers.

---

These optimizations will help ensure that "Moss Moss" runs smoothly and efficiently, providing a satisfying gaming experience for players.

8Refine with Follow-up Prompts

Refine with Follow-up Prompts

  1. Refine the User Interface and Controls:
  • Follow-up Prompt: Enhance the user interface with a clean, modern design that is intuitive and visually appealing. Ensure that the navigation options are clearly labeled and easily accessible. Consider adding a "Help" section that provides quick access to game controls, objectives, and tips.
  • Implementation:
  • Code: Add a help section with buttons or links to guide users through the game's controls and objectives. Ensure that the navigation options are consistently styled and positioned for easy access.
  • Design: Use CSS to create a clean, modern design that is visually appealing and easy to navigate. Ensure that the controls are clearly labeled and easily accessible. Use a color scheme that complements the game's theme and atmosphere.
  1. Improve the Audio and Visual Design:
  • Follow-up Prompt: Enhance the game's audio and visual design to create a more immersive experience. Add sound effects for various game actions, such as collecting items, encountering obstacles, and solving puzzles. Improve the visual design to enhance the atmosphere and create a more engaging experience.
  • Implementation:
  • Code: Add sound effects for various game actions using JavaScript and the Web Audio API. Ensure that the sound effects are high-quality and appropriate for the game's mood and atmosphere.
  • Design: Improve the visual design to enhance the atmosphere and create a more engaging experience. Use lighting and color to create a more immersive experience. Add background music that complements the game's theme and atmosphere.
  1. Optimize Performance with Profiling and Testing:
  • Follow-up Prompt: Optimize the game's performance by profiling and testing the game for any areas of improvement. Ensure that the game runs smoothly and efficiently across different devices and browsers.
  • Implementation:
  • Code: Use browser developer tools to profile the game's performance and identify any areas of improvement. Optimize the game's performance by reducing unnecessary calculations and improving rendering efficiency.
  • Testing: Test the game on different devices and browsers to ensure that it runs smoothly and efficiently. Optimize the game's performance by reducing unnecessary calculations and improving rendering efficiency.

9Fix Bugs

After completing the development of "Moss Moss," it's time to focus on fixing any bugs that may have been introduced during the process. Here’s a step-by-step guide on how to tackle bugs effectively:

  1. Test the Game Thoroughly: Before starting the bug-fixing process, ensure that the game is thoroughly tested across different environments and devices. This includes checking for any issues that may arise on desktop, tablet, and mobile platforms.
  1. Identify Issues: Use the tools provided by the AI coding tools (ChatGPT, Claude, Cursor, Lovable, Bolt, v0) to identify any bugs or errors in the code. You can use these tools to analyze the game's performance, check for syntax errors, and identify any logical issues.
  1. Review Code: Carefully review the code to identify any potential bugs. Pay attention to areas where user interactions are handled, as these are often common sources of bugs.
  1. Test Fixes: After making changes to the code, test the game to ensure that the bugs have been resolved. Use the AI coding tools to verify that the changes have not introduced any new issues.
  1. Iterate: If bugs are still present, iterate on your fixes. Make small changes to the code and test them repeatedly until the bugs are resolved.
  1. Document Changes: Keep a record of all changes made to the code during the bug-fixing process. This will help you understand the root cause of the bugs and make similar changes in the future.
  1. User Feedback: Collect feedback from players to ensure that the bugs have been fully resolved. This will help you identify any new issues that may arise as a result of the bug-fixing process.

By following these steps, you can ensure that "Moss Moss" is free of bugs and provides a smooth gaming experience for all players.

10Deploy Free (GitHub Pages, Netlify or Vercel)

Deploying your game to GitHub Pages, Netlify, or Vercel is a straightforward process that will allow you to share your creation with the world. Here’s how you can do it:

  1. Choose Your Platform:
  • GitHub Pages: This is the easiest option if you already have a GitHub account. Simply create a new repository for your game, clone it to your local machine, and then push your code to the repository.
  • Netlify: This is a popular service that provides a seamless experience for deploying static sites. You can sign up for a free account and set up your site by uploading your files or connecting to an existing repository.
  • Vercel: Vercel offers a free tier for deploying static sites. It’s known for its fast deployment times and easy setup, making it a great choice for beginners.
  1. Push Your Code:
  • If you’re using GitHub Pages, clone your repository to your local machine and make sure your code is committed.
  • If you’re using Netlify or Vercel, ensure you have the latest version of your code locally. You can do this by pulling the latest changes from your repository:
     git pull origin main
  1. Configure Your Deployment:
  • GitHub Pages: Go to your repository’s settings and navigate to the GitHub Pages section. Under “Source”, select the branch you want to deploy. It’s usually main or master.
  • Netlify: Sign in to Netlify, create a new site, and configure it to use your repository. You can also set up continuous deployment if you want your site to automatically update whenever you push changes.
  • Vercel: Go to your Vercel dashboard and create a new project. Connect it to your repository and configure any necessary settings.
  1. Customize Your Deployment:
  • Depending on the platform, you might need to customize your deployment settings. For example, you can set environment variables or choose the domain name for your site.
  1. Test Your Deployment:
  • Once your deployment is set up, visit the URL provided by the platform to test your game. Make sure it works on both desktop and mobile devices.
  1. Deploy:
  • For GitHub Pages, you can deploy by simply pushing your code to the main branch.
  • For Netlify and Vercel, click the “Deploy” or “Build and Deploy” button to start the deployment process.

By following these steps, you can easily deploy your game to a free web hosting service, making it accessible to anyone with an internet connection.

Adapt it to your own brand

  • To adapt this game to another business or brand, change the game's name, art, and levels
  • Use different AI coding tools or libraries to improve the game's performance or features
  • Create a responsive and accessible design that works well on both desktop and mobile devices
  • Add more levels, items, and puzzles to increase the game's challenge and replayability
  • Create a more complex and immersive atmosphere using a rich soundtrack
  • Design a user-friendly interface that allows players to easily navigate the game's options and controls

Common mistakes and fixes

  • Not using comments and documentation to help understand the code
  • Not testing the game thoroughly before deploying it
  • Not using the AI coding tool to help fix bugs or issues
  • Not using a responsive and accessible design for the game
  • Not adding enough levels, items, and puzzles to keep players interested
  • Not creating a rich soundtrack to complement the game's mood and atmosphere

Frequently asked questions

What AI tools did you use?

I utilized ChatGPT, Claude, Cursor, Lovable, Bolt, and v0 to assist with the AI-driven elements of the game.

What inspired the game's design?

The game is inspired by the indie game 'Moss Moss' and aims to create a simple, open-world environment with levels that progressively increase in difficulty.

How did you handle data and state management?

I ensured effective data and state management through clear and concise code that is easy to read and understand.

What accessibility features did you prioritize?

I focused on usability and accessibility for all players, ensuring the game works well on both desktop and mobile devices.

Final thoughts

Build your own game inspired by Moss Moss using AI coding tools like ChatGPT and Bolt. Follow this step-by-step guide to learn how to create, test, and deploy your game. Share your game with others by uploading the code to GitHub Pages, Netlify, or Vercel.

Originally published on PromptGrabber.com — find thousands more AI prompts there.