示例#1
0
 public Form1(Game game = null)
 {
     InitializeComponent();
     this.Width           = formWidth;
     this.Height          = formHeight;
     this.FormBorderStyle = FormBorderStyle.None;
     ball2     = new Ball(BallType.Basic, 4, 4);
     racket    = new Racket();
     this.game = game;
     if (this.game == null)
     {
         Level level = new Level();
         this.game = new Game(level);
     }
     this.game.level.Ball   = ball2;
     this.game.level.Racket = racket;
     SetBall();
     playground.Controls.Add(ball2.Box);
     playground.Controls.Add(racket.Box);
     racket.Box.Top = playground.Bottom - (playground.Bottom / 10);
     if (Game.Loaded)
     {
         this.game.LoadGame();
     }
     else
     {
         this.game.StartNewGame();
     }
     Draw();
     labelStart.Text = "Press Enter to start level " + this.game.currentLevel.ToString() + "\n Press Esc to open Menu";;
 }
示例#2
0
        /// <summary>
        /// Handles the movement with the mouse on the canvas.
        /// </summary>
        /// <param name="mouseOnCanvas">The mouseOnCanvas.</param>
        /// <param name="canvas">The canvas.</param>
        /// <param name="racket">The racket.</param>
        public void CanvasMouseMove(Point mouseOnCanvas, Canvas canvas, Racket racket)
        {
// TODO: bug: the relative position on the racket gets overwritten
            if (PositionX - racket.PositionX < 0)
            {
                // The ball's x position is before the racket's x position.
                if (mouseOnCanvas.X <= 0)
                {
                    PositionX = 0;
                }
                else if (mouseOnCanvas.X + (racket.PositionX + racket.Width - positionX) >= canvas.Width) //
                {
                    PositionX = canvas.Width - (racket.PositionX + racket.Width - positionX);
                }
                else
                {
                    PositionX = mouseOnCanvas.X;
                }
            }
            else
            {
                // The ball's x position is after the racket's x position.
                if (mouseOnCanvas.X <= (PositionX - racket.PositionX))
                {
                    PositionX = (PositionX - racket.PositionX);
                }
                else if (mouseOnCanvas.X + (PositionX - racket.PositionX) >= canvas.Width)
                {
                    PositionX = canvas.Width - (PositionX - racket.PositionX);
                }
                else
                {
                    PositionX = mouseOnCanvas.X;
                }
            }
        }
