github verknüpfung und neuses snake
This commit is contained in:
@ -1,51 +1,222 @@
|
||||
<!DOCTYPE html>
|
||||
<!-- saved from url=(0060)https://test.creative-crafter.de/programmieren/spiele/Snake/ -->
|
||||
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Snake Game</title>
|
||||
<style>
|
||||
body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #000; }
|
||||
canvas { border: 1px solid #fff; }
|
||||
#startScreen { display: none; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; text-align: center; }
|
||||
</style>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>Snake Game</title>
|
||||
<style>
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
background: linear-gradient(135deg, #3498db, #9b59b6);
|
||||
color: white;
|
||||
font-family: "Coda", system-ui;
|
||||
font-weight: 800;
|
||||
font-style: normal;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
canvas {
|
||||
border: 4px solid #ffffff81;
|
||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
|
||||
background-color: #2c3e5085;
|
||||
}
|
||||
#overlay {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
text-align: center;
|
||||
display: none;
|
||||
}
|
||||
#overlay h1 {
|
||||
font-size: 3em;
|
||||
margin-bottom: 0.5em;
|
||||
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
|
||||
}
|
||||
#scoreboard {
|
||||
position: absolute;
|
||||
top: 20px;
|
||||
left: 20px;
|
||||
font-size: 18px;
|
||||
z-index: 2;
|
||||
}
|
||||
.score-text {
|
||||
font-size: 1.5em;
|
||||
font-weight: bold;
|
||||
}
|
||||
.highscore-text {
|
||||
font-size: 1.5em;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
||||
<div id="startScreen" style="display: block;"><h1>Snake Game</h1><p>Press Space to Start</p></div>
|
||||
<script>
|
||||
const canvas = document.getElementById('gameCanvas'), ctx = canvas.getContext('2d'), startScreen = document.getElementById('startScreen');
|
||||
const gridSize = 20; let snake = [{ x: 200, y: 200 }], direction = { x: 0, y: 0 }, food = { x: 0, y: 0 }, gameOver = false, gameStarted = false;
|
||||
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
||||
<div id="scoreboard">
|
||||
<div class="score-text">Score: <span id="score">0</span></div>
|
||||
<div class="highscore-text">Highscore: <span id="highscore">0</span></div>
|
||||
</div>
|
||||
<div id="overlay">
|
||||
<h1>Snake Game</h1>
|
||||
<p>Press Space to Start</p>
|
||||
</div>
|
||||
|
||||
const getRandomFoodPosition = () => ({ x: Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize, y: Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize });
|
||||
const drawRect = (x, y, color) => { ctx.fillStyle = color; ctx.fillRect(x, y, gridSize, gridSize); };
|
||||
const update = () => {
|
||||
if (gameOver) return;
|
||||
const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
|
||||
if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || snake.some(segment => segment.x === head.x && segment.y === head.y)) {
|
||||
gameOver = true;
|
||||
location.reload(); // Reload the page when the game is over
|
||||
return;
|
||||
}
|
||||
snake.unshift(head);
|
||||
if (head.x === food.x && head.y === food.y) food = getRandomFoodPosition(); else snake.pop();
|
||||
};
|
||||
const draw = () => { ctx.clearRect(0, 0, canvas.width, canvas.height); snake.forEach(segment => drawRect(segment.x, segment.y, 'lime')); drawRect(food.x, food.y, 'red'); };
|
||||
const gameLoop = () => { update(); draw(); if (!gameOver) setTimeout(gameLoop, 100); };
|
||||
const resetGame = () => { snake = [{ x: 200, y: 200 }]; direction = { x: 0, y: 0 }; food = getRandomFoodPosition(); gameOver = false; gameLoop(); };
|
||||
<script>
|
||||
const canvas = document.getElementById('gameCanvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
const overlay = document.getElementById('overlay');
|
||||
const scoreDisplay = document.getElementById('score');
|
||||
const highscoreDisplay = document.getElementById('highscore');
|
||||
|
||||
window.addEventListener('keydown', e => {
|
||||
switch (e.key) {
|
||||
case 'ArrowUp': case 'w': if (direction.y === 0) direction = { x: 0, y: -gridSize }; break;
|
||||
case 'ArrowDown': case 's': if (direction.y === 0) direction = { x: 0, y: gridSize }; break;
|
||||
case 'ArrowLeft': case 'a': if (direction.x === 0) direction = { x: -gridSize, y: 0 }; break;
|
||||
case 'ArrowRight': case 'd': if (direction.x === 0) direction = { x: gridSize, y: 0 }; break;
|
||||
case ' ': if (gameOver) resetGame(); else if (!gameStarted) { startScreen.style.display = 'none'; gameStarted = true; gameLoop(); } break;
|
||||
}
|
||||
});
|
||||
const gridSize = 20;
|
||||
let snake = [{ x: 200, y: 200 }];
|
||||
let direction = { x: 0, y: 0 };
|
||||
let nextDirection = { x: 0, y: -gridSize };
|
||||
let food = {};
|
||||
let score = 0;
|
||||
let highscore = parseInt(localStorage.getItem('snakeHighscore')) || 0;
|
||||
let gameRunning = false;
|
||||
let gameOver = false;
|
||||
let speed = 100;
|
||||
let timeoutId;
|
||||
|
||||
food = getRandomFoodPosition();
|
||||
startScreen.style.display = 'block';
|
||||
</script>
|
||||
highscoreDisplay.textContent = highscore;
|
||||
|
||||
</body></html>
|
||||
function getRandomPosition() {
|
||||
return {
|
||||
x: Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize,
|
||||
y: Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize
|
||||
};
|
||||
}
|
||||
|
||||
function placeFood() {
|
||||
food = getRandomPosition();
|
||||
while (snake.some(s => s.x === food.x && s.y === food.y)) {
|
||||
food = getRandomPosition();
|
||||
}
|
||||
}
|
||||
|
||||
// Funktion zum Zeichnen eines abgerundeten Rechtecks (für die Snake)
|
||||
function drawRoundedRect(x, y, width, height, radius, color) {
|
||||
ctx.fillStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x + radius, y);
|
||||
ctx.lineTo(x + width - radius, y);
|
||||
ctx.arcTo(x + width, y, x + width, y + height, radius);
|
||||
ctx.lineTo(x + width, y + height - radius);
|
||||
ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius);
|
||||
ctx.lineTo(x + radius, y + height);
|
||||
ctx.arcTo(x, y + height, x, y + height - radius, radius);
|
||||
ctx.lineTo(x, y + radius);
|
||||
ctx.arcTo(x, y, x + radius, y, radius);
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
}
|
||||
|
||||
function update() {
|
||||
direction = nextDirection;
|
||||
|
||||
const head = {
|
||||
x: snake[0].x + direction.x,
|
||||
y: snake[0].y + direction.y
|
||||
};
|
||||
|
||||
if (
|
||||
head.x < 0 || head.x >= canvas.width ||
|
||||
head.y < 0 || head.y >= canvas.height ||
|
||||
snake.some(segment => segment.x === head.x && segment.y === head.y)
|
||||
) {
|
||||
endGame();
|
||||
return;
|
||||
}
|
||||
|
||||
snake.unshift(head);
|
||||
|
||||
if (head.x === food.x && head.y === food.y) {
|
||||
score++;
|
||||
scoreDisplay.textContent = score;
|
||||
placeFood();
|
||||
} else {
|
||||
snake.pop();
|
||||
}
|
||||
}
|
||||
|
||||
function draw() {
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
// Snake als abgerundetes Rechteck zeichnen
|
||||
snake.forEach((segment, index) => {
|
||||
const color = index === 0 ? 'limegreen' : 'darkgreen'; // Kopf grün, Rest dunkelgrün
|
||||
drawRoundedRect(segment.x, segment.y, gridSize, gridSize, 5, color);
|
||||
});
|
||||
// Essen zeichnen
|
||||
drawRoundedRect(food.x, food.y, gridSize, gridSize, 5, 'tomato');
|
||||
}
|
||||
|
||||
function gameLoop() {
|
||||
if (!gameRunning) return;
|
||||
update();
|
||||
draw();
|
||||
timeoutId = setTimeout(gameLoop, speed);
|
||||
}
|
||||
|
||||
function startGame() {
|
||||
snake = [{ x: 200, y: 200 }];
|
||||
direction = { x: 0, y: -gridSize };
|
||||
nextDirection = { x: 0, y: -gridSize };
|
||||
score = 0;
|
||||
speed = 100; // Standard-Geschwindigkeit ohne Erhöhung
|
||||
scoreDisplay.textContent = score;
|
||||
gameOver = false;
|
||||
gameRunning = true;
|
||||
overlay.style.display = 'none';
|
||||
placeFood();
|
||||
gameLoop();
|
||||
}
|
||||
|
||||
function endGame() {
|
||||
gameRunning = false;
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (score > highscore) {
|
||||
highscore = score;
|
||||
localStorage.setItem('snakeHighscore', highscore);
|
||||
highscoreDisplay.textContent = highscore;
|
||||
}
|
||||
|
||||
overlay.innerHTML = '<h1>Game Over</h1><p>Press Space to Restart</p>';
|
||||
overlay.style.display = 'block';
|
||||
}
|
||||
|
||||
window.addEventListener('keydown', e => {
|
||||
switch (e.key) {
|
||||
case 'ArrowUp':
|
||||
case 'w':
|
||||
if (direction.y === 0) nextDirection = { x: 0, y: -gridSize };
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
case 's':
|
||||
if (direction.y === 0) nextDirection = { x: 0, y: gridSize };
|
||||
break;
|
||||
case 'ArrowLeft':
|
||||
case 'a':
|
||||
if (direction.x === 0) nextDirection = { x: -gridSize, y: 0 };
|
||||
break;
|
||||
case 'ArrowRight':
|
||||
case 'd':
|
||||
if (direction.x === 0) nextDirection = { x: gridSize, y: 0 };
|
||||
break;
|
||||
case ' ':
|
||||
if (!gameRunning) startGame();
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
overlay.style.display = 'block';
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user