xxxxxxxxxx
227
let offset=0;
let x,y;
let value1 = 0;
let value2 = 0;
let value3 = 0;
let value4 = 0;
let value= 100;
// 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(0);
strokeWeight(2);
stroke(51);
//Starting locations and directions of the rectangulars behind the subject,verb,adjective and noun.
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)) {
if(mouseIsPressed){
fill(0);
rect(0,0,width/4,height);
}
fill(0);
}
fill("yellow")
rect(leftX,buttonTopY,quarterW,quarterH);
// mid Left button
if ((mouseX >width/3 && mouseX<width/2)&&
(mouseY>3*height/4)) {
if(mouseIsPressed){
fill(0)
rect(width/4,0,width/4,height);
}
fill (0);
}
fill("red")
rect(leftMidX,buttonTopY,quarterW,quarterH);
// mid Right button
if ((mouseX >width/2 && mouseX<3*width/4)&&
(mouseY>3*height/4)) {
if(mouseIsPressed){
fill(0)
rect(width/2,0,width/4,height);
}
fill(0);
}
fill("blue")
rect(midX,buttonTopY,quarterW,quarterH)
// Right button
if ((mouseX >3*width/4)&&
(mouseY>3*height/4)) {
if(mouseIsPressed){
fill(0)
rect(3*width/4,0,width/4,height);
}
fill(0);
}
fill("green")
rect(rightMidX,buttonTopY,quarterW,quarterH);
//mouse circle
fill(0);
circle(mouseX,mouseY,30,30)
//text styling
fill(255);
textSize(30);
textAlign(CENTER);
//Stable colorful circles at each corner of the window
//random color function for x,y
stroke(0);
strokeWeight(3);
for (var x=0;x<width;x+=30){
fill(random(255),random(255),random(255));
rect(x,0,30,30);
rect(x,height-30,30,30)
}
for (var y=0;y<height;y+=30){
fill(random(255),random(255),random(255));
rect(0,y,30,30);
rect(width-30,y,30,30);
}
// calling "subject", "verb", etc, gets the word from the "mousePressed" function at the bottom of this code (which also depends on the selectWord function
fill(255);
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(15);
text("Press to change sentence",0,height-quarterH-50,width,height)
textAlign(LEFT);
//Colorful moving circles in the begining of the game
for (var x=0;x<width;x+=30){
for (var y=0;y<height;y+=30){
fill(random(255),random(255),random(255));
rect(x+offset,y,30,30);
}
}
offset=offset+10;
}
// 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");
}
}
//Turn backgrond grey if mouse is pressed to buttons, if not say grey
function mousePressed() {
if (value1 === 255) {
value1 = 0;
} else {
value1 =100;
}
if (value2===255){
value2 = 0;
} else{
value2 = 100;
}
if (value3===255){
value3 = 0;
} else{
value3 = 100;
}
if (value4===100){
value4 =100;
} else{
value4 =100;
}}