示例#3
0
        /// <summary>
        /// Handles the movement with keys.
        /// </summary>
        /// <param name="horizontalDisplacement">The horizontalDisplacement.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="canvas">The canvas.</param>
        /// <param name="racket">The racket.</param>
        public void MoveKey(double horizontalDisplacement, string direction, Canvas canvas, Racket racket)
        {
// TODO: bug: the relative position on the racket gets overwritten
            if (direction.ToLower() == "left")
            {
                if ((PositionX - racket.PositionX) < 0)
                {
                    // The ball's x position is before the racket's x position.
                    if (PositionX <= 0)
                    {
                        PositionX = 0;
                    }
                    else
                    {
                        PositionX -= horizontalDisplacement;
                    }
                }
                else
                {
                    // The ball's x position is after the racket's x position.
                    if (PositionX <= (PositionX - racket.PositionX))
                    {
                        PositionX = (PositionX - racket.PositionX);
                    }
                    else
                    {
                        PositionX -= horizontalDisplacement;
                    }
                }
            }
            else if (direction.ToLower() == "right")
            {
                if ((racket.PositionX + racket.Width - PositionX) < Radius)
                {
                    // The ball's x position is before the racket's x position.
                    if (PositionX + (racket.PositionX + racket.Width - PositionX) >= canvas.ActualWidth)
                    {
                        PositionX = canvas.ActualWidth - (racket.PositionX + racket.Width - PositionX);
                    }
                    else
                    {
                        PositionX += horizontalDisplacement;
                    }
                }
                else
                {
                    // The ball's x position is after the racket's x position.
                    if (PositionX >= canvas.ActualWidth - (racket.PositionX + racket.Width - PositionX))
                    {
                        PositionX = canvas.ActualWidth - (racket.PositionX + racket.Width - PositionX);
                    }
                    else
                    {
                        PositionX += horizontalDisplacement;
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Handles the movement with the mouse on the canvas.
        /// </summary>
        /// <param name="mouseOnCanvas">The mouseOnCanvas.</param>
        /// <param name="canvas">The canvas.</param>
        /// <param name="racket">The racket.</param>
        public void CanvasMouseMove(Point mouseOnCanvas, Canvas canvas, Racket racket)
        {
// TODO: bug: the relative position on the racket gets overwritten
            if (PositionX - racket.PositionX < 0)
            {
                // The ball's x position is before the racket's x position.
                if (mouseOnCanvas.X <= 0)
                {
                    PositionX = 0;
                }
                else if (mouseOnCanvas.X + (racket.PositionX + racket.Width - positionX) >= canvas.Width) //
                {
                    PositionX = canvas.Width - (racket.PositionX + racket.Width - positionX);
                }
                else
                {
                    PositionX = mouseOnCanvas.X;
                }
            }
            else
            {
                // The ball's x position is after the racket's x position.
                if (mouseOnCanvas.X <= (PositionX - racket.PositionX))
                {
                    PositionX = (PositionX - racket.PositionX);
                }
                else if (mouseOnCanvas.X + (PositionX - racket.PositionX) >= canvas.Width)
                {
                    PositionX = canvas.Width - (PositionX - racket.PositionX);
                }
                else
                {
                    PositionX = mouseOnCanvas.X;
                }
            }
        }
示例#5
0
        /// <summary>
        /// Handles the movement with keys.
        /// </summary>
        /// <param name="horizontalDisplacement">The horizontalDisplacement.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="canvas">The canvas.</param>
        /// <param name="racket">The racket.</param>
        public void MoveKey(double horizontalDisplacement, string direction, Canvas canvas, Racket racket)
        {
// TODO: bug: the relative position on the racket gets overwritten
            if (direction.ToLower() == "left")
            {
                if ((PositionX - racket.PositionX) < 0)
                {
                    // The ball's x position is before the racket's x position.
                    if (PositionX <= 0)
                    {
                        PositionX = 0;
                    }
                    else
                    {
                        PositionX -= horizontalDisplacement;
                    }
                }
                else
                {
                    // The ball's x position is after the racket's x position.
                    if (PositionX <= (PositionX - racket.PositionX))
                    {
                        PositionX = (PositionX - racket.PositionX);
                    }
                    else
                    {
                        PositionX -= horizontalDisplacement;
                    }
                }
            }
            else if (direction.ToLower() == "right")
            {
                if ((racket.PositionX + racket.Width - PositionX) < Radius)
                {
                    // The ball's x position is before the racket's x position.
                    if (PositionX + (racket.PositionX + racket.Width - PositionX) >= canvas.ActualWidth)
                    {
                        PositionX = canvas.ActualWidth - (racket.PositionX + racket.Width - PositionX);
                    }
                    else
                    {
                        PositionX += horizontalDisplacement;
                    }
                }
                else
                {
                    // The ball's x position is after the racket's x position.
                    if (PositionX >= canvas.ActualWidth - (racket.PositionX + racket.Width - PositionX))
                    {
                        PositionX = canvas.ActualWidth - (racket.PositionX + racket.Width - PositionX);
                    }
                    else
                    {
                        PositionX += horizontalDisplacement;
                    }
                }
            }
        }
        /// <summary>
        /// Loads the values from the xml file.
        /// </summary>
        private void LoadValuesFromFile()
        {
            #region PresetValues

            lifePoint = 3;
            scoreValue = 0;
            gameInSession = false;
            gameIsPaused = false;
            gameOver = false;
            gameOverStatus = "";
            mapName = "notfound";
            bonusSpeed = 1;
            mapMaxNumber = 5;
            timeSpan = 3;
            ballHorizontalMovement = 5;
            ballVerticalMovement = 5;
            racketHorizontalMovement = 10;
            ballSpeed = 1;
            speedScale = 1;
            brickWidth = 27.7;
            brickHeight = 8;
            racketWidth = 80;
            racketHeight = 8;
            bonusWidth = 24;
            bonusHeigth = 24;
            ballRadius = 5;
            smallBall = 3;
            bigBall = 15;
            ballExaminationProximity = 4;
            horizontalScaleNumber = 1;
            verticalScaleNumber = 1;
            racketMaxSize = 160;
            racketMinSize = 40;
            racketDifference = 16;

            if (bonusList.Count != 0)
            {
                bonusList.Clear();
            }
            if (racketList.Count != 0)
            {
                racketList.Clear();
            }
            if (ballList.Count != 0)
            {
                ballList.Clear();
            }
            if (brickList.Count != 0)
            {
                brickList.Clear();
            }

            optionsSettings = new OptionsSettings("", "", "", "", "", 0, false, false, 0, false);
            timeOfGame = new DateTime();
            movingTimer = new DispatcherTimer();
            mPlayer = new MediaPlayer();
            rnd = new Random();

            #endregion PresetValues

            #region OptionsHandler

            try
            {
                if (!File.Exists(@"..\..\Resources\OptionsSettings.xml"))
                {
                    if (MessageBox.Show("Couldn't find the xml file for the settings. \n Would you like to create a new with default settings?", "Error", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        XElement mouseElement = new XElement("mouse", "true");
                        XElement keyboardElement = new XElement("keyboard", "true");
                        XElement soundElement = new XElement("sound", "true");
                        XElement resolutionElement = new XElement("resolution", "1024x768");
                        XElement leftkeyElement = new XElement("leftkey", "Left");
                        XElement rightkeyElement = new XElement("rightkey", "Right");
                        XElement firekeyElement = new XElement("firekey", "Space");
                        XElement pausekeyElement = new XElement("pausekey", "P");
                        XElement difficultyElement = new XElement("difficulty", "1");
                        XElement mapElement = new XElement("map", "1");
                        XAttribute newAttribute = new XAttribute("id", 1);
                        XElement newElements = new XElement("option", newAttribute, mouseElement, keyboardElement, soundElement, resolutionElement, leftkeyElement, rightkeyElement, firekeyElement, pausekeyElement, difficultyElement, mapElement);
                        XElement newOptions = new XElement("Options", newElements);
                        XDocument newDocument = new XDocument(newOptions);
                        newDocument.Save(@"..\..\Resources\OptionsSettings.xml");
                    }
                    // If the OptionsSettings xml doesn't exist, then send message.
                }

                if (File.Exists(@"..\..\Resources\OptionsSettings.xml"))
                {
                    XDocument settingsFromXml = XDocument.Load(@"..\..\Resources\OptionsSettings.xml");
                    var readDataFromXml = settingsFromXml.Descendants("option");
                    var fromXml = from x in readDataFromXml
                                  select x;
                    // Load the values stored in the xml.

                    foreach (var oneElement in fromXml)
                    {
                        optionsSettings.GraphicsResolution = (string)oneElement.Element("resolution");
                        optionsSettings.LeftKey = (string)oneElement.Element("leftkey");
                        optionsSettings.RightKey = (string)oneElement.Element("rightkey");
                        optionsSettings.PauseKey = (string)oneElement.Element("pausekey");
                        optionsSettings.FireKey = (string)oneElement.Element("firekey");
                        optionsSettings.DifficultyLevel = (int)oneElement.Element("difficulty");
                        optionsSettings.MouseIsOn = (bool)oneElement.Element("mouse");
                        optionsSettings.KeyboardIsOn = (bool)oneElement.Element("keyboard");
                        optionsSettings.MapNumber = (int)oneElement.Element("map");
                        optionsSettings.SoundIsOn = (bool)oneElement.Element("sound");
                    }
                    // Gives the values from the xml to the OptionSettings object, thus sets the object with the default values.
                }
            }
            catch
            {

            }

            #endregion OptionsHandler

            #region SetScaling

            switch (optionsSettings.GraphicsResolution)
            {
                case "640x480":
                    horizontalScaleNumber = 1;
                    verticalScaleNumber = 1;
                    break;
                case "800x600":
                    horizontalScaleNumber = 1.25;
                    verticalScaleNumber = 1.25;
                    break;
                case "1024x768":
                    horizontalScaleNumber = 1.6;
                    verticalScaleNumber = 1.6;
                    break;
            }

            switch (optionsSettings.DifficultyLevel)
            {
                case 1:
                    speedScale = 1;
                    break;
                case 2:
                    speedScale = 1.2;
                    break;
                case 3:
                    speedScale = 1.4;
                    break;
            }

            bonusWidth *= horizontalScaleNumber;
            bonusHeigth *= verticalScaleNumber;
            racketWidth *= horizontalScaleNumber;
            racketHeight *= verticalScaleNumber;
            brickWidth *= horizontalScaleNumber;
            brickHeight *= verticalScaleNumber;
            ballHorizontalMovement *= speedScale;
            ballVerticalMovement *= speedScale;
            ballRadius *= horizontalScaleNumber;
            smallBall *= horizontalScaleNumber;
            bigBall *= horizontalScaleNumber;
            bonusSpeed *= speedScale;
            ballSpeed *= speedScale;
            racketHorizontalMovement *= speedScale;
            ballExaminationProximity *= horizontalScaleNumber;
            racketMaxSize *= horizontalScaleNumber;
            racketMinSize *= horizontalScaleNumber;
            racketDifference *= horizontalScaleNumber;
            // Set the scaling.

            #endregion SetScaling

            #region MapSelection

            try
            {
                if (optionsSettings.MapNumber > 0 && optionsSettings.MapNumber < 6)
                {
                    switch (optionsSettings.MapNumber)
                    {
                        case 1:
                            if (!File.Exists(@"..\..\Resources\maps\FirstMap.txt"))
                            {
                                mapName = "notfound";
                            }
                            else
                            {
                                mapName = @"..\..\Resources\maps\FirstMap.txt";
                            }
                            break;
                        case 2:
                            if (!File.Exists(@"..\..\Resources\maps\SecondMap.txt"))
                            {
                                mapName = "notfound";
                            }
                            else
                            {
                                mapName = @"..\..\Resources\maps\SecondMap.txt";
                            }
                            break;
                        case 3:
                            if (!File.Exists(@"..\..\Resources\maps\ThirdMap.txt"))
                            {
                                mapName = "notfound";
                            }
                            else
                            {
                                mapName = @"..\..\Resources\maps\ThirdMap.txt";
                            }
                            break;
                        case 4:
                            if (!File.Exists(@"..\..\Resources\maps\FourthMap.txt"))
                            {
                                mapName = "notfound";
                            }
                            else
                            {
                                mapName = @"..\..\Resources\maps\FourthMap.txt";
                            }
                            break;
                        case 5:
                            if (!File.Exists(@"..\..\Resources\maps\FifthMap.txt"))
                            {
                                mapName = "notfound";
                            }
                            else
                            {
                                mapName = @"..\..\Resources\maps\FifthMap.txt";
                            }
                            break;
                    }
                    // Based on the selected map sets the name of the map that will open if it exists.
                }
                // Only switch if the number is correct.
            }
            catch
            {

            }

            #endregion MapSelection

            #region BrickListFill

            try
            {
                if (!String.IsNullOrEmpty(mapName) && mapName != "notfound")
                {
                    double X = 0;
                    double Y = 0;
                    FileStream fs = new FileStream(mapName, FileMode.Open, FileAccess.Read, FileShare.None);
                    BinaryReader rd = new BinaryReader(fs);

                    while (rd.BaseStream.Position != rd.BaseStream.Length)
                    {
                        char[] oneLine = rd.ReadChars(24);

                        for (int i = 0; i < oneLine.Length; i++)
                        {
                            switch (oneLine[i])
                            {
                                case '.':
                                    break;
                                case '1':
                                    Brick brick1 = new Brick(X, Y, brickHeight, brickWidth, Brick.brickType.Easy, @"..\..\Resources\media\brick\easybrick.jpg");
                                    brick1.ScorePoint = 10;
                                    brick1.BreakNumber = 1;
                                    brickList.Add(brick1);
                                    canvasLayer.Children.Add(brick1.GetRectangle());
                                    break;
                                case '2':
                                    Brick brick2 = new Brick(X, Y, brickHeight, brickWidth, Brick.brickType.Medium, @"..\..\Resources\media\brick\mediumbrick.jpg");
                                    brick2.ScorePoint = 20;
                                    brick2.BreakNumber = 2;
                                    brickList.Add(brick2);
                                    canvasLayer.Children.Add(brick2.GetRectangle());
                                    break;
                                case '3':
                                    Brick brick3 = new Brick(X, Y, brickHeight, brickWidth, Brick.brickType.Hard, @"..\..\Resources\media\brick\hardbrick.jpg");
                                    brick3.ScorePoint = 30;
                                    brick3.BreakNumber = 5;
                                    brickList.Add(brick3);
                                    canvasLayer.Children.Add(brick3.GetRectangle());
                                    break;
                                case '4':
                                    Brick brick4 = new Brick(X, Y, brickHeight, brickWidth, Brick.brickType.Steel, @"..\..\Resources\media\brick\steelbrick.jpg");
                                    brick4.ScorePoint = 40;
                                    brick4.BreakNumber = 1;
                                    brickList.Add(brick4);
                                    canvasLayer.Children.Add(brick4.GetRectangle());
                                    break;
                            }

                            X += brickWidth;
                        }

                        X = 0;
                        Y += brickHeight;
                    }

                    rd.Close();
                    fs.Close();
                    // Adds the bricks as they are stored in the map file.
                }
                // If the map name is valid, then load map.
            }
            catch
            {

            }

            #endregion BrickListFill

            #region SetValues

            mPlayer.Open(new Uri(@"..\..\Resources\media\sounds\play_this.mp3", UriKind.Relative));

            Width = int.Parse(optionsSettings.GraphicsResolution.Split('x')[0]);
            Height = int.Parse(optionsSettings.GraphicsResolution.Split('x')[1]);
            canvasLayer.Width = int.Parse(optionsSettings.GraphicsResolution.Split('x')[0]) - 30;
            canvasLayer.Height = int.Parse(optionsSettings.GraphicsResolution.Split('x')[1]) - 50;
            // Set resolution.

            Racket racket = new Racket((canvasLayer.Width / 2) - (racketWidth / 2), canvasLayer.Height - racketHeight, racketHeight, racketWidth, @"..\..\Resources\media\racket\normalracket.jpg");
            racketList.Add(racket);
            canvasLayer.Children.Add(racket.GetRectangle());
            // First racket.

            Ball ball = new Ball((canvasLayer.Width / 2) - ballRadius, canvasLayer.Height - racketHeight - (ballRadius * 2), ballRadius, Ball.ballType.Normal, @"..\..\Resources\media\ball\normalball.jpg");
            ball.VerticalMovement = ballVerticalMovement > 0 ? ballVerticalMovement : -ballVerticalMovement;
            ball.HorizontalMovement = ballHorizontalMovement < 0 ? ballHorizontalMovement : -ballHorizontalMovement;
            ball.BallInMove = false;
            ballList.Add(ball);
            canvasLayer.Children.Add(ball.GetEllipse());
            // First ball.

            #endregion SetValues
        }
        /// <summary>
        /// Handles the event when the ball falls down.
        /// </summary>
        private void DisposeOfBall()
        {
            canvasLayer.Children.Clear();
            racketList.Clear();
            ballList.Clear();
            bonusList.Clear();
            // Dispose balls, rackets, bonuses.

            Racket racket = new Racket((canvasLayer.Width / 2) - (racketWidth / 2), canvasLayer.Height - racketHeight, racketHeight, racketWidth, @"..\..\Resources\media\racket\normalracket.jpg");
            racketList.Add(racket);
            canvasLayer.Children.Add(racket.GetRectangle());
            // Add new racket.

            Ball ball = new Ball((canvasLayer.Width / 2) - ballRadius, canvasLayer.Height - racketHeight - (ballRadius * 2), ballRadius, Ball.ballType.Normal, @"..\..\Resources\media\ball\normalball.jpg");
            ball.VerticalMovement = ballVerticalMovement > 0 ? ballVerticalMovement : -ballVerticalMovement;
            ball.HorizontalMovement = ballHorizontalMovement < 0 ? ballHorizontalMovement : -ballHorizontalMovement;
// TODO: bug at new ball movement
            ball.BallInMove = false;
            ballList.Add(ball);
            canvasLayer.Children.Add(ball.GetEllipse());
            // Add new ball.

            RefreshCanvas();
        }