Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
//block is player ship
const blockWidth = 50;
const blockHeight = 50;
let blockX = canvas.width / 2 - blockWidth / 2;
const blockY = canvas.height - blockHeight;
//circle is enemy ship
const circleRadius = 20;
let circles = [];
const circleSpeed = 0.9;
const bulletWidth = 5;
const bulletHeight = 10;
const bulletSpeed = 5;
let bullets = [];
let score = 0;
let highScore = 0
let tempHighScore = getHighScoreFromCookie ();
highScore = Number(tempHighScore);
const keys = {};
document.addEventListener("keydown", (event) => {
keys[event.key] = true;
});
document.addEventListener("keyup", (event) => {
keys[event.key] = false;
});
let gameRunning = false;
function gameLoop() {
if (!gameRunning) {
return;
}
requestAnimationFrame(gameLoop);
if (keys["ArrowLeft"] && blockX > 0) {
blockX -= 2;
}
if (keys["ArrowRight"] && blockX < canvas.width - blockWidth) {
blockX += 2;
}
circles.forEach((circle) => {
if (circle.y + circleRadius > blockY && circle.y < blockY + blockHeight &&
circle.x + circleRadius > blockX && circle.x < blockX + blockWidth) {
gameOver();
}
});
circles.forEach((circle) => {
circle.y += circleSpeed;
if (circle.y > canvas.height + circleRadius) {
circle.x = Math.random() * (canvas.width - circleRadius * 2) + circleRadius;
circle.y = -circleRadius;
}
});
bullets.forEach((bullet) => {
bullet.y -= bulletSpeed;
if (bullet.y < 0) {
bullets.splice(bullets.indexOf(bullet), 1);
}
circles.forEach((circle) => {
if (bullet.x + bulletWidth > circle.x - circleRadius &&
bullet.x < circle.x + circleRadius &&
bullet.y + bulletHeight > circle.y - circleRadius &&
bullet.y < circle.y + circleRadius) {
bullets.splice(bullets.indexOf(bullet), 1);
circles.splice(circles.indexOf(circle), 1);
score += 1;
}
});
});
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "white";
const blockImage = new Image();
blockImage.src = "image/mainship.png";
ctx.drawImage(blockImage, blockX, blockY, blockWidth, blockHeight);
circles.forEach((circle) => {
const circleImage = new Image();
circleImage.src = "image/scout.png";
ctx.drawImage(circleImage, circle.x - circleRadius, circle.y - circleRadius, circleRadius * 2, circleRadius * 2);
});
ctx.fillStyle = "orange";
bullets.forEach((bullet) => {
ctx.fillRect(bullet.x, bullet.y, bulletWidth, bulletHeight);
});
ctx.fillStyle = "white";
ctx.font = "20px Arial";
ctx.fillText("Score: " + score, 10, 30);
ctx.fillText("High Score: " + highScore, 10, 60);
}
function gameOver() {
gameRunning = false;
document.getElementById("gameOverScreen").style.display = "flex";
if (score > highScore) {
highScore = score;
saveHighScoreToCookie(highScore);
}
}
function restartGame() {
blockX = canvas.width / 2 - blockWidth / 2;
circles = [];
bullets = [];
score = 0;
gameRunning = true;
document.getElementById("gameOverScreen").style.display = "none";
gameLoop();
}
function shoot() {
const bulletX = blockX + blockWidth / 2 - bulletWidth / 2;
const bulletY = blockY;
bullets.push({ x: bulletX, y: bulletY });
}
document.addEventListener("keydown", (event) => {
if (event.key === " ") {
shoot();
}
});
const startButton = document.getElementById("startButton");
startButton.addEventListener("click", () => {
gameRunning = true;
document.getElementById("startScreen").style.display = "none";
circles = [];
bullets = [];
gameLoop();
});
const restartButton = document.getElementById("restartButton");
restartButton.addEventListener("click", restartGame);
function SpawnEnemy() {
const circleX = Math.random() * (canvas.width - circleRadius * 2) + circleRadius;
const circleY = -circleRadius;
circles.push({ x: circleX, y: circleY });
}
const randomInterval = Math.random() * 1000 + 500;
setInterval(SpawnEnemy, randomInterval);
function saveHighScoreToCookie(highScore) {
document.cookie = `highScore=${highScore}`;
}
function getHighScoreFromCookie(cname = "highScore") {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}