Below is a complete HTML code for a simple mobile-friendly "Pasta Maker" game. This game will allow players to drag and drop ingredients to make pasta, with a fun and interactive interface. It uses HTML, CSS, and JavaScript, all embedded in a single HTML file. The game is designed to be responsive and work well on mobile devices.
Features of the Game:
- Drag and Drop Ingredients: Players can drag ingredients (flour, eggs, water, etc.) into a mixing bowl.
- Mixing Animation: Once all ingredients are added, players can "mix" them to create dough.
- Score System: Players earn points for completing steps correctly.
- Responsive Design: Works on both desktop and mobile devices.
Here is the complete single HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pasta Maker Game</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
overflow: hidden;
touch-action: none; /* Prevents default touch behavior on mobile */
}
h1 {
font-size: 24px;
color: #333;
margin-bottom: 20px;
}
#game-container {
position: relative;
width: 90vw;
max-width: 400px;
height: 70vh;
background-color: #fff;
border: 2px solid #333;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#bowl {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: 100px;
height: 100px;
background-color: #ddd;
border: 2px solid #999;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
color: #666;
text-align: center;
}
.ingredient {
position: absolute;
width: 50px;
height: 50px;
background-size: cover;
cursor: move;
user-select: none;
touch-action: none; /* Prevents default touch behavior on mobile */
}
#flour { background-image: url('https://img.icons8.com/ios-filled/50/000000/flour.png'); top: 20px; left: 20px; }
#egg { background-image: url('https://img.icons8.com/ios-filled/50/000000/egg.png'); top: 20px; left: 100px; }
#water { background-image: url('https://img.icons8.com/ios-filled/50/000000/water.png'); top: 20px; left: 180px; }
#mix-button {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
padding: 10px 20px;
font-size: 16px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
display: none;
}
#mix-button:hover {
background-color: #218838;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 18px;
color: #333;
}
#message {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 20px;
color: #333;
text-align: center;
display: none;
}
/* Mixing Animation */
@keyframes mix {
0% { transform: translateX(-50%) rotate(0deg); }
25% { transform: translateX(-50%) rotate(10deg); }
75% { transform: translateX(-50%) rotate(-10deg); }
100% { transform: translateX(-50%) rotate(0deg); }
}
.mixing {
animation: mix 0.5s infinite;
}
/* Mobile Responsiveness */
@media (max-width: 600px) {
h1 {
font-size: 20px;
}
#game-container {
width: 95vw;
height: 60vh;
}
#bowl {
width: 80px;
height: 80px;
font-size: 12px;
}
.ingredient {
width: 40px;
height: 40px;
}
#mix-button {
padding: 8px 16px;
font-size: 14px;
}
#score {
font-size: 16px;
}
#message {
font-size: 18px;
}
}
</style>
</head>
<body>
<h1>Pasta Maker Game</h1>
<div id="game-container">
<div id="bowl">Drop Ingredients Here</div>
<div id="flour" class="ingredient" draggable="true"></div>
<div id="egg" class="ingredient" draggable="true"></div>
<div id="water" class="ingredient" draggable="true"></div>
<button id="mix-button">Mix Ingredients</button>
<div id="score">Score: 0</div>
<div id="message"></div>
</div>
<script>
const bowl = document.getElementById('bowl');
const ingredients = document.querySelectorAll('.ingredient');
const mixButton = document.getElementById('mix-button');
const scoreDisplay = document.getElementById('score');
const messageDisplay = document.getElementById('message');
let score = 0;
let ingredientsInBowl = [];
// Drag and Drop for Desktop
ingredients.forEach(ingredient => {
ingredient.addEventListener('dragstart', dragStart);
});
bowl.addEventListener('dragover', dragOver);
bowl.addEventListener('drop', drop);
// Touch Events for Mobile
ingredients.forEach(ingredient => {
ingredient.addEventListener('touchstart', touchStart, { passive: false });
ingredient.addEventListener('touchmove', touchMove, { passive: false });
ingredient.addEventListener('touchend', touchEnd, { passive: false });
});
mixButton.addEventListener('click', mixIngredients);
let draggedElement = null;
function dragStart(e) {
draggedElement = e.target;
}
function dragOver(e) {
e.preventDefault();
}
function drop(e) {
e.preventDefault();
if (draggedElement) {
addIngredientToBowl(draggedElement);
draggedElement = null;
}
}
// Touch Handling
function touchStart(e) {
e.preventDefault();
draggedElement = e.target;
draggedElement.style.opacity = '0.5';
}
function touchMove(e) {
e.preventDefault();
const touch = e.touches[0];
draggedElement.style.left = `${touch.pageX - draggedElement.offsetWidth / 2}px`;
draggedElement.style.top = `${touch.pageY - draggedElement.offsetHeight / 2}px`;
}
function touchEnd(e) {
e.preventDefault();
draggedElement.style.opacity = '1';
const bowlRect = bowl.getBoundingClientRect();
const touch = e.changedTouches[0];
if (
touch.pageX >= bowlRect.left &&
touch.pageX <= bowlRect.right &&
touch.pageY >= bowlRect.top &&
touch.pageY <= bowlRect.bottom
) {
addIngredientToBowl(draggedElement);
} else {
resetIngredientPosition(draggedElement);
}
draggedElement = null;
}
function addIngredientToBowl(ingredient) {
if (!ingredientsInBowl.includes(ingredient.id)) {
ingredientsInBowl.push(ingredient.id);
ingredient.style.display = 'none';
score += 10;
scoreDisplay.textContent = `Score: ${score}`;
bowl.textContent = `Ingredients: ${ingredientsInBowl.length}/3`;
if (ingredientsInBowl.length === 3) {
mixButton.style.display = 'block';
}
}
}
function resetIngredientPosition(ingredient) {
if (ingredient.id === 'flour') {
ingredient.style.left = '20px';
ingredient.style.top = '20px';
} else if (ingredient.id === 'egg') {
ingredient.style.left = '100px';
ingredient.style.top = '20px';
} else if (ingredient.id === 'water') {
ingredient.style.left = '180px';
ingredient.style.top = '20px';
}
}
function mixIngredients() {
if (ingredientsInBowl.length === 3) {
bowl.classList.add('mixing');
mixButton.style.display = 'none';
setTimeout(() => {
bowl.classList.remove('mixing');
bowl.textContent = 'Dough Ready!';
score += 50;
scoreDisplay.textContent = `Score: ${score}`;
messageDisplay.textContent = 'Great Job! Pasta Dough is Ready!';
messageDisplay.style.display = 'block';
setTimeout(() => {
resetGame();
}, 3000);
}, 2000);
}
}
function resetGame() {
ingredientsInBowl = [];
ingredients.forEach(ingredient => {
ingredient.style.display = 'block';
resetIngredientPosition(ingredient);
});
bowl.textContent = 'Drop Ingredients Here';
mixButton.style.display = 'none';
messageDisplay.style.display = 'none';
score = 0;
scoreDisplay.textContent = `Score: ${score}`;
}
</script>
</body>
</html>
How to Use:
- Copy the Code: Copy the entire code above into a new file and save it as
index.html.
- Open in Browser: Open the file in a web browser (preferably Chrome or Safari for mobile testing).
- Play the Game:
- Drag and drop (or touch and move on mobile) the ingredients (flour, egg, water) into the bowl.
- Once all ingredients are in the bowl, a "Mix Ingredients" button will appear.
- Click the "Mix Ingredients" button to mix the ingredients and create dough.
- Earn points for each step and see a success message when the dough is ready.
- The game resets automatically after a few seconds.
Notes:
- Icons: The game uses icons from Icons8 (via URLs). If you want to use custom images, replace the
background-image URLs in the CSS with your own image paths.
- Mobile Compatibility: The game is designed to work on mobile devices using touch events. Ensure you test it on a mobile browser to verify functionality.
- Enhancements: You can enhance the game by adding sound effects, more ingredients, or additional steps (e.g., rolling out the dough, cutting pasta shapes).
This code provides a fun and interactive experience for a pasta-making game, all within a single HTML file!