Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container">
	<div id="points">
		<p id="current-points">Points: 0</p>
		<p id="high-score">High score: 0</p>
	</div>
	<canvas id="room" width="250" height="250"></canvas>
	
	<p style="width: 250px">Use the arrow keys to move the snake. The blue square is food. The gray squares are obstacles - running into them ends the game.</p>
</div>
              
            
!

CSS

              
                body {
    margin: 0;
    padding: 0;
}

p {
	pading: 20px;
}

#container {
    position: relative;
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

#points {
    position: relative;
    display: flex;
    width: 250px;
}

#current-points, #high-score {
    position: relative;
    flex: 1;
    text-align: center;
}

#room {
    background-color: black;
}
              
            
!

JS

              
                //Grab the canvas and context
const room = document.querySelector('#room');
const ctx = room.getContext('2d');

//Grab points elements
const curPointsDisplay = document.querySelector('#current-points');
const highScoreDisplay = document.querySelector('#high-score');


//Set initial game variables
const frameRate = 1000 / 15;
const roomWidth = room.width;
const roomHeight = room.height;
const unit = 10;
const roomUnitWidth = room.width / unit;
const roomUnitHeight = room.height / unit;
let curPoints = 0;
let highScore = 0;
let obstacles = [];


//Snake object
let snake = {
    //Props
    "x": 4,
    "y": 4,
    "horDir": 0,
    "vertDir": 0,
    "body": [],
    "bodyLength": 5,
    "canMove": true,

    //Methods
    "travel": (wrapX, wrapY) => {
        //Move snake
        snake.x += snake.horDir;
        snake.y += snake.vertDir;

        //Handle border wrapping
        if (snake.x > wrapX - 1) {
            snake.x = 0;
            snake.canMove = false;
        } else if (snake.x < 0) {
            snake.x = wrapX - 1;
            snake.canMove = false;
        } else if (snake.y > wrapY - 1) {
            snake.y = 0;
            snake.canMove = false;
        } else if (snake.y < 0) {
            snake.y = wrapY - 1;
            snake.canMove = false;
        }
    },

    "checkDeath": (obstacles = []) => {
        //Handle dying
        for (let i = 0; i < snake.body.length; i++) {
            if (snake.horDir + snake.vertDir === 0) { break; } //Break out of loop if snake has yet to move
            if (snake.x === snake.body[i].x && snake.y === snake.body[i].y) {
                return true;
            }
        }

        for (let i = 0; i < obstacles.length; i++) {
            if (snake.x === obstacles[i].x && snake.y === obstacles[i].y) {
                return true;
            }
        }
    },

    "handleBody": () => {
        //Handle body placement
        snake.body.push({ "x": snake.x, "y": snake.y });
        while (snake.body.length > snake.bodyLength) {
            snake.body.shift();
        }
    },

    "eat": () => {
        snake.bodyLength++;
    },

    "reset": () => {
        snake.x = 4;
        snake.y = 4;
        snake.horDir = 0;
        snake.vertDir = 0;
        snake.body = [];
        snake.bodyLength = 5;
    }
};

//Snake controls
function controlSnake(e) {
    let curKey = e.key;
    let direction = snake.horDir + snake.vertDir === 0 ? 'start' : snake.horDir !== 0 ? 'hor' : 'vert'; //Set direction to 'hor' or 'vert' if not first move; otherwise, set it to 'start'

    if (snake.canMove === true) {
        switch (direction) {
            case 'hor':
                if (curKey === 'ArrowUp') {
                    snake.horDir = 0;
                    snake.vertDir = -1;
                    snake.canMove = false;
                } else if (curKey === 'ArrowDown') {
                    snake.horDir = 0;
                    snake.vertDir = 1;
                    snake.canMove = false;
                }
                break;
            case 'vert':
                if (curKey === 'ArrowLeft') {
                    snake.horDir = -1;
                    snake.vertDir = 0;
                    snake.canMove = false;
                } else if (curKey === 'ArrowRight') {
                    snake.horDir = 1;
                    snake.vertDir = 0;
                    snake.canMove = false;
                }
                break;
            case 'start':
                switch (curKey) {
                    case 'ArrowUp':
                        snake.horDir = 0;
                        snake.vertDir = -1;
                        snake.canMove = false;
                        break;
                    case 'ArrowDown':
                        snake.horDir = 0;
                        snake.vertDir = 1;
                        snake.canMove = false;
                        break;
                    case 'ArrowLeft':
                        snake.horDir = -1;
                        snake.vertDir = 0;
                        snake.canMove = false;
                        break;
                    case 'ArrowRight':
                        snake.horDir = 1;
                        snake.vertDir = 0;
                        snake.canMove = false;
                }
        }
    }
}

