碰撞后拆除障碍物?

时间:2014-10-08 06:29:19

标签: c# collision-detection

我正在制作游戏(Breakout),我有一个问题,

问题是我怎样才能在击球后消除障碍? 当然,球之后必须能够通过赛道(如一般的突破游戏)

接下来的问题是,我可以在运行时模式中设置障碍吗?

由于

picture

private void timer1_Tick(object sender, EventArgs e)
    {
        ball.Top += step;
        ball.Left += stepleft;

        //board simulate collision
        bool collisonX = ball.Location.X + ball.Width > board.Location.X && ball.Location.X < board.Location.X + board.Width;
        bool collisonY = ball.Top + ball.Height == board.Location.Y || ball.Top + ball.Height - 1 == board.Location.Y;

        //board2(button1) simulate collision
        bool collisonX2 = ball.Location.X + ball.Width > board2.Location.X && ball.Location.X < board2.Location.X + board2.Width;
        bool collisonY2 = ball.Top + ball.Height == board2.Location.Y || ball.Top + ball.Height - 1 == board2.Location.Y;

        //Collision the ball with under buttons 
        bool collsionButtonY = ball.Top - ball.Height == board2.Location.Y || ball.Top - ball.Height == board2.Location.Y - 1;

        //collision leftwall 
        bool leftWall = ball.Left == 0 || ball.Left == -1 || ball.Left == 1;
        //collision rightwall 
        bool topWall = ball.Top == 0 || ball.Top == -1 || ball.Top == 1;

        bool bottomWall = collisonX && collisonY;
        bool toppWall = collisonX2 && collisonY2;

        //collision 
        bool barrier = collisonX2 && collsionButtonY; 

        bool collisionLeft = ((ball.Location.Y + ball.Height >= board2.Location.Y) && (ball.Location.Y <= board2.Location.Y + board2.Height) && (ball.Location.X + ball.Width >= board2.Location.X) && (ball.Location.X <= board2.Location.X + board2.Height));

        //rightwall
        bool rightWall = ball.Left + ball.Width == this.ClientSize.Width || ball.Left + ball.Width == this.ClientSize.Width - 1;
        // sidewall = collision rightwall or leftwall 
        bool sideWall = leftWall || rightWall;

        //Check the ball hit the ground 
        bool check = ball.Top + ball.Height < this.ClientSize.Height;

        //if topWall true,This means that the ball is hit to the topwall
        if (topWall)
        {
            flagBottom = false;
            flagTop = true;
            if (stepleft > 0)
            {
                step = 2;
            }
            else if (stepleft < 0)
            {
                step = 2;
            }
        }
        //if bottomWall true,This means that the ball is hit to the board
        else if (bottomWall)
        {
            flagBottom = true;
            flagTop = false;
            if (stepleft > 0)
            {
                step = step * -1;
            }
            else if (stepleft < 0)
            {
                step = step * -1;
            }
        }
        //if barrier true and flagbottom true,This means that the ball is hit to the board2(button1)
        else if (barrier && flagBottom)
        {
            collisionLeft = false;
            if (stepleft > 0)
            {
                step = step * -1;
            }
            else if (stepleft < 0)
            {
                step = step * -1;
            }
        }
        //if toppWall true and flagTop true,This means that the ball is hit to The top button is hit 
        else if (toppWall && flagTop)
        {
            collisionLeft = false;
            if (stepleft > 0)
            {
                step = step * -1;
            }
            else if (stepleft < 0)
            {
                step = step * -1;
            }
        }
        else if (flagTop && collisionLeft)
        {
            barrier = false;
            if (stepleft > 0)
            {
                stepleft = -2;
                step = 2;
            }
            else if (stepleft < 0)
            {
                stepleft = 2;
                step = 2;
            }
        }
        else if (flagBottom && collisionLeft)
        {
            barrier = false;
            if (stepleft > 0)
            {
                stepleft = -2;
                step = -2;
            }
            else if (stepleft < 0)
            {
                stepleft = 2;
                step = -2;
            }
        }
        else if (sideWall)
        {
            //if leftwall true,This means that the ball is hit to the left side wall
            if (leftWall)
            {
                if (flagTop)
                {
                    stepleft = 2;
                }
                else if (flagBottom)
                {
                    stepleft = 2;
                }
            }
            //if rightWall true,This means that the ball is hit to the left side wall
            else if (rightWall)
            {
                if (flagTop)
                {
                    stepleft = -2;
                }
                else if (flagBottom)
                {
                    stepleft = -2;
                }
            }
        }
        //check if ckeck==ture,this mean the ball is hit the ground
        else if (!check)
        {
            timer1.Enabled = false;
        }
    }

    private void board_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private void board_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            board.Left = e.X + board.Left - MouseDownLocation.X;
        }

1 个答案:

答案 0 :(得分:1)

您需要学习使用列表和正确使用类。

例如:

public class GameObject{

  public GameObject(int x, int y, int width, int height){
     this.X = x;
     this.Y = y;
     this.Width = width;
     this.Height = height;
  }

  int X;
  int Y;
  int Width;
  int Height;

  public bool DetectCollision(bool Ball){
     //code to detect collision
  }
}

然后在你的主课堂上,你可以做那样的事情。

List<GameObject> gameObjects = new List<GameObject>();
gameObjects.add(new GameObject(10,10,50,50));
gameObjects.add(new GameObject(20,10,20,50));
gameObjects.add(new GameObject(30,10,50,70));
gameObjects.add(new GameObject(40,10,90,50));

并检测碰撞:

foreach (GameObject gameObject in gameObjects){
   if (gameObject.DetectCollision(ball)){
      //do something
   }
}