KAUSHIK SARKARBlogs
Game Prompts

Dark Maze Game with Light Yellow Pathways: A Modern Canvas Adventure

By Kaushik Sarkar · · 13 min read
Hero screenshot of the finished Maze game

I recently started an ambitious project to build a modern, polished Maze game as a single HTML file using HTML5 Canvas. The game features a classic maze, keyboard and touch input controls, a score display, a sound toggle, and a restart button. I wanted to create a visually appealing game with a clean and modern design. The game is responsive and accessible, ensuring it works well on different devices and screen sizes. I also included smooth animations and transitions to enhance the user experience.

What you need

  • Basic knowledge of HTML, CSS, and JavaScript.
  • Access to an AI tool like ChatGPT, Claude, Bolt, or v0.
  • A text editor or code editor of your choice.
  • A browser to run your game.

The full prompt

Build a modern, polished Maze game as a single HTML file using HTML5 Canvas. The game should have the following features:

- A classic maze with walls, start and finish points.
- User can control the maze movement with keyboard (WASD) and touch input.
- A score is displayed on the screen that increments with each step.
- A sound toggle allows the user to turn sound on or off.
- A restart button is provided to allow the user to start a new game.
- The game should have a visually appealing design with colors, typography, and layout that match a modern style.
- The game should be responsive and accessible, ensuring it works well on different devices and screen sizes.
- The game should have smooth animations and transitions to enhance the user experience.

For the visual design, use dark background with a light yellow maze. Use a clean and modern font for the text. The start and finish points should be labeled clearly. The score and sound toggle button should be placed at the top right corner.

The controls should be intuitive and easy to understand. The restart button should be positioned near the end of the maze.

Data/state handling should be managed using JavaScript. The game state should be stored and updated as the user interacts with the game.

The game should be responsive and accessible. The layout should be flexible and adapt to different screen sizes. The game should be tested for accessibility and should follow best practices for game development.

Output: A single HTML file with a Canvas, including all necessary JavaScript code and styles to create a polished and responsive Maze game. A detailed README file with instructions on how to run the game in a web browser.

1Set up your development environment

Install the latest version of ChatGPT or Claude from their official websites. These AI tools can assist with generating code snippets and helping with planning the game structure. Choose a code editor like Visual Studio Code or Sublime Text, which offer advanced features for coding and debugging. Additionally, ensure you have Node.js installed to handle JavaScript projects efficiently. The prompt above provides detailed specifications for the game, so make sure to refer back to it as you progress through the steps.

Pro tip: Make sure to have the necessary tools installed.

2Create the basic structure of your HTML file

Sure, here’s the markdown for Step 2 of building the modern Maze game:

---

