Exemplo n.º 1
0
        public SnakeClass(Canvas playfield)
        {
            this._playField     = playfield;
            this.SnakeParts     = new List <Image>();
            this.SnakeDriection = Direction.Right;
            this._colorPath     = @"Your Path";

            _foodGenerator = new FoodGenerator(_playField);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int x = 120;
            int y = 50;

            Console.SetWindowSize(x, y);
            Console.SetBufferSize(x, y);

            Walls walls = new Walls(x, y);

            walls.Draw();

            Point p     = new Point(4, 5, '*');
            Snake snake = new Snake(p, 4, Direction.Right);

            snake.Draw();

            FoodGenerator foodGenerator = new FoodGenerator(x, y, '$', new Random());
            Point         food          = foodGenerator.SpawnFood();
            int           score         = 0;

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }

                if (snake.Eat(food))
                {
                    score++;
                    food = foodGenerator.SpawnFood();
                }
                else
                {
                    snake.Move();
                }

                Thread.Sleep(100);

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.Handle(key.Key);
                }
            }

            Console.SetCursorPosition(x / 2, y / 2);
            Console.WriteLine($"Game over. Score: {score}");
            Console.ReadKey();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.SetBufferSize(120, 50);

            /*
             * Инициализация змейки с длиной тела, равной 3 и игрового поля.
             * Начальное положение змейки в центре игрового поля.
             */
            GameBorders gameBorders = new GameBorders(120, 50);

            Models.Snake snake = new Models.Snake(new Point(60, 25),
                                                  Direction.Right, 3
                                                  );

            FoodGenerator gen       = new FoodGenerator(120, 50);
            Point         foodPoint = gen.GenerateFood();

            foodPoint.Draw(gen.symbol);

            /*
             * Обработка нажатий клавиш управления пользователем
             */
            while (true)
            {
                if (snake.IsHitWall(gameBorders.borders) || snake.IsHitTail())
                {
                    break;
                }

                if (snake.Eat(foodPoint))
                {
                    foodPoint = gen.GenerateFood();
                    foodPoint.Draw(gen.symbol);
                }
                else
                {
                    snake.Move();
                }

                Thread.Sleep(100);

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.ButtonHandler(key.Key);
                }
            }

            WriteGameOver();
            Console.ReadLine();
        }
Exemplo n.º 4
0
 static void StartGame()
 {
     isGameOver         = false;
     bestScore          = ScoreManager.GetBestScoreFromBinary();
     snake              = new Snake(40, 40);
     snake.OnEat       += Eat;
     snake.OnCollision += Die;
     direction          = Direction.Right;
     allCells           = FoodGenerator.GetAllCells();
     GenerateWalls();
     GenerateFood();
     timer          = new System.Windows.Forms.Timer(components);
     timer.Tick    += Timer_Tick;
     timer.Interval = 200;
     timer.Enabled  = true;
     timer.Start();
 }
Exemplo n.º 5
0
        public void StartGame()
        {
            Score score = new Score();

            score.PrintScore();
            Walls walls = new Walls(81, 25);

            walls.Draw();

            //Initializing Snake
            Point tail  = new Point(4, 5, '*');
            Snake snake = new Snake(tail, 3, Direction.Right);

            snake.Draw();
            snake.PrintSpeed();

            FoodGenerator foodGenerator = new FoodGenerator(78, 23, '#');
            Point         food          = foodGenerator.CreateFood(snake);

            food.DrawPoint();

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    if (Score.score > Score.maxScore)
                    {
                        Score.maxScore = Score.score;
                    }
                    score.PrintGameOver();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.SetCursorPosition(39, 16);
                    while (true)
                    {
                        if (Console.KeyAvailable)
                        {
                            ConsoleKeyInfo key = Console.ReadKey();
                            if (key.Key == ConsoleKey.Y)
                            {
                                Console.Clear();
                                Score.score = 0;
                                StartGame();
                            }
                            else if (key.Key == ConsoleKey.N)
                            {
                                Environment.Exit(0);
                            }
                            else
                            {
                                Console.Write("");
                            }
                        }
                    }
                }

                if (snake.Eat(food))
                {
                    food = foodGenerator.CreateFood(snake);
                    food.DrawPoint();
                    score.PrintScore();
                    snake.PrintSpeed();
                }
                else
                {
                    snake.Move();
                }

                //Thread.Sleep(100);

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    snake.KeyHandler(key.Key);
                }
            }
        }
Exemplo n.º 6
0
 static void GenerateFood()
 {
     food = FoodGenerator.GetFreeCell(allCells, snake, wall);
 }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            int speed = 200;

            Walls();
            FoodGenerator.GenerateNewFood();
            Player.GrowthUp();
            Player.DrawSnake();

            Thread ChangeKeyy = new Thread(changeKey);

            ChangeKeyy.Start();

            while (!IsCrashed)
            {
                for (int i = 0; i < Player.Tail.Count; i++)
                {
                    if (Player.Head == Player.Tail[i])
                    {
                        IsCrashed = true;
                        GameOver();
                    }
                }
                if (Player.Head.x == 0 || Player.Head.x == 49 || Player.Head.y == 0 || Player.Head.y == 20)
                {
                    IsCrashed = true;
                    GameOver();
                }
                if (Player.Head == FoodGenerator.Food)
                {
                    FoodGenerator.GenerateNewFood();
                    Player.GrowthUp();
                    Score++;
                    speed = speed - 10;
                }
                Thread.Sleep(speed);
                switch (alpha)
                {
                case "UpArrow":
                    Player.Move();
                    Player.Head.y--;
                    Player.DrawSnake();
                    break;

                case "DownArrow":
                    Player.Move();
                    Player.Head.y++;
                    Player.DrawSnake();
                    break;

                case "RightArrow":
                    Player.Move();
                    Player.Head.x++;
                    Player.DrawSnake();
                    break;

                case "LeftArrow":
                    Player.Move();
                    Player.Head.x--;
                    Player.DrawSnake();
                    break;
                }
            }
            GameOver();
        }