Multiple Image Buttons, Images within variables?
in
Programming Questions
•
1 years ago
I'm very new to Processing and programming in general, so bear with me.
For a school project, I'm creating a fortune cookie game, where there are six different cookie images on the screen. When each cookie is clicked, a random fortune message will be taken from an array and be displayed in an area on the screen. Basically, I'm trying to create a class for all of the cookies, since the same function would be applied to all of them. Here is what the final product would look like:
How could I go about having a variable that would allow for varying button images?
Any help would be greatly appreciated!
This is the code that I have thus far:
Note** There are probably other mistakes in the code, given that this is my first time working in Processing.
PFont f;
boolean button = false;
Cookie myCookie1; //cookie objects initialized
Cookie myCookie2;
Cookie myCookie3;
Cookie myCookie4;
Cookie myCookie5;
Cookie myCookie6;
String[] fortunes = {
"Fortune One" ,
"Fortune Two" ,
"Fortune Three",
"Fortune Four",
"Fortune Five",
"Fortune Six",
"Fortune Seven",
"Forutne Eight",
"Fortune Nine",
"Fortune Ten",
};
int index = int(random(fortunes.length));
void setup() {
size(800,600);
f= createFont("Helvetica", 12, true);
fill(255); //font color
PImage cookie1 = loadImage("cookie1.png");
PImage cookie2 = loadImage("cookie2.png");
PImage cookie3 = loadImage("cookie3.png");
PImage cookie4 = loadImage("cookie4.png");
PImage cookie5 = loadImage("cookie5.png");
PImage cookie6 = loadImage("cookie6.png");
myCookie1 = new Cookie(60, 130, 153, 162, PImage cookie1 );
myCookie2 = new Cookie(300, 130, 168, 160, PImage cookie2);
myCookie3 = new Cookie(550, 130, 187, 164, PImage cookie3);
myCookie4 = new Cookie(60, 360, 164,163, PImage cookie4);
myCookie5 = new Cookie(300, 360, 185, 160, PImage cookie5);
myCookie6 = new Cookie(550, 360, 175, 163, PImage cookie6);
}
void draw() {
background(0);
myCookie1.display();
myCookie2.display();
myCookie3.display();
myCookie4.display();
myCookie5.display();
myCookie6.display();
}
class Cookie {//class defined
int x;
int y;
int w;
int h;
PImage i;
Cookie(int xcookie, int ycookie, int cookiewidth, int cookieheight, PImage cookiepic){ //cookie constructor
x = xcookie;
y = ycookie;
w = cookiewidth;
h = cookieheight;
i = cookiepic;
}
void display(){ //Random fortune is taken from "fortunes" array and displayed on screen.
if (button) {
textFont(f, 12);
fill(0);
text(fortunes[index],320, 62);
} else {
background(0);
}
void mousePressed() {
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
button = !button;
index = int(random(fortunes.length));
}
}
}
1