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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              
            
          
!

CSS

            
              
            
          
!

JS

            
               /*
    * Platforms loop infinitely through an open space, speeding up with time
    * A ball moves through the world
    * Keep the ball from falling or you'll lose the game!
    * The ball can only jump when it's solidly on a platform
    * */
    var canvasWidth = 900;
    var canvasHeight = 600;

    var rectWidth =
        [
            100,
            100,
            100,
            100,
            100,
            100
        ];

    var rectOffset =
        [
            rectWidth[0],
            rectWidth[1] + rectWidth[0] * 2,
            rectWidth[2] + rectWidth[1] + rectWidth[0] * 2,
            rectWidth[3] + rectWidth[2] + rectWidth[1] + rectWidth[0] * 3,
            rectWidth[4] + rectWidth[3] + rectWidth[2] + rectWidth[1] + rectWidth[0] * 4,
            rectWidth[5] + rectWidth[4] + rectWidth[3] + rectWidth[2] + rectWidth[1] + rectWidth[0] * 5
        ];

    var rectHeight =
        [
            200,
            240,
            260,
            230,
            250,
            240
        ];

    var rectColors = [
        { r: 220, g: 220, b: 50},
        { r: 50, g: 220, b: 220},
        { r: 220, g: 50, b: 220},
        { r: 220, g: 220, b: 50},
        { r: 220, g: 220, b: 50},
        { r: 50, g: 220, b: 220}
    ];

    var platformSpeed = 2;

    var ballSpeedX = 0;
    var ballSpeedY = 0;
    var ballMoving = false;
    var leftArrowIsDown = false;
    var rightArrowIsDown = false;
    var ballIsInAir = false;
    var ballIsOnPlatform = true;

    var ballColor = [
        { r: 220, g: 210, b: 50}
    ];

    var ballX = 50;
    var ballY = 180;
    var ballSize = 20;

    var score = 0;

    var gameOver = false;
    var gameStart = false;

    var t=0;
    var tInc = 2;
    var n;
    var h;

    var jumpSound;
    var loseSound;

    var waves;
    var wavesX = 0;
    var wavesX2 = canvasWidth;

    function preload()
    {
        soundFormats('mp3');
        /*plays a sound when the space bar is pressed/the ball jumps*/
        jumpSound = loadSound('https://s3-us-west-2.amazonaws.com/s.cdpn.io/2174865/boing-sound.mp3');
        /*plays a sound when the ball falls*/
        loseSound = loadSound('https://s3-us-west-2.amazonaws.com/s.cdpn.io/2174865/pitchfall-sound.mp3');
        /*scrolling waves at the bottom of the screen*/
        waves = loadImage('https://s3-us-west-2.amazonaws.com/s.cdpn.io/2174865/waves.png');
    }

    function setup()
    {
        createCanvas(canvasWidth, canvasHeight);
    }

    function draw()
    {

        background(200, 200, 255);
        drawBg();

        if (!gameStart) preGame();
        if (gameStart) runGame();

        if (wavesX < -canvasWidth) wavesX = canvasWidth;
        image(waves, wavesX, canvasHeight-75, canvasWidth, 100);
        if (wavesX2 < -canvasWidth) wavesX2 = canvasWidth;
        image(waves, wavesX2,canvasHeight-75, canvasWidth, 100);
        wavesX -= platformSpeed;
        wavesX2 -= platformSpeed;

    }

    function keyPressed()
    {
        if (keyCode === LEFT_ARROW)
        {
            //if (ballIsInAir) return;
            ballSpeedX = -10;
            ballMoving = true;
            leftArrowIsDown = true;
        }
        else if (keyCode === RIGHT_ARROW)
        {
            if (rightArrowIsDown && ballIsInAir) return;
            ballSpeedX = 10;
            ballMoving = true;
            rightArrowIsDown = true;
        }
        else if (keyCode === 32)
        {
            if (!gameStart) {
                gameStart = true;
                return;
            }
            if (ballIsInAir) return;
            jumpSound.setVolume(0.9);
            jumpSound.play();
            ballSpeedY = -5;
            ballMoving = true;
            ballIsInAir = true;
        }
    }

    function keyReleased()
    {
        if (keyCode === LEFT_ARROW)
        {
            ballSpeedX = 0;
            leftArrowIsDown = false;
        }
        else if (keyCode === RIGHT_ARROW)
        {
            ballSpeedX = 0;
            rightArrowIsDown = false;
        }

    }

    function gameStatus()
    {
        if (ballY > canvasHeight + ballSize && !gameOver) {
            gameOver = true;
            loseSound.setVolume(0.9);
            loseSound.play();
        }
        if (gameOver) {
            textAlign(CENTER, CENTER);
            stroke(0,0,0);
            strokeWeight(1);
            fill(220,210,50);
            textSize(40);
            text("Game Over", canvasWidth/2,canvasHeight/2);
            textSize(20);
            text("Your score was " + score + ".", canvasWidth/2, canvasHeight/2 + 40);
            text("Click to start again.", canvasWidth/2, canvasHeight/2 + 60);
        }
    }

    function speedLevel()
    {
        if (score % 6 === 0 && score != 0) {
            platformSpeed += .01;
            tInc += .5;
        }
    }

    function drawBg() {
        var c1, c2;
        c1 = color(102, 203, 255);
        c2 = color(0, 102, 153);

        for (var i = 0; i < canvasHeight; i+=5) {
            var inter = map(i, 0, canvasHeight, 0, 1);
            var c = lerpColor(c1, c2, inter);
            strokeWeight(5);
            stroke(c);
            line(0, i, width, i);
        }
    }

    function mouseReleased() {
        if (gameOver) {
            window.location.reload();
        }
    }

    function preGame() {
        textAlign(CENTER, CENTER);
        stroke(0,0,0);
        strokeWeight(1);
        fill(220,210,50);
        textSize(40);
        text("Platform Jump", canvasWidth/2,canvasHeight/2-40);
        textSize(20);
        text("A ball moves through a world of platforms.", canvasWidth/2, canvasHeight/2);
        text("Keep it from falling in the water or you'll lose!", canvasWidth/2, canvasHeight/2 + 20);
        text("Use the left and right arrow keys to move left and right.", canvasWidth/2, canvasHeight/2 + 40);
        text("Use the space bar to jump - you can only jump when on a platform.", canvasWidth/2, canvasHeight/2 + 60);
        text("Press space to start the game.", canvasWidth/2, canvasHeight/2 + 80);
    }

    function runGame() {
        stroke(0,0,0);
        strokeWeight(1);
        fill(220,210,50);

        if (ballX <= 0) ballX = 0;
        if (ballX >= canvasWidth-ballSize) ballX = canvasWidth-ballSize;

        fill(ballColor[0].r, ballColor[0].g, ballColor[0].b);
        rect(ballX,ballY,ballSize,ballSize);
        //rect(ballX+5,ballY - (ballSize/2), ballSize/2, ballSize/2);
        ballX += ballSpeedX;
        ballY += ballSpeedY;
        if (ballIsInAir) ballSpeedY += 0.1;

        for (var i=0; i < 6; i++) {
            fill(rectColors[i].r, rectColors[i].g, rectColors[i].b);
            rect(rectOffset[i], rectHeight[i], rectWidth[i], 10);
            rectOffset[i] -= platformSpeed;
        }

        if (!gameOver) speedLevel();

        ballIsInAir = true;

        for (var j=0; j < 6; j++) {
            if (rectOffset[j] - ballSize < ballX && ballX < rectOffset[j] + rectWidth[j]) {
                if (ballY > rectHeight[j] - (ballSize+1) && ballY < rectHeight[j]+1)
                {
                    ballColor[0] = rectColors[j];
                    ballSpeedY = 0;
                    ballIsInAir = false;
                    ballIsOnPlatform = true;
                    ballY = rectHeight[j] - ballSize;
                }
            }
        }


        for (var k=0; k<6; k++) {
            if (rectOffset[k] < -rectWidth[k]) {
                n=noise(t);
                h=map(n, 0, 1, 100, 550);
                rectHeight[k] = h;
                rectOffset[k] = canvasWidth + rectWidth[k];
                if (!gameOver) score +=1;
                t+=tInc;
            }
        }

        textAlign(CENTER, CENTER);
        stroke(0,0,0);
        strokeWeight(1);
        fill(220,210,50);
        textSize(20);
        text("Score:  " + score, canvasWidth/2,30);

        gameStatus();
    }
            
          
!
999px

Console