xxxxxxxxxx
83
let strings = []; // An array to hold all quotes
let emotion;
let quotes = [];
let buttons = [];
let reset = false;
function preload() {
// Load the CSV file containing quotes
strings = loadStrings("quotes.csv");
}
function setup() {
createCanvas(400, 400);
// Display the prompt and create buttons for different emotions
background(220);
text("How are you feeling today?", width / 4, height / 4);
createEmotionButton("Happy", 0, width / 4, height / 2);
createEmotionButton("Excited", 1, width / 4, height / 2 + 30);
createEmotionButton("Anxious", 2, width / 4, height / 2 + 60);
createEmotionButton("Angry", 3, width / 4, height / 2 + 90);
createEmotionButton("Tired", 4, width / 4, height / 2 + 120);
createEmotionButton("Upset", 5, width / 4, height / 2 + 150);
createEmotionButton("Motivated", 6, width / 4, height / 2 + 180);
createEmotionButton("Demotivated", 7, width / 4, height / 2 + 210);
}
function draw() {
// Check if the reset flag is true
if (reset) {
// Clear the screen
clear();
// Reset the background
background(220);
// Reset the flag
reset = false;
// Display the prompt and recreate buttons
text("How are you feeling today?", width / 4, height / 4);
for (let btn of buttons) {
btn.show(); // Show the buttons
}
}
}
function createEmotionButton(label, index, x, y) {
// Create a button for the given emotion
let button = createButton(label);
button.position(x, y);
button.mousePressed(() => {
// Set the selected emotion
emotion = index;
// Load quotes for the selected emotion
quotes = split(strings[emotion], ",");
// Hide the buttons
for (let btn of buttons) {
btn.hide();
}
// Clear the screen
clear();
// Display the quote
let randomIndex = int(random(0, quotes.length));
text(quotes[randomIndex], 20, height / 2);
text("click anywhere to restart", width / 3, height / 2);
// Set the reset flag to true
reset = true;
});
buttons.push(button); // Store the button in the array
}
function mouseClicked() {
// Check if a quote has been displayed, and if so, restart the program
if (reset) {
redraw(); // Redraw the canvas (equivalent to calling draw())
}
}