# Checkers or chess movement

how can i move a character on tiles making them count ??? in the sense I would like to make it move 3 steps 2 steps or 10 steps and the character should suggest and follow a path giving also limits …
for example … if the character can take 3 steps … I click on a tile 3 steps away and he gets there … or maybe he has 20 steps but I want him to do only 5 steps I click on the tile I want … maybe even a path appears … is there a tutorial? has anyone done something like this ??

Hi, I’ve once programed kind of a chess board, so I can give an advice about that. It might not be the best solution, depending on the case.
My solution is more the mathematical one, but using GDevelop already-made features, and depending on what you want, you might not need to do what I suggest. For complexe moves like in chess, I guess my solution is not too bad.

I would give all squares on the board coordinates, x and y. Each piece on the board has 2 variables x and y. For instance the white queen at the start of the game is at x=4, y=1.
All calculations about moves and right to move woud be done with this coordinates numbers.
For example a rook can add or substract a certain number from its x or y, as long as it stays between 1 and 8. (chessboard is 8x8)

I’d do this, to simplify your task of programming each type piece’s rights of move. Of course, you’d program each “type” of piece, not each piece.
The advantage is that you only deal here with positive natural numbers, that match your board’s squares and rules. You don’t deal with actual pixels coordinates.

The trick is to convert these natural numbers coordinates into pixel when it will come to display the sprites on the player screen.

Conversion from numbers to actual game pixels :
x is your natural number coordinate for a pawn.
You multiply x by the scale of ypur board. If one square of your board is 10 pixel long, you multiply x by 10.
So a pawn that is at x=2 in natural numbers will end up being placed in your game at x=20+2*10 = 40 pixels

So here it what would happen in the game :

Player’s query :
for example player click on a pawn. If you use a condition with the sprite collision with mouse it’s easy, but you may want to use the x an y position of mouse. To indicate a certain board’s square for example. In this case you’ll receive x and y as pixels, but you’ll need to convert them to naturals numbers doing the reversed conversion as indicated above. You’ll send to the “query analysis” code, these informations : type of piece selected, selected square, x and y of selected piece, …

Query analysis :
the program checks the rights to move the piece. Here it uses only natural numbers. It may be complex to program, but basicaly, for each kind of piece, you need to set rules like how much can be added to x, y, minimum, maximum, according to the board size…
The program returns either an imposibility, either possible, with the new x and y possible for your piece.

Display for player :
If the query is possible, you can display the result for the player. It can be for showing the possible path, or to actually move the piece.
In any case, you’ll need to convert back the natural x and y, to pixels x and y.

So that’s roughly how I’d do it. Hopefully someone else has other advices to give you
Good luck!

5 Likes

There’s an extension for grid-based movement, it might be what you’re looking for.

1 Like

I can’t find it in g develop what’s the name of this extension?