Beispiel #1
0
 public void Deflection(Brick brick)
 {
     if (!collided)
     {
         motion.Y *= -1;
         collided = true;
     }
 }
Beispiel #2
0
        /// <summary>
        /// ��ʼ��һ��2ά���飬����ש����������
        /// </summary>
        /// <param name="brickArray">��Ҫ������������</param>
        /// <param name="columns">ש������</param>
        /// <param name="rows">ש������</param>
        /// <param name="brickHeight">ÿ��ש��ĸ߶� (����)</param>
        /// <param name="brickPadding">ש���϶ (����)</param>
        /// <param name="borderPadding">���ÿ��ש��ı� (����)</param>
        private void InitializeBricks(Brick[,] brickArray, int columns, int rows, int brickHeight, int brickPadding, int borderPadding, int level)
        {
            int brickWidth = ((this.Width - borderPadding * 2) / brickColumns) - brickPadding;//���ש����
            int columnWidth = ((this.Width - borderPadding * 2) / brickColumns);
            int rowHeight = brickHeight + brickPadding;

            Random randy = new Random();
            for (int j = 0; j < brickRows; j++)
            {
                for (int i = 0; i < brickColumns; i++)
                {
                    int x = borderPadding + i * columnWidth;
                    int y = (j * rowHeight) + borderPadding;
                    brickArray[i, j] = new Brick(x, y, brickWidth, brickHeight, randy, level); //����һ�����ש��
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// �����������ש���Ƿ�����ײ�������ж���ķ����Ƿ���Ҫ��ת
        /// </summary>
        /// <param name="ball">��Ҫ������</param>
        /// <param name="brick">��Ҫ����ש��</param>
        /// <param name="hit">true���������ײ</param>
        /// <param name="reverseX">��ֵΪtrueʱ��X�����ٶ���Ҫ����ת</param>
        /// <param name="reverseY">��ֵΪtrueʱ��Y�����ٶ���Ҫ����ת</param>
        private void DetectBrickCollision(Ball ball, Brick brick, out bool hit, out bool reverseX, out bool reverseY)
        {
            reverseX = false; //ÿ��ש��ֻ�ܷ�����һ��
            reverseY = false;
            hit = false;
            //����ٶȲ��ܳ�����ֱ������������޷���������������ֱ�Ӵ�Խש��

            //�����ƶ�ʱ����ײ�ж�
            if ((ball.YSpeed < 0) //�ж����Ƿ��������ƶ�
            && (ball.Top + ball.YSpeed < brick.Bottom) //�����ƶ�ʱ������ϱ߽�����ٶ����С��ש���±߽������±߽�����ٶȴ���ש���±߽磬��Y��������ײ
            && (ball.Bottom + ball.YSpeed > brick.Bottom)
            && (ball.Right + ball.XSpeed >= brick.Left) //��
            && (ball.Left + ball.XSpeed <= brick.Right)) //��
            {
                hit = true;
                reverseY = true;

            }

            //�����ƶ�ʱ����ײ�ж�
            if ((ball.YSpeed > 0) //�ж����Ƿ��������ƶ�
            && (ball.Bottom + ball.YSpeed > brick.Top) //ͬ�ϣ�������ײ�ж�
            && (ball.Top + ball.YSpeed < brick.Top)
            && (ball.Right + ball.XSpeed >= brick.Left)
            && (ball.Left + ball.XSpeed <= brick.Right))
            {
                hit = true;
                reverseY = true;
            }

            //�����ƶ�ʱ����ײ�ж�
            if ((ball.XSpeed > 0) //�ж����Ƿ��������ƶ�
               && (ball.Left + ball.XSpeed < brick.Left) //ͬ�ϣ�������ײ�ж�
               && (ball.Right + ball.XSpeed > brick.Left)
               && (ball.Bottom + ball.YSpeed >= brick.Top)
               && (ball.Top + ball.YSpeed <= brick.Bottom))
            {
                hit = true;
                reverseX = true;

            }

            //�����ƶ�ʱ����ײ�ж�
            if ((ball.XSpeed < 0) //�ж����Ƿ��������ƶ�
                && (ball.Right + ball.XSpeed > brick.Right) //ͬ�ϣ�������ײ�ж�
                && (ball.Left + ball.XSpeed < brick.Right)
                && (ball.Bottom + ball.YSpeed >= brick.Top)
                && (ball.Top + ball.YSpeed <= brick.Bottom))
            {
                hit = true;
                reverseX = true;
            }

            if (hit && ball.FromPaddle)
                bulletList.RemoveAt(bulletList.IndexOf(ball));
        }
Beispiel #4
0
        /// <summary>
        /// �����������ש�����ײ�Ƿ���������������ж����ש������û���������
        /// </summary>
        /// <param name="ball">Ҫ������</param>
        /// <param name="bricks">Ҫ�����Ƿ��е��ߵ�ש��</param>
        private void CheckBricks(Ball ball, Brick[,] bricks)
        {
            bool ultimatelyReverseX = false; //����������ȷ����ֻ����һ��ʱ���ڱ���תһ��
            bool ultimatelyReverseY = false; //���������ֱ���ת����ֱ�Ӵ���ש������

            foreach (Brick brick in bricks)
            {
                bool hit, reverseX, reverseY;
                if (brick.Strength > 0) //ש��û������ʱ����Ƿ�����ײ
                {
                    //ִ����ײ���
                    DetectBrickCollision(ball, brick, out hit, out reverseX, out reverseY);
                    if (hit)
                    {
                        brick.Hit(); //��ש���һ��Ѫ
                        if (brick.Strength == 0)//ש��Ѫ��Ϊ0��ʱ��Ӧ����ʧ
                        {
                            CheckBrickForPowerUps(brick); //���ש�����Ƿ����������
                            brickCount--; //��ש������1
                        }
                        score += level * brick.CalculateScore();
                        brickSound.Play();
                        if (reverseX) ultimatelyReverseX = true;
                        if (reverseY) ultimatelyReverseY = true;

                    }
                }
            }
            if (ultimatelyReverseY) ball.ReverseY();
            if (ultimatelyReverseX) ball.ReverseX();
        }
Beispiel #5
0
        /// <summary>
        /// ����ײ����ã����ש�����Ƿ����������
        /// ����У����߽��ᱻ������Ϸ�е�list powerup
        /// </summary>
        /// <param name="brick">��Ҫ����ש��</param>
        private void CheckBrickForPowerUps(Brick brick)
        {
            if (brick.powerBall) //�������������Ч
            {
                int radius = 7; //�����ɵ���İ뾶�������
                ballList.Add(new Ball(Brushes.White, radius, brick.Centre.X, brick.Centre.Y, false));
                //��������ש����������ɵ�
            }

            //�������Ҫ������������Ч
            string powerUpLabel = null;
            Brush powerUpBrush = null;

            if (brick.powerShrink)
            {
                powerUpLabel = "����";
                powerUpBrush = Brushes.LightBlue;
            }
            if (brick.powerStretch)
            {
                powerUpLabel = "�쳤";
                powerUpBrush = Brushes.LightGreen;
            }
            if (brick.powerMulti)
            {
                powerUpLabel = "������";
                powerUpBrush = Brushes.Pink;
            }

            if (brick.powerBullet)
            {
                powerUpLabel = "�ӵ�";
                powerUpBrush = Brushes.Red;
            }
            if (powerUpLabel != null)
            {
                PowerUp powerup = new PowerUp(powerUpLabel, powerUpBrush, brick.Centre.X, brick.Centre.Y);
                powerUpList.Add(powerup);
            } //ֻ��ש������������ߵ�����²�����������
        }
Beispiel #6
0
        /// <summary>
        /// Called after a collision. Checks the brick to see if it had any powerups inside it
        /// If it does, the powerups are added to the list of powerups in play
        /// </summary>
        /// <param name="brick">The brick to check for powerups</param>
        private void CheckBrickForPowerUps(Brick brick)
        {
            if (brick.powerBall) //extra ball instantly takes effect
            {
                Random randy = new Random();
                int radius = randy.Next(3, 8); //new ball has a random radius
                ballList.Add(new Ball(Brushes.Black, radius, brick.Centre.X, brick.Centre.Y, false));
                //new ball is created centred at the centre of the old brick
            }

            //these powerups have to be caught by the paddle
            string powerUpLabel = null;
            Brush powerUpBrush = null;

            if (brick.powerShrink)
            {
                powerUpLabel = "Shrink";
                powerUpBrush = Brushes.LightBlue;
            }
            if (brick.powerStretch)
            {
                powerUpLabel = "Stretch";
                powerUpBrush = Brushes.LightGreen;
            }
            if (brick.powerMulti)
            {
                powerUpLabel = "Multi";
                powerUpBrush = Brushes.Pink;
            }

            if (powerUpLabel != null)
            {
                PowerUp powerup = new PowerUp(powerUpLabel, powerUpBrush, brick.Centre.X, brick.Centre.Y);
                powerUpList.Add(powerup);
            } //only add the powerup if a powerup is found in the brick
        }
Beispiel #7
0
        /// <summary>
        /// Given a ball and brick, checks if they collide, and whether the direction of the ball should be reversed
        /// </summary>
        /// <param name="ball">Ball object to check for collision</param>
        /// <param name="brick">Brick object to check for collision</param>
        /// <param name="hit">True if the ball collides with the brick</param>
        /// <param name="reverseX">True if the ball's x-direction should be reversed</param>
        /// <param name="reverseY">True if the ball's y-direction should be reversed</param>
        private void DetectBrickCollision(Ball ball, Brick brick, out bool hit, out bool reverseX, out bool reverseY)
        {
            reverseX = false; //each brick can only reverse the ball once
            reverseY = false;
            hit = false;
            //note the velocity of the ball cannot exceed its diameter - or this will not work
            //the ball could sail right through the brick

                //upwards collision
                if ((ball.YSpeed < 0) //is the ball travelling upwards?
                && (ball.Top + ball.YSpeed < brick.Bottom) //is the top of the ball inside the brick?
                && (ball.Bottom + ball.YSpeed > brick.Bottom)  //is the bottom of the ball under the brick?
                && (ball.Right + ball.XSpeed >= brick.Left) //is the right of the ball inside the brick
                && (ball.Left + ball.XSpeed <= brick.Right)) //is the left of the ball inside the brick
                {
                    hit = true;
                    reverseY = true;
                }

                //downwards collision
                if ((ball.YSpeed > 0) //is the ball travelling downwards?
                && (ball.Bottom + ball.YSpeed > brick.Top) //is the bottom of the ball inside the brick (y)
                && (ball.Top + ball.YSpeed < brick.Top) // is the top of the ball about the brick? (y)
                && (ball.Right + ball.XSpeed >= brick.Left)
                && (ball.Left + ball.XSpeed <= brick.Right))// do the x-coords match?
                {
                    hit = true;
                    reverseY = true;
                }

               //rightwards collision
               if ((ball.XSpeed > 0) //is the ball moving right
               && (ball.Left + ball.XSpeed < brick.Left) //is the left side of the ball to the left of the brick?
               && (ball.Right + ball.XSpeed > brick.Left) //is the right side of the ball inside the brick?
               && (ball.Bottom + ball.YSpeed >= brick.Top)
               && (ball.Top + ball.YSpeed <= brick.Bottom)) // do the y-coords match?
                {
                    hit = true;
                    reverseX = true;
                }

                //leftwards collision
                if ((ball.XSpeed < 0) //is the ball moving Left?
                    && (ball.Right + ball.XSpeed > brick.Right) //is the right side of the ball to the right of the brick?
                    && (ball.Left + ball.XSpeed < brick.Right) //is the left side of the ball inside the brick
                    && (ball.Bottom + ball.YSpeed >= brick.Top)
                    && (ball.Top + ball.YSpeed <= brick.Bottom))// do the y-coords match?
                {
                    hit = true;
                    reverseX = true;
                }
        }
Beispiel #8
0
 /// <summary>
 /// Given a ball and brick, checks them for collision
 /// if they collide, checks if the brick had powerups inside it
 /// </summary>
 /// <param name="ball">Ball object to check for collision</param>
 /// <param name="bricks">Brick object to check for powerups</param>
 private void CheckBricks(Ball ball, Brick[,] bricks)
 {
     bool ultimatelyReverseX = false; //these variables exist so that the ball can only be reversed
     bool ultimatelyReverseY = false; //once per timer tick, otherwise sometimes the ball can be reversed twice
                                 //and plough right through bricks
     foreach (Brick brick in bricks)
     {
         bool hit, reverseX, reverseY;
         if (brick.Strength > 0) //only detect collision if the brick hasn't been hit
         {
             //perform collision detection
             DetectBrickCollision(ball, brick, out hit, out reverseX, out reverseY);
             if (hit)
             {
                 brick.Hit(); //subtract the strength
                 if (brick.Strength == 0)
                 {
                     CheckBrickForPowerUps(brick); //check if the brick had any powerups
                     brickCount--; //update the new brickcount
                     }
                 score += level * brick.CalculateScore();
                 brickSound.Play();
                 if (reverseX) ultimatelyReverseX = true;
                 if (reverseY) ultimatelyReverseY = true;
             }
         }
     }
     if (ultimatelyReverseY) ball.ReverseY();
     if (ultimatelyReverseX) ball.ReverseX();
 }
Beispiel #9
0
        private void StartGame()
        {
            paddle.SetInStartPosition();
            ball.SetInStartPosition(paddle.GetBounds());

            bricks = new Brick[bricksWide, bricksHigh];

            for (int y = 0; y < bricksHigh; y++)
            {
                Color tint = Color.White;

                switch (y)
                {
                    case 0:
                        tint = Color.Blue;
                        break;
                    case 1:
                        tint = Color.Red;
                        break;
                    case 2:
                        tint = Color.Green;
                        break;
                    case 3:
                        tint = Color.Yellow;
                        break;
                    case 4:
                        tint = Color.Purple;
                        break;
                }

                for (int x = 0; x < bricksWide; x++)
                {
                    bricks[x, y] = new Brick(
                        brickImage,
                        new Rectangle(
                            x * brickImage.Width,
                            y * brickImage.Height,
                            brickImage.Width,
                            brickImage.Height),
                            tint);
                }
            }
        }