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.

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>
    <div id="game" style="background-color:#000000;"></div>
    
  </body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                /* getRandomColor()
Helper function, returns hex color value
*/
function getRandomColor() {
    var letters = '0123456789ABCDEF';
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}

/* key class */
function Key () {
  this.location; //where the key is placed
  this.crafty; //CraftyJS specific rendering data
  
  //sets up CraftyJS handling info
  this.draw = function (assocRoom) {
    this.crafty = Crafty.e('2D, DOM, Color, key, Collision')
    .attr({x: this.location.x + 10, y: (50 + (assocRoom.depth * 15)), w: 10, h: 10, z:assocRoom.depth})
    .color(assocRoom.color)
    .onHit("player", function(from) { //when the player touches the key
        assocRoom.unlock(); //unlock the associated room
        this.destroy(); //delete the key
     });
  }
}

/* room class */
function Room () {
  this.parentRoom;
  this.depth;
  this.key;
  this.x;
  this.y;
  this.z;
  this.width;
  this.height;
  this.color;
  this.crafty;
  
  this.init = function (setParentRoom,setDepth) {
    this.parentRoom = setParentRoom;
    this.depth = setDepth;
  };
  
  this.createDraw = function (x,y,width,height,color,solid) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.color = color;
    
    this.crafty = Crafty.e('2D, DOM, Color')
    .attr({x: this.x, y: this.y, w: this.width, h: this.height, z:this.depth})
    .color(this.color);
    
    if (solid){
      this.crafty.addComponent('solid');
    }
  };
  
  this.unlock = function() {
    this.crafty.removeComponent('solid',false);
  };
}

function roomGenerator (depthMax) {
  var roomsToCheck = [];
  var finishedRooms = [];
  
  //create the very first container room
  var initialRoom = new Room;
  initialRoom.init("none",0);
  initialRoom.createDraw (10,10,780,230,'#0000ff',false);
  roomsToCheck[roomsToCheck.length] = initialRoom;
  
  //loop thru all rooms
  while (roomsToCheck.length > 0){
    var currentRoom = roomsToCheck[0];
    
    //if you haven't reached max depth
    if(currentRoom.depth < depthMax){
      //setup a new room
      var newRoom = new Room;
      newRoom.init(currentRoom,currentRoom.depth+1);
      
      //prepare to create a list of parent rooms
      var allParentRooms = [];
      var roomCheck = newRoom;
      //compile room list
      while (roomCheck.parentRoom != "none") {
        allParentRooms[allParentRooms.length] = roomCheck.parentRoom;
        roomCheck = roomCheck.parentRoom;
      }
      
      //create key
      var curKey = new Key;
      //set key location randomly
      curKey.location = allParentRooms[Math.floor(Math.random() * allParentRooms.length)];
      //draw the new room and its key
      newRoom.createDraw(currentRoom.x + 50,10,780,230,getRandomColor(),true);
      curKey.draw(newRoom);
      //associate the room with its key
      newRoom.key = curKey;
      
      //add the new room to be checked, move the old one to finished
      roomsToCheck[roomsToCheck.length] = newRoom;
      finishedRooms[finishedRooms.length] = currentRoom;
      roomsToCheck.splice(0,1);
      
    }
    else {
      //at max depth, finished making rooms
      finishedRooms.push(currentRoom);
      roomsToCheck.splice(0,1);
    }
  }
}

//create CraftyJS window
Crafty.init(800,250, document.getElementById('game'));

//call room generator
roomGenerator(10);

//create basic player instance
Crafty.e('2D, DOM, Color, Fourway,Collision,player')
.attr({x: 20, y: 20, w: 20, h: 20, z: 100})
.color('#ffffff')
.fourway(200)
.bind('Moved', function(from) {
        if (this.hit('solid')) {
            this.attr({x: this.x - 10, y:this.y});
        };
});
              
            
!
999px

Console