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