示例#1
0
        private void wczytajLevel(int level)
        {
            if (level > ileLevel)
            {
                MessageBox.Show("Wygrałeś! Gratulujemy.");
                Application.Exit();
            }
            lista.Clear();
            listaBonus.Clear();
            listaMessage.Clear();
            gra = false;
            string odczyt = "";

            System.IO.FileStream plik =
                new FileStream("Dane/" + poziomy[level - 1], FileMode.Open);//otwarcie pliku odpowiedzialnego za kolejny poziom
            StreamReader sr     = new StreamReader(plik);
            int          a      = Int16.Parse(sr.ReadLine());
            int          b      = Int16.Parse(sr.ReadLine());
            int          width  = Int16.Parse(sr.ReadLine());
            int          height = Int16.Parse(sr.ReadLine());

            odczyt = sr.ReadLine();
            tworzMape(a, b, width, height, odczyt);

            Message poziom = new Message(Width / 2, Height / 2, "Poziom :" + level, 1000, false, Color.Black);

            listaMessage.Add(poziom);                                                          //dodanie wiadomości

            zycia = new Message(20, 400, "Piłki :" + pilka.getBalls(), 10, true, Color.White); //statyczny tekst
            listaMessage.Add(zycia);


            pilka.setX(Width / 2);
            pilka.setY(Height - belkaBit.Height - 40 - pilkaBit.Height);//ustawienie poczatkowe pilki
        }
示例#2
0
        //Checks ball position and updates it depending on whether collides or not
        private void updateBallPosition()
        {
            //Check if game is started
            if (isStarted)
            {
                //Check winning condition
                if (_bricks.Count <= 0)
                {
                    winningCondition = true;
                    isStarted        = false;
                }
                else
                {
                    //Get ball's position values
                    float xPos = ball.getX();
                    float yPos = ball.getY();

                    //Check collision against borders
                    if (xPos > GameCanvas.Width - ball.getWidth())
                    {
                        Canvas.SetLeft(ball.getBall(), GameCanvas.ActualWidth - ball.getWidth());
                        ball.setXVector(ball.getXVector() * -1);
                    }
                    if (xPos < 0)
                    {
                        Canvas.SetLeft(ball.getBall(), 0);
                        ball.setXVector(ball.getXVector() * -1);
                    }
                    if (yPos < 0)
                    {
                        Canvas.SetTop(ball.getBall(), 0);
                        ball.setYVector(ball.getYVector() * -1);
                    }
                    //Check bottom border
                    if (yPos > GameCanvas.Height - ball.getHeight())
                    {
                        gameOver();
                    }
                    //If ball is close enough to top border
                    if (ball.getY() < 150)
                    {
                        //Check collision againt every brick
                        foreach (Brick brick in _bricks)
                        {
                            //If collision occurs
                            if (brick.collides(ball.getHitBox()))
                            {
                                //Break brick
                                brick.Break();
                                //Determine effect
                                this.impactEffect(brick);
                                //Add Score
                                this.calculateScore();
                                //Remove if fully broken
                                if (brick.isBrickBroken())
                                {
                                    _bricks.Remove(brick);
                                    GameCanvas.Children.Remove(brick.getBrick());
                                }
                                //Determine ball direction depending of it hitting the side (left/right o top/bottom)
                                if (ball.getX() >= brick.getX() && ball.getX() <= brick.getX() + brick.getWidth() || (ball.getX() * ball.getHeight()) >= brick.getX() && (ball.getX() * ball.getHeight()) <= brick.getX() + brick.getWidth())
                                {
                                    ball.setYVector(ball.getYVector() * -1);
                                }
                                else if (ball.getY() >= brick.getY() && ball.getY() <= brick.getY() + brick.getHeight() || (ball.getY() * ball.getHeight()) >= brick.getY() && (ball.getY() * ball.getHeight()) <= brick.getY() + brick.getWidth())
                                {
                                    ball.setXVector(ball.getXVector() * -1);
                                }
                                break;
                            }
                        }
                    }
                    //If ball is near the bottom enough calculate collision against paddle
                    if (ball.getY() > 200)
                    {
                        if (paddle.collides(ball.getHitBox()))
                        {
                            ball.setY(paddle.getY() - 11);
                            ball.setYVector(ball.getYVector() * -1);
                        }
                    }

                    //Set Ball new psoition
                    xPos += ball.getXVector() * ball.getSpeed();
                    yPos += ball.getYVector() * ball.getSpeed();
                    ball.setX((int)xPos);
                    ball.setY((int)yPos);
                    Canvas.SetTop(ball.getBall(), ball.getY());
                    Canvas.SetLeft(ball.getBall(), ball.getX());
                }
            }
        }