You are on page 1of 2

<!

DOCTYPE html>
<html>
<head>
<title>Шутер игра</title>
<style>
body {
background-color: black;
margin: 0;
overflow: hidden;
}

#game {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}

#player {
width: 50px;
height: 50px;
background-color: red;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}

#enemy {
width: 50px;
height: 50px;
background-color: green;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
animation: enemyMove 2s linear infinite;
}

@keyframes enemyMove {
0% {top: 0;}
50% {top: 90vh;}
100% {top: 0;}
}

#bullet {
width: 10px;
height: 10px;
background-color: white;
position: absolute;
top: 95%;
left: 50%;
transform: translateX(-50%);
visibility: hidden;
}

#score {
color: white;
font-size: 20px;
position: absolute;
top: 10px;
right: 10px;
}
</style>
</head>
<body>
<div id="game">
<div id="player"></div>
<div id="enemy"></div>
<div id="bullet"></div>
<div id="score">Score: 0</div>
</div>

<script>
let player = document.getElementById("player");
let enemy = document.getElementById("enemy");
let bullet = document.getElementById("bullet");
let score = 0;

document.addEventListener("keydown", movePlayer);

function movePlayer(e) {
if (e.key === "ArrowLeft") {
player.style.left = parseInt(player.style.left) - 10 +
"px";
} else if (e.key === "ArrowRight") {
player.style.left = parseInt(player.style.left) + 10 +
"px";
} else if (e.key === " ") {
shoot();
}
}

function shoot() {
bullet.style.visibility = "visible";
let bulletPos = parseInt(bullet.style.top);
let enemyPos = parseInt(enemy.style.top);

let bulletInterval = setInterval(function() {


bulletPos -= 10;
bullet.style.top = bulletPos + "px";

if (bulletPos <= enemyPos + 50 && bulletPos >= enemyPos) {


score++;
document.getElementById("score").innerHTML = "Score:
" + score;
clearInterval(bulletInterval);
bullet.style.visibility = "hidden";
}
}, 50);
}
</script>
</body>
</html>

You might also like