Elior
Elior

Reputation: 3266

Mouse Event in C#

I'm trying to build a Checkers/Draughts game in C#. The project is build using Windows Form.

What I did is to decompose the game board. So I have a "Board" class. and "BoardSquare" class (this class draw a square in the right position and color)

The board is build from 8 X 8 BoardSquare.

I thought about this solution, the square needs to know if it's empty or not, and if it is not empty it means that there is a "GameSoldier" on it.

So when I want to move the soldier to another square I need to click on the current square to get the GameSoldier, and then to press on the destination square.

So what I want to do is to add mouse functionality to the BoardSquare class to get the properties of the squares, like positions. Any ideas?

Upvotes: 0

Views: 421

Answers (3)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

There's couple of ways to go about this, your boardsquare class might get in the way of the simple one because it will take focus.

When you click on an occupied board square it's mouse event fires. It's position on the board can be identified by it's Top / Height and Left / width Armed with that you can then pass that up to a BoardSquareSelected event handler defined on the board. When you click on an unoccupied square mouse position is x / square width ,y / square height You'll need a bit more logic for first click is occupied, second isn't legal move and such. You could look at a drag and drop derivative as well. But personally I'd get rid of boardsquare as a component. If board has an 8x8 array of squares and you use Invalidate(rect) to avoid redrawing the entire thing on any change, you don't really need it. Then all your selection stuff is wholly encapsulated by the board class.

Upvotes: 1

John Willemse
John Willemse

Reputation: 6698

You can get the coordinates of the cursor from the EventArgs when the board is clicked. To translate these coordinates to a square on the board, you have to layout the tiles from top left to bottom right, across rows and store them in an indexed list or array.

The X coordinate (in "tiles") is then Math.Floor(X_Clicked / TileWidth)

And the Y coordinate is Math.Floor(Y_Clicked / TileHeight)

For example, if your tiles are 50 pixels wide and 50 pixels high, and the user clicks at (329, 186) this would be the tile at:

329 / 50 = 6,58 = 6

186 / 50 = 3,72 = 3

The tile at (6, 3) was clicked. To convert this to an index in the list/array:

x + (y * tiles_in_a_row) = 6 + (3 * 8) = 30, so Tile[30] was clicked.

Remember that this is a zero-based coordinate system, so the first tile is at (0, 0).

Upvotes: 1

b0wter
b0wter

Reputation: 148

What you want to do is add an Eventhandler for the MouseClick event of the control you are rendering you're board in. In the EventArgs for that event you will be able to retrieve the mouse coordinates. You can then combine that information with the size of the control itself to calculate which square has been clicked.

Here is some more information:

Obtain Position/Button of mouse click on DoubleClick event

(especially Moose's answer)

Upvotes: 0

Related Questions