przygotuj mi działącą grę w snake
- User
przygotuj mi działącą grę w snake
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
przygotuj mi działącą grę w snake
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto prosty kod gry Snake w JavaScript, który możesz uruchomić w przeglądarce:
<!DOCTYPE html>
<html>
<head>
<title>Gra Snake</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #333;
background-color: #eee;
}
.game-container {
text-align: center;
}
.score {
margin-top: 10px;
font-size: 24px;
}
</style>
</head>
<body>
<div class="game-container">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="score">Wynik: <span id="scoreValue">0</span></div>
</div>
<script>
// Pobieranie elementów
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('scoreValue');
// Ustawienia gry
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let score = 0;
// Wąż
let snake = [
{x: 10, y: 10}
];
let dx = 0;
let dy = 0;
let lastDx = 0;
let lastDy = 0;
// Jedzenie
let food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// Prędkość gry
let gameSpeed = 100;
let gameInterval;
// Czy gra się zakończyła
let gameOver = false;
// Funkcja główna gry
function gameLoop() {
if (gameOver) return;
// Aktualizacja pozycji węża
updateSnake();
// Sprawdzenie kolizji
checkCollision();
// Rysowanie
drawGame();
}
// Aktualizacja pozycji węża
function updateSnake() {
// Zapisanie ostatniego kierunku
lastDx = dx;
lastDy = dy;
// Nowa głowa węża
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
// Dodanie nowej głowy na początek tablicy
snake.unshift(head);
// Sprawdzenie czy wąż zjadł jedzenie
if (head.x === food.x && head.y === food.y) {
// Zwiększenie wyniku
score++;
scoreElement.textContent = score;
// Wygenerowanie nowego jedzenia
generateFood();
// Przyspieszenie gry co 5 punktów
if (score % 5 === 0 && gameSpeed > 50) {
gameSpeed -= 10;
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, gameSpeed);
}
} else {
// Usunięcie ogona węża
snake.pop();
}
}
// Sprawdzenie kolizji
function checkCollision() {
const head = snake[0];
// Kolizja ze ścianą
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
endGame();
return;
}
// Kolizja z własnym ciałem
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
endGame();
return;
}
}
}
// Rysowanie gry
function drawGame() {
// Czyszczenie canvas
ctx.fillStyle = '#eee';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Rysowanie węża
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#2e8b57' : '#3cb371';
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
});
// Rysowanie jedzenia
ctx.fillStyle = '#ff6347';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
// Rysowanie siatki
ctx.strokeStyle = '#ddd';
ctx.lineWidth = 0.5;
for (let i = 0; i < tileCount; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize, 0);
ctx.lineTo(i * gridSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * gridSize);
ctx.lineTo(canvas.width, i * gridSize);
ctx.stroke();
}
// Rysowanie komunikatu o końcu gry
if (gameOver) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = '30px Arial';
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.fillText('Koniec gry!', canvas.width / 2, canvas.height / 2 - 20);
ctx.font = '20px Arial';
ctx.fillText(`Wynik: ${score}`, canvas.width / 2, canvas.height / 2 + 20);
ctx.fillText('Naciśnij spację, aby zagrać ponownie', canvas.width / 2, canvas.height / 2 + 60);
}
}
// Generowanie nowego jedzenia
function generateFood() {
let newFood;
let foodOnSnake;
do {
foodOnSnake = false;
newFood = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// Sprawdzenie czy jedzenie nie jest na wężu
for (let segment of snake) {
if (segment.x === newFood.x && segment.y === newFood.y) {
foodOnSnake = true;
break;
}
}
} while (foodOnSnake);
food = newFood;
}
// Zakończenie gry
function endGame() {
gameOver = true;
clearInterval(gameInterval);
drawGame();
}
// Resetowanie gry
function resetGame() {
snake = [{x: 10, y: 10}];
dx = 0;
dy = 0;
lastDx = 0;
lastDy = 0;
score = 0;
scoreElement.textContent = score;
gameSpeed = 100;
gameOver = false;
generateFood();
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, gameSpeed);
}
// Obsługa klawiatury
document.addEventListener('keydown', (e) => {
if (gameOver) {
if (e.code === 'Space') {
resetGame();
}
return;
}
switch (e.key) {
case 'ArrowUp':
if (lastDy !== 1) {
dx = 0;
dy = -1;
}
break;
case 'ArrowDown':
if (lastDy !== -1) {
dx = 0;
dy = 1;
}
break;
case 'ArrowLeft':
if (lastDx !== 1) {
dx = -1;
dy = 0;
}
break;
case 'ArrowRight':
if (lastDx !== -1) {
dx = 1;
dy = 0;
}
break;
}
});
// Rozpoczęcie gry
resetGame();
</script>
</body>
</html>
Czy chciałbyś, żebym wyjaśnił lub omówił ten kod?