Beispiel #1
0
        public bool Move(Wall wall, Paddle paddle)
        {
            if (Visible == false)
            {
                return(false);
            }
            X = X + XVelocity;
            Y = Y + YVelocity;

            //check for wall hits
            if (X < 1)
            {
                X         = 1;
                XVelocity = XVelocity * -1;
                PlaySound(gameContent.wallBounceSound);
            }
            if (X > ScreenWidth - Width + 5)
            {
                X         = ScreenWidth - Width + 5;
                XVelocity = XVelocity * -1;
                PlaySound(gameContent.wallBounceSound);
            }
            if (Y < 1)
            {
                Y         = 1;
                YVelocity = YVelocity * -1;
                PlaySound(gameContent.wallBounceSound);
            }
            if (Y + Height > ScreenHeight)
            {
                Visible = false;
                Y       = 0;
                PlaySound(gameContent.missSound);
                return(false);
            }
            //check for paddle hit
            //paddle is 70 pixels. we'll logically divide it into segments that will determine the angle of the bounce

            Rectangle paddleRect = new Rectangle((int)paddle.X, (int)paddle.Y, (int)paddle.Width, (int)paddle.Height);
            Rectangle ballRect   = new Rectangle((int)X, (int)Y, (int)Width, (int)Height);

            if (HitTest(paddleRect, ballRect))
            {
                PlaySound(gameContent.paddleBounceSound);
                int offset = Convert.ToInt32((paddle.Width - (paddle.X + paddle.Width - X + Width / 2)));
                offset = offset / 5;
                if (offset < 0)
                {
                    offset = 0;
                }
                switch (offset)
                {
                case 0:
                    XVelocity = -6;
                    break;

                case 1:
                    XVelocity = -5;
                    break;

                case 2:
                    XVelocity = -4;
                    break;

                case 3:
                    XVelocity = -3;
                    break;

                case 4:
                    XVelocity = -2;
                    break;

                case 5:
                    XVelocity = -1;
                    break;

                case 6:
                    XVelocity = 1;
                    break;

                case 7:
                    XVelocity = 2;
                    break;

                case 8:
                    XVelocity = 3;
                    break;

                case 9:
                    XVelocity = 4;
                    break;

                case 10:
                    XVelocity = 5;
                    break;

                default:
                    XVelocity = 6;
                    break;
                }
                YVelocity = YVelocity * -1;
                Y         = paddle.Y - Height + 1;
                return(true);
            }
            bool hitBrick = false;

            for (int i = 0; i < 7; i++)
            {
                if (hitBrick == false)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Brick brick = wall.BrickWall[i, j];
                        if (brick.Visible)
                        {
                            Rectangle brickRect = new Rectangle((int)brick.X, (int)brick.Y, (int)brick.Width, (int)brick.Height);
                            if (HitTest(ballRect, brickRect))
                            {
                                PlaySound(gameContent.brickSound);
                                brick.Visible = false;
                                Score         = Score + 7 - i;
                                YVelocity     = YVelocity * -1;
                                bricksCleared++;
                                hitBrick = true;
                                break;
                            }
                        }
                    }
                }
            }
            return(true);
        }
        private void gameTick(object sender, EventArgs e)
        {
            //////////////////
            // paddle logic //
            //////////////////

            // move paddle
            if (paddle.GoRight)
            {
                paddle.GoRight = false;
                paddle.X      += 10;
            }
            if (paddle.GoLeft)
            {
                paddle.GoLeft = false;
                paddle.X     -= 10;
            }

            // paddle collision (if out of bounds set it back in bounds)
            if (paddle.X <= 0)
            {
                paddle.X = 0;
            }
            if (paddle.X + paddle.Width >= wpfCanvas.Width)
            {
                paddle.X = (int)wpfCanvas.Width - paddle.Width;
            }

            // update paddle canvas position
            Canvas.SetTop(paddle.GetRectangle(), paddle.Y);
            Canvas.SetLeft(paddle.GetRectangle(), paddle.X);

            ////////////////
            // ball logic //
            ////////////////
            List <Ball> removeBalls = new List <Ball>();

            foreach (Ball ball in balls)
            {
                ball.UpdatePosition(defaultSpeed, incrementSpeed, level);

                // update ball canvas position
                Canvas.SetTop(ball.GetEllipse(), ball.Y);
                Canvas.SetLeft(ball.GetEllipse(), ball.X);

                // edge canvas ball bounce
                if (ball.Y <= 0) // top
                {
                    ball.Dy *= -1;
                    WallHit();
                }
                if (ball.Y + ball.Diameter >= wpfCanvas.Height) // bottom
                {
                    ball.Dy *= -1; WallHit();
                    removeBalls.Add(ball);
                }
                if (ball.X + ball.Diameter >= wpfCanvas.Width) // right
                {
                    ball.Dx *= -1;
                    WallHit();
                }
                if (ball.X <= 0) // left
                {
                    ball.Dx *= -1;
                    WallHit();
                }

                // ball too low to be recovered
                if (ball.getCenter().Y >= paddle.Y)
                {
                    removeBalls.Add(ball);
                }

                // paddle collision
                if (paddle.X <= ball.X + ball.GetRadius() && ball.X + ball.GetRadius() <= paddle.X + paddle.Width) // ball in paddle X range?
                {
                    if (paddle.Y <= ball.Y + ball.Diameter)                                                        // ball at paddle Y level?
                    {
                        // hit paddle
                        ball.Dy *= -1;
                        PaddleHit(paddle);//added this - mg
                    }
                }
                else // not on top of paddle, maybe it is on the corners
                {
                    // ball corner collision with paddle
                    Point ballCenter  = ball.getCenter();
                    bool  topRightHit = CalcDistance(ballCenter.X, ballCenter.Y, paddle.X + paddle.Width, paddle.Y) <= ball.GetRadius();
                    bool  topLeftHit  = CalcDistance(ballCenter.X, ballCenter.Y, paddle.X, paddle.Y) <= ball.GetRadius();

                    if (topRightHit)
                    {
                        CornerBounce(ball, paddle.X + paddle.Width, paddle.Y);
                        PaddleHit(paddle);
                    }
                    if (topLeftHit)
                    {
                        CornerBounce(ball, paddle.X, paddle.Y);
                        PaddleHit(paddle);
                    }
                }

                // brick collision
                List <Brick> removeBricks = new List <Brick>();
                foreach (Brick brick in bricks)
                {
                    Point ballCenter = ball.getCenter();

                    if (brick.X - ball.GetRadius() <= ballCenter.X && ballCenter.X <= brick.X + brick.Width + ball.GetRadius())
                    {
                        if (brick.Y - ball.GetRadius() <= ballCenter.Y && ballCenter.Y <= brick.Y + brick.Height + ball.GetRadius())
                        {
                            // ball is within range to bounce on top, bottom, right, left or maybe the corners

                            // top and bottom
                            if (brick.X <= ballCenter.X && ballCenter.X <= brick.X + brick.Width)
                            {
                                ball.Dy *= -1;
                                BrickHit(brick);
                            }
                            else if (brick.Y <= ballCenter.Y && ballCenter.Y <= brick.Y + brick.Height) // left and right
                            {
                                ball.Dx *= -1;
                                BrickHit(brick);
                            }
                            else
                            {
                                // corners
                                if (CalcDistance(ballCenter.X, ballCenter.Y, brick.X, brick.Y) <= ball.GetRadius()) // top left
                                {
                                    CornerBounce(ball, brick.X, brick.Y);
                                    BrickHit(brick);
                                }
                                if (CalcDistance(ballCenter.X, ballCenter.Y, brick.X + brick.Width, brick.Y) <= ball.GetRadius()) // top right
                                {
                                    CornerBounce(ball, brick.X + brick.Width, brick.Y);
                                    BrickHit(brick);
                                }
                                if (CalcDistance(ballCenter.X, ballCenter.Y, brick.X, brick.Y + brick.Height) <= ball.GetRadius()) // bottom left
                                {
                                    CornerBounce(ball, brick.X, brick.Y + brick.Height);
                                    BrickHit(brick);
                                }
                                if (CalcDistance(ballCenter.X, ballCenter.Y, brick.X + brick.Width, brick.Y + brick.Height) <= ball.GetRadius()) // bottom right
                                {
                                    CornerBounce(ball, brick.X + brick.Width, brick.Y + brick.Height);
                                    BrickHit(brick);
                                }
                            }
                        }
                    }

                    // add to remove list to be removed outside of enumeration
                    if (brick.Remove)
                    {
                        removeBricks.Add(brick);
                    }
                }

                // remove bricks from canvas and list
                foreach (Brick brick in removeBricks)
                {
                    wpfCanvas.Children.Remove(brick.GetRectangle());
                    bricks.Remove(brick);
                }
                removeBricks.Clear();
            }//end of ball forloop

            // remove balls
            foreach (Ball ball in removeBalls)
            {
                wpfCanvas.Children.Remove(ball.GetEllipse());
                balls.Remove(ball);
            }
            removeBalls.Clear();

            //////////////////
            // bullet logic //
            //////////////////

            List <Bullet> removeBullets = new List <Bullet>();

            foreach (Bullet bullet in bullets)
            {
                bullet.UpdatePosition();
                // bullet deletion
                if (bullet.Y <= 0)
                {
                    wpfCanvas.Children.Remove(bullet.GetEllipse()); removeBullets.Add(bullet);
                }                                                                                                 // top
                if (bullet.Y + bullet.Diameter >= wpfCanvas.Height)
                {
                    wpfCanvas.Children.Remove(bullet.GetEllipse()); removeBullets.Add(bullet);
                }                                                                                                                                  // bottom
                if (bullet.X + bullet.Diameter >= wpfCanvas.Width)
                {
                    wpfCanvas.Children.Remove(bullet.GetEllipse()); removeBullets.Add(bullet);
                }                                                                                                                                 // right
                if (bullet.X <= 0)
                {
                    wpfCanvas.Children.Remove(bullet.GetEllipse()); removeBullets.Add(bullet);
                }                                                                                                 // right
                // update ball canvas position
                Canvas.SetTop(bullet.GetEllipse(), bullet.Y);
                Canvas.SetLeft(bullet.GetEllipse(), bullet.X);
            }

            //bullet brick collision
            List <Brick> shotBricks = new List <Brick>();

            foreach (Bullet bullet in bullets)
            {
                foreach (Brick brick in bricks)
                {
                    Point bulletCenter = bullet.getCenter();
                    // top and bottom
                    //TODO: fix the collision
                    if (brick.X <= bulletCenter.X + bullet.GetRadius() && bulletCenter.X - bullet.GetRadius() <= brick.X + brick.Width)
                    {
                        if (brick.Y <= bulletCenter.Y + bullet.GetRadius() && bulletCenter.Y - bullet.GetRadius() <= brick.Y + brick.Height) // left and right
                        {
                            //manually remove the bullet from canvas
                            //wpfCanvas.Children.Remove(bullet.GetEllipse());

                            removeBullets.Add(bullet);
                            BrickHit(brick);
                        }
                        //BrickHit(brick);
                    }
                    //else if (brick.Y <= bulletCenter.Y && bulletCenter.Y <= brick.Y + brick.Height) // left and right
                    //{

                    //   BrickHit(brick);
                    //}
                    if (brick.Remove)
                    {
                        shotBricks.Add(brick);
                    }
                }
                // remove bricks from canvas and list
                foreach (Brick brick in shotBricks)
                {
                    wpfCanvas.Children.Remove(brick.GetRectangle());
                    bricks.Remove(brick);
                }
            }//end of bullet forloop

            //remove bullets
            foreach (Bullet bulletToRemove in removeBullets)
            {
                wpfCanvas.Children.Remove(bulletToRemove.GetEllipse());
                bullets.Remove(bulletToRemove);
            }
            removeBullets.Clear();


            ////////////////////
            // Power up logic //
            ////////////////////
            List <PowerUp> removePowerUps = new List <PowerUp>();

            foreach (PowerUp powerUp in powerUps)
            {
                // update position
                powerUp.UpdatePosition();
                Canvas.SetTop(powerUp.GetRectangle(), powerUp.Y);
                Canvas.SetLeft(powerUp.GetRectangle(), powerUp.X);

                // paddle collision
                if (paddle.Y <= powerUp.Y + powerUp.Height && powerUp.Y <= paddle.Y + paddle.Height)   // Y range
                {
                    if (paddle.X <= powerUp.X + powerUp.Width && powerUp.X <= paddle.X + paddle.Width) // X range
                    {
                        // power up hit paddle
                        if (powerUp.Type == "extra_ball")
                        {
                            Ball newBall = new Ball(40, wpfCanvas.Width / 2, 3 * (wpfCanvas.Height / 4), 0.6, -0.8);
                            wpfCanvas.Children.Add(newBall.GetEllipse());
                            Canvas.SetTop(newBall.GetEllipse(), newBall.Y);
                            Canvas.SetLeft(newBall.GetEllipse(), newBall.X);
                            balls.Add(newBall);
                        }
                        else if (powerUp.Type == "wide_paddle")
                        {
                            // remove paddle
                            wpfCanvas.Children.Remove(paddle.GetRectangle());

                            // create paddle
                            int addWidth = 40;
                            paddle = new Paddle(paddle.X - (addWidth / 2), paddle.Y, paddle.Width + addWidth, paddle.Height);
                            wpfCanvas.Children.Add(paddle.GetRectangle());
                            Canvas.SetTop(paddle.GetRectangle(), paddle.Y);
                            Canvas.SetLeft(paddle.GetRectangle(), paddle.X);
                        }
                        else if (powerUp.Type == "bullets")
                        {
                            ammo += 5;
                            labelAmmoCounter.Content = ammo.ToString();
                        }

                        // remove power up
                        removePowerUps.Add(powerUp);
                    }
                }

                // canvas collision
                if (powerUp.Y + powerUp.Height >= wpfCanvas.Height)
                {
                    removePowerUps.Add(powerUp);
                }
            }

            // remove power up
            foreach (PowerUp powerUp in removePowerUps)
            {
                wpfCanvas.Children.Remove(powerUp.GetRectangle());
                powerUps.Remove(powerUp);
            }
            removePowerUps.Clear();

            // Game Over: balls too far back to hit end game
            if (EndGame() || EndLevel())
            {
                PauseEvent(sender, null);
            }
        }
        private void LoadLevel(int level)
        {
            // remove existing bricks if any
            List <Brick> removeBricks = new List <Brick>();

            removeBricks.AddRange(bricks);
            foreach (Brick brick in removeBricks)
            {
                wpfCanvas.Children.Remove(brick.GetRectangle());
            }
            removeBricks.Clear();
            bricks.Clear();

            // remove existing paddle if any
            if (paddle != null)
            {
                wpfCanvas.Children.Remove(paddle.GetRectangle());
            }
            paddle = null;

            // remove existing balls if any
            List <Ball> removeBalls = new List <Ball>();

            removeBalls.AddRange(balls);
            foreach (Ball ball in removeBalls)
            {
                wpfCanvas.Children.Remove(ball.GetEllipse());
            }
            removeBalls.Clear();
            balls.Clear();

            // remove existing bullets if any
            List <Bullet> removeBullets = new List <Bullet>();

            removeBullets.AddRange(bullets);
            foreach (Bullet bullet in removeBullets)
            {
                wpfCanvas.Children.Remove(bullet.GetEllipse());
            }
            bullets.Clear();

            // remove exisint power ups if any
            foreach (PowerUp powerUp in powerUps)
            {
                wpfCanvas.Children.Remove(powerUp.GetRectangle());
            }
            powerUps.Clear();

            // open file and get default width and height
            string[] lines  = File.ReadAllLines("../../level" + level + ".csv");
            int      width  = Int32.Parse(lines[0].Split(',')[0]);
            int      height = Int32.Parse(lines[0].Split(',')[1]);

            // create bricks
            foreach (string line in lines.Skip(1))
            {
                string[] values  = line.Split(',');
                int      x       = Int32.Parse(values[0]);
                int      y       = Int32.Parse(values[1]);
                string   color   = values[2];
                string   powerUp = values[4];

                bricks.Add(new Brick(x, y, width, height, color, powerUp));
            }

            // add bricks to canvas
            foreach (Brick curBrick in bricks)
            {
                wpfCanvas.Children.Add(curBrick.GetRectangle());
                Canvas.SetTop(curBrick.GetRectangle(), curBrick.Y);
                Canvas.SetLeft(curBrick.GetRectangle(), curBrick.X);
            }

            // recreate and reset paddle
            paddle   = new Paddle(0, (int)wpfCanvas.Height - 50, 100, 30);
            paddle.X = (int)(wpfCanvas.Width / 2) - (paddle.Width / 2);
            wpfCanvas.Children.Add(paddle.GetRectangle());
            Canvas.SetTop(paddle.GetRectangle(), paddle.Y);
            Canvas.SetLeft(paddle.GetRectangle(), paddle.X);

            // recreate and resest ball
            balls.Add(new Ball(40, wpfCanvas.Width / 2, 3 * (wpfCanvas.Height / 4), .6, -0.8));
            foreach (Ball ball in balls)
            {
                wpfCanvas.Children.Add(ball.GetEllipse());
                Canvas.SetTop(ball.GetEllipse(), ball.Y);
                Canvas.SetLeft(ball.GetEllipse(), ball.X);
            }

            // remove ammo
            //ammo = 0;
            //this.labelAmmoCounter.Content = ammo.ToString();
        }
Beispiel #4
0
 //abstract functions are functions that must be overidden by a class that inherits from this class
 public abstract void GivePowerup(Paddle paddleToPowerUp);