Ejemplo n.º 1
0
        static void CheckForOpstacles()
        {
            var currentOpstacleRow = CurrentOpstacle.rowPossition;
            var currentOpstacleCol = CurrentOpstacle.colPossition;

            var lastSnakeEmenentId  = SnakeEmenents[SnakeEmenents.Count - 1].Id;
            var lastSnakeEmenentRow = SnakeEmenents[SnakeEmenents.Count - 1].rowPossition;
            var lastSnakeEmenentCol = SnakeEmenents[SnakeEmenents.Count - 1].colPossition;

            if (SnakeEmenents[0].rowPossition == currentOpstacleRow && SnakeEmenents[0].colPossition == currentOpstacleCol)
            {
                bool validOpstacle = true;
                Score += 10 * Level;

                while (validOpstacle)
                {
                    bool checkedSnake = true;
                    CurrentOpstacle = SnakeObstaclesList[Random.Next(0, SnakeObstaclesList.Count)];

                    for (int i = 0; i < SnakeEmenents.Count; i++)
                    {
                        if (CurrentOpstacle.rowPossition == SnakeEmenents[i].rowPossition &&
                            CurrentOpstacle.colPossition == SnakeEmenents[i].colPossition)
                        {
                            checkedSnake = false;
                        }
                    }

                    if (checkedSnake)
                    {
                        validOpstacle = false;
                    }
                }

                Write($"{SnakeHeadFigureSymbolOpstacle}", SnakeEmenents[0].rowPossition, SnakeEmenents[0].colPossition);
                Thread.Sleep(50);

                if (MusicPlayer == false)
                {
                    new Thread(() =>
                    {
                        PlayMusic(@"C:\Users\yyord\OneDrive\Desktop\My projects\Console_Games\ConsoleGamesCSharp\songs\Minecraft-eat2.wav");
                    }).Start();
                }

                Write($"{SnakeHeadFigureSymbol}", SnakeEmenents[0].rowPossition, SnakeEmenents[0].colPossition);
                SnakeEmenents.Add(new SnakeItem {
                    Id = lastSnakeEmenentId, rowPossition = lastSnakeEmenentRow, colPossition = lastSnakeEmenentCol
                });
            }
        }
Ejemplo n.º 2
0
        static void Main()
        {
            //console settings
            Console.Title           = "Snake V1.0 by y.yordanov21";
            Console.OutputEncoding  = Encoding.UTF8;
            Console.OutputEncoding  = Encoding.Unicode;
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.CursorVisible   = false;
            Console.WindowHeight    = ConsoleRows;
            Console.WindowWidth     = ConsoleCols;
            Console.BufferHeight    = ConsoleRows;
            Console.BufferWidth     = ConsoleCols;

            if (File.Exists(ScoresFileName))
            {
                var allScores = File.ReadAllLines(ScoresFileName);
                foreach (var score in allScores)
                {
                    var match = Regex.Match(score, @" => (?<score>[0-9]+)");
                    HighScore = Math.Max(HighScore, int.Parse(match.Groups["score"].Value));
                }
            }

            for (int row = 1; row <= SnakeField.GetLength(0); row++)
            {
                var id = 0;
                for (int col = 1; col <= SnakeField.GetLength(1); col++)
                {
                    SnakeObstaclesList.Add(new OpstacleItem {
                        Id = id, rowPossition = row, colPossition = col
                    });
                    id++;
                }
            }

            if (MusicPlayer)
            {
                player.SoundLocation = @"C:\Users\yyord\OneDrive\Desktop\My projects\Console_Games\ConsoleGamesCSharp\songs\01 - Super Mario Bros.wav";
                player.PlayLooping();
            }

            CurrentOpstacle = SnakeObstaclesList[SnakeObstaclesList.Count / 2 + 2];
            while (true)
            {
                UpdateLevel();
                // Read user input
                if (Console.KeyAvailable)
                {
                    var key = Console.ReadKey();

                    if (key.Key == ConsoleKey.O && MusicPlayer == true)
                    {
                        player.Stop();
                        MusicPlayer = false;
                    }
                    if (key.Key == ConsoleKey.P && MusicPlayer == false)
                    {
                        player.PlayLooping();
                        MusicPlayer = true;
                    }
                    if (key.Key == ConsoleKey.Spacebar && PauseMode == false)
                    {
                        PauseMode = true;

                        Write("╔═══════════════╗", SnakeFieldRows / 2 - 1, SnakeFieldCols / 2 - 7);
                        Write("║               ║", SnakeFieldRows / 2, SnakeFieldCols / 2 - 7);
                        Write("║     Pause     ║", SnakeFieldRows / 2 + 1, SnakeFieldCols / 2 - 7);
                        Write("║               ║", SnakeFieldRows / 2 + 2, SnakeFieldCols / 2 - 7);
                        Write("╚═══════════════╝", SnakeFieldRows / 2 + 3, SnakeFieldCols / 2 - 7);
                        player.Stop();
                        Console.ReadKey();
                        if (MusicPlayer)
                        {
                            player.PlayLooping();
                        }
                    }

                    if (key.Key == ConsoleKey.Spacebar && PauseMode == true)
                    {
                        PauseMode = false;
                    }

                    if (key.Key == ConsoleKey.Escape)
                    {
                        return;
                    }

                    if ((key.Key == ConsoleKey.LeftArrow || key.Key == ConsoleKey.A) && OpposideDirectionLeft)
                    {
                        ChangeDirection("DirectionLeft");
                        OpposideDirectionLeft  = true;
                        OpposideDirectionRight = false;
                        OpposideDirectionUp    = true;
                        OpposideDirectionDown  = true;
                    }

                    if ((key.Key == ConsoleKey.RightArrow || key.Key == ConsoleKey.D) && OpposideDirectionRight)
                    {
                        ChangeDirection("DirectionRight");
                        OpposideDirectionRight = true;
                        OpposideDirectionLeft  = false;
                        OpposideDirectionUp    = true;
                        OpposideDirectionDown  = true;
                    }

                    if ((key.Key == ConsoleKey.UpArrow || key.Key == ConsoleKey.W) && OpposideDirectionUp)
                    {
                        ChangeDirection("DirectionUp");
                        OpposideDirectionUp    = true;
                        OpposideDirectionDown  = false;
                        OpposideDirectionLeft  = true;
                        OpposideDirectionRight = true;
                    }

                    if ((key.Key == ConsoleKey.DownArrow || key.Key == ConsoleKey.S) && OpposideDirectionDown)
                    {
                        ChangeDirection("DirectionDown");
                        OpposideDirectionDown  = true;
                        OpposideDirectionUp    = false;
                        OpposideDirectionLeft  = true;
                        OpposideDirectionRight = true;
                    }
                }

                //Update the game state
                if (DirectionDown)
                {
                    SnakeFigureRow++;
                }
                else if (DirectionUp)
                {
                    SnakeFigureRow--;
                }
                else if (DirectionLeft)
                {
                    SnakeFigureCol--;
                }
                else if (DirectionRight)
                {
                    SnakeFigureCol++;
                }

                //Draw Game Units
                DrawBorder();
                DrawInfo();
                DrawSnake();
                MoveSnake();
                DrawOpstacle();
                Thread.Sleep(SleepSec);
            }
        }