Making a fully functional game board with 2D Array

I need to make a game board - using an array - for a Bomberman game remake in Processing.

This means the game board / array (which is rather large [9x9] and has 3 values [a,b,c] throughout), has to be able to:

  • Define the color/sprite of the according fields
  • Set the limit for where the character can walk
  • Have unique properties for each value/field (one value represents the open space where the player can move around, another is a solid block, and another one breaks when a bomb is blown up next to it and transforms into an open field type)

I'm pretty much a programming noob and I barely a clue how to make all of this work.. I've already set up the array though which look like this:

    int [][] board =  { 
    {b, b, b, b, b, b, b, b, b},  
    {b, a, b, a, b, a, b, c, b}, 
    {b, c, c, a, a, a, a, a, b},
    {b, c, a, a, c, a, a, a, b}, 
    {b, c, c, b, a, b, c, a, b}, 
    {b, a, c, a, a, a, a, a, b}, 
    {b, b, a, b, c, b, b, c, b}, 
    {b, a, a, a, c, a, a, c, b}, 
    {b, b, b, b, b, b, b, b, b}  };

(>b is solid, a is walking space and c is breakable)

And I have managed to draw it as a monochrome chessboard. Now I just have to figure out how to give each value the properties of the according block type.

Thanks for any help in advance :)

Answers

  • edited May 2017

    Do you know classes and objects?

    Easiest would be a class Cell for the board holding position and type (bomb...) and color for each cell

    But not strictly necessary

    Use 2D array as you have above - see tutorial on that

    Use a double for loop to fill it from your list above

    type[i][j] = board [i][j]; or just stick with the list you have

    see below

  • Cell [] grid;
    
    int unit = 200; // size of one tile
    
    int wideCount ;
    int highCount ;
    
    String[] list = {"b", "c", "d", "QQ"}; 
    
    // ---------------------------------------------------------
    
    void setup() {  
      size(400, 400);
    
      noStroke();
      background(255);
      int count;
      wideCount = width / unit; 
      highCount = height / unit; 
      count = wideCount * highCount;
    
    
    
      grid = new Cell[count];
    
      int index = 0;
      int k=0; 
      for (int x=0; x< wideCount; x++) {
        for (int y=0; y< highCount; y++) {
          //start creating objects
          grid[index] = new Cell (x*unit, y*unit, unit, list[k]);
          index++;
          k++;
        }
      }
    }
    //
    void draw() {
      //
      background(255);
      int index = 0;
      //
      for (int x=0; x< wideCount; x++) {
        for (int y=0; y< highCount; y++) {
          grid[index++].display();
        }
      }
    }
    
    // ========================================================================
    // and the class
    
    
    class Cell {
      float x, y;
      float side;
      String text=""; 
      //  int col;
    
      Cell (float tempX, float tempY, 
        float tempSide, 
        String tempText) {
        x = tempX;
        y = tempY;
        side = tempSide;
        text = tempText; 
        //
        float fate = random(2);
      }
    
      void display() {
        if (mouseX>x&&mouseX<x+side&&mouseY>y&&mouseY<y+side)
          fill(122, 0, 0); // dark
        else
          fill(255, 0, 0);  // light 
        stroke(255);
        rect (x, y, side, side);
    
        fill(255); 
        text(text, x+55, y+60);
      }
    }
    
Sign In or Register to comment.