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 class="wrapper">
    <div id="game" class="game">
    <div id="layers-container" class="layers">
      <div id="layer-one" class="parallax-container">
        <div class="clouds">
          <svg viewbox="0 0 35 100">
            <circle cx="8" cy="3.4" r="1.5" fill="#F4F4F4"/>
            <circle cx="9" cy="4" r="1" fill="#FCF7FF"/>
            <circle cx="6.8" cy="3.5" r="1.1" fill="#F1F1F1"/>

            <circle cx="20" cy="7.4" r="1.5" fill="#F4F4F4"/>
            <circle cx="21" cy="8" r="1" fill="#FCF7FF"/>
            <circle cx="18.8" cy="7.5" r="1.1" fill="#F1F1F1"/>
          </svg>	
        </div>
      </div>
      <div id="layer-two">
        <div class="buildings">
          <svg viewbox="0 0 100 100">
            <rect x="0" y="-10" height="45" width="30" fill="#85929E" stroke-width="4"></rect>
            <rect x="30" y="20" height="15" width="25" fill="#17202A" stroke-width="4"></rect>
            <rect x="55" y="5" height="30" width="15" fill="#FDFEFE" stroke-width="4"></rect>
            <rect x="70" y="15" height="20" width="30" fill="#BCBAB6" stroke-width="4"></rect>
          </svg>
        </div>
      </div>
      <div id="layer-three">
        <div class="obstacles">
          <svg>
            <g>
             <title>background</title>
             <rect fill="none" id="canvas_background" height="102" width="102" y="-1" x="-1"/>
             <g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid">
              <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/>
             </g>
            </g>
            <g class="obstacle">
             <title>Layer 1</title>
             <rect id="svg_40" height="0.190476" width="0" y="102.083333" x="91.904762" stroke-opacity="null" stroke-width="0" stroke="#000" fill="#ff0000"/>
             <g stroke="null" transform="rotate(-91 49.999996185302734,48.28294372558594) " id="svg_47">
              <rect stroke="#000" rx="2" id="svg_6" height="80.988244" width="76.354815" y="7.72481" x="7.583925" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <ellipse stroke="#000" ry="6.905791" rx="1.703454" id="svg_11" cy="14.687149" cx="8.786361" stroke-opacity="null" stroke-width="0.2" fill="#ffe377"/>
              <line stroke="#000" stroke-linecap="null" stroke-linejoin="null" id="svg_19" y2="20.878549" x2="83.237321" y1="21.592941" x1="7.483719" stroke-opacity="null" stroke-width="0.4" fill="none"/>
              <ellipse stroke="#000" ry="6.42953" rx="0.501015" id="svg_20" cy="14.44902" cx="83.537929" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <ellipse stroke="#000" ry="6.905791" rx="1.703454" id="svg_22" cy="28.260601" cx="8.646077" stroke-opacity="null" stroke-width="0.2" fill="#ffe377"/>
              <line stroke="#000" stroke-linecap="null" stroke-linejoin="null" id="svg_24" y2="34.21387" x2="83.638134" y1="34.928262" x1="7.884532" stroke-opacity="null" stroke-width="0.4" fill="none"/>
              <ellipse stroke="#000" ry="6.42953" rx="0.501015" id="svg_25" cy="27.951034" cx="83.678213" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <ellipse stroke="#000" ry="6.905791" rx="1.703454" id="svg_26" cy="41.762615" cx="8.786361" stroke-opacity="null" stroke-width="0.2" fill="#ffe377"/>
              <line stroke="#000" stroke-linecap="null" stroke-linejoin="null" id="svg_27" y2="47.715885" x2="83.778419" y1="48.430277" x1="8.024817" stroke-opacity="null" stroke-width="0.4" fill="none"/>
              <ellipse stroke="#000" ry="6.42953" rx="0.501015" id="svg_28" cy="41.286358" cx="83.678213" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <ellipse stroke="#000" ry="6.905791" rx="1.703454" id="svg_29" cy="55.097939" cx="8.786361" stroke-opacity="null" stroke-width="0.2" fill="#ffe377"/>
              <line stroke="#000" stroke-linecap="null" stroke-linejoin="null" id="svg_30" y2="61.051208" x2="83.778419" y1="61.7656" x1="8.024817" stroke-opacity="null" stroke-width="0.4" fill="none"/>
              <ellipse stroke="#000" ry="6.42953" rx="0.501015" id="svg_31" cy="54.45499" cx="83.537929" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <ellipse stroke="#000" ry="6.905791" rx="1.703454" id="svg_32" cy="68.266571" cx="8.646077" stroke-opacity="null" stroke-width="0.2" fill="#ffe377"/>
              <line stroke="#000" stroke-linecap="null" stroke-linejoin="null" id="svg_33" y2="74.21984" x2="83.638134" y1="74.934232" x1="7.884532" stroke-opacity="null" stroke-width="0.4" fill="none"/>
              <ellipse stroke="#000" ry="6.42953" rx="0.501015" id="svg_34" cy="68.123696" cx="83.678213" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <ellipse stroke="#000" ry="6.905791" rx="1.703454" id="svg_35" cy="81.935277" cx="8.786361" stroke-opacity="null" stroke-width="0.2" fill="#ffe377"/>
              <ellipse stroke="#000" ry="6.42953" rx="0.501015" id="svg_37" cy="68.123696" cx="83.678213" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <ellipse stroke="#000" ry="6.42953" rx="0.501015" id="svg_38" cy="81.45902" cx="83.678213" stroke-opacity="null" stroke-width="0.4" fill="#ff0000"/>
              <rect stroke="#000" id="svg_39" height="1.190657" width="8.817904" y="13.496392" x="83.437838" stroke-opacity="null" stroke-width="0" fill="#ffffff"/>
              <rect stroke="#000" id="svg_41" height="1.190657" width="8.817904" y="27.069883" x="83.838652" stroke-opacity="null" stroke-width="0" fill="#ffffff"/>
              <rect stroke="#000" id="svg_42" height="1.190657" width="8.817904" y="39.928981" x="84.239466" stroke-opacity="null" stroke-width="0" fill="#ffffff"/>
              <rect stroke="#000" id="svg_43" height="1.190657" width="8.817904" y="53.264341" x="84.039059" stroke-opacity="null" stroke-width="0" fill="#ffffff"/>
              <rect stroke="#000" id="svg_44" height="1.190657" width="8.817904" y="66.599702" x="84.239466" stroke-opacity="null" stroke-width="0" fill="#ffffff"/>
              <rect stroke="#000" id="svg_45" height="1.190657" width="8.817904" y="66.599702" x="84.239466" stroke-opacity="null" stroke-width="0" fill="#ffffff"/>
              <rect stroke="#000" id="svg_46" height="1.190657" width="8.817904" y="80.411325" x="84.039059" stroke-opacity="null" stroke-width="0" fill="#ffffff"/>
             </g>
            </g>
          </svg>
        </div>
      </div>
    </div>
    <div id="layer-four">
      <svg id="player-container">
        <circle id="player" cx="45" cy="115" r="15" fill="#FCF7FF"/>
      </svg>
    </div>
    </div>
