Code for maze game using the html css and javascript
Follow us on instagram: https://www.instagram.com/webstreet_code/
Code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Challenging Maze Game</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { background: #000; font-family: 'Arial', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; overflow: hidden; } h1 { position: absolute; top: 20px; color: #00FF00; font-size: 2rem; text-align: center; width: 100%; } .game-container { position: relative; width: 400px; height: 400px; border: 5px solid #00FF00; display: flex; justify-content: center; align-items: center; } .maze { position: relative; width: 380px; height: 380px; background: radial-gradient(circle, rgba(0, 0, 0, 0.9), rgba(0, 255, 0, 0.1)); border: 2px dashed #00FF00; } .ball { position: absolute; top: 10px; left: 10px; width: 20px; height: 20px; background-color: #FF4500; border-radius: 50%; box-shadow: 0 0 15px #FF4500, 0 0 50px #FF4500, 0 0 100px #FF4500; transition: 0.1s linear; } .walls { position: absolute; background-color: #00FF00; z-index: 1; } /* Existing walls */ .wall1 { top: 0; left: 100px; width: 20px; height: 180px; } .wall2 { top: 200px; left: 100px; width: 20px; height: 180px; } /* Wall 3 with hole */ .wall3 { top: 100px; left: 300px; width: 20px; height: 120px; } .wall3-hole { top: 260px; left: 300px; width: 20px; height: 120px; background-color: transparent; } /* Wall 4 with hole */ .wall4 { top: 100px; left: 100px; width: 100px; height: 20px; } .wall4-hole { top: 100px; left: 220px; width: 100px; height: 20px; background-color: transparent; } /* New additional walls */ .wall5 { top: 50px; left: 200px; width: 20px; height: 100px; } .wall6 { top: 150px; left: 200px; width: 100px; height: 20px; } .wall7 { top: 250px; left: 50px; width: 20px; height: 130px; } .wall8 { top: 280px; left: 170px; width: 130px; height: 20px; } .wall9 { top: 330px; left: 100px; width: 120px; height: 20px; } .wall10 { top: 50px; left: 50px; width: 150px; height: 20px; } .finish { position: absolute; top: 10px; right: 10px; font-size: 2rem; color: #FFD700; z-index: 10; } </style> </head> <body> <h1>Maze Game - Reach the Home!</h1> <div class="game-container"> <div class="maze"> <!-- Existing walls --> <div class="walls wall1"></div> <div class="walls wall2"></div> <div class="walls wall3"></div> <div class="walls wall3-hole"></div> <div class="walls wall4"></div> <div class="walls wall4-hole"></div> <!-- Additional walls for more challenge --> <div class="walls wall5"></div> <div class="walls wall6"></div> <div class="walls wall7"></div> <div class="walls wall8"></div> <div class="walls wall9"></div> <div class="walls wall10"></div> <!-- Ball --> <div class="ball" id="ball"></div> <!-- Home Icon (Finish Point) --> <div class="finish" id="finish">?</div> </div> </div> <script> const ball = document.getElementById('ball'); const finish = document.getElementById('finish'); const maze = document.querySelector('.maze'); let ballX = 10; // Initial position let ballY = 10; const ballSize = 20; const mazeSize = 380; function updateBallPosition() { ball.style.left = ballX + 'px'; ball.style.top = ballY + 'px'; } document.addEventListener('keydown', moveBall); function moveBall(e) { const step = 10; // The ball moves 10px per key press switch(e.key) { case 'ArrowUp': if (ballY > 0) ballY -= step; break; case 'ArrowDown': if (ballY < mazeSize - ballSize) ballY += step; break; case 'ArrowLeft': if (ballX > 0) ballX -= step; break; case 'ArrowRight': if (ballX < mazeSize - ballSize) ballX += step; break; } updateBallPosition(); checkCollision(); } function checkCollision() { const walls = document.querySelectorAll('.walls:not(.wall3-hole):not(.wall4-hole)'); walls.forEach(wall => { const wallRect = wall.getBoundingClientRect(); const ballRect = ball.getBoundingClientRect(); if (ballRect.left < wallRect.right && ballRect.right > wallRect.left && ballRect.top < wallRect.bottom && ballRect.bottom > wallRect.top) { resetGame(); } }); // Check if ball reaches the home icon (?) const finishRect = finish.getBoundingClientRect(); const ballRect = ball.getBoundingClientRect(); if (ballRect.left < finishRect.right && ballRect.right > finishRect.left && ballRect.top < finishRect.bottom && ballRect.bottom > finishRect.top) { alert('You Win!'); document.removeEventListener('keydown', moveBall); } } function resetGame() { ballX = 10; ballY = 10; updateBallPosition(); alert("You hit a wall! Try again."); } </script> </body> </html>
The above is the detailed content of Code for maze game using the html css and javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
