By Vijay Pancholi | Simple Catch the Emoji! Game | Full Code (HTML + CSS + JS )

by Vijay Pancholi

HTML

<h1>🎯 Catch the Emoji!</h1>
<div id="score">Score: 0</div>
<div id="timer">Time: 30</div>
<button id="startBtn">Start Game</button>
<div id="gameArea"></div>

CSS

* {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: sans-serif;
      text-align: center;
      background: linear-gradient(to top right, #ffecd2 0%, #fcb69f 100%);
      overflow: hidden;
    }

    h1 {
      margin: 20px;
      font-size: 2.5rem;
    }

    #score, #timer {
      font-size: 1.5rem;
      margin: 10px;
    }

    #gameArea {
      position: relative;
      width: 100vw;
      height: 80vh;
      overflow: hidden;
      border-top: 2px solid #ccc;
    }

    .emoji {
      position: absolute;
      font-size: 2rem;
      cursor: pointer;
      user-select: none;
      animation: fall linear;
    }

    @keyframes fall {
      to {
        transform: translateY(100vh);
        opacity: 0;
      }
    }

    #startBtn {
      padding: 10px 20px;
      font-size: 1.2rem;
      border: none;
      border-radius: 5px;
      background: #ff6b81;
      color: white;
      cursor: pointer;
      margin: 20px;
    }

    #startBtn:hover {
      background: #ff4757;
    }

JavaScript

const emojis = ['πŸ˜€','🐱','🐢','🦊','🐼','🐸','πŸ¦„','πŸ•','🍩','🌈','🎈','πŸš€','πŸ’Ž'];
    const gameArea = document.getElementById('gameArea');
    const scoreDisplay = document.getElementById('score');
    const timerDisplay = document.getElementById('timer');
    const startBtn = document.getElementById('startBtn');

    let score = 0;
    let timeLeft = 30;
    let gameInterval, timerInterval;

    function randomEmoji() {
      const emoji = document.createElement('div');
      emoji.classList.add('emoji');
      emoji.innerText = emojis[Math.floor(Math.random() * emojis.length)];
      emoji.style.left = Math.random() * 90 + 'vw';
      emoji.style.top = '-40px';
      emoji.style.animationDuration = (2 + Math.random() * 3) + 's';

      emoji.addEventListener('click', () => {
        score++;
        scoreDisplay.textContent = "Score: " + score;
        emoji.remove();
      });

      gameArea.appendChild(emoji);

      // Remove after animation ends
      setTimeout(() => emoji.remove(), 5000);
    }

    function startGame() {
      score = 0;
      timeLeft = 30;
      scoreDisplay.textContent = "Score: 0";
      timerDisplay.textContent = "Time: 30";
      startBtn.disabled = true;
      gameInterval = setInterval(randomEmoji, 500);
      timerInterval = setInterval(() => {
        timeLeft--;
        timerDisplay.textContent = "Time: " + timeLeft;
        if (timeLeft <= 0) {
          endGame();
        }
      }, 1000);
    }

    function endGame() {
      clearInterval(gameInterval);
      clearInterval(timerInterval);
      alert("⏰ Time's up! Your score: " + score);
      startBtn.disabled = false;
    }

    startBtn.addEventListener('click', startGame);