So I’m pretty new to Processing 3, and I need to make a game of battleships for an assignment. I have come to the part where I need to make a grid and be able to check which grid is being clicked. This is where I have a problem. I tried to create a grid, it doesn’t quite match te size of the rectangle I set.
This is the piece of my code that is drawing the grids.
void setup() {
size(500, 500);
}
void draw() {
final int XPLAYFIELD1 = width/4;
final int YPLAYFIELD1= height/2;
final int XPLAYFIELD2 = width/4*3;
final int YPLAYFIELD2 = height/2;
final int LENGTHPLAYFIELD = width/3;
final int LEFTPLAYFIELD1 = XPLAYFIELD1 - LENGTHPLAYFIELD/2;
final int TOPPLAYFIELD1 = YPLAYFIELD1 - LENGTHPLAYFIELD/2;
final int LEFTPLAYFIELD2 = XPLAYFIELD2 - LENGTHPLAYFIELD/2;
final int TOPPLAYFIELD2 = YPLAYFIELD2 - LENGTHPLAYFIELD/2;
final int gridAmount = 10;
int gridSize = LENGTHPLAYFIELD/gridAmount;
int[][] playfield1 = new int[gridAmount][gridAmount];
//settings
rectMode(CENTER);
//playfield 1
fill(#000000);
stroke(0);
noFill();
rect(XPLAYFIELD1, YPLAYFIELD1, LENGTHPLAYFIELD, LENGTHPLAYFIELD);
for (int row = 0; row < LENGTHPLAYFIELD; row+=gridSize) {
line(LEFTPLAYFIELD1+row, TOPPLAYFIELD1, LEFTPLAYFIELD1+row, TOPPLAYFIELD1+LENGTHPLAYFIELD);
}
for (int column = 0; column < LENGTHPLAYFIELD; column+=gridSize){
line(LEFTPLAYFIELD1, TOPPLAYFIELD1+column, LEFTPLAYFIELD1+LENGTHPLAYFIELD, TOPPLAYFIELD1+column);
}
//playfield 2
rect(XPLAYFIELD2, YPLAYFIELD2, LENGTHPLAYFIELD, LENGTHPLAYFIELD);
for (int row = 0; row < LENGTHPLAYFIELD; row+=gridSize) {
line(LEFTPLAYFIELD2+row, TOPPLAYFIELD2, LEFTPLAYFIELD2+row, TOPPLAYFIELD2+LENGTHPLAYFIELD);
}
for (int column = 0; column < LENGTHPLAYFIELD; column+=gridSize){
line(LEFTPLAYFIELD2, TOPPLAYFIELD2+column, LEFTPLAYFIELD2+LENGTHPLAYFIELD, TOPPLAYFIELD2+column);
}
}
For some reason there is a little bit of space left over on the right and the bottom of the grid. Anyone know why this is happening and how to fix this? Thanks!