HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html>
<head>
</head>
<body>
<div id="game" style="background-color:#000000;"></div>
</body>
</html>
/* 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});
};
});
Also see: Tab Triggers