Skip to content

Yürüme #495

Description

@rasim04744-sudo

// 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();

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions