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

              
                <div id="board">
  <div class="player"/>
</div>
              
            
!

CSS

              
                .player {
  width: 10em;
  height: 10em;
  background: url("https://i7.pngflow.com/pngimage/474/102/png-color-by-number-pokemon-pixel-art-minecraft-pokemon-yellow-pokemon-heartgold-and-soulsilver-pixel-art-video-game-pokemon-area-pokemon-pikachu-clipart-thumb.png");   background-size: cover;
  position: fixed;
}

#board {
  width: 100em;
  height: 100em;
}
              
            
!

JS

              
                // Initialize Firebase our database, you don't have to touch this part
var firebaseConfig = {
    apiKey: "AIzaSyBHcN0HVMwxxW-xBkXHYAm2X-kGWjxPzNk",
    authDomain: "basiccrud-ffe19.firebaseapp.com",
    databaseURL: "https://basiccrud-ffe19.firebaseio.com",
    projectId: "basiccrud-ffe19",
    storageBucket: "basiccrud-ffe19.appspot.com",
    messagingSenderId: "47974982638",
    appId: "1:47974982638:web:9a034202ca787ee47764ff"
};
firebase.initializeApp(firebaseConfig);
function getUniqueId() { //a silly function just to let you fork this codepen and have your own database
  var CODEPEN_ID = /[codepen|cdpn]\.io\/[^/]+\/(?:pen|debug|fullpage|fullembedgrid)\/([^?#]+)/;
  var id;
  if(CODEPEN_ID.test(window.location.href)) {
    id = CODEPEN_ID.exec(window.location.href)[1];
  } else if (CODEPEN_ID.test(document.location.href)){
    id = CODEPEN_ID.exec(window.location.href)[1];
  } else {
    var metas = document.getElementsByTagName('link');    
    for(i=0;i<metas.length;i++) {
      if(metas[i].getAttribute('rel') == 'canonical') {
        if(CODEPEN_ID.test(metas[i].getAttribute('href')))
        id = CODEPEN_ID.exec(metas[i].getAttribute('href'))[1];  
      }
    }
  }
  return id || `randoDB${Math.floor(Math.random()*10000000)}`;
}

//this is the "handler" for YOUR database where we can store and read values from anyone at your site!
let yourID = getUniqueId();
let yourDatabase = firebase.database().ref("sillygame").child(yourID);

let x = 0;
let y = 0;
let speed = 1;
let direction = "down";

yourDatabase.child("direction").on("value", function(ss){
  direction = ss.val() || "right";
});

let showSquare = function(){
  $(".player").css("left",`${x}vw`);
  $(".player").css("top",`${y}vh`);
}

let moveRight = function(){
  x = (x + speed) % 100;
  showSquare();
}

let moveLeft = function(){
  x = (x - speed + 100) % 100;
  showSquare();
}

let moveDown = function(){
  y = (y + speed) % 100;
  showSquare();
}

let moveUp = function(){
  y = (y - speed + 100) % 100;
  showSquare();
}

let setDatabaseDirection = function(aDir){
  yourDatabase.child("direction").set(aDir);
}

setInterval(function(){
  if (direction == "up"){
    moveUp();
  } else if (direction == "down"){
    moveDown();
  } else if (direction == "left"){
    moveLeft();
  } else if (direction == "right"){
    moveRight();
  }
}, 40);

$(window).bind("keydown",function(e) {
    switch(e.which) {
        case 37: // left
        setDatabaseDirection("left");
        break;

        case 38: // up
        setDatabaseDirection("up");
        break;

        case 39: // right
        setDatabaseDirection("right");
        break;

        case 40: // down
        setDatabaseDirection("down");
        break;

        default: return; // exit this handler for other keys
    }
    e.preventDefault(); // prevent the default action (scroll / move caret)
});
              
            
!
999px

Console