砖块碰撞

Breakout Brick Collision

我真的需要一些帮助来让碰撞检测与我的 breakout 克隆中的积木一起工作。我花了几个小时阅读不同网站上的答案,但大多数似乎都是用我不太熟悉的其他编码语言编写的,也不确定如何翻译它们。

我在墙壁、球拍和球上进行了碰撞,但我无法让它与 bricks.I 一起工作我不确定您是否需要查看所有代码来自不同的 类,但这里是砖块、球和主要形式 类。希望我格式化正确哈哈。如有任何帮助或建议,我们将不胜感激。

变砖Class:

    class BrickTest2
    {
      public int x {get; set;}
      public int y {get; set;}
      public int brWidth { get; set; }
      public int brHeight { get; set; }
      public static int rows = 4;
      public static int columns = 10;

      public Image brickTest2Img;

      List<Rectangle> brick = new List<Rectangle>();

      public Rectangle brickRect2;

      public Rectangle BrickRect2
      {
        get { return brickRect2; }
      }

      public BrickTest2()
      {
        x = 0;
        y = 0;
        brWidth = 32;
        brHeight = 12;

        brickTest2Img = Breakout.Properties.Resources.brickRed;
        brickRect2 = new Rectangle(x, y, brWidth, brHeight);

      }

      public void drawBrickTest2(Graphics paper)
      {

        for (int c = 0; c < columns; c++)
            for (int r = 0; r < rows; r++)
            {
               Rectangle brickRect2 = new Rectangle(x + c * brWidth, y + r * brHeight, brWidth, brHeight);
                brick.Add(brickRect2);

                paper.DrawImage(brickTest2Img, brickRect2);

               switch(r)
                {
                    case 0:
                        brickTest2Img = Breakout.Properties.Resources.brickRed;
                        break;
                    case 1:
                        brickTest2Img = Breakout.Properties.Resources.BrickYellow;
                        break;
                    case 2:
                        brickTest2Img = Breakout.Properties.Resources.BrickGreen;
                        break;
                    case 3:
                        brickTest2Img = Breakout.Properties.Resources.BrickBlue;
                        break;
                   default:
                        break;
                }
            }
     }
   }
 }

球Class:

    public class Ball
    {
      public int x, y, width, height;
      public int xSpeed, ySpeed;
      private Random randSpeed;

      private Image ballImage;
      private Rectangle ballRec;

      public Rectangle BallRec
      {
        get { return ballRec; }
      }

      public Ball()
      {
        randSpeed = new Random();
        x = 130;
        y = 130;
        width = 8;
        height = 8;

        xSpeed = randSpeed.Next(5, 7);
        ySpeed = randSpeed.Next(5, 7);

        ballImage = Breakout.Properties.Resources.ball2;

        ballRec = new Rectangle(x, y, width, height);
      }

      public void drawBall(Graphics paper)
      {
        paper.DrawImage(ballImage, ballRec);

      }

      public void moveBall()
      {
        ballRec.X += xSpeed;
        ballRec.Y += ySpeed;
      }

      public void collideWallBall()
      {
        if (ballRec.X < 0 || ballRec.X > 270)
        {
            xSpeed *= -1;
        }

        if (ballRec.Y < 0)
        {
            ySpeed *= -1;
        }

      }

      public void BallDead()
      {
        if (ballRec.Y > 260)
        {
           // ySpeed *= -1; for testing
            Settings.gmOver = true;
        }
      }

      public void collidePaddleBall(Rectangle paddleRec)
      {
        if (paddleRec.IntersectsWith(ballRec))
        {
            ySpeed *= -1;
        }
      }


      public void collideBrickBall(Rectangle brickRec2)
      {
        if (brickRec2.IntersectsWith(ballRec))
        {
            // desperate attempt to get it to work
            if(brickRec2.X == ballRec.X)
            {
                ySpeed *= -1;
            }
        }
      }

   }
 }

主窗体:

public partial class Form1 : Form
{

    // initiallizes class files
    Graphics paper;
    Paddle gmPaddle = new Paddle();
    Ball gmBall = new Ball();
    BrickTest2 gmBrickTest = new BrickTest2();

    public Form1()
    {

        // initializes
        InitializeComponent();
        GameStart();

    }


    private void GameStart()
    {

        // sets up a new game
        new Settings();
        Settings.gmOver = false;
        lblGameOver1.Visible = false;
        lblGameOver2.Visible = false;
        Cursor.Hide();


    }


    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        paper = e.Graphics;

        if (Settings.gmOver == true)
        {
            // once game is over, stops drawing object and brings up game over message
            string gameOver1 = "Game over!";
            string gameOver2 = "Press Enter to try again";
            lblGameOver1.Text = gameOver1;
            lblGameOver2.Text = gameOver2;
            lblGameOver1.Visible = true;
            lblGameOver2.Visible = true;
        }

        if(Settings.gmOver != true)
        {
            // draws objects if game is not over
            gmPaddle.drawPaddle(paper);
            gmBall.drawBall(paper);
            gmBrickTest.drawBrickTest2(paper);

        } 
    }


    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        // calls mouse movement for the paddle
        gmPaddle.movePaddle(e.X);
        this.Invalidate();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        //updates collision
        gmBall.moveBall();
        gmBall.collideWallBall();
        gmBall.collidePaddleBall(gmPaddle.PaddleRec);
        gmBall.BallDead();
        gmBall.collideBrickBall(gmBrickTest.BrickRect2);

        this.Invalidate();
    }



    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        // restarts game, still needs work
        GameStart();

    }


 }

}

查看您发布的代码后,我建议验证您是否正确使用 brickRect2 变量。

在class构造函数中你总是赋值brickRect2 = new Rectangle(x, y, brWidth, brHeight);,所以它总是(0, 0, 32, 12)

然后,在drawBrickTest2(Graphics paper)方法中你不直接绘制矩形,而是根据行和列变量移动它:Rectangle brickRect2 = new Rectangle(x + c * brWidth, y + r * brHeight, brWidth, brHeight);,所以这里你只使用局部与 Your class.

中的字段同名的变量

我看不到您调用 collideBrickBall(Rectangle brickRec2) 方法的代码,但我想您可以尝试使用 class 中的字段,它始终是 (0, 0, 32, 12)

如果是这种情况,您可以使用以下几种方法来解决此问题:

  • 将适当的矩形坐标分配给 brickRect2 字段而不是在绘制时移动它
  • 不要使用IntersectWith(Rectangle rect)方法并手动进行碰撞比较,考虑到坐标偏移