Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 碰撞后移除障碍物?_C#_Collision Detection - Fatal编程技术网

C# 碰撞后移除障碍物?

C# 碰撞后移除障碍物?,c#,collision-detection,C#,Collision Detection,我正在进行一场比赛(突破),我有一个问题 问题是,在障碍物被球击中后,我如何移除障碍物? 当然,在那之后球必须能够穿过跑道(就像一般的突破赛) 下一个问题是,我可以在运行时模式下设置障碍吗 谢谢 private void timer1_Tick(object sender, EventArgs e) { ball.Top += step; ball.Left += stepleft; //board simulate collision

我正在进行一场比赛(突破),我有一个问题


问题是,在障碍物被球击中后,我如何移除障碍物? 当然,在那之后球必须能够穿过跑道(就像一般的突破赛)

下一个问题是,我可以在运行时模式下设置障碍吗

谢谢

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;
        }

private void timer1\u勾选(对象发送方,事件参数e)
{
球。顶部+=台阶;
ball.Left+=步左;
//板模拟碰撞
bool collisonX=ball.Location.X+ball.Width>board.Location.X&&ball.Location.Xboard2.Location.X&&ball.Location.X=board2.Location.Y)和&(ball.Location.Y=board2.Location.X)和&(ball.Location.X 0)
{
步骤=2;
}
else if(单步左<0)
{
步骤=2;
}
}
//如果bottomWall为真,这意味着球被打到了板上
else if(底壁)
{
flagBottom=true;
flagTop=false;
如果(单步左>0)
{
步骤=步骤*-1;
}
else if(单步左<0)
{
步骤=步骤*-1;
}
}
//如果barrier为true,flagbottom为true,则表示球被击中板2(按钮1)
否则,如果(屏障和标志底部)
{
collisionLeft=false;
如果(单步左>0)
{
步骤=步骤*-1;
}
else if(单步左<0)
{
步骤=步骤*-1;
}
}
//如果toppWall为true且flagTop为true,则表示球被击中顶部按钮被击中
else if(顶墙和旗杆顶)
{
collisionLeft=false;
如果(单步左>0)
{
步骤=步骤*-1;
}
else if(单步左<0)
{
步骤=步骤*-1;
}
}
else if(flagTop&&collisionLeft)
{
屏障=假;
如果(单步左>0)
{
stepleft=-2;
步骤=2;
}
else if(单步左<0)
{
stepleft=2;
步骤=2;
}
}
else if(flagBottom&&collisionLeft)
{
屏障=假;
如果(单步左>0)
{
stepleft=-2;
步骤=-2;
}
else if(单步左<0)
{
stepleft=2;
步骤=-2;
}
}
否则,如果(侧壁)
{
//如果leftwall为true,则表示球击中左侧墙
if(左墙)
{
if(旗杆顶部)
{
stepleft=2;
}
else if(旗杆底部)
{
stepleft=2;
}
}
//如果rightWall为true,则表示球击中左侧墙
else if(右墙)
{
if(旗杆顶部)
{
stepleft=-2;
}
else if(旗杆底部)
{
stepleft=-2;
}
}
}
//检查ckeck==true,这意味着球落地了
否则如果(!检查)
{
timer1.Enabled=false;
}
}
专用无效板\u鼠标向下(对象发送器,鼠标指针e)
{
if(e.Button==MouseButtons.Left)
{
MouseDownLocation=e.位置;
}
}
专用无效板\u鼠标移动(对象发送器,鼠标目标e)
{
if(e.Button==MouseButtons.Left)
{
board.Left=e.X+board.Left-鼠标向下位置.X;
}

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

例如:

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));

如果我们只知道球、板、板2等是什么东西,这会有帮助。问题是,在被球击中后,我如何才能移除障碍物?当然,球必须能够在那之后穿过跑道(就像一般的突破赛)
foreach (GameObject gameObject in gameObjects){
   if (gameObject.DetectCollision(ball)){
      //do something
   }
}