예제 #1
0
파일: Game.cs 프로젝트: szanuj/snake
        /// <summary>
        /// Tworzy niektóre składowe obiektu Game i przygotowuje grę do działania.
        /// </summary>
        private void Initialize()
        {
            Vector2Int userInterfacePosition = Constants.UserInterfacePosition;
            Vector2Int userInterfaceSize     = new Vector2Int(WindowSize.X, Constants.UserInterfaceSizeY);
            Vector2Int levelPosition         = new Vector2Int(userInterfacePosition.X, userInterfacePosition.Y + userInterfaceSize.Y);
            Vector2Int levelSize             = new Vector2Int(WindowSize.X, WindowSize.Y - userInterfaceSize.Y);

            currentKey = (ConsoleKey)(-1);
            Snake   snake   = ConvertString.ToSnake(FileManager.LoadLines(Constants.iniPath));
            Bonuses bonuses = new Bonuses();

            gameObjects = new List <GameObject> {
                snake, bonuses
            };
            level = new Level(levelPosition, levelSize);
            level.UpdateLevel(gameObjects);
            bonuses.SpawnBonus(level.PickAnEmptyPoint());
            level.UpdateLevel(gameObjects);
            Score     = Constants.Score;
            HighScore = FileManager.LoadHighScore(Constants.highScorePath);
            scoreBar  = new UserInterface(userInterfacePosition, userInterfaceSize, HighScore, Score);
            InGame    = true;
        }
예제 #2
0
파일: Game.cs 프로젝트: szanuj/snake
        /// <summary>
        /// Aktualizuje stan gry.
        /// </summary>
        /// <param name="checkInput">Określa, czy metoda ma obłsużyć wejście.</param>
        public void Update(bool checkInput)
        {
            bool       updateDirection = false;
            Directions newDirection    = Directions.Up;

            if (checkInput)
            {
                // Obsługa wejścia.
                switch (currentKey)
                {
                case ConsoleKey.UpArrow:
                    updateDirection = true;
                    newDirection    = Directions.Up;
                    break;

                case ConsoleKey.DownArrow:
                    updateDirection = true;
                    newDirection    = Directions.Down;
                    break;

                case ConsoleKey.LeftArrow:
                    updateDirection = true;
                    newDirection    = Directions.Left;
                    break;

                case ConsoleKey.RightArrow:
                    updateDirection = true;
                    newDirection    = Directions.Right;
                    break;

                default:
                    break;
                }
            }
            // Aktualizacja obiektów gry.
            foreach (GameObject gameObject in gameObjects)
            {
                // Aktualizacja węży.
                if (gameObject is Snake)
                {
                    Snake snake = gameObject as Snake;
                    // Obsługa zmiany kierunku.
                    if (updateDirection)
                    {
                        Vector2Int zero = new Vector2Int(0, 0);
                        if (ConvertDirection.ToVector2Int(newDirection) + ConvertDirection.ToVector2Int(snake.Direction) != zero)
                        {
                            snake.UpdatedDirection       = newDirection;
                            snake.DirectionUpdateWaiting = true;
                        }
                    }
                    // Obsługa zmiany kierunku, ruchu, zebrania bonusu i kolizji.
                    double secondsPerMove = (1D / snake.Speed);
                    int    timesToMove    = (int)(snake.speedTimer.TimeElapsed.TotalSeconds / secondsPerMove);
                    if (timesToMove > 0)
                    {
                        snake.UpdateDirection();
                        snake.speedTimer.SubtractTimeElapsed(TimeSpan.FromSeconds(timesToMove * secondsPerMove));
                        for (int i = 0; i < timesToMove; i++)
                        {
                            Vector2Int futureHead = snake.Points[0] + ConvertDirection.ToVector2Int(snake.Direction);
                            if (futureHead.X >= 0 && futureHead.Y >= 0 && futureHead.X < level.Size.X && futureHead.Y < level.Size.Y)
                            {
                                PointTypes pointType = level.GetPointType(futureHead);
                                if (pointType == PointTypes.Empty)
                                {
                                    // Zwykły ruch.
                                    snake.Move();
                                    level.UpdateLevel(gameObjects);
                                }
                                else if (pointType == PointTypes.Bonus)
                                {
                                    // Ruch z zebraniem bonusu i urośnięciem węża.
                                    Bonuses bonuses = level.WhatObjectIsHere(futureHead, gameObjects) as Bonuses;
                                    snake.Move(true);
                                    bonuses.DeleteBonus(futureHead);
                                    level.UpdateLevel(gameObjects);
                                    bonuses.SpawnBonus(level.PickAnEmptyPoint());
                                    level.UpdateLevel(gameObjects);
                                    Score++;
                                    scoreBar.UpdateScores(Score);
                                }
                                else
                                {
                                    // Kolizja z wężem.
                                    InGame = false;
                                }
                            }
                            else
                            {
                                // Kolizja z granicą levelu.
                                InGame = false;
                            }
                        }
                    }
                }
            }
        }