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 & HelpSyntax Questions › fill colors changing
Page Index Toggle Pages: 1
fill colors changing (Read 1204 times)
fill colors changing
Oct 16th, 2007, 5:54am
 
I'm trying to create a game board with colored piece objects,  but the colors on the objects keep changing? How can I fix it? Thanks for any help.


//*************** GAME PIECE CLASS ***************//

class gamePiece{

 color c;
 int tlxPos, tlyPos, brxPos, bryPos;

 gamePiece(color iC, int itlxPos, int itlyPos, int ibrxPos, int ibryPos){
   c = iC;
   tlxPos = itlxPos;        //top left x pos
   tlyPos = itlyPos;        //top left y pos
   brxPos = ibrxPos;        //bottom right x pos
   bryPos = ibryPos;        //bottom right y pos

     fill(c);
   rect(tlxPos, tlyPos, brxPos, bryPos);
 }

 public color getColor(){

   return c;
 }  

 public int getTLXPos(){
   return tlxPos;
 }

 public int getTLYPos(){
   return tlyPos;
 }

 public int getBRXPos(){
   return brxPos;
 }

 public int getBRYPos(){
   return bryPos;
 }

 public boolean compareColors(gamePiece a, gamePiece b){

   if(a.getColor() == b.getColor()){
     return true;
   }
   else{
     return false;
   }
 }
}









//*************** GAME BOARD CLASS ***************//

class gameBoard{

 int boardSize;

 public gameBoard(int i){
   boardSize = i;
 }

 public int getBoardSize(){
   return boardSize;
 }

 public void createRandomPieces(int arraySize){

   int numPieces = arraySize * arraySize;

   int columns[] = new int[arraySize];
   int rows[] = new int[arraySize];

   int tlxLocation = 0;
   int brxLocation = 20;

   int tlyLocation = 0;
   int bryLocation = 20;

   gamePiece[] pieces = new gamePiece[numPieces];

   for(int columnNumber = 0; columnNumber < columns.length; columnNumber++){
     for(int rowNumber = 0; rowNumber < rows.length; rowNumber++){

       pieces[rowNumber] = new gamePiece(palette[int(random(colorRange))], tlxLocation, tlyLocation, brxLocation, bryLocation);

       tlyLocation += 20;
       bryLocation += 20;
     }

     tlyLocation = 0;
     bryLocation = 20;

     tlxLocation += 20;
     brxLocation += 20;      
   }
 }
}

//*************** GLOBAL VARIABLES****************//
color[] palette = new color[5];
int gameBoardSize = 10;
int colorRange = 5;
gameBoard test = new gameBoard(gameBoardSize);

//*************** SETUP FUNCTION ***************//

void setup(){

 size(test.getBoardSize() * 20, test.getBoardSize() * 20);
 frameRate(1);

}

//*************** DRAW FUNCTION ***************//

void draw(){
 background(255);
 smooth();

 palette[0] = color(255,0,0);       //RED
 palette[1] = color(0, 255, 0);     //GREEN
 palette[2] = color(0, 0, 255);     //BLUE
 palette[3] = color(0, 0, 0);       //BLACK
 palette[4] = color(255, 165, 0);   //ORANGE

 test.createRandomPieces(test.getBoardSize());
}
Re: fill colors changing
Reply #1 - Oct 16th, 2007, 8:36am
 
obviously just stop calling createRandomPieces() ...

what are you trying to do?

F
Re: fill colors changing
Reply #2 - Oct 16th, 2007, 1:03pm
 
g8rjosh, the appropriate response to my asking you not to double post is not to delete that message and post a third.
Re: fill colors changing
Reply #3 - Oct 16th, 2007, 4:41pm
 
fjen wrote on Oct 16th, 2007, 8:36am:
obviously just stop calling createRandomPieces() ...

what are you trying to do

F


But I don't understand logically how to modify this to get me what I want. That is, how do I get my program to fill in the squares with their colors and then stop once it fills the last square
Re: fill colors changing
Reply #4 - Oct 16th, 2007, 7:21pm
 
What fjen means is that you keep calling random() in draw() with: pieces[rowNumber] = new gamePiece(palette[int(random(colorRange))],... wich generates a new random set of colors every frame. so call it only on the frame you need a new set of random colors.

frank
Re: fill colors changing
Reply #5 - Oct 16th, 2007, 7:41pm
 
right, for example create the boxes in setup() and just draw them in draw() ..

F
Re: fill colors changing
Reply #6 - Oct 16th, 2007, 9:59pm
 
fjen wrote on Oct 16th, 2007, 7:41pm:
right, for example create the boxes in setup() and just draw them in draw() ..

F


Alright, I think I understand a little better. But I am still confused on how to actually implement this. Can someone show me an example of something similar I'm sorry, I am still very new to programming and something so simple to someone might require some little trick that I don't really know about or something.

I understand that I should set up the boxes in the setup(), but I dont know what goes into "just drawing them in draw(). Sorry, and thanks again guys. You have been a very good help for me so far.
Re: fill colors changing
Reply #7 - Oct 16th, 2007, 10:23pm
 
ok. no tricks, no magic involved ..

put the:

fill(c);
rect(tlxPos, tlyPos, brxPos, bryPos);

into a function (drawPiece() ?) inside gamePiece.class

add a function to gameBoard (drawAllPieces ?) similar to createRandomPi ... but with a change in that line:
pieces[rowNumber] = new gamePiece ...
to:
pieces[rowNumber].drawPiece();
( yes from above )

now do your createRandom.. inside setup() and inside draw() you call your newly created drawAllPieces()

F
Re: fill colors changing
Reply #8 - Oct 17th, 2007, 11:25am
 
exactly. follow fjen's steps and you're done.

just to help you understand a little more :
- the setup() method executes once, at start.
- the draw() method executes every frame : i.e. every second, since you defined frameRate(1).

you should just *create* your gamePieces in setup() and *draw* them in draw(). this way, you will set a color for each piece only once, at setup(), and the draw() method will always draw the same thing (unless you start moving the pieces).
Re: fill colors changing
Reply #9 - Oct 17th, 2007, 7:57pm
 
Thank you very much. I understand, and I have figured it out, except using a 2 dimensional array to populate the game board. Thank you very much for your help.
Page Index Toggle Pages: 1