问题在于碰撞方法,球确实与蝙蝠发生碰撞但发生的事情是......它只会从蝙蝠的下方碰撞到图片框的下边缘。我希望相反的事情发生,所以当球从图片框的顶部掉落时,它会从球棒上反弹回到图片框的顶部。任何帮助将不胜感激,任何问题请询问,谢谢你提前。
如果您需要更多,请仅发布相关代码,请询问。
private Graphics paper;
private SolidBrush brush, brushBlue, brushGreen, brushRed,
brushBlk, brushYellow;
private int rectX, rectY;
private int x, y, xDirection, yDirection, xBat;
private int playCount, brushSize;
private Random randomNumber;
private bool btnDisplayBricksClicked;
private Rectangle ball;
private Rectangle bat;
public Form1()
{
InitializeComponent();
paper = picBox.CreateGraphics();
brush = new SolidBrush(Color.Red);
randomNumber = new Random();
btnDisplayBricksClicked = false;
}
private void MoveBall()
{
timer1.Interval = randomNumber.Next(50, 100);
timer1.Enabled = true;
x = x + xDirection;
y = y + yDirection;
if (x >= picBox.Width)
xDirection = -xDirection;
if (y >= picBox.Height)
yDirection = -yDirection;
if (x <= 0)
xDirection = -xDirection;
if (y <= 0)
yDirection = -yDirection;
}
private void DrawBall()
{
ball = new Rectangle(x, y, brushSize, brushSize);
brushSize = 15;
paper.FillEllipse(brush, ball);
}
private void DrawBat(Graphics paper)
{
brushBlue = new SolidBrush(Color.Blue);
rectX = xBat - 25;
rectY = picBox.Height - 30;
bat = new Rectangle(rectX, rectY, 50, 20);
paper.FillRectangle(brushBlue, bat);
}
private void CheckCollision()
{
if (ball.IntersectsWith(bat))
{
yDirection = -yDirection; //problems here
}
}
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
CheckCollision();
if (btnDisplayBricksClicked)
{
DrawBricks(paper);
}
}