xxxxxxxxxx
177
var rollover =false;
let value1 = 255;
let value2 = 255;
let value3 = 255;
let value4 = 255;
// The goal of the sketch as it is right now is to generate a random sentence, composed of subject, verb, adjective and noun
let quarterW; let quarterH;
let leftButton; let leftMidButton; let rightMidButton;
let rightButton;
// create a "sentence" object with a subject, verb, adjective and noun. Each part of the sentence has a list of possible choices that the buttons will select from. Add more words by adding to the list in "choices"
let subject; let verb; let adjective; let noun;
const sentence = {
subject : {
choices: ["I","You","We"],
},
verb : {
choices: ["love", "respect","dislike", "consume","sell"],
},
adjective : {
choices: ["fluffy","spiky","stinky","rectangular","flaccid","dry","pale","bright"]
},
noun : {
choices: ["monkeys","spiders","cashews","raisins","basketballs"],
},
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Variables to use for buttons and layout
quarterW = width/4
quarterH = height/4
leftX=0;
leftMidX = quarterW;
midX=2*quarterW;
rightMidX = 3*quarterW;
rightX= width;
buttonTopY = height-quarterH;
// initialize the state of each button to true, and set the initial value of each part of the sentence, which will start the program with a sentence instead of a blank screen
leftButton = true;
leftMidButton=true;
rightMidButton=true;
rightButton=true;
subject = selectWord("subject");
verb = selectWord("verb");
adjective = selectWord("adjective");
noun = selectWord("noun");
}
function draw() {
background(177, 156, 217);
strokeWeight(2);
stroke(51);
fill(value1);
rect(0,0,width/4,height);
fill(value2);
rect(width/4,0,width/4,height);
fill(value3);
rect(width/2,0,width/4,height);
fill(value4);
rect(3*width/4,0,width/4,height)
//Hovering buttons- Be black when hovering buttons.
// left button
if ((mouseX >0 && mouseX<width/4)&&
(mouseY>3*height/4)) {
fill(0)
}else{
fill("yellow");
}
rect(leftX,buttonTopY,quarterW,quarterH);
// mid Left button
if ((mouseX >width/3 && mouseX<width/2)&&
(mouseY>3*height/4)) {
fill(0)
}else{
fill ("red");
}
rect(leftMidX,buttonTopY,quarterW,quarterH);
// mid Right button
if ((mouseX >width/2 && mouseX<3*width/4)&&
(mouseY>3*height/4)) {
fill(0)
}else{
fill("blue");
}
rect(midX,buttonTopY,quarterW,quarterH)
// Right button
if ((mouseX >3*width/4)&&
(mouseY>3*height/4)) {
fill(0)
}else{
fill("green");
}
rect(rightMidX,buttonTopY,quarterW,quarterH);
//text styling
fill(255);
textSize(18);
textAlign(CENTER);
// calling "subject", "verb", etc, gets the word from the "mousePressed" function at the bottom of this code (which also depends on the selectWord function
text(subject,0,quarterH,quarterW,quarterH);
text(verb,quarterW,quarterH,quarterW,quarterH);
text(adjective,
quarterW*2,quarterH,quarterW,quarterH);
text(noun,quarterW*3,quarterH,quarterW,quarterH);
// styles the "press to change sentence" text
fill(255,0,0);
textSize(20);
text("Press to change sentence",0,height-quarterH-50,width,height)
textAlign(LEFT);
}
// this function randomly chooses a word from the list of choices defined in the word object
function selectWord (type){
randWord = round(random(sentence[type].choices.length-1));
getWord = sentence[type].choices[randWord];
return getWord;
}
// when the mouse is clicked,
function mouseClicked() {
if (mouseX < leftMidX && mouseY > buttonTopY) {
//leftButton = !leftButton; <- not necessary
subject = selectWord("subject");
}
else if (mouseX < midX && mouseY > buttonTopY) {
//leftMidButton = !leftMidButton;
verb = selectWord("verb");
}
else if (mouseX < rightMidX && mouseY > buttonTopY) {
//rightMidButton = !rightMidButton ;
adjective = selectWord("adjective");
}
else if (mouseX < rightX && mouseY >buttonTopY) {
//rightButton = !rightButton;
noun = selectWord("noun");
}
}
function mousePressed() {
if (value1 === 255) {
value1 = 0;
} else if (value2 === 255) {
value2 = 0;
} else if (value3 === 255) {
value3 = 0;
} else if (value4 === 255 ){
value4 = 0;
}
}