Створити браузерну гру
- User
Створити браузерну гру
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Створити браузерну гру
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Я допоможу вам створити браузерну гру! Ось простий приклад гри "Збирач монет" з використанням HTML, CSS та JavaScript:
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Збирач монет</title>
<style>
body {
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.game-container {
background: white;
border-radius: 15px;
padding: 20px;
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
#gameCanvas {
border: 3px solid #333;
border-radius: 10px;
background: #87CEEB;
}
.game-info {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
font-size: 18px;
font-weight: bold;
}
.controls {
text-align: center;
margin-top: 10px;
color: #666;
}
</style>
</head>
<body>
<div class="game-container">
<div class="game-info">
<div>Рахунок: <span id="score">0</span></div>
<div>Життя: <span id="lives">3</span></div>
</div>
<canvas id="gameCanvas" width="600" height="400"></canvas>
<div class="controls">
Використовуйте стрілки або WASD для руху
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
// Гравець
const player = {
x: 50,
y: 200,
width: 30,
height: 30,
speed: 5,
color: '#FF6B6B'
};
// Ігрові об'єкти
let coins = [];
let enemies = [];
let score = 0;
let lives = 3;
let gameRunning = true;
// Керування
const keys = {};
document.addEventListener('keydown', (e) => {
keys[e.key.toLowerCase()] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.key.toLowerCase()] = false;
});
// Створення монет
function createCoin() {
coins.push({
x: canvas.width,
y: Math.random() * (canvas.height - 20),
width: 20,
height: 20,
speed: 3,
color: '#FFD700'
});
}
// Створення ворогів
function createEnemy() {
enemies.push({
x: canvas.width,
y: Math.random() * (canvas.height - 25),
width: 25,
height: 25,
speed: 4,
color: '#FF4757'
});
}
// Перевірка колізій
function checkCollision(rect1, rect2) {
return rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y;
}
// Оновлення гри
function update() {
if (!gameRunning) return;
// Рух гравця
if ((keys['arrowup'] || keys['w']) && player.y > 0) {
player.y -= player.speed;
}
if ((keys['arrowdown'] || keys['s']) && player.y < canvas.height - player.height) {
player.y += player.speed;
}
if ((keys['arrowleft'] || keys['a']) && player.x > 0) {
player.x -= player.speed;
}
if ((keys['arrowright'] || keys['d']) && player.x < canvas.width - player.width) {
player.x += player.speed;
}
// Оновлення монет
coins.forEach((coin, index) => {
coin.x -= coin.speed;
// Перевірка зібрання монети
if (checkCollision(player, coin)) {
coins.splice(index, 1);
score += 10;
scoreElement.textContent = score;
}
// Видалення монет за межами екрану
if (coin.x + coin.width < 0) {
coins.splice(index, 1);
}
});
// Оновлення ворогів
enemies.forEach((enemy, index) => {
enemy.x -= enemy.speed;
// Перевірка зіткнення з ворогом
if (checkCollision(player, enemy)) {
enemies.splice(index, 1);
lives--;
livesElement.textContent = lives;
if (lives <= 0) {
gameRunning = false;
alert(`Гра закінчена! Ваш рахунок: ${score}`);
location.reload();
}
}
// Видалення ворогів за межами екрану
if (enemy.x + enemy.width < 0) {
enemies.splice(index, 1);
}
});
// Створення нових об'єктів
if (Math.random() < 0.02) {
createCoin();
}
if (Math.random() < 0.015) {
createEnemy();
}
}
// Відображення гри
function draw() {
// Очищення canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Малювання гравця
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
// Додавання простих деталей гравця
ctx.fillStyle = 'white';
ctx.fillRect(player.x + 5, player.y + 5, 8, 8);
ctx.fillRect(player.x + 17, player.y + 5, 8, 8);
// Малювання монет
coins.forEach(coin => {
ctx.fillStyle = coin.color;
ctx.beginPath();
ctx.arc(coin.x + coin.width/2, coin.y + coin.height/2, coin.width/2, 0, Math.PI * 2);
ctx.fill();
});
// Малювання ворогів
enemies.forEach(enemy => {
ctx.fillStyle = enemy.color;
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
});
}
// Головний ігровий цикл
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Запуск гри
gameLoop();
</script>
</body>
</html>
Ця гра включає:
Геймплей: Гравець керує червоним квадратом, збирає золоті монети та уникає червоних ворогів.
Керування: Стрілки або клавіші WASD для руху у всіх напрямках.
Механіки: Система очок за зібрані монети, система життів, випадкове створення об'єктів.
Візуал: Простий але привабливий дизайн з градієнтами та тінями.
Щоб запустити гру, просто збережіть код у файл з розширенням .html та відкрийте у браузері. Гра автоматично почнеться, і ви зможете грати одразу!
Хочете, щоб я додав якісь інші функції або створив інший тип гри?
Хочу
Чудово! Ось кілька варіантів, які я можу створити для вас:
Типи ігор:
Або додаткові функції до поточної гри:
Що вас більше цікавить? Або якщо у вас є своя ідея для гри, розкажіть мені про неї!