Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <html>
    <head></head>
    <body>
        <p>Click this frame to enable arrow keys.</p>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    </body>
</html>
              
            
!

CSS

              
                body {
    padding: 20px;
}

p {
    color: #555;
    font-family: sans-serif;
    margin-bottom: 20px;
    font-size: 0.7em;
}

.square {
    position: absolute;
    background-color: #7b92ee;
}

#grid {
    background-color: #dfe4f9;
}
              
            
!

JS

              
                class Grid
    constructor: (@snake, @squaresX = 25, @squaresY = 15) ->
    
        @snake.cage @squaresX, @squaresY
    
        @grid = $('<div id="grid"></div>')
        
        @squareWidth = 15
        @squareHeight = 15
        
        @grid.width @squareWidth * @squaresX
        @grid.height @squareHeight * @squaresY
        
        square = $('<div class="square"></div>')
        square.width(@squareWidth).height(@squareHeight)

        @activeSquares = @snake.chain.map => square.clone().appendTo @grid
        
        @grid.insertBefore $('body script').eq(0)
        
    update: ->    
        for piece, index in @snake.chain
            @activeSquares[index].css
                top: piece.y * @squareHeight + @grid.offset().top
                left: piece.x * @squareWidth + @grid.offset().left

class Snake
    constructor: (@direction = 'down') ->
    
        @x = 4
        @y = 4
        @length = 6
        @boundaryX = null
        @boundaryY = null
        
        @queuedDirection = @direction
        
        @chain = ( { x: @x, y: @y- piece } for piece in [0..@length - 1] ) 
        
        @setupControls()
        
    setupControls: ->
        $(window).keydown (event) =>
            newDirection = @queuedDirection
            switch event.keyCode
                when 37 then newDirection = 'left'
                when 38 then newDirection = 'up'
                when 39 then newDirection = 'right'
                when 40 then newDirection = 'down'
                
            @queuedDirection = newDirection unless @isOpposite newDirection 
            console.log @queuedDirection

    isOpposite: (newDirection) ->
        return true if newDirection is 'left' and @direction is 'right'
        return true if newDirection is 'right' and @direction is 'left'
        return true if newDirection is 'up' and @direction is 'down'
        return true if newDirection is 'down' and @direction is 'up'
        return false

    cage: (squaresX, squaresY) ->
        @boundaryX = squaresX
        @boundaryY = squaresY        

    move: -> 
        @direction = @queuedDirection    
        switch @direction
            when 'up'
                return if @y <= 0
                @y -= 1
            when 'right'
                return if @x >= @boundaryX - 1
                @x += 1
            when 'down'
                return if @y >= @boundaryY - 1
                @y += 1
            when 'left'
                return if @x <= 0
                @x -= 1            
                
        moveTo = x: @x, y: @y
        temp = x: @chain[0].x, y: @chain[0].y
        
        for piece, index in @chain
        
            piece.x = moveTo.x
            piece.y = moveTo.y
            
            moveTo.x = temp.x
            moveTo.y = temp.y
            
            temp.x = @chain[index + 1]?.x
            temp.y = @chain[index + 1]?.y
       
snake = new Snake
grid = new Grid snake
       
run = ->
    snake.move()
    grid.update()
    
setInterval run, 150
run()

              
            
!
999px

Console