İşte tamamen tek bir HTML dosyası içinde , sadece kopyala-yapıştır ile çalışacak, maksimum süslemelere sahip, renkli ve animasyonlu bir "Yılan Oyunu (Snake Game)" . Bu versiyon sade HTML içinde gömülü CSS ve JavaScript kullanır ama tek bir dosyadadır :
🔻 Kopyala-yapıştır yaparak çalıştırabilirsin (örneğin snake.html olarak kaydedip çalıştır):
html Copy code
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Yılan Oyunu</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
font-family: 'Segoe UI', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: white;
}
canvas {
background: #111;
box-shadow: 0 0 30px #00ffcc;
border: 5px solid #0ff;
animation: glow 2s infinite alternate;
}
@keyframes glow {
from { box-shadow: 0 0 10px #0ff; }
to { box-shadow: 0 0 30px #0ff, 0 0 60px #0ff; }
}
#score {
position: absolute;
top: 20px;
font-size: 24px;
text-shadow: 0 0 10px #0ff;
}
</style>
</head>
<body>
<div id="score">Skor: 0</div>
<canvas id="game" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const scale = 20;
const rows = canvas.height / scale;
const cols = canvas.width / scale;
let snake;
let food;
let score = 0;
class Snake {
constructor() {
this.body = [{ x: 5, y: 5 }];
this.xSpeed = 1;
this.ySpeed = 0;
}
draw() {
for (let i = 0; i < this.body.length; i++) {
ctx.fillStyle = `hsl(${i * 30}, 100%, 50%)`;
ctx.fillRect(this.body[i].x * scale, this.body[i].y * scale, scale, scale);
ctx.strokeStyle = "#000";
ctx.strokeRect(this.body[i].x * scale, this.body[i].y * scale, scale, scale);
}
}
update() {
const head = { x: this.body[0].x + this.xSpeed, y: this.body[0].y + this.ySpeed };
this.body.unshift(head);
if (head.x === food.x && head.y === food.y) {
score++;
document.getElementById("score").innerText = "Skor: " + score;
placeFood();
} else {
this.body.pop();
}
}
changeDirection(dir) {
switch (dir) {
case "ArrowUp":
if (this.ySpeed === 0) { this.xSpeed = 0; this.ySpeed = -1; }
break;
case "ArrowDown":
if (this.ySpeed === 0) { this.xSpeed = 0; this.ySpeed = 1; }
break;
case "ArrowLeft":
if (this.xSpeed === 0) { this.xSpeed = -1; this.ySpeed = 0; }
break;
case "ArrowRight":
if (this.xSpeed === 0) { this.xSpeed = 1; this.ySpeed = 0; }
break;
}
}
checkCollision() {
const head = this.body[0];
if (head.x < 0 || head.x >= cols || head.y < 0 || head.y >= rows) return true;
for (let i = 1; i < this.body.length; i++) {
if (head.x === this.body[i].x && head.y === this.body[i].y) return true;
}
return false;
}
}
function placeFood() {
food = {
x: Math.floor(Math.random() * cols),
y: Math.floor(Math.random() * rows)
};
}
function drawFood() {
ctx.fillStyle = "#ff0066";
ctx.shadowColor = "#ff0066";
ctx.shadowBlur = 20;
ctx.fillRect(food.x * scale, food.y * scale, scale, scale);
ctx.shadowBlur = 0;
}
function setup() {
snake = new Snake();
placeFood();
window.setInterval(() => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
snake.update();
snake.draw();
drawFood();
if (snake.checkCollision()) {
alert("Oyun Bitti! Skor: " + score);
score = 0;
document.location.reload();
}
}, 150);
}
window.addEventListener("keydown", e => snake.changeDirection(e.key));
setup();
</script>
</body>
</html>
Özellikler:
🔥 Glow/Neon renkli stil (canvas kenarları ve yılan renkleri)
🍎 Hareketli ve renkli yem (food)
⌨️ Klavye ile kontrol (Yön tuşları)
🎯 Dinamik skor sayacı
💀 Çarptığında otomatik bitiş ve yeniden başlatma
🟢 Tüm kod sadece tek HTML dosyasında , harici bağımlılık veya dosya gerekmez.
Eğer istersen bu oyuna:
Yüksek skor sistemi
Başlangıç ekranı
Mobil destek
Ses efektleri
gibi eklemeler de yapılabilir, sadece söyle yeter. 🎮
İyi oyunlar! 🐍