Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
function resizeCanvas() {
canvas.width = windowWidth;
canvas.height = windowHeight;
}
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
const speed = 3.5;
let keyWPressed = false;
let keyAPressed = false;
let keySPressed = false;
let keyDPressed = false;
const backGround = new Image();
backGround.src = 'images/starBG.jpg';
const asteroidImage = new Image();
asteroidImage.src = 'images/asteroid.png';
const rocketWidth = 35;
const rocketHeight = 250;
let xCor = canvas.width / 2 - rocketWidth / 2;
let yCor = canvas.height - rocketHeight;
const rocket = new Image();
rocket.src = 'images/raket-met-vlam.png';
let fps = 165;
start = Date.now(),
frameDuration = 1000 / fps,
lag = 0;
//Score script
let score = 0;
let tempHighscore = getHighscoreFromCookies();
let highscore = 0;
if (tempHighscore == "") {
highscore = 0;
} else {
highscore = Number(tempHighscore);
}
function getHighscoreFromCookies(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 "";
}
function setHighscoreInCookies() {
document.cookie = `highscore=${highscore}`;
}
// Reset highscore button
const resetHighscoreButton = document.getElementById('resetHighscoreButton');
resetHighscoreButton.addEventListener('click', resetHighscore);
document.addEventListener('keypress', function (event) {
if (event.key === ' ' || event.code === 'Space') {
if (isGameStarted == false) {
startGame();
}
}
});
function resetHighscore() {
// Reset the highscore in memory
highscore = 0;
// Update the highscore in cookies
setHighscoreInCookies();
// Display the updated highscore
displayHighscore();
}
document.addEventListener('keypress', function (event) {
if (isGameOver == true) {
if (event.key === ' ' || event.code === 'Space') {
location.reload();
}
}
});
backGround.onload = function () {
ctx.drawImage(backGround, 0, 0, canvas.width, canvas.height);
}
rocket.onload = function () {
gameLoop();
};
function startGame() {
isGameStarted = true;
score = 0;
}
function stopGame() {
isGameOver = true;
}
function gameLoop() {
window.requestAnimationFrame(gameLoop, canvas);
let current = Date.now(),
elapsed = current - start;
if (elapsed >= frameDuration) {
updateLoop();
start = current;
actualFps = Math.floor(1000 / elapsed);
}
}
let isGameStarted = false;
let isGameOver = false;
function updateLoop() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (isGameStarted == false) {
// Set the text properties
ctx.font = "3rem subhead"; // Set font size and type for the main text
ctx.fillStyle = "#dc133b";
let text = "Druk op de SPATIEBALK om te Starten";
let textWidth = ctx.measureText(text).width;
let x = (canvas.width - textWidth) / 2;
let y = canvas.height / 2;
// Draw background
ctx.drawImage(backGround, 0, 0, canvas.width, canvas.height);
// Draw main text
ctx.fillText(text, x, y);
// Set the text properties for the controls text
ctx.font = "1.5rem main";
let textControls = "Gebruik W, A, S, D om de Raket te Besturen!";
let textControlsWidth = ctx.measureText(textControls).width;
let xTextControls = (canvas.width - textControlsWidth) / 2;
// Draw controls text
ctx.fillStyle = "white";
ctx.fillText(textControls, xTextControls, y + 100); // Adjusting the vertical position
return;
//niet
}
if (isGameOver == true) {
// Set the text properties
ctx.font = "3rem subhead"; // Set font size and type for the main text
ctx.fillStyle = "#dc133b";
let text = "Je bent Gecrasht!";
let textWidth = ctx.measureText(text).width;
let x = (canvas.width - textWidth) / 2;
let y = canvas.height / 2;
// Draw background
ctx.drawImage(backGround, 0, 0, canvas.width, canvas.height);
// Draw main text
ctx.fillText(text, x, y);
// Set the text properties for the controls text
ctx.font = "1.5rem main";
// let textControls = "Press SPACEBAR to RESTART";
let textControlsWidth = ctx.measureText("Druk op de SPATIEBALK om Terug te gaan naar het Startscherm").width;
let xTextControls = (canvas.width - textControlsWidth) / 2;
// Set the text properties for the score text
ctx.font = "1.5rem main";
let textScore = "Je Score is: " + score + " Meter!";
let textScoreWidth = ctx.measureText(textScore).width;
let xTextScore = (canvas.width - textScoreWidth) / 2;
//draw score text
ctx.fillStyle = "white";
ctx.fillText(textScore, xTextScore, y + 200); // Adjusting the vertical position
// Draw controls text
ctx.fillStyle = "white";
ctx.fillText("Druk op de SPATIEBALK om Terug te gaan naar het Startscherm", xTextControls, y + 100); // Adjusting the vertical position
if (score > highscore) {
highscore = score;
setHighscoreInCookies();
}
return;
}
// Draw the rocket with boundary checks
if (xCor < 0) {
xCor = 0;
}
if (xCor > canvas.width - rocketWidth) {
xCor = canvas.width - rocketWidth;
}
if (yCor < 0) {
yCor = 0;
}
if (yCor > canvas.height - rocketHeight) {
yCor = canvas.height - rocketHeight;
}
// Move the rocket
if (keyAPressed) {
xCor -= speed;
}
if (keyWPressed) {
yCor -= speed;
}
if (keyDPressed) {
xCor += speed;
}
if (keySPressed) {
yCor += speed;
}
ctx.drawImage(rocket, xCor, yCor, rocketWidth, rocketHeight);
// Update and draw asteroids
for (const asteroid of asteroids) {
if (asteroid.active) {
asteroid.update();
asteroid.draw();
}
}
// Generate new asteroids at random intervals
if (Math.random() < 0.03) {
const asteroidSize = Math.random() * 30 + 20; // Random size between 20 and 50
const asteroidSpeed = Math.random() * 2 + 1; // Random speed between 1 and 3
const asteroidX = Math.random() * (canvas.width - asteroidSize);
const asteroid = new Asteroid(asteroidX, 0, asteroidSize, asteroidSpeed);
asteroids.push(asteroid);
}
score += 3;
// Increment score every second
console.log(score);
displayScore();
displayHighscore();
// Reset the rocket and asteroids if collision occurs
for (const asteroid of asteroids) {
if (
xCor < asteroid.x + asteroid.size &&
xCor + rocketWidth > asteroid.x &&
yCor < asteroid.y + asteroid.size &&
yCor + rocketHeight > asteroid.y
) {
xCor = canvas.width / 2 - rocketWidth / 2;
yCor = canvas.height - rocketHeight;
for (const asteroid of asteroids) {
asteroid.active = false;
}
}
}
}
document.addEventListener("keydown", function (event) {
switch (event.key) {
case "a":
keyAPressed = true;
break;
case "d":
keyDPressed = true;
break;
case "w":
keyWPressed = true;
break;
case "s":
keySPressed = true;
break;
}
});
document.addEventListener("keyup", function (event) {
switch (event.key) {
case "a":
keyAPressed = false;
break;
case "d":
keyDPressed = false;
break;
case "w":
keyWPressed = false;
break;
case "s":
keySPressed = false;
break;
}
});
class Asteroid {
constructor(x, y, size, speed) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
this.active = true;
}
update() {
this.y += this.speed;
if (xCor < this.x + this.size &&
xCor + rocketWidth > this.x &&
yCor < this.y + this.size &&
yCor + rocketHeight > this.y) {
console.log("Rocket collided with an asteroid!");
isGameOver = true;
// alert("GAME OVER! your score is " + score + " meters");
}
if (this.y > canvas.height) {
this.active = false;
}
}
draw() {
ctx.drawImage(asteroidImage, this.x, this.y, this.size, this.size);
}
}
const asteroids = [];
function displayScore() {
ctx.fillStyle = "white";
ctx.font = "24px subhead";
ctx.fillText("Score: " + score + ' Meters', 10, 30);
}
function displayHighscore() {
ctx.fillStyle = "white";
ctx.font = "24px subhead";
ctx.fillText("Highscore: " + highscore + ' Meters', 10, 80);
}
// hitbox verticaal verkleinen, startscherm maken VRAGEN VOOR PETER