Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                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;
}
              
            
!

JS

              
                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);
              
            
!
999px

Console