Oto prosta gra w węża napisana w HTML z tabelą wyników:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gra Wąż</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
#game-container {
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 20px;
margin-top: 20px;
}
#game-board {
border: 2px solid #333;
background-color: #222;
}
#score-board {
width: 300px;
border: 2px solid #333;
border-collapse: collapse;
margin: 20px auto;
}
#score-board th, #score-board td {
border: 1px solid #333;
padding: 8px;
}
#score-board th {
background-color: #444;
color: white;
}
#score-board tr:nth-child(even) {
background-color: #f2f2f2;
}
#score-board tr:nth-child(odd) {
background-color: #e6e6e6;
}
button {
padding: 10px 20px;
margin: 10px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#player-form {
margin: 20px;
}
input {
padding: 8px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Gra Wąż</h1>
<div id="player-form">
<input type="text" id="player-name" placeholder="Wpisz swoje imię">
<button id="start-game">Rozpocznij grę</button>
</div>
<div id="game-info">
<p>Aktualny gracz: <span id="current-player">Gość</span></p>
<p>Wynik: <span id="score">0</span></p>
</div>
<div id="game-container">
<canvas id="game-board" width="400" height="400"></canvas>
</div>
<h2>Tabela wyników</h2>
<table id="score-board">
<thead>
<tr>
<th>Miejsce</th>
<th>Gracz</th>
<th>Wynik</th>
<th>Data</th>
</tr>
</thead>
<tbody id="score-table-body">
<!-- Tutaj będą dodawane wyniki graczy -->
</tbody>
</table>
<script>
// Zmienne gry
let canvas, ctx;
let snake = [];
let food = {};
let direction = 'right';
let gameInterval;
let score = 0;
let gameSpeed = 100;
let gridSize = 20;
let gameRunning = false;
let currentPlayer = "Gość";
let highScores = JSON.parse(localStorage.getItem('snakeHighScores')) || [];
// Inicjalizacja gry
document.getElementById('start-game').addEventListener('click', startGame);
function startGame() {
if (gameRunning) return;
// Pobierz imię gracza
let playerName = document.getElementById('player-name').value.trim();
currentPlayer = playerName || "Gość";
document.getElementById('current-player').textContent = currentPlayer;
// Resetuj wynik
score = 0;
document.getElementById('score').textContent = score;
// Inicjalizacja planszy
canvas = document.getElementById('game-board');
ctx = canvas.getContext('2d');
// Inicjalizacja węża
snake = [
{x: 5 * gridSize, y: 10 * gridSize},
{x: 4 * gridSize, y: 10 * gridSize},
{x: 3 * gridSize, y: 10 * gridSize}
];
// Inicjalizacja jedzenia
createFood();
// Ustawienie kierunku
direction = 'right';
// Rozpoczęcie gry
gameRunning = true;
gameInterval = setInterval(gameLoop, gameSpeed);
// Obsługa klawiatury
document.addEventListener('keydown', changeDirection);
}
function gameLoop() {
// Wyczyść planszę
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Rysuj węża
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#4CAF50' : '#8BC34A';
ctx.fillRect(segment.x, segment.y, gridSize, gridSize);
ctx.strokeStyle = '#222';
ctx.strokeRect(segment.x, segment.y, gridSize, gridSize);
});
// Rysuj jedzenie
ctx.fillStyle = '#FF5722';
ctx.fillRect(food.x, food.y, gridSize, gridSize);
// Poruszaj wężem
moveSnake();
// Sprawdź kolizje
checkCollisions();
}
function moveSnake() {
// Stwórz nową głowę
const head = {x: snake[0].x, y: snake[0].y};
// Zmień pozycję głowy w zależności od kierunku
switch(direction) {
case 'up':
head.y -= gridSize;
break;
case 'down':
head.y += gridSize;
break;
case 'left':
head.x -= gridSize;
break;
case 'right':
head.x += gridSize;
break;
}
// Dodaj nową głowę na początek węża
snake.unshift(head);
// Sprawdź czy wąż zjadł jedzenie
if (head.x === food.x && head.y === food.y) {
// Zwiększ wynik
score++;
document.getElementById('score').textContent = score;
// Stwórz nowe jedzenie
createFood();
// Przyspiesz grę co 5 punktów
if (score % 5 === 0 && gameSpeed > 50) {
clearInterval(gameInterval);
gameSpeed -= 5;
gameInterval = setInterval(gameLoop, gameSpeed);
}
} else {
// Usuń ogon węża
snake.pop();
}
}
function createFood() {
// Stwórz jedzenie w losowym miejscu
food = {
x: Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize,
y: Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize
};
// Sprawdź czy jedzenie nie pojawiło się na wężu
const onSnake = snake.some(segment => segment.x === food.x && segment.y === food.y);
if (onSnake) createFood();
}
function checkCollisions() {
const head = snake[0];
// Kolizja ze ścianą
if (
head.x < 0 ||
head.x >= canvas.width ||
head.y < 0 ||
head.y >= canvas.height
) {
gameOver();
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) {
gameOver();
return;
}
}
}
function changeDirection(event) {
const key = event.key;
// Zapobiegaj zmianie kierunku o 180 stopni
if (key === 'ArrowUp' && direction !== 'down') {
direction = 'up';
} else if (key === 'ArrowDown' && direction !== 'up') {
direction = 'down';
} else if (key === 'ArrowLeft' && direction !== 'right') {
direction = 'left';
} else if (key === 'ArrowRight' && direction !== 'left') {
direction = 'right';
}
}
function gameOver() {
clearInterval(gameInterval);
gameRunning = false;
// Zapisz wynik
saveScore();
// Wyświetl komunikat
alert(`Koniec gry! Twój wynik: ${score}`);
// Usuń obsługę klawiatury
document.removeEventListener('keydown', changeDirection);
}
function saveScore() {
// Dodaj nowy wynik
const newScore = {
player: currentPlayer,
score: score,
date: new Date().toLocaleDateString()
};
highScores.push(newScore);
// Sortuj wyniki malejąco
highScores.sort((a, b) => b.score - a.score);
// Ogranicz do 10 najlepszych wyników
if (highScores.length > 10) {
highScores = highScores.slice(0, 10);
}
// Zapisz w localStorage
localStorage.setItem('snakeHighScores', JSON.stringify(highScores));
// Aktualizuj tabelę wyników
updateScoreBoard();
}
function updateScoreBoard() {
const tableBody = document.getElementById('score-table-body');
tableBody.innerHTML = '';
highScores.forEach((scoreData, index) => {
const row = document.createElement('tr');
const placeCell = document.createElement('td');
placeCell.textContent = index + 1;
const playerCell = document.createElement('td');
playerCell.textContent = scoreData.player;
const scoreCell = document.createElement('td');
scoreCell.textContent = scoreData.score;
const dateCell = document.createElement('td');
dateCell.textContent = scoreData.date;
row.appendChild(placeCell);
row.appendChild(playerCell);
row.appendChild(scoreCell);
row.appendChild(dateCell);
tableBody.appendChild(row);
});
}
// Inicjalizacja tabeli wyników przy załadowaniu strony
updateScoreBoard();
</script>
</body>
</html>
Czy chciałbyś, żebym wyjaśnił lub omówił ten kod?