Processing: How can I color the squares drawn by my 2D Array in different colors?

岁酱吖の 提交于 2019-12-25 05:04:04

问题


I'm trying to make a game board for bomberman in processing with a 2D array. (The final version will have colors or bitmaps and the properties of each block type and will define the walking space that the player has.)

I want this game board which consists of 11 rows and 11 columns to have squares of different colors for each block type in the game. ( j being a solid block, i being walking space and k being a breakable block, as you'll see in the array)

I've already managed to draw the array/board, but now I need to color it right.

I've tried to color one block type in one certain way but it just colored the whole board.

This is my code for now:

//Amnt of Columns & Rows
int cols = 11;
int rows = 11;

//Block types
int i;
int j;
int k;


void setup() {
  size(440, 440);


//Game Board
  int [][] field =  { 
    {j, j, j, j, j, j, j, j, j, j, j}, 
    {j, i, i, i, i, k, i, i, i, i, j}, 
    {j, i, j, i, j, i, j, i, j, k, j}, 
    {j, k, k, i, i, i, i, i, i, i, j}, 
    {j, i, j, i, j, k, j, i, j, i, j}, 
    {j, i, i, i, i, k, i, i, i, i, j}, 
    {j, k, j, k, j, i, j, k, j, i, j}, 
    {j, i, k, i, i, i, i, i, i, i, j}, 
    {j, i, j, i, j, k, j, i, j, i, j}, 
    {j, i, i, i, i, k, i, i, i, k, j}, 
    {j, j, j, j, j, j, j, j, j, j, j}  };


  // Draw Board
  for ( i = 0; i < cols; i+=1) {
    for ( j = 0; j < rows; j+=1) {

      if(field[i][j]==1)

      if(field[i][j]==i){
        fill(0);}

        if(field[i][j]==k){
        fill(200,0,0);} else
        if(field[i][j]==j){
        fill(0,200,0);}

      rect(i * 40, j * 40, 40, 40);


    }
  }
}

And this is my result:

As you can see it's all red (and not only the k blocks)


回答1:


It doesn't make a ton of sense for you to store the i, j, and k variables at the top of your sketch, and then use them as your loop variables. What exactly are you comparing in those if statements? Please get into the habit of debugging your program so you understand exactly what it's doing.

But basically, you should really use two different sets of variables, and you should name them something more meaningful. something like this:

int spaceType = 0;
int blockType = 1;
int bombType = 2;

Then in your for loops, you would want to use different variables, that again have more descriptive names:

for (int cellX = 0; cellY < cols; cellX++) {
   for (int cellY = 0; cellY < rows; cellY++) {

Finally, your if statements need to check whether the cell is a particular type. Note that your current if statements don't make a ton of sense:

if(field[i][j]==1)
  if(field[i][j]==i){
    fill(0);
  }

This first checks whether the cell is 1, and then checks whether it equals i, which is a loop variable.

Also, please get into the habit of using proper indentation and curly brackets {} around every if statement and for loop. I'm guessing that you wanted two separate if statements above, but since it doesn't have curly brackets, that's not how it's working. Again, stepping through this with a debugger (or even just adding println() statements would help you figure a lot of this stuff out.



来源:https://stackoverflow.com/questions/44101117/processing-how-can-i-color-the-squares-drawn-by-my-2d-array-in-different-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!