Пример #1
0
 public Snake(int rowPosition, int columnPosition)
 {
     HeadPosition  = new ArenaPosition(rowPosition, columnPosition);
     HeadDirection = SnakeHeadDirerctionEnum.InPlace;
     Body          = new List <ArenaPosition>(); //object reference null hiba ellen!
     Lenght        = 6;
 }
Пример #2
0
        private void ItsTimeToDisplay(object sender, EventArgs e)
        {
            if (!isGameStarted)
            {
                return;
            }
            //megjegyezzük a fej pozicióját
            CurrentPosition = new ArenaPosition(Snake.HeadPosition.RowPosition, Snake.HeadPosition.ColumnPosition);

            //a fej új poziciója:
            switch (Snake.HeadDirection)
            {
            case SnakeHeadDirerctionEnum.Up:
                Snake.HeadPosition.RowPosition = Snake.HeadPosition.RowPosition - 1;
                break;

            case SnakeHeadDirerctionEnum.Down:
                Snake.HeadPosition.RowPosition = Snake.HeadPosition.RowPosition + 1;
                break;

            case SnakeHeadDirerctionEnum.Left:
                Snake.HeadPosition.ColumnPosition = Snake.HeadPosition.ColumnPosition - 1;
                break;

            case SnakeHeadDirerctionEnum.Right:
                Snake.HeadPosition.ColumnPosition = Snake.HeadPosition.ColumnPosition + 1;
                break;

            case SnakeHeadDirerctionEnum.InPlace:
                break;
            }

            //falnak ütközés detektálása:

            if (Snake.HeadPosition.RowPosition < 0 || Snake.HeadPosition.RowPosition > RowCount - 1 ||
                Snake.HeadPosition.ColumnPosition < 0 || Snake.HeadPosition.ColumnPosition > ColumnCount - 1)
            {
                EndOfGame();
                return;
            }

            //testtel ütközés detektálása:
            if (Snake.Body.Any(x => x.RowPosition == Snake.HeadPosition.RowPosition &&
                               x.ColumnPosition == Snake.HeadPosition.ColumnPosition))
            {
                EndOfGame();
                Console.WriteLine("saját farkába harapott");
            }


            //testtel ütközés 2. (levél PG-nek)
            //if (Snake.Body.Any(x => x == Snake.HeadPosition))
            //{
            //    EndOfGame();
            //    Console.WriteLine("saj fark har");
            //}



            ShowSnakeHead(Snake.HeadPosition.RowPosition, Snake.HeadPosition.ColumnPosition, IconEnum.Head);

            // a régi fejből test lett, belerakjuk a Body listába:
            Snake.Body.Add(new ArenaPosition(CurrentPosition.RowPosition, CurrentPosition.ColumnPosition));

            // töröljük a régi fejet, helytte test lesz:
            ShowSnakeHead(CurrentPosition.RowPosition, CurrentPosition.ColumnPosition, IconEnum.Body);

            if (Snake.Body.Count > Snake.Lenght)
            {
                var end = Snake.Body[0];
                ShowSnakeHead(end.RowPosition, end.ColumnPosition, IconEnum.Empty);
                Snake.Body.RemoveAt(0);
            }

            //van-e evés esemény

            //ütközött-e a fej elemózsiával
            var food = Foods.FoodPositions[0];

            if (Snake.HeadPosition.RowPosition == food.RowPosition && Snake.HeadPosition.ColumnPosition == food.ColumnPosition)
            {
                Eating(food.RowPosition, food.ColumnPosition);
            }
        }