I need help with implementing automated movement for the AI bat in my game. The human player’s bat works fine with mouse input, but I can’t get the computer bat to track the ball properly. I want the AI to follow the ball but still have some imperfection so it can miss sometimes.
MainForm.cs
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
gameManager.UpdatePlayerBat(e.Location.Y);
}
GameManager.cs
public void UpdatePlayerBat(int mouseY)
{
playerBat.SetBatPosition(mouseY);
}
Bat.cs
public void SetBatPosition(int yCoordinate)
{
location.Y = yCoordinate;
}
For the AI bat, I tried this approach but it doesn’t work correctly:
Bat.cs
public void SetAIBatPosition(int newYCoord)
{
location.Y = newYCoord;
}
GameManager.cs
public void UpdateAIBat(int currentAIPosition)
{
if (gameBall.Location.Y >= aiBat.Location.Y)
{
currentAIPosition += SPEED_CONSTANT;
}
else if (gameBall.Location.Y <= aiBat.Location.Y)
{
currentAIPosition -= gameBall.Direction.Y;
}
}
public void GameLoop()
{
UpdateAIBat(aiBat.Location.Y);
}
The bat doesn’t move at all. What am I doing wrong with the AI movement logic?