gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Придумай браузерную игру и напиши её код в одном файле js, css и html
- User
Придумай браузерную игру и напиши её код в одном файле js, css и html
- Assistant
Придумай браузерную игру и напиши её код в одном файле js, css и html
Создадим простую браузерную игру "Уклоняйся от квадратов". В этой игре игрок управляет маленьким человечком и должен уклоняться от падающих квадратов. Если квадрат попадает в игрока, игра заканчивается.
Вот пример кода для игры:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Уклоняйся от квадратов</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="game-container">
<div id="player"></div>
</div>
<script src="game.js"></script>
</body>
</html>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#game-container {
position: relative;
width: 400px;
height: 600px;
background-color: white;
overflow: hidden;
border: 2px solid #333;
}
#player {
position: absolute;
bottom: 10px;
left: 50%;
width: 30px;
height: 30px;
background-color: #007bff;
transform: translateX(-50%);
}
.square {
position: absolute;
top: 0;
width: 30px;
height: 30px;
background-color: red;
animation: fall linear infinite;
}
@keyframes fall {
from { top: 0; }
to { top: 600px; }
}
const gameContainer = document.getElementById('game-container');
const player = document.getElementById('player');
let isGameOver = false;
let score = 0;
function createSquare() {
const square = document.createElement('div');
square.classList.add('square');
square.style.left = Math.floor(Math.random() * (gameContainer.clientWidth - 30)) + 'px';
gameContainer.appendChild(square);
moveSquare(square);
}
function moveSquare(square) {
let squareInterval = setInterval(() => {
let squareTop = square.offsetTop;
if (squareTop > gameContainer.clientHeight) {
clearInterval(squareInterval);
square.remove();
if (!isGameOver) {
createSquare();
score++;
}
} else {
if (detectCollision(square)) {
clearInterval(squareInterval);
alert('Игра окончена! Ваш счет: ' + score);
isGameOver = true;
}
square.style.top = squareTop + 2 + 'px';
}
}, 20);
}
function detectCollision(square) {
const playerRect = player.getBoundingClientRect();
const squareRect = square.getBoundingClientRect();
return !(playerRect.right < squareRect.left ||
playerRect.left > squareRect.right ||
playerRect.bottom < squareRect.top ||
playerRect.top > squareRect.bottom);
}
document.addEventListener('keydown', (e) => {
if (isGameOver) return;
const playerLeft = player.offsetLeft;
if (e.key === 'ArrowLeft' && playerLeft > 0) {
player.style.left = playerLeft - 10 + 'px';
} else if (e.key === 'ArrowRight' && playerLeft < (gameContainer.clientWidth - player.clientWidth)) {
player.style.left = playerLeft + 10 + 'px';
}
});
createSquare();
Not yet included in a published release.