2D Coordinate Based Game [duplicate]

I want to make a game for Android that has a 10x10 Grid Plane as UI.In this game users going to touch on a grid and tihs grid will be painted.

My question is how do I understand which gird or tile was clicked ? I thought maybe I will make this grid on a coordinate system so that if the user touchs a point on the gamescreen aapp will take this point and will find out in which coordinate interval is in.

Like so:

I touched -> [-2.68,3.85]
The grid -> [-3,4] will be painted.

How do I do this in Android Studio?

• "How do I do this in Android Studio,with java or any Android Game Engine." - please pick one. – Philipp Aug 24 '15 at 7:40
• Android Studio than , if it is possible, if not any engine is acceptable :) – Recomer Aug 24 '15 at 7:41

For a grid system, I haven't yet implemented this code in anything other than an engine, but I'd imagine it would be similar.

I implemented the Tetris clone in Unity 5, using this website: http://noobtuts.com/unity/2d-tetris-game

The grid section of this webpage might be of some use to you. I will try my best to convert it into java code, and make it android relevant though.

For setting up the grid I'd imagine a 2D array would be best, so you can keep track of the positions inside of the grid:

public class Grid
{
public static int width = 10;    // The width of the grid.
public static int height = 10;   // The height of the grid.
public static GameObject[] gridObjects; // What the grid will be filled with, the paint objects? You might want to change this array type.

// If you want a variable width and height, just pass them in here, instead of declaring values above.
// Whenever you want to make a grid, sets up the 2D array (and width and height if you want).
public Grid()
{
// Creates a 2D array for the grid structure based on width and height.
gridObjects = new GameObject[width * height];
}


To round out the coordinates, you would need to make use of Android's Math.round() function, this function can either take a double (which returns a long) or a float (which returns an int) - I assume you would want to pass in a float and gain an int.

    // This will round out any decimal coordinates.
public Vector2 roundVec2(Vector2 gridPosition)
{
// Create your own Vector2 class in Android?
// Or just pass in two floats, x and y!
// Math.round() can take either a double or a float in android.
return new Vector2(Math.round(gridPosition.x),
Math.round(gridPosition.y));
}

// This will round out any decimal coordinates.
// Use this twice, once for the x coordinate and once for the y coordinate.
// If you're dealing with floats instead of Vector2.
public int roundFloat(float gridPosition)
{
return (Math.round(gridPosition));
}


Finally, all you would need to do is setup your grid check to see if the new position is inside of the grid, so when a touch occurs on the screen, you can check to see if it is a valid grid position! A bool function which checks the borders (like on the website I linked above) should do.

Then wherever you have used onTouch for your controls, in this function I assume:

public boolean onTouch(View v, MotionEvent event)


You can use the grid functions like so, maybe use a pointer to the grid class, in the same class you handle your touch input.

Vector2 touchPosition = new Vector2(event.getX(), event.getY());
grid.roundVec2(touchPosition);
// Then pass in this point to your border checking function to see if the touch is within the grid boundaries.
// If it is within the bounds, PAINT the cell.

// If you are using floats instead.
int roundedTouchX = grid.roundFloat(event.getX());
int roundedTouchY = grid.roundFloat(event.getY());
// Then pass these two integers into your border checking function.
// If it is within the bounds, PAINT the cell.

// Make the array element at that coordinate equal to the PAINT object!
// If you are using a boolean array instead of a GameObject array, just switch the element to true, might be easier?

// Replace touchPosition.getX() with roundedTouchX and touchPosition.getY() with roundedTouchY if you are using floats instead of Vector2
// If you are using an array of GameObjects.
grid.gridObjects[touchPosition.getX() * touchPosition.getY()] = paintObject;

// If you are using an array of booleans.
grid.gridObjects[touchPosition.getX() * touchPosition.getY()] = true;


Hope this has helped in some way!

If you are going to just paint the grid, you can check out GridLayout provided by android UI. I have tried for 3X3 grid and used ImageView to set some images. To understand gridLayout , http://developer.android.com/reference/android/widget/GridLayout.html