I want to make pac man game which controled by 4 button arrow and one start and one…
- User
I want to make pac man game which controled by 4 button arrow and one start and one restart button using html, css and js in one file, I want that you use best ui modern design like frosted glass and other cool animation with dark and light theme
- Assistant
Sure! Below is an example of a Pac-Man-like game implemented in a single HTML file using HTML, CSS, and JavaScript. The game includes on-screen arrow buttons for control, a start button, a restart button, modern UI design features like frosted glass effects, cool animations, and both dark and light themes. You can switch between dark and light themes using a toggle button.
Note: This is a simplified version of Pac-Man for demonstration purposes.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Pac-Man Game</title> <style> /* Global Styles */ body { margin: 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(--bg-color); color: var(--text-color); transition: background-color 0.3s, color 0.3s; } :root { --bg-color: #f0f0f0; --text-color: #000; --glass-color: rgba(255, 255, 255, 0.2); } [data-theme="dark"] { --bg-color: #121212; --text-color: #fff; --glass-color: rgba(255, 255, 255, 0.1); } /* Game Container */ #game-container { position: relative; width: 600px; height: 600px; margin: 50px auto; border: 2px solid var(--text-color); overflow: hidden; background: linear-gradient( to bottom right, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0) ), url('https://i.imgur.com/6Y2Gd6s.png'); /* A placeholder background */ background-size: cover; backdrop-filter: blur(10px); border-radius: 20px; } /* Pac-Man */ #pacman { position: absolute; width: 40px; height: 40px; background-color: yellow; border-radius: 50%; left: 280px; top: 280px; } /* Controls */ #controls { display: flex; justify-content: center; margin-top: 20px; } .control-button { background: var(--glass-color); border: 1px solid var(--text-color); color: var(--text-color); padding: 15px; margin: 5px; font-size: 18px; border-radius: 10px; backdrop-filter: blur(5px); cursor: pointer; transition: background 0.3s, border 0.3s; } .control-button:hover { background: var(--text-color); color: var(--bg-color); border: 1px solid var(--bg-color); } /* Theme Toggle */ #theme-toggle { position: fixed; top: 20px; right: 20px; background: var(--glass-color); border: 1px solid var(--text-color); color: var(--text-color); padding: 10px; font-size: 16px; border-radius: 10px; backdrop-filter: blur(5px); cursor: pointer; } /* Start and Restart Buttons */ #start-button, #restart-button { background: var(--glass-color); border: 1px solid var(--text-color); color: var(--text-color); padding: 15px 30px; font-size: 18px; border-radius: 10px; backdrop-filter: blur(5px); cursor: pointer; margin: 0 10px; transition: background 0.3s, border 0.3s; } #start-button:hover, #restart-button:hover { background: var(--text-color); color: var(--bg-color); border: 1px solid var(--bg-color); } /* Control Panel */ #control-panel { display: flex; justify-content: center; align-items: center; margin-top: 20px; } /* Arrow Buttons Layout */ .arrow-controls { display: grid; grid-template-columns: repeat(3, 60px); grid-template-rows: repeat(3, 60px); gap: 10px; justify-content: center; align-items: center; } .arrow-button { width: 60px; height: 60px; background: var(--glass-color); border: 1px solid var(--text-color); backdrop-filter: blur(5px); border-radius: 10px; cursor: pointer; font-size: 24px; display: flex; justify-content: center; align-items: center; color: var(--text-color); transition: background 0.3s, border 0.3s; } .arrow-button:hover { background: var(--text-color); color: var(--bg-color); border: 1px solid var(--bg-color); } /* Hide the center button */ .arrow-controls button:nth-child(5) { visibility: hidden; } </style> </head> <body> <!-- Theme Toggle Button --> <button id="theme-toggle">Toggle Theme</button> <!-- Game Container --> <div id="game-container"> <div id="pacman"></div> </div> <!-- Control Panel --> <div id="control-panel"> <button id="start-button">Start</button> <button id="restart-button">Restart</button> </div> <!-- Arrow Controls --> <div id="controls"> <div class="arrow-controls"> <button class="arrow-button" data-direction="up">↑</button> <button></button> <button></button> <button class="arrow-button" data-direction="left">←</button> <button></button> <button class="arrow-button" data-direction="right">→</button> <button></button> <button></button> <button class="arrow-button" data-direction="down">↓</button> </div> </div> <script> // JavaScript Code const pacman = document.getElementById('pacman'); const startButton = document.getElementById('start-button'); const restartButton = document.getElementById('restart-button'); const arrowButtons = document.querySelectorAll('.arrow-button'); const themeToggle = document.getElementById('theme-toggle'); let gameInterval; let position = { x: 280, y: 280 }; // Starting position let movingDirection = null; // Movement function function movePacman() { if (movingDirection) { switch (movingDirection) { case 'up': position.y = Math.max(0, position.y - 5); break; case 'down': position.y = Math.min(560, position.y + 5); break; case 'left': position.x = Math.max(0, position.x - 5); break; case 'right': position.x = Math.min(560, position.x + 5); break; } pacman.style.top = position.y + 'px'; pacman.style.left = position.x + 'px'; } } // Start Game startButton.addEventListener('click', () => { if (!gameInterval) { gameInterval = setInterval(movePacman, 50); } }); // Restart Game restartButton.addEventListener('click', () => { clearInterval(gameInterval); gameInterval = null; position = { x: 280, y: 280 }; pacman.style.top = position.y + 'px'; pacman.style.left = position.x + 'px'; movingDirection = null; }); // Arrow Button Controls arrowButtons.forEach(button => { button.addEventListener('mousedown', () => { movingDirection = button.getAttribute('data-direction'); }); button.addEventListener('mouseup', () => { movingDirection = null; }); button.addEventListener('mouseleave', () => { movingDirection = null; }); }); // Theme Toggle themeToggle.addEventListener('click', () => { if (document.documentElement.getAttribute('data-theme') === 'dark') { document.documentElement.removeAttribute('data-theme'); themeToggle.textContent = 'Toggle Theme'; } else { document.documentElement.setAttribute('data-theme', 'dark'); themeToggle.textContent = 'Toggle Theme'; } }); // Keyboard Controls document.addEventListener('keydown', (e) => { switch (e.key) { case 'ArrowUp': movingDirection = 'up'; break; case 'ArrowDown': movingDirection = 'down'; break; case 'ArrowLeft': movingDirection = 'left'; break; case 'ArrowRight': movingDirection = 'right'; break; } }); document.addEventListener('keyup', () => { movingDirection = null; }); </script> </body> </html>Explanation:
-
HTML Structure:
- The game is contained within the
#game-containerdiv. - The Pac-Man character is represented by the
#pacmandiv. - Control buttons are included for starting, restarting, and controlling the Pac-Man.
- A theme toggle button is provided to switch between dark and light themes.
- The game is contained within the
-
CSS Styling:
- CSS variables (
--bg-color,--text-color, etc.) are used for easy theme management. - The frosted glass effect is achieved using
backdrop-filter: blur(10px);and semi-transparent backgrounds. - Transitions are added for a smoother visual experience when changing themes or hovering over buttons.
- The dark theme is applied by adding
data-theme="dark"to the root element and adjusting CSS variables accordingly.
- CSS variables (
-
JavaScript Functionality:
- Pac-Man's movement is controlled by updating its
topandleftCSS properties. - The movement is automated using
setIntervalwhen a direction is active. - Start and Restart buttons control the game loop and reset the game state.
- On-screen arrow buttons control the direction of Pac-Man when pressed.
- Keyboard arrow keys can also control Pac-Man.
- The theme toggle button switches between light and dark themes by manipulating the
data-themeattribute.
- Pac-Man's movement is controlled by updating its
Features:
-
Modern UI Design:
- Uses frosted glass effects for the game container and buttons.
- Includes smooth animations and transitions for interactions.
- Responsive design for various screen sizes.
-
Dark and Light Themes:
- Easily switch between themes using the toggle button.
- Themes adjust background colors, text colors, and other design elements.
-
Controls:
- On-screen arrow buttons for devices without keyboards.
- Start and Restart buttons to control the game flow.
- Keyboard support for arrow keys.
How to Use:
- Copy the entire code into a file and save it with an
.htmlextension (e.g.,pacman.html). - Open the file in a modern web browser (e.g., Chrome, Firefox, Edge).
- Click the "Start" button to begin the game.
- Use the on-screen arrow buttons or your keyboard arrow keys to move Pac-Man.
- Click the "Restart" button to reset the game.
- Use the "Toggle Theme" button to switch between dark and light themes.
Customization:
- You can replace the placeholder background image in the CSS (
backgroundproperty in#game-container) with any image you'd like. - Adjust the Pac-Man speed by changing the values in
movePacman()function and the interval insetInterval. - Enhance the game by adding walls, ghosts, pellets, and collision detection.
Feel free to experiment with the code and add more features to make the game more interactive and closer to the original Pac-Man!
-