Snake Game

Play Snake on any webpage by pressing 'S'

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greatest.deepsurf.us/scripts/491883/1355808/Snake%20Game.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Snake Game
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Play Snake on any webpage by pressing 'S'
// @author       You
// @match        https://*/*
// @match        http://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let snakeInterval;
    let snake;
    let food;
    let direction;
    let score;

    function initSnake() {
        snake = [{x: 10, y: 10}];
        food = getRandomPosition();
        direction = 'right';
        score = 0;
        snakeInterval = setInterval(moveSnake, 100);
    }

    function getRandomPosition() {
        return {
            x: Math.floor(Math.random() * 20),
            y: Math.floor(Math.random() * 20)
        };
    }

    function moveSnake() {
        const head = { ...snake[0] };

        switch (direction) {
            case 'up':
                head.y--;
                break;
            case 'down':
                head.y++;
                break;
            case 'left':
                head.x--;
                break;
            case 'right':
                head.x++;
                break;
        }

        if (head.x < 0 || head.x >= 20 || head.y < 0 || head.y >= 20 || isSnakeCollided(head)) {
            clearInterval(snakeInterval);
            alert('Game Over! Score: ' + score);
            initSnake();
            return;
        }

        snake.unshift(head);

        if (head.x === food.x && head.y === food.y) {
            score++;
            food = getRandomPosition();
        } else {
            snake.pop();
        }

        drawSnake();
    }

    function isSnakeCollided(head) {
        return snake.some(segment => segment.x === head.x && segment.y === head.y);
    }

    function drawSnake() {
        const canvas = document.getElementById('snakeCanvas');
        const ctx = canvas.getContext('2d');

        ctx.clearRect(0, 0, canvas.width, canvas.height);

        snake.forEach(segment => {
            ctx.fillStyle = 'green';
            ctx.fillRect(segment.x * 20, segment.y * 20, 20, 20);
        });

        ctx.fillStyle = 'red';
        ctx.fillRect(food.x * 20, food.y * 20, 20, 20);
    }

    function handleKeydown(event) {
        switch (event.key) {
            case 'ArrowUp':
                direction = 'up';
                break;
            case 'ArrowDown':
                direction = 'down';
                break;
            case 'ArrowLeft':
                direction = 'left';
                break;
            case 'ArrowRight':
                direction = 'right';
                break;
            case 's':
            case 'S':
                if (!snakeInterval) {
                    initSnake();
                }
                break;
        }
    }

    function createCanvas() {
        const canvas = document.createElement('canvas');
        canvas.id = 'snakeCanvas';
        canvas.width = 400;
        canvas.height = 400;
        canvas.style.border = '1px solid black';
        canvas.style.position = 'fixed';
        canvas.style.top = '50%';
        canvas.style.left = '50%';
        canvas.style.transform = 'translate(-50%, -50%)';
        canvas.style.zIndex = '9999';
        document.body.appendChild(canvas);
    }

    createCanvas();
    document.addEventListener('keydown', handleKeydown);

})();