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

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

              
                <div class="game-board">
  <div class="points"></div>
  <div class="coin"></div>
  <div class="player"></div>
</div>
<div class="arrow-keys-container">
  <div class="arrow-keys-instructions">Use arrow keys to move</div> 
  <img class="arrow-keys" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/126441/arrow-keys2.png">
</div>
              
            
!

CSS

              
                .game-board {
  position: relative;
  height: 300px;
  width: 300px;
  background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/126441/dirt.png");
}

.player {
  position: absolute;
  width: 30px;
  height: 30px;
  background-size: contain;
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/126441/little-alien.png");
  background-repeat: no-repeat;
  background-position: center center;
  transition: all .1s;
}

.coin {
  position: absolute;
  top: 0;
  left: 0;
  width: 30px;
  height: 30px;
  background-size: contain;
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/126441/coin.png");
  background-repeat: no-repeat;
  background-position: center center;
}

.points {
  position: absolute;
  right: -110px;
  top: 0;
  width: 100px;
  font-size: 18px;
}

.arrow-keys-container {
  width: 300px;
  margin-top: 8px;
  text-align: center;
}

.arrow-keys-instructions {
  display: inline;
}

.arrow-keys {
  position: relative;
  top: 5px;
  height: 30px;
}
              
            
!

JS

              
                // Setup

var player = document.querySelector(".player");
var playerTopPosition = 60;
var playerLeftPosition = 60;

var coin = document.querySelector(".coin");
var coinTopPosition = 180;
var coinLeftPosition = 180;

var points = document.querySelector(".points");
var pointsCount = 0;

// Actions and render

function actions () {
  player.style.top = playerTopPosition + "px";
  player.style.left = playerLeftPosition + "px";
  
  coin.style.top = coinTopPosition + "px";
  coin.style.left = coinLeftPosition + "px";
  
  if (playerLeftPosition === coinLeftPosition 
      && playerTopPosition === coinTopPosition) {
    pointsCount = pointsCount + 10;
    points.innerText = pointsCount;
    ion.sound.play("coin");
    randomizeCoinPosition();
  }
};

// Game loop

function gameLoop () {
  actions();
  requestAnimationFrame(gameLoop);
};

gameLoop();

// Moving

function move (x, y) {
  playerTopPosition = playerTopPosition + y;
  playerLeftPosition = playerLeftPosition + x;
};

var listener = new window.keypress.Listener();

listener.simple_combo("up", function() {
  move(0, -30);
});

listener.simple_combo("right", function() {
  move(30, 0);
});

listener.simple_combo("down", function() {
  move(0, 30);
});

listener.simple_combo("left", function() {
  move(-30, 0);
});

// Placing coins

function randomCoordinate () {
  var randomNumber = _.random(0,9);
  return randomNumber * 30;
}

function randomizeCoinPosition () {
  coinTopPosition = randomCoordinate();
  coinLeftPosition = randomCoordinate();
}

// Coin sound

ion.sound({
    sounds: [
        {name: "coin"}
    ],
    path: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/126441/",
    preload: true,
    multiplay: true,
    volume: 1
});
              
            
!
999px

Console