zrób żeby przenikać przez ściany
- User
zrób żeby przenikać przez ściany
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
zrób żeby przenikać przez ściany
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Świetnie! Zmodyfikuję grę tak, aby wąż mógł przechodzić przez ściany – czyli zamiast kończyć grę przy kolizji ze ścianą, wąż będzie pojawiał się po przeciwnej stronie ekranu (tzw. teleportacja).
gameOver() przy wyjeździe poza planszę.<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game - Przechodzenie przez ściany</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #1a1a1a;
color: white;
font-family: Arial, sans-serif;
}
#gameCanvas {
border: 2px solid white;
background-color: #333;
}
#score {
font-size: 24px;
margin: 10px 0;
}
</style>
</head>
<body>
<h1>Snake Game</h1>
<div id="score">Wynik: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{ x: 10, y: 10 }];
let food = { x: 15, y: 15 };
let dx = 1;
let dy = 0;
let score = 0;
let gameLoop;
function drawSnake() {
snake.forEach(segment => {
ctx.fillStyle = '#00ff00';
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
});
}
function drawFood() {
ctx.fillStyle = '#ff0000';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
}
function update() {
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
// 🔁 Teleportacja przez ściany
if (head.x < 0) head.x = tileCount - 1;
if (head.x >= tileCount) head.x = 0;
if (head.y < 0) head.y = tileCount - 1;
if (head.y >= tileCount) head.y = 0;
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = `Wynik: ${score}`;
spawnFood();
} else {
snake.pop();
}
checkCollisions();
ctx.fillStyle = '#333';
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawSnake();
drawFood();
}
function spawnFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
const onSnake = snake.some(segment => segment.x === food.x && segment.y === food.y);
if (onSnake) spawnFood();
}
function checkCollisions() {
const head = snake[0];
// Kolizja z ciałem
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
}
}
}
function gameOver() {
clearInterval(gameLoop);
alert(`Koniec gry! Wynik: ${score}\nNaciśnij OK, aby zrestartować.`);
location.reload();
}
document.addEventListener('keydown', (e) => {
switch (e.key) {
case 'ArrowUp':
if (dy !== 1) { dx = 0; dy = -1; }
break;
case 'ArrowDown':
if (dy !== -1) { dx = 0; dy = 1; }
break;
case 'ArrowLeft':
if (dx !== 1) { dx = -1; dy = 0; }
break;
case 'ArrowRight':
if (dx !== -1) { dx = 1; dy = 0; }
break;
}
});
function startGame() {
gameLoop = setInterval(update, 100);
}
startGame();
</script>
</body>
</html>
Chcesz dodać coś więcej? Oto kilka pomysłów:
Daj znać, jeśli chcesz dodać którąś z tych opcji!