stwórz prostą grę Snake w html
- User
stwórz prostą grę Snake w html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
stwórz prostą grę Snake w html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto prosty kod gry Snake w HTML z JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Prosta Gra Snake</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
canvas {
border: 3px solid #333;
background-color: #000;
margin-top: 20px;
}
#score {
font-size: 24px;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Gra Snake</h1>
<div id="score">Punkty: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
// Ustawienia gry
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const box = 20;
const canvasSize = 400;
const boxes = canvasSize / box;
let score = 0;
// Wąż
let snake = [];
snake[0] = { x: 10 * box, y: 10 * box };
// Jedzenie
let food = {
x: Math.floor(Math.random() * boxes) * box,
y: Math.floor(Math.random() * boxes) * box
};
// Kierunek
let direction;
// Sterowanie
document.addEventListener("keydown", changeDirection);
function changeDirection(event) {
if(event.keyCode == 37 && direction != "RIGHT") {
direction = "LEFT";
} else if(event.keyCode == 38 && direction != "DOWN") {
direction = "UP";
} else if(event.keyCode == 39 && direction != "LEFT") {
direction = "RIGHT";
} else if(event.keyCode == 40 && direction != "UP") {
direction = "DOWN";
}
}
// Sprawdzanie kolizji
function collision(head, array) {
for(let i = 0; i < array.length; i++) {
if(head.x == array[i].x && head.y == array[i].y) {
return true;
}
}
return false;
}
// Rysowanie gry
function draw() {
// Czyszczenie canvas
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvasSize, canvasSize);
// Rysowanie węża
for(let i = 0; i < snake.length; i++) {
ctx.fillStyle = (i == 0) ? "green" : "lime";
ctx.fillRect(snake[i].x, snake[i].y, box, box);
ctx.strokeStyle = "darkgreen";
ctx.strokeRect(snake[i].x, snake[i].y, box, box);
}
// Rysowanie jedzenia
ctx.fillStyle = "red";
ctx.fillRect(food.x, food.y, box, box);
// Stara pozycja głowy
let snakeX = snake[0].x;
let snakeY = snake[0].y;
// Zmiana kierunku
if(direction == "LEFT") snakeX -= box;
if(direction == "RIGHT") snakeX += box;
if(direction == "UP") snakeY -= box;
if(direction == "DOWN") snakeY += box;
// Jeśli wąż zje jedzenie
if(snakeX == food.x && snakeY == food.y) {
score++;
document.getElementById("score").innerHTML = "Punkty: " + score;
food = {
x: Math.floor(Math.random() * boxes) * box,
y: Math.floor(Math.random() * boxes) * box
};
} else {
// Usuwanie ogona
snake.pop();
}
// Nowa głowa
let newHead = {
x: snakeX,
y: snakeY
};
// Koniec gry - kolizja ze ścianą lub z wężem
if(snakeX < 0 || snakeX >= canvasSize ||
snakeY < 0 || snakeY >= canvasSize ||
collision(newHead, snake)) {
clearInterval(game);
alert("Koniec gry! Twój wynik: " + score);
location.reload();
}
// Dodawanie nowej głowy
snake.unshift(newHead);
}
// Uruchomienie gry
let game = setInterval(draw, 100);
</script>
</body>
</html>
Czy chciałbyś, żebym wyjaśnił lub omówił ten kod?