0% found this document useful (0 votes)
7 views

Code

weqweqw
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Code

weqweqw
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

let currentQuestion = 0;

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
}
];

let answerXpos = 25;


let answerYpos = 375;
let startButton;
let isInQuestion = false;
let answerCorrect = false;

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);

for (let i = 0; i < 4; i++) {


const optionX = answerXpos + (i % 2) * 448;
const optionY = answerYpos + Math.floor(i / 2) * 125;

fill("white");
rect(optionX, optionY, 300, 75);
fill("black");
text(questions[questionIndex].options[i], optionX + 130, optionY + 75);
}
}

function checkAnswer(mouseX, mouseY) {


const optionWidth = 300;
const optionHeight = 75;

for (let i = 0; i < 4; i++) {


const optionX = answerXpos + (i % 2) * 448;
const optionY = answerYpos + Math.floor(i / 2) * 125;

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);
}

// Move to the next question after a delay


currentQuestion++;
if (currentQuestion < questions.length) {
setTimeout(() => {
clear();
answerCorrect = false;
displayQuestion(currentQuestion);
}, 2000); // Display the next question after 2 seconds
} else {
// Quiz is over
background(157, 0, 255);
textSize(24);
text("Quiz is over!", 310, 300);
}

break; // Exit the loop


}
}
}

function mousePressed() {
if (isInQuestion) {
checkAnswer(mouseX, mouseY);
}
}

You might also like