//Food object
let food = {
    //Props
    "x": 10,
    "y": 10,

    //Methods
    "newPos": (horRange, vertRange, blockedRange, obstacles) => {
        let newX = Math.floor(Math.random() * horRange);
        let newY = Math.floor(Math.random() * vertRange);

        //Block off current food position and all wall positions
        blockedRange.push({ "x": food.x, "y": food.y });
        obstacles.forEach(obj => blockedRange.push(obj));

        for (let i = 0; i < blockedRange.length; i++) {
            if (blockedRange[i].x === newX && blockedRange[i].y === newY) {
                newX = Math.floor(Math.random() * horRange);
                newY = Math.floor(Math.random() * vertRange);
                i = -1;
            }
        }

        food.x = newX;
        food.y = newY;
    },

    "reset": () => {
        food.x = 10;
        food.y = 10;
    }
}

//Wall class
class Wall {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

//Game
document.addEventListener('DOMContentLoaded', () => {

    //*Essential game functions
    //Handles starting the game
    function startGame() {
        placeObstacles();
        setInterval(runGame, frameRate);
    }

    //Runs the game every frame
    function runGame() {
        //Cycle vars
        let gameOver = false;

        //Runtime progression
        snake.travel(roomUnitWidth, roomUnitHeight); //Move the snake
        snake.canMove = true;

        if (snake.x === food.x && snake.y === food.y) { //Check for snake collision with food; eat food if collision occurs
            food.newPos(roomUnitWidth, roomUnitHeight, snake.body.concat([]), obstacles);
            snake.eat();
            addPoints();
        }

        gameOver = snake.checkDeath(obstacles) ? true : false; //Check if the snake has died and game has ended //!This has to be done before handleBody() to avoid immediate snake death
        snake.handleBody(); //Build the snake body

        //Draw things
        refreshRoom();
        drawRoom();

        //End of cycle checks
        if (gameOver) { restartGame(); }
    }

    //Place obstacles
    function placeObstacles() {
        let positions = [
            [1, 1],
            [1, 2],
            [1, 3],
            [2, 1],
            [3, 1],
            [roomUnitWidth - 2, 1],
            [roomUnitHeight - 3, 1],
            [roomUnitHeight - 4, 1],
            [roomUnitWidth - 2, 2],
            [roomUnitWidth - 2, 3],
            [1, roomUnitHeight - 2],
            [2, roomUnitHeight - 2],
            [3, roomUnitHeight - 2],
            [1, roomUnitHeight - 3],
            [1, roomUnitHeight - 4],
            [roomUnitWidth - 2, roomUnitHeight - 2],
            [roomUnitWidth - 3, roomUnitHeight - 2],
            [roomUnitWidth - 4, roomUnitHeight - 2],
            [roomUnitWidth - 2, roomUnitHeight - 3],
            [roomUnitWidth - 2, roomUnitHeight - 4]
        ];

        for (let i = 0; i < positions.length; i++) {
            let newWall = new Wall(positions[i][0], positions[i][1]);
            obstacles.push(newWall);
        }
    }

    //Adds points
    function addPoints() {
        //Add points
        curPoints++;
        if (curPoints > highScore) { highScore = curPoints; }

        //Display points
        curPointsDisplay.textContent = 'Points: ' + curPoints;
        highScoreDisplay.textContent = 'High score: ' + highScore;
    }

    //Resets the game
    function restartGame() {
        snake.reset();
        food.reset();
        curPoints = 0;
        curPointsDisplay.textContent = 'Points: ' + curPoints;
    }

    //Refreshes the canvas
    function refreshRoom() {
        room.width = roomWidth;
    }

    //Draws all entities
    function drawRoom() {

        //Draw snake head
        ctx.fillStyle = 'white';
        ctx.fillRect(snake.x * unit, snake.y * unit, unit, unit);

        //Draw snake body
        ctx.fillStyle = 'white';
        for (let i = 0; i < snake.body.length; i++) {
            let curX = snake.body[i].x;
            let curY = snake.body[i].y;
            ctx.fillRect(curX * unit, curY * unit, unit, unit);
        }

        //Draw food
        ctx.fillStyle = 'cyan';
        ctx.fillRect(food.x * unit, food.y * unit, unit, unit);

        //Draw obstacles
        ctx.fillStyle = 'gray';
        for (let i = 0; i < obstacles.length; i++) {
            let x = obstacles[i].x;
            let y = obstacles[i].y;
            ctx.fillRect(x * unit, y * unit, unit, unit);
        }
    }

    //Listen for controls
    document.addEventListener('keydown', controlSnake);

    //Initialize game
    startGame();

});
              
            
!
999px

Console