处理:如何为2D阵列绘制的正方形涂上不同的颜色? [英] Processing: How can I color the squares drawn by my 2D Array in different colors?

查看:97
本文介绍了处理:如何为2D阵列绘制的正方形涂上不同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为轰炸机制造游戏板,以处理2D阵列。
(最终版本将具有颜色或位图以及每种块类型的属性,并将定义播放器具有的行走空间。)

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.)

我希望由11行11列组成的游戏板对于游戏中的每种块类型具有不同颜色的正方形。 ( j 是一个固体块, i 是一个步行空间, k 是一个易碎块,正如您将在数组中看到的那样)

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

这是我现在的代码:

//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);


    }
  }
}

这是我的结果:

如您所见,它全是红色的(不仅是k个块)

推荐答案

对于存储 i j k 变量在草图的顶部,然后将它们用作循环变量。您在那些 if 语句中到底要比较什么?请养成对程序进行调试的习惯,这样您就可以准确了解程序的作用。

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;

然后在 for 循环中,想要使用不同的变量,它们又具有更多的描述性名称:

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++) {

最后,您的 if 语句需要检查单元格是否为特定类型。请注意,您当前的 if 语句没有任何意义:

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);
  }

这首先检查单元格是否为 1 ,然后检查它是否等于 i ,这是一个循环变量。

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

请养成在每个 if 语句和 {} 的习惯> for 循环。我猜想您希望上面有两个单独的 if 语句,但是由于它没有大括号,所以它不是这样工作的。再次,使用调试器逐步解决此问题(甚至只是添加 println()语句都可以帮助您了解很多这些内容。

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.

这篇关于处理:如何为2D阵列绘制的正方形涂上不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