JS VS
JS VS
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Shooter Game</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/howler.min.js"></script>
<style>
canvas {
border: 1px solid #b55a5a;
background-image: url("file:///D:/kuliah%20ajg/download%20(1).jpeg");
background-size: cover;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="1024" height="768"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
function playShootSound() {
shootSound.play();
}
function playGameOverSound() {
gameOverSound.play();
}
const player = {
x: 50,
y: 768 / 2,
width: 30,
height: 30,
color: 'blue',
speed: 8,
};
function createEnemies() {
for (let i = 0; i < 3; i++) {
enemies.push({
x: canvas.width + Math.random() * 200,
y: Math.random() * (canvas.height - 30),
size: 30,
color: 'purple',
speed: Math.random() * 3 + 1,
});
}
}
let score = 0;
let isGameOver = false;
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.beginPath();
ctx.moveTo(player.x, player.y);
ctx.lineTo(player.x, player.y + player.height);
ctx.lineTo(player.x + player.width, player.y + player.height / 2);
ctx.fill();
}
function drawEnemies() {
ctx.fillStyle = 'purple';
for (const enemy of enemies) {
const angleStep = (Math.PI * 2) / 5;
ctx.beginPath();
for (let j = 0; j < 5; j++) {
const angle = j * angleStep;
const enemyX = enemy.x + Math.cos(angle) * enemy.size;
const enemyY = enemy.y + Math.sin(angle) * enemy.size;
if (j === 0) {
ctx.moveTo(enemyX, enemyY);
} else {
ctx.lineTo(enemyX, enemyY);
}
}
ctx.closePath();
ctx.fill();
}
}
function drawBullets() {
ctx.fillStyle = 'red';
for (const bullet of bullets) {
if (bullet.state === 'fire') {
ctx.beginPath();
ctx.arc(bullet.x, bullet.y, bullet.radius, 0, Math.PI * 2);
ctx.fill();
}
}
}
function drawScore() {
ctx.fillStyle = 'red';
ctx.font = '24px Arial';
ctx.fillText('Score: ' + score, 10, 30);
}
function drawGameOver() {
ctx.fillStyle = 'red';
ctx.font = '48px Arial';
ctx.fillText('Game Over! Press R to Restart', canvas.width / 2 - 300, canvas.height / 2);
}
function updatePositions() {
if (!isGameOver) {
for (const enemy of enemies) {
enemy.x -= enemy.speed;
if (enemy.x + enemy.size < 0) {
enemy.x = canvas.width + Math.random() * 200;
enemy.y = Math.random() * (canvas.height - 30);
}
}
checkCollision();
}
}
function checkCollision() {
for (let i = bullets.length - 1; i >= 0; i--) {
for (let j = enemies.length - 1; j >= 0; j--) {
const bullet = bullets[i];
const enemy = enemies[j];
if (
bullet.x + bullet.radius > enemy.x - enemy.size / 2 &&
bullet.x - bullet.radius < enemy.x + enemy.size / 2 &&
bullet.y + bullet.radius > enemy.y - enemy.size / 2 &&
bullet.y - bullet.radius < enemy.y + enemy.size / 2
) {
score++;
bullet.state = 'ready';
bullets.splice(i, 1);
enemies.splice(j, 1);
enemies.push({
x: canvas.width + Math.random() * 200,
y: Math.random() * (canvas.height - 30),
size: 30,
color: 'purple',
speed: Math.random() * 3 + 1,
});
break;
}
}
}
function gameOver() {
isGameOver = true;
playGameOverSound();
drawGameOver();
}
function restartGame() {
if (isGameOver) {
isGameOver = false;
score = 0;
player.x = 50;
player.y = 768 / 2;
enemies.length = 0;
createEnemies();
draw();
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawEnemies();
drawBullets();
drawScore();
updatePositions();
if (!isGameOver) {
requestAnimationFrame(draw);
}
}
createEnemies();
draw();
</script>
</body>
</html>