示例#1
0
        public Form1()
        {
            InitializeComponent();

            gameOverLabel.Visible = false;

            brickColours = new BrickColour();
            paddle       = new Paddle(120, 20, new Point(0, 0), brickColours.getGreenColour(), 20);

            AddBricksToBrickList();
            CalculateBrickStartingPoints();

            ball = new Ball(20, 20, new Point(0, 0), brickColours.getGreenColour(), 3, 0, 0);

            livesLabel.Text = ball.getLives().ToString();

            gameTimer.Interval = 1000 / 20;
            gameTimer.Tick    += UpdateScreen;
            gameTimer.Start();

            StartGame();
        }
示例#2
0
        private void TimerTick(object sender, EventArgs e)
        {
            // TODO && lives == 0
            // Show insert coin button

            // This isn't really needed
            if (ball == null || ball.IsDead)
            {
                // You lost
                GameOver(false);
            }

            if (!ball.CheckPanelBoundries())
            {
                PictureBox ballPicture = this.ball.GetPictureBox();

                Point pos      = ball.GetPosition(),
                      nextPos  = this.ball.GetNewPosition(),
                      velocity = ball.GetVelocity();

                if (paddle.CheckCollision(nextPos.X, nextPos.Y, ballPicture))
                {
                    ball.SetNewVelocity(paddle);
                }

                if (cherry != null)
                {
                    if (cherry.CheckCollision(nextPos.X, nextPos.Y, ballPicture))
                    {
                        cherry.RemoveFromPanel();
                        cherry = null;

                        ball.SpeedBoost(4.20);
                    }
                }

                // Consider creating a method that converts Brick[,] to Brick[] and just foreach it.
                // I don't think I need the row and column value for anything other than getting the target brick.
                for (int x = 0; x < columns; x++)
                {
                    for (int y = 0; y < rows; y++)
                    {
                        Brick target = bricks[x, y];

                        if (!target.IsDead && target.CheckCollision(nextPos.X, nextPos.Y, ballPicture))
                        {
                            collided.Add(target);
                            this.totalBricks--;
                        }
                    }
                }

                if (this.totalBricks == 0)
                {
                    // You win
                    GameOver(true);
                }

                // The ball didn't collide with any bricks
                if (collided.Count == 0)
                {
                    ball.UpdatePosition(nextPos.X, nextPos.Y);
                }
                else
                {
                    // We don't have to loop through the collided bricks because the outcome will be the same for both
                    // so I might aswell just pick the 0 index every time and it won't make a difference.
                    Brick target = collided[0];

                    ball.SetNewVelocity(target);

                    // Update new position after setting new velocity
                    ball.UpdatePosition(ball.GetNewPosition());

                    this.PlaySound(Properties.Resources.Brick);

                    // Hide collided bricks
                    foreach (Brick brick in collided)
                    {
                        BrickColour colour = brick.GetColour();

                        if (colour.Equals(BrickColour.RED))
                        {
                            if (cherry == null)
                            {
                                // Random chance to spawn a Cherry
                                Random rand = new Random();
                                if (rand.Next(0, 2) == 0) // 1 in 3 chance
                                {
                                    cherry = new Cherry(GamePanel);
                                }
                            }
                        }

                        this.AddPoints(colour.GetPoints());
                        brick.SetDead(true);
                    }
                }

                // Empty the collided list
                collided.Clear();
            }
            else // the ball hit the bottom of the panel past the paddle
            {
                this.PlaySound(Properties.Resources.Death);

                this.timer.Stop();
                this.SetLives(this.lives - 1);

                ball.ResetPosition();
                ContinueButton.Visible = true;
            }
        }