stworz gre wezyk
- User
stworz gre wezyk
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
stworz gre wezyk
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<!DOCTYPE html>
<html>
<head>
<title>Gra Wężyk</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #333;
background-color: #fff;
}
#score {
margin-top: 10px;
font-size: 24px;
}
#start-btn {
margin-top: 20px;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Gra Wężyk</h1>
<div id="score">Wynik: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<button id="start-btn">Start Gry</button>
<script>
// Inicjalizacja zmiennych
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startButton = document.getElementById('start-btn');
const blockSize = 20;
const width = canvas.width / blockSize;
const height = canvas.height / blockSize;
let snake;
let food;
let score;
let direction;
let gameInterval;
let gameSpeed = 130;
let gameRunning = false;
// Inicjalizacja gry
function init() {
snake = [
{x: 5, y: 5},
{x: 4, y: 5},
{x: 3, y: 5}
];
direction = 'right';
score = 0;
scoreElement.textContent = 'Wynik: ' + score;
createFood();
if (gameInterval) clearInterval(gameInterval);
gameRunning = true;
gameInterval = setInterval(gameLoop, gameSpeed);
}
// Tworzenie jedzenia w losowym miejscu
function createFood() {
food = {
x: Math.floor(Math.random() * width),
y: Math.floor(Math.random() * height)
};
// Upewnienie się, że jedzenie nie pojawi się na wężu
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
createFood();
return;
}
}
}
// Główna pętla gry
function gameLoop() {
if (!gameRunning) return;
// Poruszanie węża
const head = {x: snake[0].x, y: snake[0].y};
switch(direction) {
case 'right': head.x++; break;
case 'left': head.x--; break;
case 'up': head.y--; break;
case 'down': head.y++; break;
}
// Sprawdzanie kolizji
if (
head.x < 0 || head.x >= width ||
head.y < 0 || head.y >= height ||
checkCollision(head, snake)
) {
gameOver();
return;
}
// Zjadanie jedzenia
const ateFood = head.x === food.x && head.y === food.y;
if (ateFood) {
score++;
scoreElement.textContent = 'Wynik: ' + score;
createFood();
}
// Dodawanie nowej głowy
snake.unshift(head);
// Usuwanie ogona, jeśli nie zjedzono jedzenia
if (!ateFood) {
snake.pop();
}
// Rysowanie planszy
draw();
}
// Sprawdzanie kolizji z ciałem węża
function checkCollision(position, bodyArray) {
for (let i = 0; i < bodyArray.length; i++) {
if (position.x === bodyArray[i].x && position.y === bodyArray[i].y) {
return true;
}
}
return false;
}
// Rysowanie gry
function draw() {
// Czyszczenie planszy
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Rysowanie węża
for (let segment of snake) {
ctx.fillStyle = 'green';
ctx.fillRect(segment.x * blockSize, segment.y * blockSize, blockSize, blockSize);
ctx.strokeStyle = 'darkgreen';
ctx.strokeRect(segment.x * blockSize, segment.y * blockSize, blockSize, blockSize);
}
// Rysowanie głowy w innym kolorze
ctx.fillStyle = 'darkgreen';
ctx.fillRect(snake[0].x * blockSize, snake[0].y * blockSize, blockSize, blockSize);
// Rysowanie jedzenia
ctx.fillStyle = 'red';
ctx.fillRect(food.x * blockSize, food.y * blockSize, blockSize, blockSize);
}
// Obsługa klawiatury
document.addEventListener('keydown', (event) => {
if (!gameRunning) return;
switch(event.key) {
case 'ArrowRight':
if (direction !== 'left') direction = 'right';
break;
case 'ArrowLeft':
if (direction !== 'right') direction = 'left';
break;
case 'ArrowUp':
if (direction !== 'down') direction = 'up';
break;
case 'ArrowDown':
if (direction !== 'up') direction = 'down';
break;
}
});
// Koniec gry
function gameOver() {
gameRunning = false;
clearInterval(gameInterval);
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.font = '30px Arial';
ctx.textAlign = 'center';
ctx.fillText('Koniec Gry!', canvas.width / 2, canvas.height / 2);
ctx.font = '20px Arial';
ctx.fillText('Wynik: ' + score, canvas.width / 2, canvas.height / 2 + 40);
}
// Obsługa przycisku start
startButton.addEventListener('click', init);
// Początkowe rysowanie planszy
draw();
</script>
</body>
</html>
Czy chciałbyś, żebym wyjaśnił, jak działa ten kod lub jakieś konkretne elementy gry?