我目前正在编写国际象棋 AI,我正在使用 Monogame 进行图形处理。我想添加计算机棋子滑动到他们将棋子移动到的方块的动画,但我的代码不起作用。当 AI 只是随机移动(生成的时间可以忽略不计)时,滑动动画有效,但是我的 AI 速度较慢(生成需要几秒钟),Monogame 完全跳过动画,只是传送棋子从一个地方到另一个地方。我不确定问题出在哪里,如有任何帮助,我们将不胜感激!
这是我的更新方法代码:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (!anim)
{
drag = false;
pastHeldDown = heldDown;
compMove = null;
clicked = false;
mouseState = Mouse.GetState();
pos = new int[] { mouseState.X, mouseState.Y };
heldDown = false;
if (mouseState.LeftButton == ButtonState.Pressed)
{
heldDown = true;
}
if (!pastHeldDown && heldDown)
{
clicked = true;
dragPiece = new int[] { (int)(mouseState.X) / 100, (int)(mouseState.Y) / 100 };
dragPiece = board.flipCoords(dragPiece, FLIP);
drag = true;
index = board.indexOf(board.posFromPiece(board.getPieceArr()), dragPiece);
if (index != -1)
{
highPiece = board.getPieceArr()[index];
if (!highPiece.getDead())
dragRenderVal = highPiece.getColor()[0] + highPiece.getType();
else
dragPiece = new int[] { -1, -1 };
}
}
if (pastHeldDown && !heldDown && highPiece != null)
{
dragPiece = new int[] { (int)(mouseState.X) / 100, (int)(mouseState.Y) / 100 };
dragPiece = board.flipCoords(dragPiece, FLIP);
if (!(highPiece.getLoc()[0] == dragPiece[0] && highPiece.getLoc()[1] == dragPiece[1]))
{
if (MODE == 1 || (MODE == 2 && board.indexOf(highPiece.checkPiece(highPiece.listLegal(board), highPiece.getColor(), board, highPiece), dragPiece) != -1 && who == highPiece.getColor().Equals("white")) || (MODE == 3 && who == !COMPCOLOR && who == highPiece.getColor().Equals("white") && board.indexOf(highPiece.checkPiece(highPiece.listLegal(board), highPiece.getColor(), board, highPiece), dragPiece) != -1))
{
board.move(highPiece.getLoc(), dragPiece);
highPiece = null;
who = !who;
moves++;
}
}
else
{
highPiece = null;
dragPiece = null;
}
}
else if (MODE == 3 && who == COMPCOLOR)
{
compMove = ChessAI.genMove(board, COMPCOLOR);
slider = board.getPieceArr()[board.indexOf(board.posFromPiece(board.getPieceArr()), compMove[0])];
sliderLoc = new int[] { compMove[0][0] * 100 + ((compMove[1][0] * 100 - compMove[0][0] * 100) * animCounter) / ANIMTICKS, compMove[0][1] * 100 + ((compMove[1][1] * 100 - compMove[0][1] * 100) * animCounter) / ANIMTICKS };
who = !who;
anim = true;
animCounter = 0;
if (FLIP)
{
sliderLoc = new int[] { 700 - sliderLoc[0], 700 - sliderLoc[1] };
}
}
}
else if (compMove != null)
{
sliderLoc = new int[] { compMove[0][0] * 100 + ((compMove[1][0] * 100 - compMove[0][0] * 100) *animCounter)/ANIMTICKS, compMove[0][1] * 100 + ((compMove[1][1] * 100 - compMove[0][1] * 100) *animCounter)/ANIMTICKS };
if (FLIP)
{
sliderLoc = new int[] { 700 - sliderLoc[0], 700 - sliderLoc[1] };
}
animCounter++;
if (animCounter == ANIMTICKS)
{
anim = false;
animCounter = 0;
board.move(compMove[0], compMove[1]);
moves++;
compMove = null;
}
}
base.Update(gameTime);