html igra voće

<!-- ========================= -->
<!-- index.html -->
<!-- ========================= -->

<!DOCTYPE html>
<html lang="bs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hvatanje Povrća</title>

<style>

/* ========================= */
/* CSS */
/* ========================= */

body{
    margin:0;
    padding:0;
    font-family:Arial,sans-serif;
    background:linear-gradient(to bottom,#7ed957,#4caf50);
    text-align:center;
    overflow:hidden;
}

h1{
    color:white;
    margin-top:15px;
    font-size:40px;
}

.info{
    display:flex;
    justify-content:center;
    gap:40px;
    color:white;
    font-size:28px;
    margin:15px;
    font-weight:bold;
}

#gameArea{
    width:90%;
    max-width:900px;
    height:500px;
    margin:auto;
    background:#dcedc8;
    border:6px solid white;
    border-radius:20px;
    position:relative;
    overflow:hidden;
}

.vegetable{
    position:absolute;
    font-size:55px;
    cursor:pointer;
    user-select:none;
    transition:0.1s;
}

.vegetable:hover{
    transform:scale(1.2);
}

button{
    margin-top:20px;
    padding:15px 35px;
    border:none;
    border-radius:12px;
    background:#ff9800;
    color:white;
    font-size:22px;
    cursor:pointer;
    font-weight:bold;
}

button:hover{
    background:#f57c00;
}

#finalMessage{
    color:white;
    font-size:35px;
    margin-top:20px;
    font-weight:bold;
}

</style>
</head>

<body>

<h1>🥕 Hvatanje Povrća 🥦</h1>

<div class="info">
    <div>Poeni: <span id="score">0</span></div>
    <div>Vrijeme: <span id="time">30</span>s</div>
</div>

<div id="gameArea"></div>

<button onclick="startGame()">▶ Pokreni Igru</button>

<div id="finalMessage"></div>

<script>

/* ========================= */
/* JAVASCRIPT */
/* ========================= */

const gameArea = document.getElementById("gameArea");
const scoreText = document.getElementById("score");
const timeText = document.getElementById("time");
const finalMessage = document.getElementById("finalMessage");

const vegetables = [
    "🥕",
    "🥦",
    "🍅",
    "🥒",
    "🌽",
    "🧅"
];

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

function createVegetable(){

    const veg = document.createElement("div");

    veg.classList.add("vegetable");

    veg.innerText =
        vegetables[Math.floor(Math.random() * vegetables.length)];

    const x =
        Math.random() * (gameArea.clientWidth - 70);

    const y =
        Math.random() * (gameArea.clientHeight - 70);

    veg.style.left = x + "px";
    veg.style.top = y + "px";

    veg.addEventListener("click", () => {

        score++;

        scoreText.innerText = score;

        veg.remove();

    });

    gameArea.appendChild(veg);

    setTimeout(() => {

        if(veg.parentNode){

            veg.remove();

        }

    },1500);

}

function startGame(){

    score = 0;
    timeLeft = 30;

    scoreText.innerText = score;
    timeText.innerText = timeLeft;

    finalMessage.innerText = "";

    gameArea.innerHTML = "";

    clearInterval(gameInterval);
    clearInterval(timerInterval);

    gameInterval =
        setInterval(createVegetable,700);

    timerInterval =
        setInterval(() => {

        timeLeft--;

        timeText.innerText = timeLeft;

        if(timeLeft <= 0){

            clearInterval(gameInterval);
            clearInterval(timerInterval);

            finalMessage.innerText =
                "🎉 Kraj igre! Osvojio si " + score + " poena!";

        }

    },1000);

}

</script>

</body>
</html>```

Leave a Comment

Your email address will not be published. Required fields are marked *