Game question
in
Programming Questions
•
10 months ago
Hi,
I started making a grid puzzle game that includes a grid with randomly placed white and grey squares.
And there is a black empty square somewhere in the middle.
I want to be able to move this black square one position to its left, right, top or bottom and interchange positions with the square it moved to.
I am unable to move forward on how to have the mousePressed() conditions with the code I have. I want to build upon the existing code. I could really use some help.
Here's my code:
int cellSize = 50;
int cols, rows;
//float blackCol;
//float blackRow;
int[] grid = new int [64];
int blackSquare;
void setup(){
size(400, 400);
rectMode(CORNER);
cols = width/cellSize;
rows = height/cellSize;
// blackCol = random(cols);
// blackRow = random(rows);
int grey = 0;
int white = 0;
for (int i=0; i < 64; i++){
if (random(8) > random(8)){
grid[i] = 100;
grey++;
if (grey == 31){
for (int j=i; j < 64; j++){
grid[j] = 255;
}
break;
}
} else {
grid[i] = 255;
white++;
if (white == 32){
for (int j=i; j < 64; j++){
grid[j] = 100;
}
break;
}
}
}
blackSquare = int(random(64));
grid[blackSquare] = 0;
} // end of setup
void draw(){
for (int i=0; i < cols; i++){
for (int j=0; j < rows; j++){
int x = i*cellSize;
int y = j*cellSize;
fill(grid[i*8+j]);
stroke(0);
rect(x,y,cellSize,cellSize);
}
}
}
void mousePressed() {
}
2