</div>
<div id="start-message" class="start-message">
  <p>Press any key to start and use UP to jump.</p>
</div>

              
            
!

CSS

              
                body {
  background:
  linear-gradient(27deg, #151515 5px, transparent 5px) 0 5px,
  linear-gradient(207deg, #151515 5px, transparent 5px) 10px 0px,
  linear-gradient(27deg, #222 5px, transparent 5px) 0px 10px,
  linear-gradient(207deg, #222 5px, transparent 5px) 10px 5px,
  linear-gradient(90deg, #1b1b1b 10px, transparent 10px),
  linear-gradient(#1d1d1d 25%, #1a1a1a 25%, #1a1a1a 50%, transparent 50%, transparent 75%, #242424 75%, #242424);
  background-color: #131313;
  background-size: 20px 20px;
}
html,body {
  height: 400px;
  margin: 0;
  overflow: hidden;
}
.info {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}

.wrapper .game {
  position: relative;
  margin: 0 auto;
  width: 1000px;
  perspective: 1px;
  transform-style: preserve-3d;
  height: 400px;
  overflow-x: scroll;
  overflow-y: hidden;
}

.wrapper .game::-webkit-scrollbar { 
  display: none; 
}

.parallax-container {
  display: flex;
  flex: 1 0 auto;
  position: relative;
  height: 100%;
  transform: translateZ(-1px) scale(2);
  z-index: -1;
}

.layers {
  position: relative;
  height: 100%;
}

#layer-one {
  display: block;
  position: relative;

  left: 0;
  height: 100%;
  width: 100000px;
  background-image: linear-gradient(#144d99, #0081CF 300px, #444443 300px);
}
#layer-two {
  position: absolute;
  top: 200px;
  left: 0;
  z-index: 10;
  height: 100px;
  width: 100000px;
}

#layer-three {
  position: absolute;
  top: 325px;
  left: 1000px;
  z-index: 20;
  height: 100px;
  width: 100000px;
}

.clouds {
  filter: blur(1px);
  opacity: 0.95;
}

.clouds, .buildings {
  display: inline-block;
  height: 100%;
  width: 600px;
}

.buildings {
  float: left;
  display: block;
  height: 100%;
  width: 300px;
}


#player-container {
  position: fixed;
  bottom: 0;
  left: 100px;
  transform: translateZ(-100px) translateY(11000px) translateX(-25000px) scale(100);
  z-index: 30;
}
.start-message {
  position: fixed;
  top: 0;
  left: calc(50% - 500px);
  
  display: flex;
  justify-content: center;
  align-items: flex-start;
  
  margin: 0 auto;
  width: 1000px;

  color: white;
  font-family: 'Helvetica';
  font-size: 2em;
  font-weight: bold;

  background-color: #00e6b8;
}

.fade-out {
  animation: fadeOut 1s linear;
  opacity: 0;
}

@keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
              
            
!

JS

              
                // This functionality can access the HTML in the index.html file and store HTML elements 
// as variables (or containers for values) within the our JavaScript programs
const clouds = document.getElementsByClassName('clouds')[0];
const layerOne = document.getElementById('layer-one');
const buildings = document.getElementsByClassName('buildings')[0];
const layerTwo = document.getElementById('layer-two');
const obstacles = document.getElementsByClassName('obstacles')[0];
const obstacle = obstacles.children[0];
const layersContainer = document.getElementById('layers-container');
const startMessage = document.getElementById('start-message');
const player = document.getElementById('player');
const message = document.getElementById('start-message');

// Set some potnetial X positions for our obstacles
const obstaclePositions = [100, 150, 175, 200];

// Stores the animation loop function in a variable, the functionality it uses depends on the browser support available
const requestAnim = window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.mozRequestAnimationFrame ||
  setInterval;


// This is typical syntax for calling a function
// Here we are calling our setup function
setup();

// Get coordinates and dimensions of our player so we can detect collisions
const playerOb = {
  x: player.getBoundingClientRect().x,
  y: player.getBoundingClientRect().y,
  velocity: 0,
  height: player.getBoundingClientRect().height,
  width: player.getBoundingClientRect().width,
}

// Here we will store data about our obstacles in order to detect collisions
let obstacleArray = [];

function setGameCoordinates() {
  // Get coordinates and dimensions of our obstacles so we can detect collisions
  obstacleArray = Array.from(document.getElementsByClassName('obstacle')).map(obstacle => ({
    element: obstacle,
    x: obstacle.getBoundingClientRect().x,
    y: obstacle.getBoundingClientRect().y,
    height: obstacle.getBoundingClientRect().height,
    width: obstacle.getBoundingClientRect().width,
  }));

  playerOb.x = player.getBoundingClientRect().x;
}


// Define the settings for our game
// Has the game started? Has the user pressed a key? Track the screens X position and the rate at which it moves across the screen
const gameSettings = {
  hasStarted: false,
  jumpKeyPressed: false,
  screenX: 0,
  speed: 5,
  gravity: 0.17
}

// Function that is used to clone our clouds and buildings
function cloneNodes(element, layer) {
  for (let index = 0; index < 75; index++) {
    const newElement = element.cloneNode(true);
    layer.appendChild(newElement);
  }
}

// Function that is used to clone the obstacles
// Also sets random X positions for the obstacles so that the game isnt too boring...
function cloneObstacles() {
  for (let index = 0; index < 75; index++) {
    const newObstacle = obstacle.cloneNode(true);
    const child = newObstacle.children[0];
    child.setAttribute('transform', 
      'translate('+obstaclePositions[Math.floor(Math.random() * obstaclePositions.length)]+', 0)'
    );
    obstacles.appendChild(newObstacle);
  }
}

// Checks the x and y values of both the player and obstacle to see if they overlap
function distance(x1, y1, x2, y2) {
  let collision = false;
  const xDist = x2 - x1;
  const yDist = y2 - y1;

  if((Math.abs(xDist) < 10) && (Math.abs(yDist) < 20)) {
    collision = true;
  }
  return collision;
}

// Once we have passed an obstacle we remove it from our variables as we no longer need to detect a collision
function removePassedObstacles() {
  const obstacle = obstacleArray[0];
  if(obstacle.x + obstacle.width + playerOb.width < playerOb.x) obstacleArray.shift();
}

// Calls the distance function passing in relevant x and y values for both the player and obstacles, checking for a collision
function detectCollision() {
  let outcome = false;
  const obstacle = obstacleArray[0];
  const heightDifference = Math.abs(obstacle.height - playerOb.height);
  if(distance(playerOb.x + (playerOb.width / 2), playerOb.y - heightDifference, obstacle.x + (obstacle.width / 2), obstacle.y + heightDifference )) {
    outcome = true;
  };

  removePassedObstacles();

  return outcome
}

// Our setup and clone functions from earlier
function setup() {
  cloneNodes(clouds, layerOne);
  cloneNodes(buildings, layerTwo);
}

// Function which makes the player jump on UP key press
// Updates the players Y value to move them up and down
// Depending on whether the up key has been pressed
function jump() {
  if (playerOb.y + playerOb.velocity + 36 > 500) {
      playerOb.velocity = 0;
      playerOb.y = 500 - 36;
  } else {
      playerOb.velocity += gameSettings.gravity;
      playerOb.y += playerOb.velocity;
  } 

  player.setAttribute('cy', playerOb.y - 349);
}

// When we collide with an obstacle, reset our game settings
function restartGame() {
  gameSettings.hasStarted = false;
  gameSettings.jumpKeyPressed = false;
  gameSettings.screenX = 0;
  layersContainer.style.left = '0px';

  message.classList.remove('fade-out');
  message.style.background = 'red';
  message.children[0].textContent = 'Oops! Hit any key start again.'
}

// Moves the screen and obstacles to the left when the game starts using negative X values
function moveScreen() {
  obstacleArray.forEach(obstacle => obstacle.x -= gameSettings.speed);
  gameSettings.screenX -= gameSettings.speed;
  layersContainer.style.left = gameSettings.screenX +'px';
}

// Animation loop used in all HTML5/JavaScript games
// Calls all of our earlier defined functions to render jumping, detect collisions and finally end the game
requestAnim(function loop() {
    if(gameSettings.hasStarted) {
      if (gameSettings.jumpKeyPressed && !playerOb.velocity) {
        playerOb.velocity = -4;
        gameSettings.jumpKeyPressed = false;
      }
      moveScreen();
      if(detectCollision()) {
        restartGame();
      }
    }
    jump();

    requestAnim(loop);
});

// EVENT LISTENERS
// Once a click or keypress occurs the game will start and instructions fade out
// Here will also call the functions which clone the created obstacles and map their co-ordinates to our obstacles array, so we can effectively detect collisions

// Wait for the user to click
addEventListener('click', () => {
  if(!gameSettings.hasStarted) {
    cloneObstacles();
    setGameCoordinates();
  }
  gameSettings.hasStarted = true;
  startMessage.classList.add('fade-out');
});

// Wait for the user to press a key
// Also checks if the key pressed was the up key - and if so, will make the player jump
addEventListener('keydown', (e) => {
  if(e.key === 'ArrowUp') {
    gameSettings.jumpKeyPressed = true;
  }
  if(!gameSettings.hasStarted) {
    cloneObstacles();
    setGameCoordinates();
  }
  gameSettings.hasStarted = true;
    startMessage.classList.add('fade-out');
});
              
            
!
999px

Console