XNA 中的国际象棋逻辑

发布于 2024-08-12 22:53:00 字数 283 浏览 10 评论 0原文

因此,我使用 XNA 库等创建了一个 2D 国际象棋游戏棋盘,其中包含所有棋子。但是,如果我单击它们,我不知道如何使它们移动。这就是我对骑士作品之一的逻辑的理解。

if(mouse.LeftButton == ButtonState.Pressed 
      && mouse.X == wknight1.position.X 
      && mouse.Y == wknight1.position.Y)
{
}

如何选择该棋子然后让它移动?

So I've created a 2D chess game board with all the pieces on it using the XNA library and all. However I don't know how to make the pieces move if I click them. This is what I have for the logic of one of the knight pieces.

if(mouse.LeftButton == ButtonState.Pressed 
      && mouse.X == wknight1.position.X 
      && mouse.Y == wknight1.position.Y)
{
}

How do I select the piece then allow it to move?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2024-08-19 22:53:00

我对 XNA 不太熟悉,但我会给出一些建议。一方面,您可能希望将这些片段保留在 2D 数组或类似数组中,而不是将它们放在自己的变量中(如 wknight1)。否则,您必须在用户每次单击时检查每个变量。

我假设您想要一个系统,用户单击一个块来选择它,然后单击一个空方块将其移动到那里。这是一些类似的伪代码(我在这里使用空板位置来表示空方块):

if(mouse.LeftButton == ButtonState.Pressed
  && board[mouse.x][mouse.y] != null && pieceSelected == null)
{
    pieceSelected = board[mouse.x][mouse.y];
    selectedX = mouse.x;
    selectedY = mouse.y
}
else if (mouse.LeftButton == ButtonState.Pressed
  && board[mouse.x][mouse.y] == null && pieceSelected != null)
{
    board[selectedX][selectedY] == null;
    board[mouse.x][mouse.y] = pieceSelected;
    pieceSelected = null;
}

您可以添加更多条件,例如 isAValidMove(pieceType, startx, starty, finishx, finishy) ,您可以在其中检查玩家正在尝试移动自己的棋子,并且这是合法的国际象棋移动等。您可能也可以使其更加优雅和面向对象(和/或事件驱动)(并添加点击和拖动等),但我保留简单说明基本逻辑。

I'm not familiar with XNA specifically but I'll give a few suggestions. For one thing, you probably want to keep the pieces in a 2D array or similar rather than having them in their own variables (like wknight1). Otherwise you'll have to check every variable every time the user clicks.

I'm assuming you want a system where the user clicks a piece to select it, then clicks an empty square to move it there. Here's some pseudo-code for something like that (I'm using a null board location to mean an empty square here):

if(mouse.LeftButton == ButtonState.Pressed
  && board[mouse.x][mouse.y] != null && pieceSelected == null)
{
    pieceSelected = board[mouse.x][mouse.y];
    selectedX = mouse.x;
    selectedY = mouse.y
}
else if (mouse.LeftButton == ButtonState.Pressed
  && board[mouse.x][mouse.y] == null && pieceSelected != null)
{
    board[selectedX][selectedY] == null;
    board[mouse.x][mouse.y] = pieceSelected;
    pieceSelected = null;
}

You can add in further conditions like isAValidMove(pieceType, startx, starty, finishx, finishy) where you check that the player is trying to move their own piece and that it's a legal chess move etc. You could probably make this more elegant and OO (and/or event-driven) too (and add click-and-dragging etc) but I'm keeping it simple to illustrate the basic logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文