İşte şık ve eğlenceli bir yılan oyunu örneği! Modern tasarım, animasyonlar ve kullanıcı dostu kontrol sistemine sahip:
<!DOCTYPE html>
<html>
<head>
<title>Şık Yılan Oyunu</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
body {
margin: 0;
background: linear-gradient(to bottom, #0f0f0f, #1a1a1a);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
font-family: 'Press Start 2P', cursive;
}
canvas {
border: 4px solid #00ffcc;
box-shadow: 0 0 30px #00ffcc88, inset 0 0 20px #00ccff44;
background-color: #000;
border-radius: 15px;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.02); }
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
text-align: center;
display: none;
animation: fadeIn 1s ease-in;
}
@keyframes fadeIn {
from {opacity: 0; transform: translate(-50%, -60%);}
to {opacity: 1; transform: translate(-50%, -50%);}
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
background: #00ffcc;
border: none;
border-radius: 8px;
cursor: pointer;
font-family: 'Press Start 2P', cursive;
transition: all 0.3s ease;
}
button:hover {
background: #00cc99;
transform: scale(1.1);
}
#score {
position: absolute;
top: 20px;
left: 50%;
transform: translateX(-50%);
color: #00ffcc;
font-size: 20px;
text-shadow: 0 0 10px #00ffcc;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="600" height="600"></canvas>
<div id="score">Score: 0</div>
<div id="gameOver">
<h1>GAME OVER</h1>
<p id="finalScore">Final Score: 0</p>
<button onclick="restartGame()">Play Again</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('score');
const gameOverScreen = document.getElementById('gameOver');
const finalScore = document.getElementById('finalScore');
// Oyun değişkenleri
let snake = [{x: 150, y: 150}];
let food = {x: 300, y: 300};
let dx = 10;
let dy = 0;
let score = 0;
let speed = 100;
let gameInterval;
let isGameOver = false;
// Renk paleti
const colors = {
snake: ['#00ffff', '#00ff88'],
food: '#ff007f',
shadow: 'rgba(0, 255, 204, 0.5)'
};
// Yılanı çizen fonksiyon
function drawSnake() {
const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, colors.snake[0]);
gradient.addColorStop(1, colors.snake[1]);
snake.forEach((segment, index) => {
ctx.fillStyle = gradient;
ctx.shadowColor = colors.shadow;
ctx.shadowBlur = 15;
ctx.fillRect(segment.x, segment.y, 10, 10);
// Segmentler arası geçiş efekti
if (index > 0) {
ctx.fillStyle = colors.shadow;
ctx.fillRect(segment.x + 2, segment.y + 2, 6, 6);
}
});
ctx.shadowBlur = 0;
}
// Yem çizen fonksiyon
function drawFood() {
const gradient = ctx.createRadialGradient(
food.x + 5, food.y + 5, 2,
food.x + 5, food.y + 5, 10
);
gradient.addColorStop(0, '#ffffff');
gradient.addColorStop(1, colors.food);
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(food.x + 5, food.y + 5, 5, 0, Math.PI*2);
ctx.fill();
}
// Yem konumu ayarlayan fonksiyon
function placeFood() {
food.x = Math.floor(Math.random() * (canvas.width/10)) * 10;
food.y = Math.floor(Math.random() * (canvas.height/10)) * 10;
// Yılanın üstüne gelmesini engelle
snake.forEach(segment => {
if (segment.x === food.x && segment.y === food.y) {
placeFood();
}
});
}
// Çarpışma kontrolü
function checkCollision() {
// Kendine çarpma
for (let i = 1; i < snake.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) {
return true;
}
}
// Duvara çarpma
return (
snake[0].x < 0 ||
snake[0].x >= canvas.width ||
snake[0].y < 0 ||
snake[0].y >= canvas.height
);
}
// Oyunu yeniden başlat
function restartGame() {
snake = [{x: 150, y: 150}];
dx = 10;
dy = 0;
score = 0;
scoreDisplay.textContent = "Score: 0";
placeFood();
isGameOver = false;
gameOverScreen.style.display = 'none';
clearInterval(gameInterval);
gameInterval = setInterval(main, speed);
}
// Ana oyun döngüsü
function main() {
if (isGameOver) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Yılanı hareket ettir
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
// Yem yeme kontrolü
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreDisplay.textContent = "Score: " + score;
placeFood();
// Hız artırma (zorluk artışı)
if (speed > 50) {
clearInterval(gameInterval);
speed -= 2;
gameInterval = setInterval(main, speed);
}
} else {
snake.pop();
}
// Çarpışma kontrolü
if (checkCollision()) {
isGameOver = true;
finalScore.textContent = "Final Score: " + score;
gameOverScreen.style.display = 'block';
clearInterval(gameInterval);
return;
}
drawSnake();
drawFood();
}
// Tuş kontrolü
document.addEventListener('keydown', e => {
if (isGameOver) return;
if (e.key === 'ArrowUp' && dy === 0) {
dx = 0;
dy = -10;
} else if (e.key === 'ArrowDown' && dy === 0) {
dx = 0;
dy = 10;
} else if (e.key === 'ArrowLeft' && dx === 0) {
dx = -10;
dy = 0;
} else if (e.key === 'ArrowRight' && dx === 0) {
dx = 10;
dy = 0;
}
});
// Oyunu başlat
restartGame();
</script>
</body>
</html>
Özellikler:
- Modern Tasarım: Neon efektler, animasyonlu kenarlıklar ve şık fontlar
- Renk Geçişleri: Yılan ve yemde lineer/radyal gradyanlar
- Zorluk Artışı: Yem yedikçe hızlanan oyun
- Responsive: Tek bir HTML dosyasında çalışan mobil uyumlu olmayan ama masaüstü için mükemmel bir uygulama
- Etkileşimli Game Over Ekranı: Yeniden başlat butonu
- Ses Efektleri Eklenebilir: (Kendi ses dosyalarınızı ekleyebilirsiniz)
Nasıl Kullanılır:
- Kodu bir
.html dosyasına kaydedin (örneğin: snake-game.html)
- Tarayıcınızda açın
- Ok tuşlarıyla kontrol edin
Oyunun son halini görmek için bu kodu doğrudan bir tarayıcıda çalıştırabilirsiniz. Dilerseniz:
- Yılanın rengini değiştirin
- Arka plan efektlerini özelleştirin
- Ses efektleri ekleyin
- Mobil uyumlu hale getirin
- Skor tablosu ekleyin
İsterseniz bu özelliklerin nasıl ekleneceğini adım adım anlatabilirim! 🎮