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.
<div id="game-container"></div>
html,
body,
#game-container {
margin: 0;
padding: 0;
background-color: black;
}
#game-container {
min-width: 100vw;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#game-container > canvas {
border-radius: 5px;
}
/**
* Author: Dotta-ish, mostly based on work by Michael Hadley, mikewesthad.com
*
* The world assets aren't original or from a Forgotten Runes game
* they're a demo originally from:
* - Tuxemon, https://github.com/Tuxemon/Tuxemon
*
* The tilemap files and setup for this project come from:
* - https://github.com/mikewesthad/phaser-3-tilemap-blog-posts/tree/master/examples/post-1/assets
* - https://medium.com/@michaelwesthadley/modular-game-worlds-in-phaser-3-tilemaps-1-958fc7e6bbd6
*
* If you want to build your own map you can use the Tiled.app editor
* - https://gamefromscratch.com/tiled-map-editor-tutorial-series/
* - https://doc.mapeditor.org/en/stable/
*/
// Spritesheets and animations in Phaser use a globally-scoped namespace
// so we scope the name of the individual wizard's spritesheet as well.
// We'll use this wizardId throughout
const wizardId = getQueryStringParam("wizard") || "1945";
// this helps us below
const wizardTextureTag = `wizards-${wizardId}`;
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: "game-container",
pixelArt: true,
physics: {
default: "arcade",
arcade: {
gravity: { y: 0 }
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
const game = new Phaser.Game(config);
let cursors;
let player;
let showDebug = false;
function preload() {
this.load.image(
"tiles",
"https://mikewesthad.github.io/phaser-3-tilemap-blog-posts/post-1/assets/tilesets/tuxmon-sample-32px-extruded.png"
);
this.load.tilemapTiledJSON(
"map",
"https://mikewesthad.github.io/phaser-3-tilemap-blog-posts/post-1/assets/tilemaps/tuxemon-town.json"
);
// every Wizard has a spritesheet
this.load.aseprite(
wizardTextureTag,
`https://www.forgottenrunes.com/api/art/wizards/${wizardId}/spritesheet.png`,
`https://www.forgottenrunes.com/api/art/wizards/${wizardId}/spritesheet.json`
);
}
function create() {
const map = this.make.tilemap({ key: "map" });
// Parameters are the name you gave the tileset in Tiled and then the key of the tileset image in
// Phaser's cache (i.e. the name you used in preload)
const tileset = map.addTilesetImage("tuxmon-sample-32px-extruded", "tiles");
// Parameters: layer name (or index) from Tiled, tileset, x, y
const belowLayer = map.createLayer("Below Player", tileset, 0, 0);
const worldLayer = map.createLayer("World", tileset, 0, 0);
const aboveLayer = map.createLayer("Above Player", tileset, 0, 0);
worldLayer.setCollisionByProperty({ collides: true });
// By default, everything gets depth sorted on the screen in the order we created things. Here, we
// want the "Above Player" layer to sit on top of the player, so we explicitly give it a depth.
// Higher depths will sit on top of lower depth objects.
aboveLayer.setDepth(10);
// Object layers in Tiled let you embed extra info into a map - like a spawn point or custom
// collision shapes. In the tmx file, there's an object layer with a point named "Spawn Point"
const spawnPoint = map.findObject(
"Objects",
(obj) => obj.name === "Spawn Point"
);
// Create a sprite with physics enabled via the physics system. The image used for the sprite has
// a bit of whitespace, so I'm using setSize & setOffset to control the size of the player's body.
// You can debug this by hitting the "C" key in the demo
player = this.physics.add
.sprite(spawnPoint.x, spawnPoint.y, wizardTextureTag, 0)
.setSize(20, 12)
.setScale(2)
.setOffset(15, 33);
// Watch the player and worldLayer for collisions, for the duration of the scene:
this.physics.add.collider(player, worldLayer);
// Create the player's walking animations from the texture atlas. These are stored in the global
// animation manager so any sprite can access them.
const tags = this.anims.createFromAseprite(wizardTextureTag);
const camera = this.cameras.main;
camera.startFollow(player);
camera.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
cursors = this.input.keyboard.createCursorKeys();
// Help text that has a "fixed" position on the screen
this.add
.text(16, 16, "Change your wizard number in the URL\nArrow keys to move", {
font: "16px monospace",
fill: "#000000",
padding: { x: 20, y: 10 },
backgroundColor: "#ffffff"
})
.setScrollFactor(0)
.setDepth(30);
// Debug graphics
this.input.keyboard.once("keydown-C", (event) => {
// Turn on physics debugging to show player's hitbox
this.physics.world.createDebugGraphic();
// Create worldLayer collision graphic above the player, but below the help text
const graphics = this.add.graphics().setAlpha(0.75).setDepth(20);
worldLayer.renderDebug(graphics, {
tileColor: null, // Color of non-colliding tiles
collidingTileColor: new Phaser.Display.Color(243, 134, 48, 255), // Color of colliding tiles
faceColor: new Phaser.Display.Color(40, 39, 37, 255) // Color of colliding face edges
});
});
}
function update(time, delta) {
const speed = 175;
const prevVelocity = player.body.velocity.clone();
// Stop any previous movement from the last frame
player.body.setVelocity(0);
// Horizontal movement
if (cursors.left.isDown) {
player.body.setVelocityX(-speed);
} else if (cursors.right.isDown) {
player.body.setVelocityX(speed);
}
// Vertical movement
if (cursors.up.isDown) {
player.body.setVelocityY(-speed);
} else if (cursors.down.isDown) {
player.body.setVelocityY(speed);
}
// Normalize and scale the velocity so that player can't move faster along a diagonal
player.body.velocity.normalize().scale(speed);
// Update the animation last and give left/right animations precedence over up/down animations
if (cursors.left.isDown) {
player.play(
{ key: `${wizardTextureTag}-left`, startFrame: 0, repeat: -1 },
true
);
} else if (cursors.right.isDown) {
player.anims.play(
{ key: `${wizardTextureTag}-right`, startFrame: 0, repeat: -1 },
true
);
} else if (cursors.up.isDown) {
player.anims.play(
{ key: `${wizardTextureTag}-back`, startFrame: 0, repeat: -1 },
true
);
} else if (cursors.down.isDown) {
player.anims.play(
{ key: `${wizardTextureTag}-front`, startFrame: 0, repeat: -1 },
true
);
} else {
player.anims.stop();
// If we were moving, pick and idle frame to use
if (prevVelocity.x < 0) player.setTexture(wizardTextureTag, "4");
else if (prevVelocity.x > 0) player.setTexture(wizardTextureTag, "12");
else if (prevVelocity.y < 0) player.setTexture(wizardTextureTag, "8");
else if (prevVelocity.y > 0) player.setTexture(wizardTextureTag, "0");
}
}
function getQueryStringParam(param) {
var url = window.location.toString();
url.match(/\?(.+)$/);
var params = RegExp.$1;
params = params.split("&");
var queryStringList = {};
for (var i = 0; i < params.length; i++) {
var tmp = params[i].split("=");
queryStringList[tmp[0]] = unescape(tmp[1]);
}
return queryStringList[param];
}
Also see: Tab Triggers