Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
// Variabelen voor het canvas en de context
var canvas, canvasContext;
// Const voor de afmetingen en lay-out van de bakstenen
const BRICK_W = 80;
const BRICK_H = 20;
const BRICK_GAP = 2;
const BRICK_COLS = 10;
const BRICK_ROWS = 14;
// Array om de status van elke baksteen op te slaan en teller voor het aantal bakstenen
var brickGrid = new Array(BRICK_COLS * BRICK_ROWS);
var brickCount = 0;
// Variabelen voor de bal en zijn snelheid
var ballX = 75;
var ballSpeedX = 8;
var ballY = 75;
var ballSpeedY = 8;
// Variabelen voor de paddle
var paddleX = 400;
const PADDLE_THICKNESS = 15;
const PADDLE_WIDTH = 100;
const PADDLE_DIST_FROM_EDGE = 60;
// Variabelen voor de muispositie
var mouseX = 0;
var mouseY = 0;
// Variabelen voor het bijhouden van score en levens, en voor het hoogste scoren
var score = 0;
var lives = 3;
var highScore = 0;
// Functie die wordt uitgevoerd wanneer het venster wordt geladen
window.onload = function () {
// Initialisatie van het canvas en het opzetten van de animatieloop
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
var framesPerSecond = 30;
setInterval(updateAll, 1000 / framesPerSecond);
// Luisteren naar muisbewegingen voor de paddle
canvas.addEventListener('mousemove', updateMousePos);
// Resetten van het spel
brickReset();
ballRest();
// Het hoogste scoren ophalen en initialiseren
highScore = getHighScore();
}
// Functie om het hoogste scoren op te halen uit cookies
function getHighScore() {
return parseInt(getCookie("highscore")) || 0;
}
// Functie om het hoogste scoren in cookies op te slaan
function setHighScore(score) {
setCookie("highscore", score, 365);
}
// Functie om het hoogste scoren bij te werken en op te slaan als nodig
function updateHighScore() {
if (score > highScore) {
highScore = score;
setHighScore(score);
}
}
// Functie om het spel te resetten
function resetGame() {
resetScore();
resetLives();
ballRest();
brickReset();
}
// Functie die wordt opgeroepen in de animatieloop om alle updates uit te voeren
function updateAll() {
movement();
playArea();
}
// Functie om de positie van de bal te resetten
function ballRest() {
ballX = canvas.width / 6;
ballY = canvas.height / 2;
}
// Functie om het raster van bakstenen te resetten
function brickReset() {
brickCount = 0;
var i;
for (var i = 0; i < 3 * BRICK_COLS; i++) {
brickGrid[i] = false;
}
for (; i < BRICK_COLS * BRICK_ROWS; i++) {
if (Math.random() < 0.5) {
brickGrid[i] = true;
} else {
brickGrid[i] = false;
}
brickGrid[i] = true;
brickCount++;
}
}
// Functie om de muispositie bij te werken
function updateMousePos(evt) {
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
mouseX = evt.clientX - rect.left - root.scrollLeft;
mouseY = evt.clientY - rect.top - root.scrollTop;
paddleX = mouseX - PADDLE_WIDTH / 2;
}
// Functie voor de beweging van de bal
function ballMove() {
ballX += ballSpeedX;
ballY += ballSpeedY;
// Controleer of de bal buiten het canvas is
if (ballY > canvas.height) {
loseLife(); // Verminder het aantal levens
// Controleer of het spel voorbij is
if (lives <= 0) {
updateHighScore(); // Werk het hoogste scoren bij wanneer het spel eindigt
resetGame(); // Reset het spel
} else {
ballRest(); // Reset de positie van de bal
}
} else if (ballY < 0 && ballSpeedY < 0.0) {
ballSpeedY = -ballSpeedY; // Omkeren van de verticale snelheid bij botsing met de bovenrand van het canvas
}
if (ballX > canvas.width && ballSpeedX > 0.0) {
ballSpeedX = -ballSpeedX; // Omkeren van de horizontale snelheid bij botsing met de rechterrand van het canvas
} else if (ballX < 0 && ballSpeedX < 0.0) {
ballSpeedX = -ballSpeedX; // Omkeren van de horizontale snelheid bij botsing met de linkerrand van het canvas
}
}
// Functie om te controleren of er een baksteen is op de opgegeven rij en kolom
function isBrickAtColRow(col, row) {
if (col >= 0 && col < BRICK_COLS && row >= 0 && row < BRICK_ROWS) {
var brickIndexUnderCoord = rowColToArrayIndex(col, row);
return brickGrid[brickIndexUnderCoord];
} else {
return false;
}
}
// Functie voor detectie van botsingen tussen bal en bakstenen
function ballBrickColl() {
var ballBrickCol = Math.floor(ballX / BRICK_W);
var ballBrickRow = Math.floor(ballY / BRICK_H);
var brickIndexUnderBall = rowColToArrayIndex(ballBrickCol, ballBrickRow);
if (ballBrickCol >= 0 && ballBrickCol < BRICK_COLS && ballBrickRow >= 0 && ballBrickRow < BRICK_ROWS) {
if (isBrickAtColRow(ballBrickCol, ballBrickRow)) {
brickGrid[brickIndexUnderBall] = false; // Verwijder de baksteen
brickCount--; // Verminder het aantal bakstenen
score += 10; // Verhoog de score
ballSpeedY = -ballSpeedY; // Omkeren van de verticale snelheid van de bal
}
}
}
// Functie voor de beweging van de paddle en controle op botsingen met de bal
function paddleMove() {
var paddleTopEdgeY = canvas.height - PADDLE_DIST_FROM_EDGE;
var paddleBottomEdgeY = paddleTopEdgeY + PADDLE_THICKNESS;
var paddleLeftEdgeX = paddleX;
var paddleRightEdgeX = paddleX + PADDLE_WIDTH;
if (ballY > paddleTopEdgeY &&
ballY < paddleBottomEdgeY &&
ballX > paddleLeftEdgeX &&
ballX < paddleRightEdgeX) {
ballSpeedY = -ballSpeedY; // Omkeren van de verticale snelheid van de bal
var paddleCenterX = paddleX + PADDLE_WIDTH / 2;
var ballDistFromCenterX = ballX - paddleCenterX;
ballSpeedX = ballDistFromCenterX * 0.35; // Aanpassen van de horizontale snelheid van de bal op basis van de positie op de paddle
}
// Controleer of de paddle binnen de grenzen van het canvas blijft
if (paddleX < 0) {
paddleX = 0;
} else if (paddleX + PADDLE_WIDTH > canvas.width) {
paddleX = canvas.width - PADDLE_WIDTH;
}
}
// Functie die de beweging van de bal, detectie van botsingen en beweging van de paddle omvat
function movement() {
ballMove();
ballBrickColl();
paddleMove();
}
// Functie om het speelveld te tekenen
function playArea() {
// Teken het speelveld, bakstenen, paddle, bal en score
colorRect(0, 0, canvas.width, canvas.height, 'white');
colorCircle();
colorRect(paddleX, canvas.height - PADDLE_DIST_FROM_EDGE, PADDLE_WIDTH, PADDLE_THICKNESS, 'black');
drawbricks();
colorText("Score: " + score + " Lives: " + lives, 10, 35 , 'black', "bold 20px Arial");
colorText("High Score: " + highScore, canvas.width - 200, 37, 'black', "bold 20px Arial");
}
// Functie om een rechthoek te tekenen
function colorRect(leftX, topY, width, height, color) {
canvasContext.fillStyle = color;
canvasContext.fillRect(leftX, topY, width, height);
}
// Functie om tekst te tekenen
function colorText(showWords, textX, textY, fillColor, font) {
canvasContext.fillStyle = fillColor;
canvasContext.font = font;
canvasContext.fillText(showWords, textX, textY);
}
// Functie om de index in de array te berekenen op basis van de kolom- en rijwaarden
function rowColToArrayIndex(col, row) {
return col + BRICK_COLS * row;
}
// Functie om de bakstenen te tekenen
function drawbricks() {
for (var eachRow = 0; eachRow < BRICK_ROWS; eachRow++) {
for (var eachCol = 0; eachCol < BRICK_COLS; eachCol++) {
var arrayIndex = rowColToArrayIndex(eachCol, eachRow);
if (brickGrid[arrayIndex]) {
colorRect(BRICK_W * eachCol, BRICK_H * eachRow,
BRICK_W - BRICK_GAP, BRICK_H - BRICK_GAP, 'black');
}
}
}
}
// Functie om de bal te tekenen
function colorCircle() {
canvasContext.fillStyle = 'black';
canvasContext.beginPath();
canvasContext.arc(ballX, ballY, 10, 0, Math.PI * 2, true);
canvasContext.fill();
}
// Functie om de score te resetten
function resetScore() {
score = 0;
}
// Functie om het aantal levens te resetten
function resetLives() {
lives = 3;
}
// Functie om een leven te verliezen
function loseLife() {
lives--;
}
// Functies om cookies in te stellen en op te halen
function setCookie(cookieName, cookieValue, daysToExpire) {
var expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + daysToExpire);
var cookie = cookieName + "=" + encodeURIComponent(cookieValue) + "; expires=" + expirationDate.toUTCString() + "; path=/";
document.cookie = cookie;
}
function getCookie(cookieName) {
// dit is de string die de naam van de gezochte cookie bevat
var name = cookieName + "=";
// Decodeer de cookie-string om speciale tekens te verwerken
var decodedCookie = decodeURIComponent(document.cookie);
// Split de cookie-string in een array van individuele cookies
var cookieArray = decodedCookie.split(';');
// Loop door alle cookies in de cookie-array
for (var i = 0; i < cookieArray.length; i++) {
var cookie = cookieArray[i];
// Verwijder eventuele voorloopspaties uit de cookie-string
while (cookie.charAt(0) == ' ') {
cookie = cookie.substring(1);
}
// Controleer of de huidige cookie begint met de gezochte cookie-naam
if (cookie.indexOf(name) == 0) {
// Als dat het geval krijg je de waarde van de cookie
return cookie.substring(name.length, cookie.length);
}
}
// Als de gezochte cookie niet wordt gevonden krijg je een lege string
return "";
}