-
Notifications
You must be signed in to change notification settings - Fork 871
Description
// game.js
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Yılanın başlangıç uzunluğu ve yönü
let snake = [{x: 10, y: 10}];
let direction = 'RIGHT';
// Yılanın hareket ettiği her güncelleme için
const moveSnake = () => {
let head = { ...snake[0] };
if (direction === 'RIGHT') head.x += 10;
if (direction === 'LEFT') head.x -= 10;
if (direction === 'UP') head.y -= 10;
if (direction === 'DOWN') head.y += 10;
snake.unshift(head);
snake.pop();
};
// Yılanın vücudunu çiz
const drawSnake = () => {
snake.forEach(segment => {
ctx.fillStyle = 'green';
ctx.fillRect(segment.x, segment.y, 10, 10);
});
};
// Tuşlara göre yön değişikliği
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowRight') direction = 'RIGHT';
if (event.key === 'ArrowLeft') direction = 'LEFT';
if (event.key === 'ArrowUp') direction = 'UP';
if (event.key === 'ArrowDown') direction = 'DOWN';
});
// Oyun döngüsü
const gameLoop = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Ekranı temizle
moveSnake();
drawSnake();
setTimeout(gameLoop, 100); // Her 100ms'de bir güncelleme yap
};
gameLoop();