Stworzę dla Ciebie grę "Piłka na kartce" w HTML5 z Canvas. Oto kompletna implementacja:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Piłka na kartce</title>
<style>
body {
margin: 0;
padding: 20px;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
#gameContainer {
background-color: white;
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 20px rgba(0,0,0,0.1);
}
#gameCanvas {
border: 2px solid #333;
cursor: pointer;
display: block;
margin: 0 auto;
}
#info {
text-align: center;
margin-top: 15px;
font-size: 18px;
}
.player1 { color: #ff0000; font-weight: bold; }
.player2 { color: #0000ff; font-weight: bold; }
#resetButton {
display: block;
margin: 15px auto 0;
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#resetButton:hover {
background-color: #45a049;
}
#winner {
text-align: center;
font-size: 24px;
margin-top: 15px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas"></canvas>
<div id="info">Tura: <span class="player1">Gracz 1</span></div>
<div id="winner"></div>
<button id="resetButton">Nowa gra</button>
</div>
<script>
class PaperSoccer {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
// Rozmiary planszy
this.gridWidth = 9;
this.gridHeight = 13;
this.cellSize = 40;
this.dotRadius = 4;
// Ustawienia canvas
this.canvas.width = this.gridWidth * this.cellSize;
this.canvas.height = this.gridHeight * this.cellSize;
// Stan gry
this.currentPlayer = 1;
this.ballPosition = { x: 4, y: 6 };
this.lines = new Set();
this.gameOver = false;
this.continuousMove = false;
// Inicjalizacja
this.initializeGrid();
this.draw();
// Event listeners
this.canvas.addEventListener('click', this.handleClick.bind(this));
}
initializeGrid() {
// Dodaj linie krawędzi planszy
for (let x = 0; x < this.gridWidth; x++) {
for (let y = 0; y < this.gridHeight; y++) {
// Górna krawędź
if (y === 0 && x < this.gridWidth - 1) {
this.addLine(x, y, x + 1, y);
}
// Dolna krawędź
if (y === this.gridHeight - 1 && x < this.gridWidth - 1) {
this.addLine(x, y, x + 1, y);
}
// Lewa krawędź (bez bramki)
if (x === 0 && y < this.gridHeight - 1) {
if (y < 5 || y >= 7) {
this.addLine(x, y, x, y + 1);
}
}
// Prawa krawędź (bez bramki)
if (x === this.gridWidth - 1 && y < this.gridHeight - 1) {
if (y < 5 || y >= 7) {
this.addLine(x, y, x, y + 1);
}
}
}
}
// Dodaj linie bramek
// Górna bramka
this.addLine(3, 0, 3, 1);
this.addLine(5, 0, 5, 1);
this.addLine(3, 1, 5, 1);
// Dolna bramka
this.addLine(3, this.gridHeight - 1, 3, this.gridHeight - 2);
this.addLine(5, this.gridHeight - 1, 5, this.gridHeight - 2);
this.addLine(3, this.gridHeight - 2, 5, this.gridHeight - 2);
}
addLine(x1, y1, x2, y2) {
// Normalizuj kierunek linii
if (x1 > x2 || (x1 === x2 && y1 > y2)) {
[x1, x2, y1, y2] = [x2, x1, y2, y1];
}
this.lines.add(`${x1},${y1}-${x2},${y2}`);
}
hasLine(x1, y1, x2, y2) {
// Normalizuj kierunek linii
if (x1 > x2 || (x1 === x2 && y1 > y2)) {
[x1, x2, y1, y2] = [x2, x1, y2, y1];
}
return this.lines.has(`${x1},${y1}-${x2},${y2}`);
}
getValidMoves() {
const moves = [];
const directions = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [1, 0],
[-1, 1], [0, 1], [1, 1]
];
for (const [dx, dy] of directions) {
const newX = this.ballPosition.x + dx;
const newY = this.ballPosition.y + dy;
if (newX >= 0 && newX < this.gridWidth &&
newY >= 0 && newY < this.gridHeight &&
!this.hasLine(this.ballPosition.x, this.ballPosition.y, newX, newY)) {
moves.push({ x: newX, y: newY });
}
}
return moves;
}
isPointUsed(x, y) {
let usedLines = 0;
const directions = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [1, 0],
[-1, 1], [0, 1], [1, 1]
];
for (const [dx, dy] of directions) {
const adjX = x + dx;
const adjY = y + dy;
if (adjX >= 0 && adjX < this.gridWidth &&
adjY >= 0 && adjY < this.gridHeight &&
this.hasLine(x, y, adjX, adjY)) {
usedLines++;
}
}
return usedLines > 0;
}
checkGoal() {
// Górna bramka
if (this.ballPosition.y === 0 && this.ballPosition.x >= 3 && this.ballPosition.x <= 5) {
this.gameOver = true;
return 2; // Gracz 2 wygrywa
}
// Dolna bramka
if (this.ballPosition.y === this.gridHeight - 1 && this.ballPosition.x >= 3 && this.ballPosition.x <= 5) {
this.gameOver = true;
return 1; // Gracz 1 wygrywa
}
return null;
}
makeMove(x, y) {
if (this.gameOver) return false;
// Sprawdź czy ruch jest dozwolony
const validMoves = this.getValidMoves();
const isValidMove = validMoves.some(move => move.x === x && move.y === y);
if (!isValidMove) return false;
// Dodaj linię
this.addLine(this.ballPosition.x, this.ballPosition.y, x, y);
// Przesuń piłkę
this.ballPosition = { x, y };
// Sprawdź gola
const winner = this.checkGoal();
if (winner) {
document.getElementById('winner').innerHTML =
`Wygrywa <span class="${winner === 1 ? 'player1' : 'player2'}">Gracz ${winner}</span>!`;
document.getElementById('info').style.display = 'none';
return true;
}
// Sprawdź czy gracz może kontynuować ruch
if (this.isPointUsed(x, y) && this.getValidMoves().length > 0) {
this.continuousMove = true;
} else {
this.continuousMove = false;
// Zmień gracza
this.currentPlayer = this.currentPlayer === 1 ? 2 : 1;
this.updateInfo();
}
// Sprawdź czy nie ma pata
if (this.getValidMoves().length === 0) {
this.gameOver = true;
document.getElementById('winner').innerHTML =
`Przegrywa <span class="${this.currentPlayer === 1 ? 'player1' : 'player2'}">Gracz ${this.currentPlayer}</span>!`;
document.getElementById('info').style.display = 'none';
}
return true;
}
handleClick(event) {
if (this.gameOver) return;
const rect = this.canvas.getBoundingClientRect();
const x = Math.round((event.clientX - rect.left) / this.cellSize);
const y = Math.round((event.clientY - rect.top) / this.cellSize);
if (this.makeMove(x, y)) {
this.draw();
}
}
updateInfo() {
const infoElement = document.getElementById('info');
const playerClass = this.currentPlayer === 1 ? 'player1' : 'player2';
let text = `Tura: <span class="${playerClass}">Gracz ${this.currentPlayer}</span>`;
if (this.continuousMove) {
text += ' (kontynuuj ruch)';
}
infoElement.innerHTML = text;
}
draw() {
// Czyść canvas
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
// Rysuj siatkę punktów
this.ctx.fillStyle = '#666';
for (let x = 0; x < this.gridWidth; x++) {
for (let y = 0; y < this.gridHeight; y++) {
this.ctx.beginPath();
this.ctx.arc(x * this.cellSize, y * this.cellSize, this.dotRadius, 0, Math.PI * 2);
this.ctx.fill();
}
}
// Rysuj bramki
this.ctx.strokeStyle = '#00ff00';
this.ctx.lineWidth = 3;
// Górna bramka
this.ctx.strokeRect(3 * this.cellSize, -10, 2 * this.cellSize, this.cellSize + 10);
// Dolna bramka
this.ctx.strokeRect(3 * this.cellSize, (this.gridHeight - 1) * this.cellSize,
2 * this.cellSize, this.cellSize + 10);
// Rysuj linie
this.ctx.strokeStyle = '#333';
this.ctx.lineWidth = 2;
for (const line of this.lines) {
const [start, end] = line.split('-');
const [x1, y1] = start.split(',').map(Number);
const [x2, y2] = end.split(',').map(Number);
this.ctx.beginPath();
this.ctx.moveTo(x1 * this.cellSize, y1 * this.cellSize);
this.ctx.lineTo(x2 * this.cellSize, y2 * this.cellSize);
this.ctx.stroke();
}
// Rysuj możliwe ruchy
if (!this.gameOver) {
const validMoves = this.getValidMoves();
this.ctx.fillStyle = 'rgba(0, 255, 0, 0.3)';
for (const move of validMoves) {
this.ctx.beginPath();
this.ctx.arc(move.x * this.cellSize, move.y * this.cellSize,
this.dotRadius + 2, 0, Math.PI * 2);
this.ctx.fill();
}
}
// Rysuj piłkę
this.ctx.fillStyle = this.currentPlayer === 1 ? '#ff0000' : '#0000ff';
this.ctx.beginPath();
this.ctx.arc(this.ballPosition.x * this.cellSize,
this.ballPosition.y * this.cellSize,
this.dotRadius + 4, 0, Math.PI * 2);
this.ctx.fill();
}
reset() {
this.currentPlayer = 1;
this.ballPosition = { x: 4, y: 6 };
this.lines = new Set();
this.gameOver = false;
this.continuousMove = false;
this.initializeGrid();
this.updateInfo();
this.draw();
document.getElementById('winner').innerHTML = '';
document.getElementById('info').style.display = 'block';
}
}
// Inicjalizacja gry
const canvas = document.getElementById('gameCanvas');
const game = new PaperSoccer(canvas);
// Reset button
document.getElementById('resetButton').addEventListener('click', () => {
game.reset();
});
</script>
</body>
</html>
Gra zawiera wszystkie kluczowe elementy:
- Plansza - siatka 9x13 punktów z bramkami u góry i na dole
- Mechanika ruchu - gracze na zmianę rysują linie w 8 kierunkach
- Zasady:
- Nie można przechodzić przez już narysowane linie
- Gracz kontynuuje ruch gdy dotrze do punktu z już narysowanymi liniami
- Gra kończy się gdy piłka wpadnie do bramki lub gracz nie ma ruchu
- Interfejs:
- Czerwona piłka dla gracza 1, niebieska dla gracza 2
- Zielone podświetlenie pokazuje możliwe ruchy
- Informacja o aktualnym graczu
- Przycisk nowej gry
Gra jest w pełni grywalna - klikaj na zielone punkty aby wykonać ruch!