Snake Game

Play Snake on any webpage by pressing 'S'

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/491883/1355808/Snake%20Game.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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

})();