Write a maze game in HTML canvas, give it 10 levels. Progress should be saved in localStorage, make a menu main where you can select any level from those who you've already finished.
Give it a sleek UI/UX
Don't use any external asset.
The game difficulty should increase after each level
Give each level a name. The first level should be called 'Tutorial', then feel free to name the rest.
Assistant
Certainly! Below is a complete, self-contained HTML file that implements a 10-level maze game using HTML Canvas. The game features a sleek UI/UX, level progression saved in localStorage, and a main menu for selecting unlocked levels. Each level has an increasing difficulty and a unique name.
Features:
10 Levels: Each with a unique name and increasing difficulty.
Maze Generation: Procedurally generated using the Recursive Backtracker algorithm.
Player Movement: Navigate the maze using arrow keys or WASD.
Progress Saving: Completed levels are saved in localStorage.
Main Menu: Select any unlocked level from the main menu.
Sleek UI/UX: Minimalistic and responsive design without external assets.
How to Use:
Run the Code: Copy the entire code below into an .html file and open it in a modern web browser.
Navigate: Use arrow keys or WASD to move the player (blue square) through the maze to reach the goal (green square).
Progression: Upon completing a level, the next level unlocks. Access the main menu to select any unlocked level.
Main Menu (#main-menu): Displays the game title and level selection buttons.
Game Container (#game-container): Contains the canvas where the maze is drawn, a back button to return to the main menu, and a display for the current level's name. It also includes a win message overlay when a level is completed.
Styling (CSS):
Layout: Flexbox is used to center elements and manage layouts.
Buttons: Styled with transitions for hover effects to enhance interactivity.
Canvas: Given a dark background with a border for better visibility.
Win Message: Positioned absolutely to overlay the canvas upon level completion.
JavaScript Functionality:
Levels Definition: An array of objects where each level has a name and a size which determines the maze's dimensions (increasing size for higher difficulty).
Progress Saving: Uses localStorage to store an array of completed level indices. Initially, only the first level (Tutorial) is unlocked.
Main Menu Initialization (initMainMenu): Dynamically creates buttons for each level. Buttons are enabled only if the level is unlocked (completed the previous one).
Starting a Level (startLevel): Sets up the game environment by generating the maze, initializing the player's position, and setting the goal. It then draws the initial state and adds an event listener for player movement.
Maze Generation (Grid and Cell classes): Uses the Recursive Backtracker algorithm to generate a perfect maze (no loops and exactly one path between any two points). The generateMaze method removes walls between cells to carve out the maze.
Drawing the Maze and Player (draw): Renders the maze on the canvas, the goal as a green square, and the player as a blue circle.
Player Movement: Listens for arrow keys or WASD inputs to move the player. Checks for wall collisions to prevent moving through walls.
Win Condition (checkWin): When the player reaches the goal, it displays the win message, saves progress, and unlocks the next level.
Customization:
Level Names and Sizes: You can modify the levels array to add more levels, change names, or adjust difficulty by altering the size parameter.
Player and Goal Appearance: Adjust the colors or shapes in the draw function to customize the look.
Maze Generation Algorithm: For different maze patterns, you can implement alternative algorithms or tweak the existing one.
Enjoy navigating through the mazes and advancing through the levels!