Beispiel #1
0
        static void Main(string[] args)
        {
            WriteLine("Welcome to Ssssssnake!");
begin:
            WriteLine("Select Difficulty:");
            WriteLine("e (Easy), m (Medium), h (Hard), i (Impossible)");

            diffSel = ReadLine();

            if (diffSel == "e" || diffSel == "E")  //difficulty speed
            {
                speed  = 200;
                limitX = 75;
                limitY = 50;
            }
            else if (diffSel == "m" || diffSel == "M")
            {
                speed  = 100;
                limitX = 75;
                limitY = 50;
            }
            else if (diffSel == "h" || diffSel == "H")
            {
                speed  = 50;
                limitX = 50;
                limitY = 25;
            }
            else if (diffSel == "i" || diffSel == "I")
            {
                speed  = 25;
                limitX = 50;
                limitY = 25;
            }
            else
            {
                WriteLine("Invalid choice");
                goto begin;
            }

            Clear(); //may be redundant
            DrawBoard.Init();
            DrawSnake.Init();
            DrawSnake.Draw();      //initial drawing of snake and apple
            HungryForApples.Run(); //main loop

            Clear();
            WriteLine("Game Over! Play Again?"); //after main loop exits
            WriteLine("y for yes or any other key to exit");
            string gameOver = ReadLine();

            if (gameOver == "Y" || gameOver == "y")
            {
                Clear();
                diffSel = "";
                DrawSnake.snakeLength = 0;
                DrawApple.appleGotCtr = 0;
                goto begin;
            }
        }
Beispiel #2
0
        public static void Run()
        {
            movement = 0; //always start going right


            while (true)                                                                        //main game loop
            {
                Thread.Sleep(speed);                                                            //takes speed from Game.cs

                if (currentX == 0 || currentY == 0 || currentX == limitX || currentY == limitY) //ends game if hits border
                {
                    break;
                }

                if (DrawApple.appleX == currentX && DrawApple.appleY == currentY) //apple got logic
                {
                    DrawApple.appleGot = true;
                    DrawApple.appleGotCtr++;
                    DrawSnake.snakeLength++;
                    snakeBodyX[snakeLength] = currentX;
                    snakeBodyY[snakeLength] = currentY;
                }

                DrawSnake.Draw(); //draws the snake

                if (KeyAvailable)
                {
                    while (true)
                    {
                        ConsoleKeyInfo turn = ReadKey(true);
                        if (turn.Key.Equals(RightArrow) && movement != 1)
                        {
                            movement = 0;
                            //break;
                        }
                        else if (turn.Key.Equals(LeftArrow) && movement != 0)
                        {
                            movement = 1;
                            //break;
                        }
                        else if (turn.Key.Equals(DownArrow) && movement != 3)
                        {
                            movement = 2;
                            //break;
                        }
                        else if (turn.Key.Equals(UpArrow) && movement != 2)
                        {
                            movement = 3;
                            //break;
                        }
                        break;
                    }
                }
                DrawSnake.writeDirection = movement;
                switch (movement)
                {
                case 0:
                    currentX++;
                    break;

                case 1:
                    currentX--;
                    break;

                case 2:
                    currentY++;
                    break;

                case 3:
                    currentY--;
                    break;
                }
            }
        }