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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
//------------------------------------------------------------------------------
//- This setup is designed to be editable, and to become something like a game
//- engine. As of now, there are only few types of tile may be inserted into
//- the game map. Those are the below, and its usage.
//-
//- S -> The starting point.
//- F -> Escape door.
//- K -> Key to open the locked door.
//- L -> Locked door. How to input: L_x-y where x is the x index (of array)
//- of K, and y is the y index. Array is declared as 2 dimensional array.
//- W -> Wall. You can't pass here.
//- T -> Tree. You can't pass here.
//- B -> Barrel. You can't pass here.
//- M -> Monster. Don't go here, you'll lose.
//- <space> -> Floor where you can walk.
//------------------------------------------------------------------------------
- const map = [];
- map.push(['W', 'W', 'W', 'M', 'W', 'W', ' ', ' ', ' ', 'M']);
- map.push([' ', ' ', ' ', ' ', 'W', 'W', 'M', ' ', 'B', 'B']);
- map.push(['W', ' ', 'B', 'B', 'W', 'W', ' ', ' ', ' ', 'K']);
- map.push(['W', ' ', ' ', ' ', 'W', 'W', 'W', ' ', 'M', 'W']);
- map.push([' ', 'T', 'T', ' ', 'M', ' ', 'L_7-9', ' ', 'T', 'W']);
- map.push([' ', 'T', 'T', ' ', 'W', ' ', 'B', 'B', 'W', 'W']);
- map.push(['K', ' ', 'S', ' ', 'L_0-6', ' ', ' ', 'L_1-9', 'F', 'W']);
- map.push([' ', 'B', 'T', 'B', 'M', ' ', 'B', 'W', 'W', 'W']);
- map.push([' ', 'T', 'W', 'L_9-2', ' ', ' ', 'B', 'W', 'T', 'W']);
- map.push(['W', 'K', ' ', ' ', 'M', ' ', ' ', 'K', 'T', 'W']);
- const preDialog = [
- 'You were lost in an unknown area! Find your way out.',
- 'Click on the tiles to move your character to that point.',
- 'Click on some of the objects to interact, but avoid the monsters.',
- ];
- const postDialog = [
- 'You managed to escape the dungeon!',
- '',
- ];
- const loseDialog = 'You lose! You touched the monster!';
//------------------------------------------------------------------------------
//- It's recommended not to edit the source code beyond this point
- const FINISH_ID = 'show_post_dialog';
form.game-form
//- Checkboxes for the grid tiles
- for (let y = 0; y < map.length; y++)
- for (let x = 0; x < map[0].length; x++)
- const position = `${x}_${y}`;
- const object = map[y][x];
- const block = object.split('_');
input.map-check(
type="radio",
name="map-radio",
id=`grid_${position}`,
checked=object === 'S'
)
- if (block[0] === 'K') {
input.map-check(type="checkbox" id=`key_${position}`)
- }
- else if (block[0] === 'L') {
input.map-check(type="checkbox" id=`lock_${position}`)
- }
- else if (block[0] === 'M') {
input.map-check(type="checkbox" id=`monster_${position}`).monster-check
- }
//- Pre-game dialog box
- for (let i = 0; i <= preDialog.length; i++)
input(
type="radio"
name="pre_dialog"
id=`pre_dialog_${i}`
checked=i == 0).map-check.dialog-check
//- Post-game dialog box
- for (let i in postDialog)
input(
type="radio"
name="post_dialog"
id=`post_dialog_${i}`
checked=i == 0).map-check.dialog-check
//- End game dialog box
input(type="checkbox" id=FINISH_ID).map-check.dialog-check
.view
.map
- for (let y = 0; y < map.length; y++)
- for (let x = 0; x < map[0].length; x++)
- const position = `${x}_${y}`;
- const block = map[y][x].split('_');
- let className = 'grid';
- let target = `grid_${position}`;
- if (block[0] === 'W') {
- className += ' wall';
- }
- else if (block[0] === 'T') {
- className += ' tree';
- }
- else if (block[0] === 'F') {
- className += ' finish';
- target = FINISH_ID;
- }
- else if (block[0] === 'B') {
- className += ' barrel';
- }
label(class=className, for=target)
- if (block[0] === 'K') {
label.object.key(for=`key_${position}`)
- }
- else if (block[0] === 'L') {
- const lockPosition = block[1].split('-').join('_');
label.object.lock(for=`lock_${position}`)
label.object.lock(data-key=`${lockPosition}`)
- }
- else if (block[0] === 'M') {
label.object.monster(for=`monster_${position}`)
- }
.character
.fog
//- Pre-game dialog box
- for (let i = preDialog.length - 1; i >= 0; i--)
- const dialog = preDialog[i];
label(
id=`pre_dialog_trigger_${i}`
for=`pre_dialog_${i + 1}`).pre-dialog #{dialog}
//- Post-game dialog box
- for (let i = postDialog.length - 1; i >= 0; i--)
- const dialog = postDialog[i];
label(
id=`post_dialog_trigger_${i}`
for=`post_dialog_${i + 1}`).post-dialog #{dialog}
- if (i === postDialog.length - 1)
button(type="reset").reset Play again
//- Game lost dialog box
.lose-dialog
span #{loseDialog}
button(type="reset").reset Play again
@use 'sass:math';
////////////////////////////////////////////////////////////////////////////////
// If there is a change on the HTML side, the variables below should be changed
////////////////////////////////////////////////////////////////////////////////
$repeat-x: 10;
$repeat-y: 10;
$visible-count-x: 3;
$visible-count-y: 3;
$pre-dialog-count: 3;
$post-dialog-count: 2;
////////////////////////////////////////////////////////////////////////////////
// Variables to style the game
////////////////////////////////////////////////////////////////////////////////
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P');
$grid-size: 25vmin;
$fog-color: #000000;
$dialog-font-family: 'Press Start 2P', cursive;
$dialog-font-size: 16px;
$dialog-padding: 8px;
$button-width: 20rem;
$button-height: $button-width / 5.76;
////////////////////////////////////////////////////////////////////////////////
// Debugging
////////////////////////////////////////////////////////////////////////////////
$debug: false;
////////////////////////////////////////////////////////////////////////////////
// Style goes here
////////////////////////////////////////////////////////////////////////////////
* {
box-sizing: border-box;
}
.game-form {
background-color: $fog-color;
width: 100vw;
height: 100vh;
display: flex;
flex-wrap: wrap;
}
.view {
background-color: $fog-color;
width: $grid-size * $visible-count-x;
height: $grid-size * $visible-count-y;
margin: auto;
flex-shrink: 0;
overflow: hidden;
position: relative;
}
.map {
--repeat-x: 0;
--repeat-y: 0;
display: grid;
grid-template-columns: repeat($repeat-x, $grid-size);
grid-template-rows: repeat($repeat-y, $grid-size);
position: absolute;
top: $grid-size * math.floor($visible-count-x / 2);
left: $grid-size * math.floor($visible-count-y / 2);
transition: transform 1024ms ease-out;
}
.grid {
background-image: url('https://assets.codepen.io/430361/css-maze-floor.png');
background-size: $grid-size $grid-size;
display: block;
position: relative;
pointer-events: none;
image-rendering: pixelated;
&.wall {
background-image: url('https://assets.codepen.io/430361/css-maze-wall.png');
}
&.tree {
background-image: url('https://assets.codepen.io/430361/css-maze-tree.png');
}
&.finish {
background-image: url('https://assets.codepen.io/430361/css-maze-exit.png');
pointer-events: auto;
}
&.barrel {
background-image: url('https://assets.codepen.io/430361/css-maze-barrel.png');
}
.object {
background-size: $grid-size $grid-size;
width: 100%;
height: 100%;
display: block;
position: absolute;
top: 0;
left: 0;
transition: opacity 128ms ease-out;
&.key {
background-image: url('https://assets.codepen.io/430361/css-maze-key.png');
pointer-events: auto;
}
&.lock {
background-image: url('https://assets.codepen.io/430361/css-maze-door.png');
}
&.monster {
background-image: url('https://assets.codepen.io/430361/css-maze-monster.png');
}
}
}
.map-check {
@if $debug == false {
display: none;
}
}
@for $x from 0 to $repeat-x {
@for $y from 0 to $repeat-y {
$position: #{$x}_#{$y};
#grid_#{$position}:checked ~ .view .map {
transform:
translateX($x * $grid-size * -1)
translateY($y * $grid-size * -1);
.grid[for='grid_#{$x + 1}_#{$y}'],
.grid[for='grid_#{$x - 1}_#{$y}'],
.grid[for='grid_#{$x}_#{$y + 1}'],
.grid[for='grid_#{$x}_#{$y - 1}'] {
pointer-events: auto;
}
.grid {
&.wall,
&.tree,
&.barrel {
pointer-events: none;
}
}
}
#key_#{$position}:checked ~ .view .map .grid {
[for='key_#{$position}'] {
opacity: 0;
pointer-events: none;
}
[data-key='#{$position}'] {
display: none;
}
}
#lock_#{$position}:checked ~ .view .map .grid [for='lock_#{$position}'] {
opacity: 0;
pointer-events: none;
}
#grid_#{$position}:checked ~ .view .character {
animation: animate-character_#{$position} 1024ms step-end;
}
@keyframes animate-character_#{$position} {
0% {
background-position: ($grid-size * -1) 0;
}
25% {
background-position: ($grid-size * -2) 0;
}
50% {
background-position: ($grid-size * -1) 0;
}
75% {
background-position: ($grid-size * -2) 0;
}
}
}
}
.character {
background-image: url('https://assets.codepen.io/430361/css-maze-character.png');
background-size: ($grid-size * 3) $grid-size;
background-position: 0 0;
width: $grid-size;
height: $grid-size;
position: absolute;
top: 50%;
left: 50%;
image-rendering: pixelated;
transform: translateX(-50%) translateY(-50%);
}
.fog {
$visible-size: $grid-size * math.floor($visible-count-x / 2);
background-image:
radial-gradient(
circle at center,
transparent 0 ($visible-size - ($grid-size / 2)),
$fog-color ($visible-size + ($grid-size / 2))
);
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
pointer-events: none;
}
.pre-dialog, .post-dialog, .lose-dialog {
font-family: $dialog-font-family;
font-size: $dialog-font-size;
text-align: center;
width: 100%;
height: 100%;
padding: $dialog-padding;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
left: 0;
overflow: hidden;
opacity: 0;
pointer-events: none;
user-select: none;
transition: opacity 512ms ease-out;
transform: scale(0);
&::after {
content: '▼';
position: absolute;
bottom: 8px;
right: 8px;
animation: blink 1024ms ease-out infinite;
}
}
@keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.pre-dialog, .lose-dialog {
color: #ffffff;
background-color: #000000;
}
@for $i from 0 to $pre-dialog-count {
#pre_dialog_#{$i}:checked ~ .view [id='pre_dialog_trigger_#{$i}'] {
opacity: 1;
pointer-events: auto;
transform: scale(1);
}
}
.post-dialog {
background-color: #ffffff;
color: #000000;
}
@for $i from 0 to $post-dialog-count {
#post_dialog_#{$i}:checked ~ #show_post_dialog:checked ~ .view {
[id='post_dialog_trigger_#{$i}'] {
opacity: 1;
pointer-events: auto;
transform: scale(1);
}
}
}
.lose-dialog {
flex-direction: column;
justify-content: space-evenly;
transition: opacity 512ms ease-out 1024ms;
}
.reset {
color: #000000;
font-family: inherit;
font-size: 1.3rem;
font-weight: bold;
background-color: transparent;
background-image: url('https://assets.codepen.io/430361/css-maze-reset.png');
background-size: $button-width $button-height;
width: $button-width;
height: $button-height;
border: 0;
outline: 0;
}
.monster-check:checked ~ .view .map .grid {
pointer-events: none !important;
}
.monster-check:checked ~ .view .lose-dialog {
opacity: 1;
pointer-events: auto;
transform: scale(1);
}
.monster-check:checked ~ .view .character {
background-image: url('https://assets.codepen.io/430361/css-maze-character-faint.png');
background-size: ($grid-size * 3) $grid-size;
background-position: ($grid-size * -2) 0;
animation: character-faint 512ms step-end !important;
}
@keyframes character-faint {
0% {
background-image: url('https://assets.codepen.io/430361/css-maze-character.png');
background-position: 0 0;
}
33.33333% {
background-image: url('https://assets.codepen.io/430361/css-maze-character-faint.png');
background-position: 0 0;
}
66.66667% {
background-position: ($grid-size * -1) 0;
}
100% {
background-position: ($grid-size * -2) 0;
}
}
////////////////////////////////////////////////////////////////////////////////
// Nothing is written here but comments.
////////////////////////////////////////////////////////////////////////////////
// This setup is designed to be editable, and to become something like a game
// engine. As of now, there are only few types of tile may be inserted into
// the game map. Those are the below, and its usage.
//
// S -> The starting point.
// F -> Escape door.
// K -> Key to open the locked door.
// L -> Locked door. How to input: L_x-y where x is the x index (of array)
// of K, and y is the y index. Array is declared as 2 dimensional array.
// W -> Wall. You can't pass here.
// T -> Tree. You can't pass here.
// B -> Barrel. You can't pass here.
// M -> Monster. Don't go here, you'll lose.
// <space> -> Floor where you can walk.
////////////////////////////////////////////////////////////////////////////////
Also see: Tab Triggers