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="score">Score: 0</div>
<div id="currency">Coins: 0</div>
<div id="gameArea">
<div id="player">🕵</div>
<div id="startScreen");>
<button id="startButton">START ACTION</button>
</div>
<div id="gameOverScreen">
<div id="gameOverText">FINALIZED! RESOURCES: <span id="finalScore">0</span></div>
<button id="restartButton">RESTART</button>
</div>
</div>
<script src="script.js"></script>
body {
margin: 0;
overflow: hidden;
background-image:url("https://dexkit-storage.nyc3.digitaloceanspaces.com/dexkit/media-library/account/0xb35e9e256fb6684f8b72e1ef97e718f177d19a71/HIGH%20SENSE%20-%20GAME%20BACKGROUND.jpg");
color: white;
font-family: Arial, sans-serif;
}
#gameArea {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
background: rgba(0, 0, 0, 0.6);
}
#player, .enemy, .projectile, .explosion, .coin {
position: absolute;
font-size: 16px;
pointer-events: none;
}
#player {
color: green;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.enemy {
color: red;
}
.projectile {
color: white;
}
.explosion {
color: orange;
font-size: 30px;
z-index: 1;
}
.coin {
color: gold;
font-size: 24px;
}
#score, #currency {
position: fixed;
top: 10px;
font-size: 24px;
}
#score {
left: 10px;
}
#currency {
right: 10px;
}
#gameOverScreen {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
}
#startScreen {
background-image: url("https://dexkit-storage.nyc3.digitaloceanspaces.com/dexkit/media-library/account/0xb35e9e256fb6684f8b72e1ef97e718f177d19a71/HIGH%20SENSE%20-%20GAME%20START%20SCREEN.jpg");
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover;
width: 100%;
height: 100%;
}
#startButton{
transform: translate(0%, 1200%);
width: 100vw;
}
#gameOverScreen {
display: none;
}
button {
background: #9CFF1E;
border: none;
color: white;
padding: 10px 20px;
font-size: 20px;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s, transform 0.2s;
}
button:hover {
background: #ff3e30;
}
button:active {
transform: scale(0.95);
}
#gameOverText {
font-size: 24px;
text-align: center;
margin-bottom: 20px;
}
let score = 0;
let coins = 0;
let cameraShake = 0;
let gameActive = false;
let isShieldActive = false;
let rapidFire = false;
let enemyInterval;
let coinInterval;
const player = document.getElementById('player');
const gameArea = document.getElementById('gameArea');
const scoreDisplay = document.getElementById('score');
const currencyDisplay = document.getElementById('currency');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreDisplay = document.getElementById('finalScore');
const startButton = document.getElementById('startButton');
const restartButton = document.getElementById('restartButton');
let playerX = window.innerWidth / 2;
let playerY = window.innerHeight / 2;
const keys = {};
document.addEventListener('keydown', (e) => {
keys[e.key] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.key] = false;
});
document.addEventListener('mousemove', (e) => {
player.style.transform = `translate(-50%, -50%) rotate(${Math.atan2(e.clientY - playerY, e.clientX - playerX) * 180 / Math.PI + 90}deg)`;
});
document.addEventListener('click', (e) => {
if (gameActive) {
if (rapidFire) {
for (let i = 0; i < 3; i++) {
setTimeout(() => shootProjectile(e.clientX, e.clientY), i * 100);
}
} else {
shootProjectile(e.clientX, e.clientY);
}
}
});
startButton.addEventListener('click', () => {
restartGame();
});
restartButton.addEventListener('click', () => {
restartGame();
});
function restartGame() {
// Reset game state
score = 0;
coins = 0;
cameraShake = 0;
gameActive = true;
isShieldActive = false;
rapidFire = false;
// Clear existing enemies, projectiles, and coins
const existingEnemies = document.getElementsByClassName('enemy');
while (existingEnemies.length > 0) {
existingEnemies[0].remove();
}
const existingProjectiles = document.getElementsByClassName('projectile');
while (existingProjectiles.length > 0) {
existingProjectiles[0].remove();
}
const existingCoins = document.getElementsByClassName('coin');
while (existingCoins.length > 0) {
existingCoins[0].remove();
}
// Stop any ongoing intervals
if (enemyInterval) {
clearInterval(enemyInterval);
}
if (coinInterval) {
clearInterval(coinInterval);
}
// Reset player position
playerX = window.innerWidth / 2;
playerY = window.innerHeight / 2;
player.style.left = `${playerX}px`;
player.style.top = `${playerY}px`;
// Reset screens
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
scoreDisplay.innerText = `Score: ${score}`;
currencyDisplay.innerText = `Coins: ${coins}`;
gameArea.style.transform = 'translate(0, 0)';
// Start game
createEnemies();
createCoins();
updatePlayerPosition();
}
function shootProjectile(targetX, targetY) {
const projectile = document.createElement('div');
projectile.className = 'projectile';
projectile.innerHTML = '💣';
projectile.style.left = `${playerX}px`;
projectile.style.top = `${playerY}px`;
gameArea.appendChild(projectile);
const angle = Math.atan2(targetY - playerY, targetX - playerX);
const speed = 5;
const dx = Math.cos(angle) * speed;
const dy = Math.sin(angle) * speed;
function moveProjectile() {
const rect = projectile.getBoundingClientRect();
if (rect.left < 0 || rect.right > window.innerWidth || rect.top < 0 || rect.bottom > window.innerHeight) {
projectile.remove();
return;
}
projectile.style.left = `${parseFloat(projectile.style.left) + dx}px`;
projectile.style.top = `${parseFloat(projectile.style.top) + dy}px`;
const enemies = document.getElementsByClassName('enemy');
for (let enemy of enemies) {
if (isColliding(projectile, enemy)) {
score++;
scoreDisplay.innerText = `Score: ${score}`;
createExplosion(enemy);
projectile.remove();
enemy.remove();
return;
}
}
requestAnimationFrame(moveProjectile);
}
moveProjectile();
}
function createExplosion(enemy) {
const explosion = document.createElement('div');
explosion.className = 'explosion';
explosion.innerHTML = '💥';
const rect = enemy.getBoundingClientRect();
explosion.style.left = `${rect.left}px`;
explosion.style.top = `${rect.top}px`;
gameArea.appendChild(explosion);
setTimeout(() => {
explosion.remove();
}, 500);
shakeCamera();
}
function isColliding(a, b) {
const rect1 = a.getBoundingClientRect();
const rect2 = b.getBoundingClientRect();
return !(rect1.right < rect2.left ||
rect1.left > rect2.right ||
rect1.bottom < rect2.top ||
rect1.top > rect2.bottom);
}
function createEnemies() {
if (!gameActive) return;
enemyInterval = setInterval(() => {
const enemy = document.createElement('div');
enemy.className = 'enemy';
enemy.innerHTML = '₿';
const side = Math.floor(Math.random() * 4);
let x, y;
switch (side) {
case 0:
x = Math.random() * window.innerWidth;
y = -20;
break;
case 1:
x = Math.random() * window.innerWidth;
y = window.innerHeight + 20;
break;
case 2:
x = -20;
y = Math.random() * window.innerHeight;
break;
case 3:
x = window.innerWidth + 20;
y = Math.random() * window.innerHeight;
break;
}
enemy.style.left = `${x}px`;
enemy.style.top = `${y}px`;
gameArea.appendChild(enemy);
function moveEnemy() {
if (!gameActive) return;
const angle = Math.atan2(playerY - y, playerX - x);
const speed = 2;
x += Math.cos(angle) * speed;
y += Math.sin(angle) * speed;
enemy.style.left = `${x}px`;
enemy.style.top = `${y}px`;
if (isColliding(player, enemy)) {
if (!isShieldActive) {
finalScoreDisplay.innerText = score;
gameOverScreen.style.display = 'flex';
gameActive = false;
clearInterval(enemyInterval);
clearInterval(coinInterval);
} else {
isShieldActive = false;
enemy.remove();
createExplosion(enemy);
}
return;
}
requestAnimationFrame(moveEnemy);
}
moveEnemy();
}, 1000);
}
function createCoins() {
if (!gameActive) return;
coinInterval = setInterval(() => {
const coin = document.createElement('div');
coin.className = 'coin';
coin.innerHTML = '🪙';
const x = Math.random() * window.innerWidth;
const y = Math.random() * window.innerHeight;
coin.style.left = `${x}px`;
coin.style.top = `${y}px`;
gameArea.appendChild(coin);
function moveCoin() {
if (!gameActive) return;
const rect = coin.getBoundingClientRect();
if (rect.left < 0 || rect.right > window.innerWidth || rect.top < 0 || rect.bottom > window.innerHeight) {
coin.remove();
return;
}
const playerRect = player.getBoundingClientRect();
if (isColliding(coin, player)) {
coins++;
currencyDisplay.innerText = `Coins: ${coins}`;
coin.remove();
return;
}
requestAnimationFrame(moveCoin);
}
moveCoin();
}, 5000);
}
function updatePlayerPosition() {
if (!gameActive) return;
if (keys['w']) playerY -= 5;
if (keys['s']) playerY += 5;
if (keys['a']) playerX -= 5;
if (keys['d']) playerX += 5;
playerX = Math.max(0, Math.min(window.innerWidth, playerX));
playerY = Math.max(0, Math.min(window.innerHeight, playerY));
player.style.left = `${playerX}px`;
player.style.top = `${playerY}px`;
requestAnimationFrame(updatePlayerPosition);
}
function shakeCamera() {
cameraShake = 10;
function applyShake() {
if (cameraShake > 0) {
const xOffset = (Math.random() - 0.5) * cameraShake;
const yOffset = (Math.random() - 0.5) * cameraShake;
gameArea.style.transform = `translate(${xOffset}px, ${yOffset}px)`;
cameraShake *= 0.9;
requestAnimationFrame(applyShake);
} else {
gameArea.style.transform = 'translate(0, 0)';
}
}
applyShake();
}
function activateShield() {
isShieldActive = true;
setTimeout(() => isShieldActive = false, 5000);
}
function activateRapidFire() {
rapidFire = true;
setTimeout(() => rapidFire = false, 5000);
}
// Simulate power-ups every 30 seconds
setInterval(() => {
if (gameActive) {
if (Math.random() < 0.5) {
activateShield();
} else {
activateRapidFire();
}
}
}, 30000);
Also see: Tab Triggers