Пример #1
0
 public Snake(SnakeField location, Coordinates startCoord, Direction.direction startDirection)
 {
     Location      = location;
     NowDirection  = startDirection;
     NextDirection = startDirection;
     Body          = new List <Coordinates>();
     Add(startCoord);
 }
Пример #2
0
 public Snake(Game g, Coord head)
 {
     SnakeDirection = Direction.Down;
     fieldInstance  = new SnakeField(this, head);
     g.AddSnake(this);
     avail = new LinkedList <Coord>();
     game  = g;
     Push(head);
 }
Пример #3
0
 public static void DeleteBerry(SnakeField field, Coordinates coord)
 {
     if (Beries.ContainsKey(field))
     {
         if (Beries[field].ContainsKey(coord))
         {
             Beries[field][coord].Delete();
         }
     }
 }
Пример #4
0
 public static void Clear(SnakeField field)
 {
     if (Beries.ContainsKey(field))
     {
         Coordinates[] array = new Coordinates[Beries[field].Count];
         Beries[field].Keys.CopyTo(array, 0);
         foreach (var x in array)
         {
             DeleteBerry(field, x);
         }
         Beries.Remove(field);
     }
 }
Пример #5
0
            public static void RandomBerry(SnakeField location)
            {
                var         rnd = new Random();
                Coordinates newCoord;

                while (true)
                {
                    newCoord = new Coordinates(rnd.Next(location.Width), rnd.Next(location.Height));
                    if (location.ReturnCell(newCoord) == GamesSquareValues.nothing)
                    {
                        location.AddBerry(newCoord);
                        break;
                    }
                }
            }
Пример #6
0
 public EventBerry(SnakeField location, Coordinates coord, SnakeMove mover, int live)
 {
     Location = location;
     Position = coord;
     Mover    = mover;
     if (live > 0)
     {
         Live = live;
     }
     Mover.Add(Next);
     location.AddEventBerry(coord);
     if (!Beries.ContainsKey(location))
     {
         Beries.Add(location, new Dictionary <Coordinates, EventBerry>());
     }
     Beries[location].Add(coord, this);
 }
Пример #7
0
 public static void RandomEventBerry(bool multi, SnakeField location, double chanse, SnakeMove Mover, int time)
 {
     if (multi || (Beries.ContainsKey(location) ? Beries[location].Count == 0 : true))
     {
         chanse = Math.Min(1, Math.Max(0, chanse));
         var rnd    = new Random();
         var number = rnd.NextDouble();
         if (number < chanse)
         {
             Coordinates newCoord;
             while (true)
             {
                 newCoord = new Coordinates(rnd.Next(location.Width), rnd.Next(location.Height));
                 if (location.ReturnCell(newCoord) == GamesSquareValues.nothing)
                 {
                     new EventBerry(location, newCoord, Mover, time);
                     break;
                 }
             }
         }
     }
 }
Пример #8
0
 public Snake(SnakeField location) : this(location, new Coordinates(location.Width / 2, location.Height / 2), Direction.direction.up)
 {
 }
