开发者

Mouse click event blues in a Windows Forms/C#-based game

开发者 https://www.devze.com 2023-02-01 18:43 出处:网络
I am trying to code a chessgame application with C#. And I try to change the position of the chess pieces. When I first click, it should take the first coordinate information. And the secon开发者_运维

I am trying to code a chessgame application with C#. And I try to change the position of the chess pieces. When I first click, it should take the first coordinate information. And the secon开发者_运维问答d click should take the second coordinate information. And change these pieces.

How can I do it?


When you receive the first click you need to update some state in your form to register that click and store the coordinates of the click. When you click you can test the current state of the application. When you see that it is the second click you can move the piece.

This is a very rough idea of how you could do it, with a lot of details missing:

private bool hasClick;
private int xCoordinate;
private int yCoordinate;

private void Board_Click(object sender, EventArgs args)
{
    if (this.hasClick)
    {
        // Move the pieces.

        this.hasClick = false;
    }
    else
    {
        this.xCoordinate = ...;
        this.yCoordinate = ...;
        this.hasClick = true;
    }
}


User is able to click anywhere on the window - so in function mouse click event you have to obtain coordinates and find out if click occurred while mouse pointer was above field on the table. If it was - you have to locate the field. When mouse click occurs again, you have to do the same job. If it is occurred above valid field, move the figure. If the second click wasn't above the field, you have to take some other action or just ignore it.

0

精彩评论

暂无评论...
验证码 换一张
取 消