我正在制作Pong游戏,我遇到了一个问题.当球(一个矩形)与球拍下方或上方的球拍(或球棒)碰撞时,我得到一个奇怪的错误,球进入矩形并左右 - 左 - 右达到高速(因为我添加了)加速)并在对面跳出来.我知道为什么会发生这个错误:
if (ballrec.Intersects(player1rec)
&& ball.x <= 20
&& ball.y + 20 >= player.y
&& ball.y <= player.y + 100) //checks the front rebound-here's the bug
{
ball.vx *= -1; //changes x-direction
if (ball.vx < 0)
ball.vx -= 1; //increases x-velocity
effect.Play();
if (R.Next(4) == 0)
{
if (ball.vy < 0) ball.vy--;
else ball.vy++; //increases y-velocity on a special occasion
}
}
else
{
if (ballrec.Intersects(player1rec))
{
ball.vy *= -1;
effect.Play();
}
}
Run Code Online (Sandbox Code Playgroud)
ball.vy = velocity y-axis:我将它乘以-1以改变方向 …