Friday, August 12, 2011

Simple Secret Square

// make some square objects
Square square1, square2, square3, square4, square5, square6, square7, square8, square9, square10, square11, square12, square13, square14, square15, square16, square17, square18, square19, square20, square21, square22, square23, square24, square25;

// maximum  number of attempts to click on the secret square
int maxAttempts;

// current number of attempts
int attempts;

boolean done;

void setup()
{
size(600, 500);

square1 = new Square();

square1.x = 0;
square1.y = 0;

square1.r = 255;
square1.g = 0;
square1.b = 0;

square2 = new Square();
square2.r = 0;
square2.g = 255;
square2.b = 0;

square2.x = 0;
square2.y = 100;

square3 = new Square();
square3.r = 0;
square3.g = 0;
square3.b = 255;

square3.x = 0;
square3.y = 200;

square4 = new Square();

square4.x = 0;
square4.y = 300;

square4.r = 255;
square4.g = 255;
square4.b = 0;

square5 = new Square();

square5.x = 0;
square5.y = 400;

square5.r = 255;
square5.g = 155;
square5.b = 0;

square6 = new Square();

square6.x = 100;
square6.y = 0;

square6.r = 255;
square6.g = 0;
square6.b = 0;

square7 = new Square();
square7.r = 0;
square7.g = 255;
square7.b = 0;

square7.x = 100;
square7.y = 100;

square8 = new Square();
square8.r = 0;
square8.g = 0;
square8.b = 255;

square8.x = 100;
square8.y = 200;

square9 = new Square();

square9.x = 100;
square9.y = 300;

square9.r = 255;
square9.g = 255;
square9.b = 0;

square10 = new Square();

square10.x = 100;
square10.y = 400;

square10.r = 255;
square10.g = 155;
square10.b = 0;

square11 = new Square();

square11.x = 200;
square11.y = 0;

square11.r = 255;
square11.g = 0;
square11.b = 0;

square12 = new Square();
square12.r = 0;
square12.g = 255;
square12.b = 0;

square12.x = 200;
square12.y = 100;

square13 = new Square();
square13.r = 0;
square13.g = 0;
square13.b = 255;

square13.x = 200;
square13.y = 200;

square14 = new Square();

square14.x = 200;
square14.y = 300;

square14.r = 255;
square14.g = 255;
square14.b = 0;

square15 = new Square();

square15.x = 200;
square15.y = 400;

square15.r = 255;
square15.g = 155;
square15.b = 0;

square16 = new Square();

square16.x = 300;
square16.y = 0;

square16.r = 255;
square16.g = 0;
square16.b = 0;

square17 = new Square();
square17.r = 0;
square17.g = 255;
square17.b = 0;

square17.x = 300;
square17.y = 100;

square18 = new Square();
square18.r = 0;
square18.g = 0;
square18.b = 255;

square18.x = 300;
square18.y = 200;

square19 = new Square();

square19.x = 300;
square19.y = 300;

square19.r = 255;
square19.g = 255;
square19.b = 0;

square20 = new Square();

square20.x = 300;
square20.y = 400;

square20.r = 255;
square20.g = 155;
square20.b = 0;

square21 = new Square();

square21.x = 400;
square21.y = 0;

square21.r = 255;
square21.g = 0;
square21.b = 0;

square22 = new Square();
square22.r = 0;
square22.g = 255;
square22.b = 0;

square22.x = 400;
square22.y = 100;

square23 = new Square();
square23.r = 0;
square23.g = 0;
square23.b = 255;

square23.x = 400;
square23.y = 200;

square24 = new Square();

square24.x = 400;
square24.y = 300;

square24.r = 255;
square24.g = 255;
square24.b = 0;

square25 = new Square();

square25.x = 400;
square25.y = 400;

square25.r = 255;
square25.g = 155;
square25.b = 0;

// not used now
maxAttempts = 5;
attempts = 0;
done = false;
}

void draw()

{
background(100);

square1.draw();
square2.draw();
square3.draw();
square4.draw();
square5.draw();
square6.draw();
square7.draw();
square8.draw();
square9.draw();
square10.draw();
square11.draw();
square12.draw();
square13.draw();
square14.draw();
square15.draw();
square16.draw();
square17.draw();
square18.draw();
square19.draw();
square20.draw();
square21.draw();
square22.draw();
square23.draw();
square24.draw();
square25.draw();

}

void mousePressed()
{
if ( square1.isOver() )
{
println("Square 1 pressed");
}

if ( square2.isOver() )
{
println("Square 20 pressed");
}

if ( square3.isOver() )
{
println("Square 2 pressed");
}

if ( square4.isOver() )
{
println("Square 3 pressed");
}
if ( square5.isOver() )
{
println("Square 4 pressed");
}
if ( square6.isOver() )
{
println("Square 5 pressed");
}

if ( square7.isOver() )
{
println("Square 6 pressed");
}

if ( square8.isOver() )
{
println("Square 7 pressed");
}

if ( square9.isOver() )
{
println("Square 8 pressed");
}
if ( square10.isOver() )
{
println("Square 9 pressed");
}
if ( square11.isOver() )
{
println("Square 10 pressed");
}

if ( square12.isOver() )
{
println("Square 11 pressed");
}

if ( square13.isOver() )
{
println("Square 12 pressed");
}

if ( square14.isOver() )
{
println("Square 13 pressed");
}
if ( square15.isOver() )
{
println("Square 14 pressed");
}
if ( square16.isOver() )
{
println("Square 15 pressed");
}

if ( square17.isOver() )
{
println("Square 16 pressed");
}done = true;

if ( square18.isOver() )
{
println("Square 17 pressed");
}

if ( square19.isOver() )
{
println("Square 18 pressed");
}
if ( square20.isOver() )
{
println("Square 19 pressed");
}
}
if ( square21.isOver() )
{
println("Square 21 pressed");
}

if ( square22.isOver() )
{
println("Square 22 pressed");
}

if ( square23.isOver() )
{
println("Square 23 pressed");
}

if ( square24.isOver() )
{
println("Square 24 pressed");
}
if ( square25.isOver() )
{
println("Square 25 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 = 100;
r = 0;
g = 0;
b = 0;
over = false;
}

void draw()
{

// set the fill color for the 1st square
if (over)
{
fill(255);
}
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;
}

}

info

views:

This sketch has a parent
Tags: learning