Code
Code
let questions = [
{
text: "Vraag 1: Wat is de waarde van π (pi) tot op twee decimalen nauwkeurig?",
options: ["3,11", "3,12", "3,13", "3,14"],
correctAnswer: 3
},
{
text: "Vraag 2: Wat is de hoofdstad van Frankrijk?",
options: ["Londen", "Parijs", "Madrid", "Berlijn"],
correctAnswer: 1
}
];
function setup() {
createCanvas(800, 600);
background(157, 0, 255);
startButton = createButton("Start Quiz");
startButton.position(280, 220);
startButton.size(220, 140);
startButton.style("border-radius", "10px");
startButton.style("font-size", "35px");
startButton.style("font-family", "Impact");
startButton.style("color", "black");
startButton.mousePressed(startQuiz);
}
function draw() {}
function startQuiz() {
startButton.remove();
isInQuestion = true;
displayQuestion(currentQuestion);
}
function displayQuestion(questionIndex) {
background(157, 0, 255);
strokeWeight(2);
fill("white");
rect(0, 0, width, 75);
fill("black");
textSize(24);
text(questions[questionIndex].text, 10, 43);
fill("white");
rect(optionX, optionY, 300, 75);
fill("black");
text(questions[questionIndex].options[i], optionX + 130, optionY + 75);
}
}
if (
mouseX > optionX &&
mouseX < optionX + optionWidth &&
mouseY > optionY &&
mouseY < optionY + optionHeight
) {
if (questions[currentQuestion].correctAnswer === i) {
// Correct answer
background(0, 255, 68);
fill("black");
textSize(24);
textFont("Impact");
text("CORRECT ANSWER!!", 310, 300);
answerCorrect = true;
} else {
// Wrong answer
background("red");
fill("black");
textSize(24);
textFont("Impact");
text("WRONG ANSWER!!!", 310, 300);
}
function mousePressed() {
if (isInQuestion) {
checkAnswer(mouseX, mouseY);
}
}