We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Highlighting checkerboard square
Page Index Toggle Pages: 1
Highlighting checkerboard square? (Read 1347 times)
Highlighting checkerboard square?
Feb 15th, 2010, 2:14pm
 
Hey, everybody!

So, I've been trying to write a program for the basics of a "Chess game" program. So far I've been able to build my checkerboard, but I've having trouble figuring out how to highlight the square my mouse hovers over. My best guess is that a series of lines are drawn according to the mouse position. Here's what I've got so far: (Please be gentle! I'm still a little new to processing.)

void setup(){
 size (400, 400);
 smooth();
}

void draw(){
  for(int i=0; i<=400; i=i+50) {
   for(int j=0; j<=400; j=j+50)  
   {  
    if((i+j)%20==0) {  
   fill(0);      
} else  {  
   fill(200,0,0);
   }  
   rect (i, j, 100, 100);
   }
  }
}

  /* Highlight square mouse is on
  if(mouseX==i && mouseY==j){
    stroke(255,255,206);
    line(i,j,i+50,j+50);
  }*/

Thanks!
Re: Highlighting checkerboard square?
Reply #1 - Feb 15th, 2010, 2:47pm
 
As I always say, use named constants instead of magical numbers, thus signification of those numbers is clearer. See:
Code:
int SQUARE_SIZE = 50;

void setup(){
size (400, 400);
smooth();
}

void draw(){
for (int i = 0; i < width; i += SQUARE_SIZE) {
for (int j = 0; j < height; j += SQUARE_SIZE)
{
if ((i + j) % 20 == 0) {
fill(0);
} else {
fill(200, 0, 0);
}
rect (i, j, SQUARE_SIZE, SQUARE_SIZE);
}
}

int tlx = SQUARE_SIZE * (mouseX / SQUARE_SIZE);
int tly = SQUARE_SIZE * (mouseY / SQUARE_SIZE);
fill(255, 0, 0);
rect (tlx, tly, SQUARE_SIZE, SQUARE_SIZE);
}
Re: Highlighting checkerboard square?
Reply #2 - Feb 17th, 2010, 3:10pm
 
When I tried this,
this page made me very happy:

http://commons.wikimedia.org/wiki/Category:Standard_chess_tiles

you have there all the tiles.

Greetings!

Page Index Toggle Pages: 1