XNA 中的国际象棋逻辑
因此,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 XNA 不太熟悉,但我会给出一些建议。一方面,您可能希望将这些片段保留在 2D 数组或类似数组中,而不是将它们放在自己的变量中(如 wknight1)。否则,您必须在用户每次单击时检查每个变量。
我假设您想要一个系统,用户单击一个块来选择它,然后单击一个空方块将其移动到那里。这是一些类似的伪代码(我在这里使用空板位置来表示空方块):
您可以添加更多条件,例如 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):
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.