当比赛开始时,球将使用此代码完美地从边缘反弹:
if (Is_hit == true && ballFired == true )
{
//collision here
ballXPos = 960 - ballimg.Width - (ballXPos - 960 + ballimg.Width);
ballXDir = +1;
ballYDir = -1;
}
if (Ballvisible == true)
{
if (Keyboard.GetState().IsKeyDown(Keys.Space) && ballFired == false)
{
ballFired = true;
}
if (ballFired == true)
{
ballXPos = ballXPos - (ballSpeed * ballXDir);
ballYPos = ballYPos + (ballSpeed * ballYDir);
}
if (ballXPos < 0)
{
ballXPos = -ballXPos;
ballXDir = -1;
}
if (ballXPos > 960 - ballimg.Width)
{
ballXPos = 960 - ballimg.Width - (ballXPos - 960 + ballimg.Width);
ballXDir = +1;
}
if (ballYPos < 0)
{
ballYPos = +ballYPos;
ballYDir = +1;
}
if (ballYPos > 700 - ballimg.Height)
{
Ballvisible = false;
}
}
if (Ballvisible == false)
{
ballSpeed = 3;
ballXDir = -1;
ballYDir = -1;
ballXPos = 450;
ballYPos = 590;
ballFired = false;
Ballvisible = true;
}
}
但是一旦发生碰撞,球会成功地从另一个精灵(玩家球拍)反弹,但是程序似乎忽略了这个代码并且球将直接离开屏幕。
碰撞代码:
// paddle collison detection
if
( gameball.PositionRectangle_ball.Intersects(paddle.PositionRectangle_paddle))
{
collision_paddle = true;
gameball.CheckCollision(collision_paddle); // passes the collision bool value
}
答案 0 :(得分:0)
您似乎忘了清除Is_hit
标志。
如果您不需要在其他任何地方查看它,可以在发布的代码的第一个块中清除它:
if (Is_hit == true && ballFired == true )
{
//collision here
ballXPos = 960 - ballimg.Width - (ballXPos - 960 + ballimg.Width);
ballXDir = +1;
ballYDir = -1;
Is_hit = false;
}