I’m designing a simple game in an android based on the below picture
I have draw these squares by method drawLine() with screen size ratios
int r = 1; for (float i = rowHight; i <= canvasHight + rowHight; i += rowHight) { if ((r % 2) != 0) { canvas.drawLine(cellWidth / 2, i - rowHight, cellWidth / 2, i, paint); for (float j = cellWidth; j < canvasWidth; j += cellWidth) { canvas.drawLine(j + (cellWidth / 2), i - rowHight, j + (cellWidth / 2), i, paint); } } else if ((r % 2) == 0) { for (float j = cellWidth; j < canvasWidth; j += cellWidth) { canvas.drawLine(j, i - rowHight, j, i, paint); } } canvas.drawLine(0, i, canvasWidth, i, paint); r++; }
how to design these boxes by arrays in Java in this way as the rows are not equal and also each row deviates from the next row because I want design this by arrays to handle the method onTouchEvent(MotionEvent event) >> The purpose of the question is how once I touch the screen how I know Which the box I touched it
I try to use two arrays every array include similar rows like this:
public void init() { int c = 11; int s1 = (c + 1) / 2; int s2 = s1 - 1; arr1 = new int[s1][]; arr2 = new int[s2][]; int x1 = 1; for (int i = 0; i < (arr1.length / 2); i++) { arr1[i] = new int[x1]; x1 += 2; } int x2 = s2; for (int i = (arr1.length / 2); i < arr1.length; i++) { arr1[i] = new int[x2]; x2 -= 2; } int y1 = 2; for (int i = 0; i < ((arr2.length + 1) / 2); i++) { arr2[i] = new int[y1]; y1 += 2; } int y2 = s2 - 1; for (int i = ((arr2.length + 1) / 2); i < arr2.length; i++) { arr2[i] = new int[y2]; y2 -= 2; } for (int row = 0; row < arr1.length; row++) { for (int col = 0; col < arr1[row].length; col++) { arr1[row][col] = 0; } } for (int row = 0; row < arr2.length; row++) { for (int col = 0; col < arr2[row].length; col++) { arr2[row][col] = 0; } } }