Пример #9
0
        static void SnakeStart(IDrawningRectangle <SignConsole> location, SnakeSettings settings, Action startWithClose)
        {
            settings.Event       = true;
            settings.EventChance = 0.50;
            Action stop           = default;
            var    GameDictionary = new Dictionary <GamesSquareValues, SignConsole>
            {
                { GamesSquareValues.snake, new SignConsole(' ', ConsoleColor.White) },
                { GamesSquareValues.snakeBerry, new SignConsole(' ', ConsoleColor.Red) },
                { GamesSquareValues.snakeWall, new SignConsole('X', ConsoleColor.Black) },
                { GamesSquareValues.nothing, new SignConsole(' ', ConsoleColor.Black) },
                { GamesSquareValues.snakeEventBerry, new SignConsole(' ', ConsoleColor.Green) }
            };
            var gameField = new ConsoleGameField(location.Width, location.Height, location, GameDictionary);

            location.Register((0, 0), gameField, gameField.GetCoordinates());

            var snakeMove = new SnakeMove((int)settings.StartSpeed);

            var snakeField = new SnakeField(gameField.Width, gameField.Height - 5, gameField);

            gameField.Register((0, 5), snakeField, snakeField.GetCoordinates());
            snakeField.Inicializated();
            var           bigChar = new Letters("First");
            BigPixelPrint scoreField, scoreField2 = null;
            Score         score, score2 = null;

            KeyPress.SetControl("Snake");
            Snake snake, snake2 = null;

            if (!settings.Multy)
            {
                snake      = new Snake(snakeField);
                scoreField = new BigPixelPrint(18, 5, location, bigChar);
                location.Register((location.Width / 2 - 9, 0), scoreField, scoreField.GetCoordinates());
                score = new Score(scoreField);
            }
            else
            {
                snake      = new Snake(snakeField, (snakeField.Width / 3, snakeField.Height / 2), Direction.direction.up);
                scoreField = new BigPixelPrint(18, 5, location, bigChar);
                location.Register((location.Width / 2 - 20, 0), scoreField, scoreField.GetCoordinates());
                score = new Score(scoreField);

                snake2      = new Snake(snakeField, (snakeField.Width / 3 * 2, snakeField.Height / 2), Direction.direction.down);
                scoreField2 = new BigPixelPrint(18, 5, location, bigChar);
                location.Register((location.Width / 2 + 2, 0), scoreField2, scoreField2.GetCoordinates());
                score2 = new Score(scoreField2);
                Berry.RandomBerry(snakeField);
            }
            Berry.RandomBerry(snakeField);

            snake.IsEat += (Snake) => Berry.RandomBerry(Snake.Location);
            snake.IsEat += (Snake) => snakeMove.Acceleration(((double)settings.Acceleration) / 100.0);
            snake.IsEat += (Snake) => score.Add();
            if (settings.Event)
            {
                snake.IsVeryEat += (Snake) => snakeMove.Acceleration(((double)settings.Acceleration) / 100.0);
                snake.IsVeryEat += (Snake) => score.Add(5);
                snake.IsEat     += (Snake) => EventBerry.RandomEventBerry(settings.EventMulty, snakeField, settings.EventChance, snakeMove, 50);
            }
            snake.Die += (Snake) => stop?.Invoke();
            snakeMove.Add(snake.Move);
            stop += () => snakeMove.Stop();
            stop += () => KeyPress.ResetControl();


            Keys[needOption.snakeUp].Set((obj, ar) => snake.Up());
            Keys[needOption.snakeDown].Set((obj, ar) => snake.Down());
            Keys[needOption.snakeLeft].Set((obj, ar) => snake.Left());
            Keys[needOption.snakeRigh].Set((obj, ar) => snake.Right());
            if (settings.Multy)
            {
                snake2.IsEat += (Snake) => Berry.RandomBerry(Snake.Location);
                snake2.IsEat += (Snake) => snakeMove.Acceleration((double)settings.Acceleration / 100);
                snake2.IsEat += (Snake) => score2.Add();
                if (settings.Event)
                {
                    snake2.IsVeryEat += (Snake) => snakeMove.Acceleration(((double)settings.Acceleration) / 100.0);
                    snake2.IsVeryEat += (Snake) => score2.Add(5);
                }
                snake2.Die += (Snake) => stop?.Invoke();
                snakeMove.Add(snake2.Move);

                Keys[needOption.secondSnakeUp].Set((obj, ar) => snake2.Up());
                Keys[needOption.secondSnakeDown].Set((obj, ar) => snake2.Down());
                Keys[needOption.secondSnakeLeft].Set((obj, ar) => snake2.Left());
                Keys[needOption.secondSnakeRigh].Set((obj, ar) => snake2.Right());
            }

            Keys[needOption.snakePause].Set((obj, ar) =>
            {
                snakeMove.Pause();
                void cont()
                {
                    gameField.Load();
                    score.Add(0);
                    snakeMove.Continue();
                    KeyPress.SetControl("Snake");
                }
                Pause(location, cont, exit);
            });

            snakeMove.Start();
            Keys[needOption.snakeUp].Set(null);
            Keys[needOption.snakeDown].Set(null);
            Keys[needOption.snakeLeft].Set(null);
            Keys[needOption.snakeRigh].Set(null);
            if (settings.Multy)
            {
                Keys[needOption.secondSnakeUp].Set(null);
                Keys[needOption.secondSnakeDown].Set(null);
                Keys[needOption.secondSnakeLeft].Set(null);
                Keys[needOption.secondSnakeRigh].Set(null);
            }
            Console.ReadKey(true);
            void exit()
            {
                snakeMove.Stop();
                gameField.Close();
                scoreField.Close();
                scoreField2?.Close();
                location.CancelRegistration(gameField);
                startWithClose();
                EventBerry.Clear(snakeField);
            }

            exit();
        }