Pick up object on random coordinates inside of 2D-array

I have a game in which a moving player has to collect ellipses in order to gain points. The ellipses are randomly distributed on a grid by using the following code:

int rows = 40;
int cols = 60;
int objectOffset = 25;
float objectOffset2 = 12.5;
float objectOffset3 = 112.5;

int[][] items = new int[cols][rows];


void setup () {
  
  size(1500, 900);   
  
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      items[i][j] = int(random(-5, 10));
    }
  }
};


void draw () {
   
duiker();
    
for ( int i = 0; i < cols; i++) {
      for ( int j = 0; j < rows; j++) {
        int t = items[i][j];
          if ( t < 0 ) {
            //Leeg vak
          } else if ( t == 2 ) {
            //Schat
            fill(255, 255, 0);
            ellipse(objectOffset * i + objectOffset2, objectOffset * j + objectOffset3, 25, 25);
          } else if ( t == 3 ) {
            //Mijn
            fill(150);
            ellipse(objectOffset * i + objectOffset2, objectOffset * j + objectOffset3, 25, 25);
          }
       }
     }
};

Now what I’m trying to achieve is that if the player gets within the borders of an object he’ll be able to pick it up by pressing shift. I assume comparing the current player’s location and 2D-array with each other is the way to go but I don’t know how to approach it when using a randomly filled array. Anyone that could help me out or guide me into the right direction?

This is the code I use for the player to move:

int radius = 30, directieX = 0, directieY = 0;
float xPositie = 750, yPositie = 30, speed= 1;


void duiker() {
  
  xPositie = xPositie + speed * directieX;
  yPositie = yPositie + speed * directieY;
  
  // Binnen het scherm blijven
  if ((xPositie > width-radius) || (xPositie < radius)) {       
    directieX=-directieX;    
  }
  
  if ((yPositie > height-radius) || (yPositie < radius)) {       
    directieY=-directieY;    
  } 
  
  //Opmaak duiker
  fill (180); 
  ellipse (xPositie, yPositie, radius, radius);
  fill (0, 200, 255);
  rect(xPositie - 10, yPositie - 10, 20, 10);
  fill(255, 255, 0);
  ellipse (xPositie, yPositie + 5, 12, 12);
  
};


void keyPressed() {
  
  if (key == CODED) {
    if (keyCode == LEFT) { 
      directieX=-1;
      directieY=0;
    }
      else if (keyCode == RIGHT) {
        directieX=1;
        directieY=0;
      }
      else if (keyCode == UP) {
        directieY=-1;
        directieX=0;
      }
      else if (keyCode == DOWN) { 
        directieY=1;
        directieX=0;
    }
      else if (keyCode == SHIFT) { 
        //Code voor picking up objects
    }
  }
  
};
1 Like

You don’t have a list of the positions of the treasure / things, you calculate them in draw()

ellipse(objectOffset * i + objectOffset2, objectOffset * j + objectOffset3,

Therefore in the code for SHIFT

    } else if (keyCode == SHIFT) { 
      //Code voor picking up objects
      
      
      
    }

You could for loop over all items (like you do in draw()) and compare the position to the position of the player. When it’s close, he is allowed to pick up.

BUT you would have to have a

for ( int i = 0; i < cols; i++) {
    for ( int j = 0; j < rows; j++) {

again which is time-consuming on the processor.

So instead you could make the check throughout in draw():

if(dist(
xPositie, yPositie, 
objectOffset * i + objectOffset2, objectOffset * j + objectOffset3 ) < 19) {
      // hit
      if ( t == 2 ) {
           pickUpIsAllowed = true;   
           pickUpIsAllowed_i = i; 
           pickUpIsAllowed_j = j; 
      }
}

(Check out dist() in the reference: https://www.processing.org/reference/)

Make boolean pickUpIsAllowed = false; a global variable (declare it before setup())
and also say pickUpIsAllowed = false; before the for-loops in draw()

Now in the SHIFT section

say

if (pickUpIsAllowed)  {
     // pick up 
    set items of that position to -5 or so. How do you do this? Look at the dist-section and the variables we set there.
}

I hope this helps!

Warm regards,

Chrisir

2 Likes