Friday, August 12, 2011
// make some square objects
Square square1, square2, square3, square4,square5,square6,square7,square8;
// maximum number of attempts to click on the secret square
int maxAttempts;
// current number of attempts
int attempts;
boolean done;
void setup()
{
size(400, 400);
background(255);
square1 = new Square();
square1.x = 100;
square1.y = 50;
square1.r = 0;
square1.g = 0;
square1.b = 0;
square2 = new Square();
square2.r = 0;
square2.g = 0;
square2.b = 0;
square2.x = 100;
square2.y = 20;
square3 = new Square();
square3.r = 0;
square3.g = 0;
square3.b = 0;
square3.x = 200;
square3.y = 200;
square4 = new Square();
square4.x = 80;
square4.y = 60;
square4.r = 0;
square4.g = 0;
square4.b = 0;
square5 = new Square();
square5.x = 80;
square5.y = 60;
square5.r = 0;
square5.g = 0;
square5.b = 0;
// not used now
maxAttempts = 5;
attempts = 0;
done = false;
}
void draw()
{
//background(100);
int dist1 = mouseX-square1.x;
if (abs(dist1) < 20) square1.x -= dist1;
int i;
for(i = 0; i<30;) {
fill(random(50,255), random(50,255), random(50,255));
rect(random(width),
random(height), 30, 30);
int i;
for(i = 0; i<4; i++) {
fill(255,0,0); random(100); random(100);}
square1(random(width),
random(height), 30, 30);
}
square1.draw();
square2.draw();
square3.draw();
square4.draw();
}
void mousePressed()
{
if ( square1.isOver() )
{
println("Square 1 pressed");
}
if ( square2.isOver() )
{
println("Square 2 pressed");
}
if ( square3.isOver() )
{
println("Square 3 pressed");
}
if ( square4.isOver() )
{
println("Square 4 pressed");
}
}
// this is our square class
class Square
{
// width in pixels
int w;
int x;
int y;
// colors
int r;
int g;
int b;
boolean over;
// this is the "constructor" for a new Square
Square()
{
x = 0;
y = 0;
w = 50;
r = 0;
g = 0;
b = 0;
over = false;
}
void draw()
{
// set the fill color for the 1st square
if (over)
{
fill(0);
}
else
{
fill( r, g, b );
}
// draw the rectangle
rect(x, y, w, w);
}
boolean isOver()
{
if ( (mouseX > x) && (mouseX < x+w) &&
(mouseY > y) && (mouseY < y+w))
{
over = true;
}
else
{
over = false;
}
return over;
}
}
Square square1, square2, square3, square4,square5,square6,square7,square8;
// maximum number of attempts to click on the secret square
int maxAttempts;
// current number of attempts
int attempts;
boolean done;
void setup()
{
size(400, 400);
background(255);
square1 = new Square();
square1.x = 100;
square1.y = 50;
square1.r = 0;
square1.g = 0;
square1.b = 0;
square2 = new Square();
square2.r = 0;
square2.g = 0;
square2.b = 0;
square2.x = 100;
square2.y = 20;
square3 = new Square();
square3.r = 0;
square3.g = 0;
square3.b = 0;
square3.x = 200;
square3.y = 200;
square4 = new Square();
square4.x = 80;
square4.y = 60;
square4.r = 0;
square4.g = 0;
square4.b = 0;
square5 = new Square();
square5.x = 80;
square5.y = 60;
square5.r = 0;
square5.g = 0;
square5.b = 0;
// not used now
maxAttempts = 5;
attempts = 0;
done = false;
}
void draw()
{
//background(100);
int dist1 = mouseX-square1.x;
if (abs(dist1) < 20) square1.x -= dist1;
int i;
for(i = 0; i<30;) {
fill(random(50,255), random(50,255), random(50,255));
rect(random(width),
random(height), 30, 30);
int i;
for(i = 0; i<4; i++) {
fill(255,0,0); random(100); random(100);}
square1(random(width),
random(height), 30, 30);
}
square1.draw();
square2.draw();
square3.draw();
square4.draw();
}
void mousePressed()
{
if ( square1.isOver() )
{
println("Square 1 pressed");
}
if ( square2.isOver() )
{
println("Square 2 pressed");
}
if ( square3.isOver() )
{
println("Square 3 pressed");
}
if ( square4.isOver() )
{
println("Square 4 pressed");
}
}
// this is our square class
class Square
{
// width in pixels
int w;
int x;
int y;
// colors
int r;
int g;
int b;
boolean over;
// this is the "constructor" for a new Square
Square()
{
x = 0;
y = 0;
w = 50;
r = 0;
g = 0;
b = 0;
over = false;
}
void draw()
{
// set the fill color for the 1st square
if (over)
{
fill(0);
}
else
{
fill( r, g, b );
}
// draw the rectangle
rect(x, y, w, w);
}
boolean isOver()
{
if ( (mouseX > x) && (mouseX < x+w) &&
(mouseY > y) && (mouseY < y+w))
{
over = true;
}
else
{
over = false;
}
return over;
}
}
comments

Add a comment: