html
<!DOCTYPE html>
<html>
<head>
<title>Łap Owoce!</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
#game {
width: 400px;
height: 500px;
background-color: #87CEEB;
margin: 0 auto;
position: relative;
overflow: hidden;
border: 2px solid #333;
border-radius: 10px;
}
#player {
width: 80px;
height: 50px;
background-color: #8B4513;
position: absolute;
bottom: 0;
left: 160px;
border-radius: 5px 5px 0 0;
}
.fruit {
width: 30px;
height: 30px;
border-radius: 50%;
position: absolute;
top: -30px;
}
#score {
font-size: 24px;
margin: 10px;
}
#time {
font-size: 18px;
margin: 10px;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0,0,0,0.7);
color: white;
padding: 20px;
border-radius: 10px;
display: none;
font-size: 24px;
}
button {
padding: 10px 20px;
font-size: 16px;
margin-top: 10px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Łap Owoce!</h1>
<div id="score">Punkty: 0</div>
<div id="time">Czas: 60</div>
<div id="game">
<div id="player"></div>
<div id="gameOver">
Koniec gry!<br>
<span id="finalScore"></span><br>
<button onclick="startGame()">Zagraj ponownie</button>
</div>
</div>
<script>
const player = document.getElementById('player');
const game = document.getElementById('game');
const scoreDisplay = document.getElementById('score');
const timeDisplay = document.getElementById('time');
const gameOverScreen = document.getElementById('gameOver');
const finalScoreDisplay = document.getElementById('finalScore');
let score = 0;
let timeLeft = 60;
let gameInterval;
let timeInterval;
let fruits = [];
let playerPos = 160;
let isGameActive = false;
// Kolory owoców
const fruitColors = ['#ff0000', '#ffa500', '#ffff00', '#008000', '#800080'];
function movePlayer(e) {
if (!isGameActive) return;
const gameWidth = game.offsetWidth;
const playerWidth = player.offsetWidth;
if (e.key === 'ArrowLeft' && playerPos > 0) {
playerPos -= 20;
} else if (e.key === 'ArrowRight' && playerPos < gameWidth - playerWidth) {
playerPos += 20;
}
player.style.left = playerPos + 'px';
}
function createFruit() {
if (!isGameActive) return;
const fruit = document.createElement('div');
fruit.className = 'fruit';
const randomColor = fruitColors[Math.floor(Math.random() * fruitColors.length)];
fruit.style.backgroundColor = randomColor;
const gameWidth = game.offsetWidth;
const fruitWidth = 30;
const randomPos = Math.random() * (gameWidth - fruitWidth);
fruit.style.left = randomPos + 'px';
game.appendChild(fruit);
fruits.push({
element: fruit,
position: {
x: randomPos,
y: -30
},
speed: 2 + Math.random() * 3
});
}
function updateGame() {
if (!isGameActive) return;
const playerRect = player.getBoundingClientRect();
for (let i = 0; i < fruits.length; i++) {
const fruit = fruits[i];
fruit.position.y += fruit.speed;
fruit.element.style.top = fruit.position.y + 'px';
const fruitRect = fruit.element.getBoundingClientRect();
// Sprawdzenie kolizji
if (
fruitRect.bottom >= playerRect.top &&
fruitRect.right >= playerRect.left &&
fruitRect.left <= playerRect.right &&
fruitRect.top <= playerRect.bottom
) {
score += 10;
scoreDisplay.textContent = 'Punkty: ' + score;
game.removeChild(fruit.element);
fruits.splice(i, 1);
i--;
}
// Usunięcie owocu, jeśli spadł poza ekran
else if (fruit.position.y > game.offsetHeight) {
game.removeChild(fruit.element);
fruits.splice(i, 1);
i--;
}
}
}
function updateTime() {
if (!isGameActive) return;
timeLeft--;
timeDisplay.textContent = 'Czas: ' + timeLeft;
if (timeLeft <= 0) {
endGame();
}
}
function endGame() {
isGameActive = false;
clearInterval(gameInterval);
clearInterval(timeInterval);
// Usuń wszystkie owoce
fruits.forEach(fruit => {
game.removeChild(fruit.element);
});
fruits = [];
finalScoreDisplay.textContent = 'Twój wynik: ' + score;
gameOverScreen.style.display = 'block';
}
function startGame() {
// Reset gry
score = 0;
timeLeft = 60;
playerPos = 160;
player.style.left = playerPos + 'px';
scoreDisplay.textContent = 'Punkty: 0';
timeDisplay.textContent = 'Czas: 60';
gameOverScreen.style.display = 'none';
// Usuń wszystkie owoce
fruits.forEach(fruit => {
if (fruit.element.parentNode) {
game.removeChild(fruit.element);
}
});
fruits = [];
isGameActive = true;
// Ustawienie interwałów
gameInterval = setInterval(() => {
updateGame();
if (Math.random() < 0.05) {
createFruit();
}
}, 20);
timeInterval = setInterval(updateTime, 1000);
}
// Obsługa klawiatury
document.addEventListener('keydown', movePlayer);
// Automatyczne rozpoczęcie gry
startGame();
</script>
</body>
</html>