Categories: Uncategorized

Click Shape Game | JavaScript Mini Project | Kabir Shrestha

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Click Shape Game</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="game-container">
    <p class="score">Score: <span id="score">0</span></p>
    <div class="shape" id="shape"></div>
  </div>

  <script src="script.js"></script>
</body>
</html>

styles.css

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
  }
  
  .game-container {
    text-align: center;
  }
  
  .shape {
    width: 100px;
    height: 100px;
    background-color: #3498db;
    border-radius: 50%;
    margin: 20px auto;
    cursor: pointer;
  }
  
  .score {
    font-size: 24px;
  }

script.js

let score = 0;

function getRandomPosition() {
  const shape = document.getElementById('shape');
  const maxWidth = window.innerWidth - shape.clientWidth;
  const maxHeight = window.innerHeight - shape.clientHeight;
  
  const randomX = Math.floor(Math.random() * maxWidth);
  const randomY = Math.floor(Math.random() * maxHeight);

  shape.style.left = `${randomX}px`;
  shape.style.top = `${randomY}px`;
}

function increaseScore() {
  score++;
  document.getElementById('score').innerText = score;
  getRandomPosition();
}

document.getElementById('shape').addEventListener('click', increaseScore);

getRandomPosition();

 

jaminrai

Recent Posts

Computer Science Project Report Guidelines and Sample

Guidelines (Click Below to Download) SAMPLE 1 SAMPLE 2

5 months ago

Skype Closes Its Doors After 20 Years: A Look Back and the Apps That Faded with It

After 20 years of transforming global communication, Skype has officially ceased operations. Microsoft, which purchased…

12 months ago

The Duck and the Tortoise: A Tale of Patience and Teamwork

In a serene forest by the edge of a sparkling lake, there lived a cheerful…

1 year ago