Pad#C#AI

时间:2015-09-16 12:33:26

标签: c# artificial-intelligence

我无法弄清楚如何让计算机的拨片移动。我用鼠标设置了播放器的拨片。这是我用鼠标移动桨的原因。如何让计算机的球拍移动到球的哪个位置(计算机可能会丢失)?干杯!

Form.Cs

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        controller.MoveLeftPaddle(e.Location.Y);
    }

Controller.Cs

    public void MoveLeftPaddle(int newPlayerPosition)   
    {
        paddlePlayer.MovePlayerPaddle(newPlayerPosition);
    }

Paddle.CS

    public void MovePlayerPaddle(int newYPosition)
    {
        position.Y = newYPosition;   
    }

现在,我有这个代码,我试图让计算机的移动。

Paddle.CS

    public void MoveComputerPaddle(int newY2Position)
    {
        position.Y = newY2Position;   
    }

Controller.Cs

    public void MoveRightPaddle(int newComputerPosition)
    {
        if (ball.Position.Y >= paddleComputer.Position.Y)
        {
            newComputerPosition += BALLSPEED;
        }
        else if (ball.Position.Y <= paddleComputer.Position.Y)
        {
            newComputerPosition -= ball.Velocity.Y;
        }
    }

    public void Run()
    {
       MoveRightPaddle(paddleComputer.Position.Y);    
    } //im not really sure about this part. this is the only way that I didnt get squiggly line.  

然后我在Controller.cs中有一个方法来让球移动,弹跳和绘制。我也用它来绘制方法Run()内部的paddle。上面的代码

2 个答案:

答案 0 :(得分:1)

理想情况下,你的计算机和播放器的拨片是相同的,每个你都有一个单独的控制器 - PlayerController将接受用户输入并根据它调用MovePaddle,计算机控制器将检查球的位置以决定如何调用MovePaddle。

您可以让ComputerController存储前一个X帧的位置,只需选择用于决策的数据的大小,就可以调整难度。

答案 1 :(得分:0)

由于 500 - 内部服务器错误已在评论部分中指出,您只是更改传入参数的值(在本例中为newComputerPosition),而不是paddleComputer.Position.Y

我认为MoveRightPaddle根本不需要任何参数:

public void MoveRightPaddle()
{
    if (ball.Position.Y >= paddleComputer.Position.Y)
    {
        paddleComputer.Position.Y += BALLSPEED; // I don't know why this uses a different value from the else statement
    }
    else 
    {
        paddleComputer.Position.Y -= ball.Velocity.Y;
    }
}

public void Run()
{
   MoveRightPaddle();    
}

但是,如果您直接调用这些变量,我感觉某处存在代码味道,因此我建议您研究解决此问题的方法。

如何更好地移动

目前,无论球是否朝向球,你的右桨将继续跟随球,或者球是否会超出界限。

如果你知道球的x direction,你可以快速断言右桨是否需要移动。

例如:

 if (ball_x_direction > 0) // because for the ball to go right, the direction needs to be positive
 {
     // move paddle here:
 }

用于检查拨片是否将离开屏幕(仅显示屏幕顶部(即MoveRightPaddle的第二个条件)):

if (paddleComputer.Position.Y - ball.Velocity.Y < 0)
    paddleComputer.Position.Y = 0;
} else {
    paddleComputer.Position.Y -= ball.Velocity.Y;
}
相关问题