Пример #1
0
        public static void PrintOutGameBoard(List <SnakeFragment> fragments, Apple apple)
        {
            int  y = 0;
            int  x = 0;
            bool positionfilled;
            bool snakeHere;

            Console.ForegroundColor = ConsoleColor.DarkGray;
            //Console.BackgroundColor = ConsoleColor.White;
            for (y = 0; y < GameBoardSnake.YSize; y++)
            {
                Console.Write("\t\t\t\t\t");
                for (x = 0; x < GameBoardSnake.XSize; x++)
                {
                    positionfilled = false;
                    snakeHere      = false;
                    if (x == 0 || x == GameBoardSnake.XSize - 1)
                    {
                        Console.Write("\u2588");
                        positionfilled = true;
                    }
                    else if (y == 0 || y == GameBoardSnake.YSize - 1)
                    {
                        Console.Write("\u2588");
                        positionfilled = true;
                    }

                    foreach (SnakeFragment pos in fragments)
                    {
                        if (pos.XPosition == x && pos.YPosition == y)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkGreen;
                            Console.Write("\u019F");
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            positionfilled          = true;
                            snakeHere = true;
                            break;
                        }
                    }
                    if (apple.XPosition == x && apple.YPosition == y)
                    {
                        if (!snakeHere)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkRed;
                            Console.Write("\u2665");
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            positionfilled          = true;
                        }
                        else
                        {
                            SnakeGame.Counter++;
                            //add snake fragment
                            SnakeGame.AddSnakeFragment = true;
                            //set new apple
                            apple.NewApplePosition();
                        }
                    }
                    if (!positionfilled)
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
        }
Пример #2
0
        public static void PlaySnake()
        {
            bool playAgain = true;

            Console.OutputEncoding = Encoding.Unicode;
            while (playAgain)
            {
                playAgain = false;

                Counter = 0;
                GameBoardSnake.XSize = 25;
                GameBoardSnake.YSize = 15;
                Apple         apple         = new Apple();
                Snake         snake         = new Snake(5, 5);
                SnakeFragment snakeFragment = new SnakeFragment(0, 0);
                Console.Clear();
                Intro();
                Console.ForegroundColor = ConsoleColor.Gray;
                //Console.WriteLine("\u263A");
                Console.WriteLine("\n");
                Console.WriteLine("\t\t\t\tTryck valfri tangent för att starta.\n");
                Console.ReadKey(true);
                Console.Clear();
                while (snake.Alive)
                {
                    AddSnakeFragment = false;
                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine("\n\n\n\n");
                    GameBoardSnake.PrintOutGameBoard(snake.WholeSnake, apple);

                    if (Console.KeyAvailable)
                    {
                        switch (Console.ReadKey(true).Key)
                        {
                        case ConsoleKey.UpArrow:
                            if (snake.DirectionY != 1)
                            {
                                snake.DirectionX = 0;
                                snake.DirectionY = -1;
                            }
                            break;

                        case ConsoleKey.DownArrow:
                            if (snake.DirectionY != -1)
                            {
                                snake.DirectionX = 0;
                                snake.DirectionY = +1;
                            }
                            break;

                        case ConsoleKey.LeftArrow:
                            if (snake.DirectionX != 1)
                            {
                                snake.DirectionX = -1;
                                snake.DirectionY = 0;
                            }
                            break;

                        case ConsoleKey.RightArrow:
                            if (snake.DirectionX != -1)
                            {
                                snake.DirectionX = +1;
                                snake.DirectionY = 0;
                            }
                            break;

                        default:
                            break;
                        }
                    }

                    //if (AddSnakeFragment)
                    //{ snakeFragment = snake.LastSnakeFragment(); }
                    snake.WholeSnake = snake.NewPositionsForSnakeFragments();
                    //if (AddSnakeFragment)
                    //{ snake.AddOneMoreSnakeFragment(snakeFragment); }
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"\t\t\t\t\tPOÄNG: {SnakeGame.Counter}");
                    snake.CheckIfDies();
                    if (snake.DirectionY != 0)
                    {
                        System.Threading.Thread.Sleep(150);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }
                Console.WriteLine();
                Console.WriteLine("\t\t\t\t\tDu är död :(\n\n");
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("\t\t\t\t\tTryck för att se Highscore.");
                Console.ReadKey(true);

                Console.Clear();
                HighScoreEachGame.OpenHighscores("highscoresnake.txt", HighScoreEachGame.ListSnake);
                HighScoreEachGame.PrintOutHighscores("----------------------SNAKE--------------------", HighScoreEachGame.ListSnake);
                Console.WriteLine();
                Console.WriteLine($"\t\t\t\t\tDu fick {SnakeGame.Counter} poäng.\n");
                int maybeHighscore = HighScoreEachGame.SeeIfHighscore(HighScoreEachGame.ListSnake, SnakeGame.Counter);
                if (maybeHighscore < 11)
                {
                    Console.WriteLine("\t\t\t\t\tGrattis! Du kom in på Highscore-listan!");
                    HighScoreEachGame.PutHighScoreInList(maybeHighscore, SnakeGame.Counter, HighScoreEachGame.ListSnake);
                    HighScoreEachGame.PutHighScoreInFile("highscoresnake.txt", HighScoreEachGame.ListSnake);
                    Console.Clear();
                    HighScoreEachGame.OpenHighscores("highscoresnake.txt", HighScoreEachGame.ListSnake);
                    HighScoreEachGame.PrintOutHighscores("----------------SNAKE----------------", HighScoreEachGame.ListSnake);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("\t\t\t\t\tBättre lycka nästa gång!\n\n");
                }
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("\t\t\t\t\tVill du spela igen?");
                Console.WriteLine("\t\t\t\t\t[1] --> JA!");
                Console.WriteLine("\t\t\t\t\t[0] --> EXIT.");
                Console.Write("\t\t\t\t\t");
                int choice = InputNumber(0, 1);
                if (choice == 1)
                {
                    playAgain = true;
                }
            }
            Console.ReadLine();
        }