Create a new HTML file and add the basic structure. Include a canvas element where the game will be rendered. The canvas will have a width of 800px and a height of 600px for a responsive game design. Additionally, include a div element for displaying the score, a div for the sound toggle button, and a button for the restart button. Use CSS to style these elements for a modern and accessible design. Finally, include a script element at the end of the file to load the game logic.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modern Maze Game</title>
    <style>
        /* Add modern styles here */
        body, html {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
        canvas {
            display: block;
            background-color: #000;
        }
        #score, #soundToggle, #restartButton {
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 18px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <canvas id="mazeCanvas" width="800" height="600"></canvas>
    <div id="score">Score: 0</div>
    <div id="soundToggle">Sound: On</div>
    <button id="restartButton">Restart</button>
    <script src="game.js"></script>
</body>
</html>

---

This markdown provides a clear and practical guide to creating the basic structure of the HTML file for the Maze game. It includes the necessary elements and a brief overview of the CSS styles to be added for a modern look.

Pro tip: Ensure the canvas has a width and height that match the size of the game.

3Add CSS for styling

To add CSS for styling the game, follow these steps:

  1. Set the Background Color: Use a dark background color to create a contrast with the light yellow maze walls. This enhances the visual appeal and makes it easier to see the maze structure.
body {
  background-color: #1a1a1a; /* Dark gray */
  margin: 0;
  padding: 0;
  overflow: hidden;
}
  1. Style the Maze Walls: Set the color of the maze walls to light yellow to provide a clear contrast with the dark background.
canvas {
  display: block;
  margin: auto; /* Center the canvas */
  background-color: #f7f7f7; /* Light yellow */
  border: none;
}
  1. Select a Modern Font: Choose a clean and modern font for the text to match the aesthetic of the game. Use a font like 'Roboto' or 'Open Sans' for readability.
body, button, span {
  font-family: 'Roboto', 'Open Sans', sans-serif;
  font-weight: 400;
  color: #fff; /* Light white */
}
  1. Position UI Elements: Place the score and sound toggle button at the top right corner of the screen. Ensure they are easily accessible and look clean.
.score, .sound-toggle {
  position: absolute;
  top: 10px;
  right: 10px;
}
  1. Ensure Responsiveness: Use CSS media queries to ensure the game is responsive and adapts to different screen sizes. This helps in providing a consistent experience across various devices.
@media (max-width: 600px) {
  .score, .sound-toggle {
    font-size: 0.9em;
  }
}

By following these CSS rules, you can create a visually appealing and user-friendly Maze game that meets the specifications outlined in the prompt.

Pro tip: Ensure the CSS is responsive and adapts to different screen sizes.

4Initialize the game canvas

Initialize the game canvas in your JavaScript code. Set the width and height to match the size of the canvas element specified in the prompt above. For example, you can set the canvas dimensions to 800 x 600 pixels. This ensures that the game canvas is appropriately sized for the game's design and functionality.

Pro tip: Ensure the canvas is responsive and adapts to different screen sizes.

5Implement keyboard and touch controls

Implement keyboard and touch controls in your JavaScript code. Use the keydown and touchstart events to handle user input.

First, listen for keydown events on the document to handle keyboard controls. For WASD keys, map the arrow keys to the movement in the maze:

document.addEventListener('keydown', (e) => {
  const direction = {
    '37': 'left',
    '38': 'up',
    '39': 'right',
    '40': 'down'
  };

  if (direction[e.keyCode]) {
    movePlayer(direction[e.keyCode]);
  }
});

Next, implement touch controls by listening for touchstart events on the canvas. Calculate the direction of movement based on the touch position relative to the player's position:

canvas.addEventListener('touchstart', (e) => {
  const touch = e.touches[0];
  const rect = canvas.getBoundingClientRect();
  const playerX = player.x;
  const playerY = player.y;

  if (touch.clientX - rect.left < playerX + player.width / 2) {
    movePlayer('left');
  } else if (touch.clientX - rect.left > playerX + player.width / 2) {
    movePlayer('right');
  } else if (touch.clientY - rect.top < playerY + player.height / 2) {
    movePlayer('up');
  } else if (touch.clientY - rect.top > playerY + player.height / 2) {
    movePlayer('down');
  }
});

By handling both keyboard and touch inputs, the game will be accessible to users of all devices and input methods. This ensures a smooth and intuitive experience across different platforms.

Pro tip: Ensure the controls are intuitive and easy to understand.

6Create the maze

  • Create the maze in your JavaScript code. Use a grid to represent the maze and draw the walls, start, and finish points.
  • Create a 2D array to represent the maze with rows and columns. The array will contain values that represent different types of cells in the maze, such as walls (1), open spaces (0), start (2), and finish (3).
  • Set the start and finish positions in the maze array. For example, you can set maze[0][0] to 2 for the start and maze[rows-1][cols-1] to 3 for the finish.
  • Draw the maze on the canvas using the fillStyle and strokeStyle properties. For walls, use a dark gray color (#333), and for open spaces, use a light yellow color (#FFFF00).
  • Draw the start and finish points using a different color, such as green for the start and red for the finish. Use the fillStyle and strokeStyle properties to draw circles for the points.
  • Ensure the maze is responsive by adjusting the size of the canvas based on the window size. You can use the addEventListener method to listen for the resize event and update the canvas size accordingly.
  • Add padding to the maze to ensure there is enough space around the walls and points. You can use the padding property to add padding to the maze array and then use that padding when drawing the maze on the canvas.

Pro tip: Ensure the maze is visually appealing and matches the design requirements.

7Add a score counter

To add a score counter to your Maze game, you'll need to keep track of the player's progress and update the score accordingly. Here’s how you can implement this:

### Step 7: Add a Score Counter

  • Initialize the Score: Start by creating a variable to keep track of the player's score. Initialize it to 0 in your JavaScript file.
let score = 0;
  • Update the Score: Whenever the player takes a step in the maze, increment the score by 1. You can do this inside the update function, which is called every frame of the game.
function update() {
  // ... existing update logic ...

  // Increment score for each step
  score++;
}
  • Display the Score: Create a function to display the score on the screen. You can do this by creating a text element and updating its content with the current score.
function displayScore() {
  const scoreText = document.getElementById('scoreText');
  scoreText.textContent = 'Score: ' + score;
}
  • Call the Display Function: Make sure to call displayScore() in the render function to update the score display every frame.
function render() {
  // ... existing render logic ...

  // Display the score
  displayScore();
}
  • HTML Element for Score: Ensure you have a div element in your HTML where the score will be displayed.
<div id="scoreText">Score: 0</div>

By following these steps, you'll have a functioning score counter in your game, which will help keep track of the player's progress and make the game more engaging.

Pro tip: Ensure the score counter is visible and easy to read.

8Add a sound toggle

Add a sound toggle in your JavaScript code. Use the localStorage API to store and toggle sound. When the user clicks the sound toggle button, check if sound is currently enabled. If it is, disable it by stopping the audio playback and changing the button text to "Sound Off". If it's disabled, enable it by playing the audio and changing the button text to "Sound On". Save the state of sound in localStorage so it persists across sessions.

Pro tip: Ensure the sound toggle is visible and easy to access.

9Create a restart button

Create a restart button in your JavaScript code. Use the click event to restart the game.

In the JavaScript code, add a new function called restartGame(). This function should reset the game state variables, create a new maze, and update the UI to reflect the new game.

function restartGame() {
  // Reset game state variables
  score = 0;
  maze = generateMaze(); // Assuming a function to generate a new maze
  // Update UI to reflect new game
  updateUI();
}

Add a button to the HTML file, with an id of restartButton, and a click event listener that calls the restartGame() function when the button is clicked.

<button id="restartButton" onclick="restartGame()">Restart</button>

Ensure the restartButton is positioned near the end of the maze on the Canvas, as specified in the prompt above.

By implementing these steps, you will have a restart button that allows the user to start a new game from any point in the maze.

Pro tip: Ensure the restart button is visible and easy to access.

10Test the game

Test the game in a web browser to ensure it works as expected. Check for any bugs or issues. Make sure the maze is properly generated and the player can move around using the WASD keys and touch input. The score should increment with each step, and the sound toggle should work as expected. The restart button should allow the user to start a new game. The visual design should be visually appealing and the game should be responsive and accessible. Test the game on different devices and screen sizes to ensure it works well. Test the game for accessibility and follow best practices for game development. Make sure the game is smooth and has smooth animations and transitions to enhance the user experience.

Pro tip: Test the game on different devices and screen sizes to ensure it is responsive.

11Deploy the game

Deploy the game to a web server or hosting service like GitHub Pages, Netlify, or Vercel. This will make your game accessible to anyone with an internet connection. Upload your HTML file along with any necessary CSS and JavaScript files to the root directory of your repository. Create a new branch named gh-pages and push your changes to that branch. Set the gh-pages branch as the default branch for your repository. Once deployed, your game can be accessed via a URL provided by the hosting service. Test your game on different devices and screen sizes to ensure it works well and is responsive.

Pro tip: Ensure the game is optimized for performance and accessible.

Adapt it to your own brand

  • Adapt the game design to match your brand's visual identity.
  • Add additional features like power-ups, enemies, or levels.
  • Create a multiplayer version of the game.
  • Integrate the game with a backend service to store scores and player data.
  • Add voice recognition to control the game.
  • Create a mobile version of the game.

Common mistakes and fixes

  • Failing to add responsive design to the game.
  • Not implementing accessibility features for the game.
  • Not using the appropriate tools for the game development process.
  • Failing to test the game thoroughly.
  • Not deploying the game correctly.
  • Not optimizing the game for performance.

Frequently asked questions

What inspired the project idea?

The project idea was inspired by .NET Console Games, but I adapted it to use HTML5 Canvas and AI coding tools.

What are the main features of the game?

The game has a classic maze, keyboard and touch input controls, a score display, a sound toggle, and a restart button.

How is the game designed?

The game is designed with a dark background and a light yellow maze. It uses a clean and modern font for the text and has clear labels for the start and finish points. The score and sound toggle button are placed at the top right corner.

How is the game controlled?

The game controls are intuitive and easy to understand. The restart button is positioned near the end of the maze.

Final thoughts

Congratulations! You have successfully built a modern Maze game using AI tools. Feel free to customize and improve the game to suit your needs.

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