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.
.dialogue
.dialogue__content
h1 Space Shooter
ul
li Use the #[span.key.key--arrow ←] and #[span.key.key--arrow →] keys to move.
li Use #[span.key spacebar] to fire lasers.
//- li Try not to die.
button Start
ul.hud.hud--hidden
li.hud__score Score: #[span 0]
//- li.hud__lives Coming soon
$base: #ffffff;
$highlight: #ff9d00;
%button-style {
border: 1px solid currentColor;
border-radius: 5px;
display: inline-block;
padding: .8em;
}
body {
color: $base;
font-family: 'Press Start 2P', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
canvas {
display: block;
}
h1 {
font-size: 3rem;
margin-bottom: .5em;
}
ul {
margin-bottom: 2rem;
}
li {
color: $highlight;
font-size: .8rem;
margin-bottom: 1em;
text-transform: uppercase;
}
button {
@extend %button-style;
appearance: none;
background-color: $base;
cursor: pointer;
font-family: inherit;
font-size: 1rem;
outline: none;
padding: .7em .85em .6em 1em;
text-transform: uppercase;
transition: color .2s linear, background-color .2s linear;
&:focus,
&:hover {
background-color: black;
color: $highlight;
transition-duration: .07s;
}
}
.dialogue {
align-items: center;
background-color: rgba(0, 0, 0, .85);
bottom: 0;
display: flex;
justify-content: center;
left: 0;
opacity: 1;
position: fixed;
right: 0;
text-align: center;
top: 0;
transform: translateX(0);
}
.dialogue--hidden {
$duration: .3s;
opacity: 0;
transform: translateX(-100vw);
transition: opacity $duration linear 0s, transform 0s linear $duration;
}
.dialogue__content {
max-width: 31.25rem; // 500px / 16px
padding: 18px;
}
.key {
@extend %button-style;
border-color: white;
color: white;
}
.key--arrow {
font-size: 1.8rem;
padding: 0.35em 0.1em 0.6em;
position: relative;
line-height: 0.3;
top: .05em;
}
.hud {
box-sizing: border-box;
display: flex;
justify-content: flex-start;
margin: 0;
left: 0;
opacity: 1;
padding: 1rem;
position: fixed;
top: 0;
transition: opacity .07s linear .5s;
width: 100%;
}
.hud--hidden {
opacity: 0;
}
.hud__score {
margin: 0;
}
const DEV_MODE = false;
const stage = document.createElement('canvas'),
ctx = stage.getContext('2d'),
dialogue = document.querySelector('.dialogue'),
startBtn = dialogue.querySelector('button'),
hud = document.querySelector('.hud'),
scoreNode = hud.querySelector('.hud__score span');
let ship, lasers = [], enemies = [],
playing = false,
gameStarted = false,
speedMultiplier,
enemySeedFrameInterval,
score = 0,
tick = 0,
laserTick = 0;
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function calcScore(x) {
return Math.floor((1 / x) * 500);
}
function Ship(options) {
this.radius = 15;
this.x = options.x || stage.width * .5 - this.radius - .5;
this.y = options.y || stage.height - this.radius - 30;
this.width = this.radius * 2;
this.height = this.width;
this.color = options.color || 'red';
this.left = false;
this.right = false;
this.speed = 10;
this.active = true;
document.addEventListener('keydown', this.onKeyDown.bind(this));
document.addEventListener('keyup', this.onKeyUp.bind(this));
}
Ship.prototype.update = function(x) {
this.x = x;
this.y = stage.height - this.radius - 30;
}
Ship.prototype.draw = function() {
ctx.save();
if (DEV_MODE) {
ctx.fillStyle = 'skyblue';
ctx.fillRect(this.x, this.y, this.width, this.width);
}
ctx.fillStyle = this.color;
ctx.fillRect(this.x + this.radius - 5, this.y, 10, this.radius);
ctx.fillRect(this.x, this.y + this.radius, this.width, 10);
ctx.fillRect(this.x, this.y + this.radius + 10, 10, 5);
ctx.fillRect(this.x + this.width - 10, this.y + this.radius + 10, 10, 5);
ctx.restore();
}
Ship.prototype.onKeyDown = function(e) {
if (ship.active) {
if (e.keyCode === 39) this.right = true;
else if (e.keyCode === 37) this.left = true;
if (e.keyCode == 32 && !this.shooting) {
this.shooting = true;
laserTick = 0;
}
}
}
Ship.prototype.onKeyUp = function(e) {
if (e.key === 'ArrowRight') this.right = false;
else if (e.key === 'ArrowLeft') this.left = false;
else if (e.keyCode == 32) this.shooting = false;
}
function Laser(options) {
this.x = options.x - .5;
this.y = options.y || stage.height - 50;
this.width = 6;
this.height = 20;
this.speed = 15;
this.color = options.color || 'white';
this.active = true;
}
Laser.prototype.update = function(y) {
this.y = y;
}
Laser.prototype.draw = function() {
ctx.save();
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.rect(this.x, this.y, this.width, this.height);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function Enemy(options) {
this.radius = randomBetween(10, 40);
this.width = this.radius * 2;
this.height = this.width;
this.x = randomBetween(0, stage.width - this.width);
this.y = -this.radius * 2;
this.color = options != undefined && options.color ? options.color : 'white';
this.speed = 2;
this.active = true;
}
Enemy.prototype.update = function(x, y) {
this.x = x;
this.y = y;
}
Enemy.prototype.draw = function() {
if (DEV_MODE) {
ctx.fillStyle = 'skyblue';
ctx.fillRect(this.x, this.y, this.width, this.width);
}
ctx.save();
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x + this.radius, this.y + this.radius, this.radius, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function hitTest(item1, item2) {
let collision = true;
if (
item1.x > item2.x + item2.width ||
item1.y > item2.y + item2.height ||
item2.x > item1.x + item1.width ||
item2.y > item1.y + item1.height
) {
collision = false;
}
return collision;
}
function handleLaserCollision() {
for (let enemy of enemies) {
for (let laser of lasers) {
let collision = hitTest(laser, enemy);
if (collision && laser.active) {
console.log('you destroyed an enemy');
enemy.active = false;
laser.active = false;
// increase enemy speed and frequency of enemy spawns
speedMultiplier += .025;
if (enemySeedFrameInterval > 20) {
enemySeedFrameInterval -= 2;
}
// increase score
score += calcScore(enemy.radius);
scoreNode.textContent = score;
}
}
}
}
function handleShipCollision() {
// check for collisions between ship and enemies
if (enemies.length) {
for (let enemy of enemies) {
let collision = hitTest(ship, enemy);
if (collision) {
console.log('your ship was destroyed');
ship.active = false;
setTimeout(() => {
ship.active = true;
speedMultiplier = 1;
enemySeedFrameInterval = 100;
score = 0;
scoreNode.textContent = score;
}, 2000);
}
}
}
}
function drawShip(xPosition) {
if (ship.active) {
ship.update(xPosition);
ship.draw();
}
}
function drawEnemies() {
if (enemies.length) {
for (let enemy of enemies) {
// draw an enemy if it's active
if (enemy.active) {
enemy.update(enemy.x, enemy.y += enemy.speed * speedMultiplier);
enemy.draw();
}
}
}
}
function enemyCleanup() {
if (enemies.length) {
enemies = enemies.filter(enemy => {
let visible = enemy.y < stage.height + enemy.width;
let active = enemy.active === true;
return visible && active;
});
}
}
function drawLasers() {
if (lasers.length) {
for (let laser of lasers) {
if (laser.active) {
laser.update(laser.y -= laser.speed);
laser.draw();
}
}
}
}
function laserCleanup() {
lasers = lasers.filter(laser => {
let visible = laser.y > -laser.height;
let active = laser.active === true;
return visible && active;
});
}
function render(delta) {
if (playing) {
let xPos = ship.x;
// seed new enemies
if (tick % enemySeedFrameInterval === 0 && ship.active) {
const enemy = new Enemy();
enemies.push(enemy);
console.log({enemySeedFrameInterval, speedMultiplier})
}
// background
ctx.save();
ctx.fillStyle = '#222222';
ctx.fillRect(0, 0, stage.width, stage.height);
ctx.restore();
// ship movement
if (ship.left)
xPos = ship.x -= ship.speed;
else if (ship.right)
xPos = ship.x += ship.speed;
// stage boundaries
if (gameStarted) {
if (xPos < 0)
xPos = 0;
else if (xPos > stage.width - ship.width)
xPos = stage.width - ship.width;
}
// create lasers, if shooting
if (ship.active && ship.shooting) {
if (laserTick === 0 || laserTick % 10 === 0) {
let laser = new Laser({
color: 'skyblue',
x: ship.x + ship.radius - 3
});
lasers.push(laser);
}
}
drawShip(xPos);
handleShipCollision();
handleLaserCollision();
drawLasers();
drawEnemies();
enemyCleanup();
laserCleanup();
if (ship.shooting) laserTick++;
tick++;
}
requestAnimationFrame(render);
}
function startGame(e) {
console.log('starting game');
dialogue.classList.add('dialogue--hidden');
hud.classList.remove('hud--hidden');
e.currentTarget.blur();
// reset the demo/intro to the actual game settings:
speedMultiplier = 1;
enemySeedFrameInterval = 100;
ship.x = stage.width * .5 - ship.radius - .5;
ship.y = stage.height - ship.radius - 30;
enemies = [];
gameStarted = true;
}
function onResize() {
stage.width = window.innerWidth;
stage.height = window.innerHeight;
}
startBtn.addEventListener('click', startGame);
window.addEventListener('resize', onResize);
document.body.appendChild(stage);
onResize();
// start the ship off-screen:
ship = new Ship({ color:'#ff9d00', x: -100, y: -100});
// set up some ridiculous enemy speeds for the intro:
speedMultiplier = 6,
enemySeedFrameInterval = 20;
playing = true;
render();
Also see: Tab Triggers