Example #1
0
 public void Reset(GameWindow window, int width, int height)
 {
     this.Window = window;
     this.Direction = Directions.Right;
     this.HeadPosition = new Position { x = 0, y = 0 };
     this.Width = width;
     this.Height = height;
     this.Map = new int[width, height];
     this.Map[HeadPosition.x, HeadPosition.y] = EntityType.BodyGenStart;
     this.Length = 0;
     GenerateFood();
 }
Example #2
0
 static Position GenerateFood(ref int lastFoodTime, Random randGen)
 {
     Console.ForegroundColor = ConsoleColor.Green;
     do
     {
         food = new Position(randGen.Next(0, Console.WindowHeight),
          randGen.Next(0, Console.WindowWidth));
     } while (snakeElements.Contains(food));
     lastFoodTime = Environment.TickCount;
     Console.SetCursorPosition(food.col, food.row);
     Console.Write("@");
     Console.ForegroundColor = ConsoleColor.White;
     return food;
 }
Example #3
0
        public Snake()
        {
            Random rnd = new Random();
            Direction dir = (Direction)rnd.Next(0, 3);
            var StartPos = new Position() { X = 40, Y = 12 };                               //Center of the screen

            MoveDirection = new MoveDirection() { Direction = dir};
            Blocks = new List<Snake.SnakeBlock>() {
                new SnakeBlock() { IsHead = true, Symbol = "#", Position = StartPos, Type = ObjectType.Player },
                new SnakeBlock() { IsHead = false, Symbol = "#", Position = StartPos + dir.NextPos(1), Type = ObjectType.Player},
                new SnakeBlock() { IsHead = false, Symbol = "#", Position = StartPos + dir.NextPos(2), Type = ObjectType.Player}
            };
            Blocks[1].Parent = Blocks.First(); //ugh, hack to wire parents :(
            Blocks[2].Parent = Blocks.First();
        }
Example #4
0
        static void Main()
        {
            RemoveScrollbars();
            food = GenerateFood(ref lastFoodTime, randGen);
            GenerateFirstSnakeElements();
            PrintSnake(snakeElements);

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey();
                    if (pressedKey.Key == ConsoleKey.RightArrow)
                    {
                        if (direction != left) direction = right;
                    }
                    if (pressedKey.Key == ConsoleKey.LeftArrow)
                    {
                        if (direction != right) direction = left;
                    }
                    if (pressedKey.Key == ConsoleKey.DownArrow)
                    {
                        if (direction != up) direction = down;
                    }
                    if (pressedKey.Key == ConsoleKey.UpArrow)
                    {
                        if (direction != down) direction = up;
                    }
                }

                Position snakeHead = snakeElements.Last();
                Position nextDirection = directions[direction];
                Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);

                //check if snake went out of console and move snake to the other side of console
                if (snakeNewHead.col < 0) snakeNewHead.col = Console.WindowWidth - 1;
                if (snakeNewHead.col >= Console.WindowWidth) snakeNewHead.col = 0;
                if (snakeNewHead.row < 0) snakeNewHead.row = Console.WindowHeight - 1;
                if (snakeNewHead.row >= Console.WindowHeight) snakeNewHead.row = 0;

                //if  snake hit its tail -> game over
                if (snakeElements.Contains(snakeNewHead))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 4, Console.WindowHeight / 2);
                    Console.WriteLine("Game over");
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 4, Console.WindowHeight / 2 + 1);
                    int userPoints = (snakeElements.Count() - 6) * 100 - penalty;
                    userPoints = Math.Max(userPoints, 0);
                    Console.WriteLine("Your points are {0}", userPoints);
                    Console.ReadKey();
                    return;
                }

                //
                Console.SetCursorPosition(snakeHead.col, snakeHead.row);
                Console.Write("*");
                snakeElements.Enqueue(snakeNewHead);
                Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write(snakeHeadSymbol[direction]);
                Console.ForegroundColor = ConsoleColor.White;

                if (snakeNewHead.col == food.col && snakeNewHead.row == food.row)
                {
                    //feeding the snake
                    food = GenerateFood(ref lastFoodTime, randGen);
                }
                else
                {
                    //snake is just moving
                    Position last = snakeElements.Dequeue(); //remove snake tail if she didnt eat a food
                    Console.SetCursorPosition(last.col, last.row);
                    Console.Write(" ");
                }

                //check if food has disappeared
                if ((Environment.TickCount - lastFoodTime) >= foodDissapearTime)
                {
                    //peanlty for missed food
                    penalty += 50;

                    //remove old food
                    Console.SetCursorPosition(food.col, food.row);
                    Console.WriteLine(" ");

                    //create new food
                    food = GenerateFood(ref lastFoodTime, randGen);
                }

                Thread.Sleep(100);
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            end:
            Console.Title = "SNAKE - MADE BY MARTIN NIKOLOV";

            //Set Console's Windows Sizes
            Console.WindowHeight = 25;
            Console.WindowWidth = 50;

            //Set Console's Buffer Sizes;
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth;

            //Random Generator
            Random randomNumberGenerator = new Random();

            //Variable increasing the speed
            double speedIncreaser = 100;

            //First elemets of our snake
            Queue<Position> snakeElements = new Queue<Position>();
            for (int i = 0; i <= 4; i++)
            {
                snakeElements.Enqueue(new Position(0, i));
            }

            //Generating random coordinations of food
            Position food;
            int foodPushTime;
            do
            {
                food = new Position(
                    randomNumberGenerator.Next(1, Console.WindowHeight - 1),
                    randomNumberGenerator.Next(1, Console.WindowWidth - 1));
                foodPushTime = Environment.TickCount;
            }
            while (snakeElements.Contains(food));

            //Print food on the Console
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.SetCursorPosition(food.col, food.row);
            Console.Write("@");
            Console.ResetColor();

            //Rock
            List<Position> rocks = new List<Position>();
            for (int i = 0; i < randomNumberGenerator.Next(1, 11); i++)
            {
                do
                {
                    rocks.Add(new Position(randomNumberGenerator.Next(1, Console.WindowHeight - 1),
                        randomNumberGenerator.Next(1, Console.WindowWidth - 1)));
                }
                while (snakeElements.Contains(rocks[i]) ||
                       (food.row == rocks[i].row && food.col == rocks[i].col));
            }

            //Print rocks
            foreach (Position rock in rocks)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.SetCursorPosition(rock.col, rock.row);
                Console.Write("=");
                Console.ResetColor();
            }

            //Easy way to access every direction
            byte right = 0;
            byte left = 1;
            byte down = 2;
            byte up = 3;

            //Available directions
            Position[] directions = 
            {
                new Position(0,1), //Right
                new Position(0,-1), //Left
                new Position(1,0), //Down
                new Position(-1,0)//Up
            };

            //Set start direction - easiest right
            byte currentDirection = right;

            //Print snake with the default coordinations
            foreach (Position item in snakeElements)
            {
                Console.SetCursorPosition(item.col, item.row);
                Console.Write("*");
            }

            while (true)
            {
                //Check if we are typed specific key
                if (Console.KeyAvailable)
                {
                    //We are typed any key => we have to keep this key in variable
                    ConsoleKeyInfo userKeyInput = Console.ReadKey();

                    //Now we check which is this key
                    if (userKeyInput.Key == ConsoleKey.RightArrow)
                    {
                        //Change position if only old position is not opposite of new position
                        if (currentDirection != left)
                            currentDirection = right;
                    }
                    else if (userKeyInput.Key == ConsoleKey.LeftArrow)
                    {
                        if (currentDirection != right)
                            currentDirection = left;
                    }
                    else if (userKeyInput.Key == ConsoleKey.DownArrow)
                    {
                        if (currentDirection != up)
                            currentDirection = down;
                    }
                    else if (userKeyInput.Key == ConsoleKey.UpArrow)
                    {
                        if (currentDirection != down)
                            currentDirection = up;
                    }
                }

                //Get current coordinatios of snake head
                Position snakeCurrentHead = snakeElements.Last();

                //Calculation coordinatios of next direction
                Position nextDirection = directions[currentDirection];

                //Calculation coordinatios of the new snake head
                Position snakeNewHead = new Position(
                    snakeCurrentHead.row + nextDirection.row,
                    snakeCurrentHead.col + nextDirection.col);

                //Check if snake kill yourself
                if (snakeElements.Contains(snakeNewHead) || rocks.Contains(snakeNewHead))
                {
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine("GAME OVER!" + "\n" + "Your points are {0}!", (snakeElements.Count - 5) * 100);

                    Console.SetCursorPosition(10, 11);
                    Console.WriteLine("Press [SPACE] for new game...");

                    Console.SetCursorPosition(0, Console.WindowHeight - 1);
                    ConsoleKeyInfo userKeyInput = Console.ReadKey();

                    if (userKeyInput.Key == ConsoleKey.Spacebar)
                    {
                        Console.ResetColor();
                        Console.Clear();
                        goto end;
                    }
                    else
                    {
                        Console.ResetColor();

                        Console.SetCursorPosition(0, Console.WindowHeight - 1);
                        return;
                    }
                }

                //Check if snake is got the food, if not clear last element
                if (snakeNewHead.row == food.row && snakeNewHead.col == food.col)
                {
                    Console.SetCursorPosition(food.col, food.row);
                    Console.Write(" ");

                    Console.Beep(80, 50);

                    do
                    {
                        food = new Position(
                            randomNumberGenerator.Next(1, Console.WindowHeight - 1),
                            randomNumberGenerator.Next(1, Console.WindowWidth - 1));
                        foodPushTime = Environment.TickCount;
                    }
                    while (snakeElements.Contains(food) || rocks.Contains(food));

                    //Print food on the Console
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.SetCursorPosition(food.col, food.row);
                    Console.Write("@");
                    Console.ResetColor();

                    int fiftyFifty = randomNumberGenerator.Next(1, 3);

                    if (fiftyFifty == 1)
                    {
                        //Adding new rock
                        Position rockElement;

                        do
                        {
                            rockElement = new Position(randomNumberGenerator.Next(1, Console.WindowHeight - 1),
                                randomNumberGenerator.Next(1, Console.WindowWidth - 1));
                        }
                        while (snakeElements.Contains(rockElement) ||
                               (food.row == rockElement.row && food.col == rockElement.col));

                        rocks.Add(new Position(rockElement.row, rockElement.col));

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.SetCursorPosition(rockElement.col, rockElement.row);
                        Console.Write("=");
                        Console.ResetColor();
                    }
                }
                else
                {
                    //Get and clear last element of snake
                    Position lastElement = snakeElements.Dequeue();
                    Console.SetCursorPosition(lastElement.col, lastElement.row);
                    Console.Write(" ");
                }

                //Check if coordinatios of new head are available
                if (snakeNewHead.row < 0)
                    snakeNewHead.row = Console.WindowHeight - 2;
                else if (snakeNewHead.row >= Console.WindowHeight - 1)
                    snakeNewHead.row = 0;
                else if (snakeNewHead.col < 0)
                    snakeNewHead.col = Console.WindowWidth - 2;
                else if (snakeNewHead.col >= Console.WindowWidth - 1)
                    snakeNewHead.col = 0;

                //Convert old head as a body
                Console.SetCursorPosition(snakeCurrentHead.col, snakeCurrentHead.row);
                Console.WriteLine("*");

                //Print head with a individual symbol
                snakeElements.Enqueue(snakeNewHead);
                Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                if (currentDirection == right)
                    Console.Write(">");
                else if (currentDirection == left)
                    Console.Write("<");
                else if (currentDirection == down)
                    Console.Write("V");
                else if (currentDirection == up)
                    Console.Write("^");

                if (Environment.TickCount - foodPushTime >= 6000)
                {
                    Console.SetCursorPosition(food.col, food.row);
                    Console.Write(" ");

                    do
                    {
                        food = new Position(
                            randomNumberGenerator.Next(1, Console.WindowHeight - 1),
                            randomNumberGenerator.Next(1, Console.WindowWidth - 1));
                        foodPushTime = Environment.TickCount;
                    }
                    while (snakeElements.Contains(food) || rocks.Contains(food));

                    int fiftyFifty = randomNumberGenerator.Next(1, 3);

                    if (fiftyFifty == 1)
                    {
                        //Adding new rock
                        Position rockElement;

                        do
                        {
                            rockElement = new Position(randomNumberGenerator.Next(1, Console.WindowHeight - 1),
                                randomNumberGenerator.Next(1, Console.WindowWidth - 1));
                        }
                        while (snakeElements.Contains(rockElement) ||
                               (food.row == rockElement.row && food.col == rockElement.col));

                        rocks.Add(new Position(rockElement.row, rockElement.col));

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.SetCursorPosition(rockElement.col, rockElement.row);
                        Console.Write("=");
                        Console.ResetColor();
                    }
                }

                //Always show the food
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.SetCursorPosition(food.col, food.row);
                Console.Write("@");
                Console.ResetColor();

                speedIncreaser = speedIncreaser - 0.025;
                Thread.Sleep((int)speedIncreaser);
            }
        }
Example #6
0
            static void Main(string[] args)
            {
                byte right = 0;
                byte left = 1;
                byte down = 2;
                byte up = 3;

                //Environment.TickCount tova 6te broi vremeto ot kakto e pusnata programata
                //DateTime.Now
                int lastFoodTime = 0;
                int foodDisappearTime = 8000; // 8 sec delay

                lastFoodTime = Environment.TickCount;

                Position[] directions = new Position[]{
                    new Position(0,1), //right
                    new Position(0,-1),//left
                    new Position(1,0), // down
                    new Position(-1,0), // up
                };

                int sleepTime = 100;
                int direction = right; // 0 teku6tat posoka // saotvetno kat elementi na masiva positoin
                Random randomNumberGenerator = new Random();
                Console.BufferHeight = Console.WindowHeight; // consolata da nqma scrollbar i buffera da bude golqm kolkoto consolata

                Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto
                    randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata

                Queue<Position> snakeElements = new Queue<Position>(); // tova 6te e opa6kata TUKA SE PRAVQT ZMIQTA 
                Position position1 = new Position();



                for (int i = 0; i <= 5; i++)
                {
                    snakeElements.Enqueue(new Position(0, i));
                }
                foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta
                {
                    Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona
                    Console.WriteLine('*');
                }

                while (true)
                {
                    if (Console.KeyAvailable) // proverqva dali potrebitelq e natisnal nqkakvo kop4e
                    {

                        ConsoleKeyInfo userInput = Console.ReadKey(); // pro4ita ne6to tam
                        if (userInput.Key == ConsoleKey.LeftArrow)
                        {
                            if (direction != right) // ako posokata ne e nadqsno 4ak togava da se mesti nalqvo
                            {
                                direction = left;
                            }
                        }
                        if (userInput.Key == ConsoleKey.RightArrow)
                        {
                            if (direction != left)
                            {
                                direction = right;
                            }

                        }
                        if (userInput.Key == ConsoleKey.UpArrow)
                        {
                            if (direction != down)
                            {
                                direction = up;
                            }

                        }
                        if (userInput.Key == ConsoleKey.DownArrow)
                        {
                            if (direction != up) // ako posokata ne e nagore 4ak togava da se mestim nadolo
                            {
                                direction = down;
                            }
                        }

                    }
                    Position snakeHead = snakeElements.Last(); // tozi method vru6ta posledniq element ot opa6kata

                    Position nextDirection = directions[direction]; // s tozi red vzimame na kade da se dviji zmiqta kato directions e masiva a direction 0,1,2,3

                    //novata poziciq na zmiqta
                    Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);// purvo e X posle e Y kato za primer vzimame stariq direction na row i go
                    //Position newHead = snakeElements.Last();// tova ni vru6ta glavata na na6ta zmiq i q zapisva v promenlivata new head
                    snakeElements.Enqueue(snakeNewHead); // tozi kod slaga nova glava vseki put na novata poziciq
                    // tova proverqva dali glavata na zmiqta ne e izlezla ot ekrana bukvalno
                    if (snakeNewHead.row < 0 ||
                        snakeNewHead.col < 0 ||
                        snakeNewHead.row >= Console.WindowHeight ||
                        snakeNewHead.col >= Console.WindowWidth) // row = red
                    {
                        Console.SetCursorPosition(0, 0);
                        Console.WriteLine();
                        Console.WriteLine("Game over");
                        Console.WriteLine("Your points are: {0}", (snakeElements.Count - 6) * 100); // tova vru6ta broq elementi na zmiqta
                        return; // prikliu4va izpulnenieto na teku6tiq method
                    }


                    //TOVA E KODA AKO ZMIQTA APNE QBALKATA
                    if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) // tuka slagame statement koito proverqva dali glavata na zmiqta se zasi4a s tazi na xranata
                    {
                        do
                        {
                            food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto, no tozi put prosto vzima novi stoinosti za novata qbalka
                        randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata
                        }
                        while (snakeElements.Contains(food));
                        sleepTime -= 5; // da stava s 20 milisekundi po-burza
                        Random r = new Random();
                        Console.Beep(r.Next(100, 10000), r.Next(60, 500));
                    }
                    else // samo kogato zmiqta ne udrq lqbalkata 6te se maxa edna zvezda ot opajkata
                    {
                        snakeElements.Dequeue();
                    }


                    // garanciqta 4e 6te imame nova qbalka i zmiq e 4e console clear e predi tqx a ne sled tqx


                    Console.Clear(); // 4isti konzolata

                    foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta
                    {
                        Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona
                        Console.WriteLine('*');
                    }
                    lastFoodTime = Environment.TickCount; // broq na milisekundi ot na4aloto na sistemata
                    
                    // TUka e koda za kazvane na programata da 4aka 8 secundi
                    if (Environment.TickCount - lastFoodTime >= foodDisappearTime)
                    {
                        Console.SetCursorPosition(food.col, food.row); // TAKA MAXAME STARATA QBALKA
                        Console.WriteLine(" ");
                        do
                        {
                            food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto, no tozi put prosto vzima novi stoinosti za novata qbalka
                        randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata
                        }
                        while (snakeElements.Contains(food));

                    }
                    Console.SetCursorPosition(food.col, food.row); // S TOZI KOD RISUVAME QBALKATA NA RANDOM MESTO V KONZOLATA
                    Console.Write('@');



                    Thread.Sleep(sleepTime); // konzolata spira prosto bavi zmiqta s opredeleno vreme
                }
            }
        static void Main(string[] args)
        {
            byte right = 0;
            byte left = 1;
            byte up = 2;
            byte down = 3;
            int lastFoodTime = 0;
            int foodDissapearTime = 8000;
            int negativePoints = 0;

            Position[] directions = new Position[]
            {
                new Position(0,1), //right
                new Position(0, -1), //left
                new Position(-1, 0), //up
                new Position(1, 0), //down
            };
            double sleepTime = 100;
            int direction = 0;
            Random randomNumbersGenerator = new Random();
            Console.BufferHeight = Console.WindowHeight;
            lastFoodTime = Environment.TickCount;

            List<Position> obsticles = new List<Position>()
            {
                new Position(12,12),
                new Position(15,30),
                new Position(7,14),
            };
            foreach (var obstucle in obsticles)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.SetCursorPosition(obstucle.col, obstucle.row);
                Console.Write("X");
            }

            Queue<Position> snakeEl = new Queue<Position>();
            for (int i = 0; i <= 5; i++)
            {
                snakeEl.Enqueue(new Position(0, i));
            }

            Position food;
            do
            {
                food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth));
            }
            while (snakeEl.Contains(food) || obsticles.Contains(food));
            Console.SetCursorPosition(food.col, food.row);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("@");

            foreach (Position position in snakeEl)
            {
                Console.SetCursorPosition(position.col, position.row);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("*");
            }

            while (true)
            {
                negativePoints++;

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo input = Console.ReadKey();
                    if (input.Key == ConsoleKey.LeftArrow)
                    {
                        if (direction != right)
                        {
                            direction = left;
                        }
                    }
                    if (input.Key == ConsoleKey.RightArrow)
                    {
                        if (direction != left)
                        {

                            direction = right;
                        }
                    }
                    if (input.Key == ConsoleKey.UpArrow)
                    {
                        if (direction != down)
                        {
                            direction = up;
                        }
                    }
                    if (input.Key == ConsoleKey.DownArrow)
                    {
                        if (direction != up)
                        {
                            direction = down;
                        }
                    }
                }

                Position snakeHead = snakeEl.Last();
                Position nextDirection = directions[direction];
                Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);

                if (snakeNewHead.col < 0)
                {
                    snakeNewHead.col = Console.WindowWidth - 1;
                }
                if (snakeNewHead.row < 0)
                {
                    snakeNewHead.row = Console.WindowHeight - 1;
                }
                if (snakeNewHead.row >= Console.WindowHeight)
                {
                    snakeNewHead.row = 0;
                }
                if (snakeNewHead.col >= Console.WindowWidth)
                {
                    snakeNewHead.col = 0;
                }

                /*(snakeNewHead.row < 0 || snakeNewHead.col < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth || */
                if (snakeEl.Contains(snakeNewHead) || obsticles.Contains(snakeNewHead))
                {
                    Console.SetCursorPosition(0, 0);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Game Over!");
                    int userPoints = ((snakeEl.Count - 6) * 100) - negativePoints;
                    userPoints = Math.Max(userPoints, 0);
                    Console.WriteLine($"Your point are: {userPoints}");
                    return;
                }

                Console.SetCursorPosition(snakeHead.col, snakeHead.row);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("*");

                snakeEl.Enqueue(snakeNewHead);
                Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                if (direction == right) Console.Write(">");
                if (direction == left) Console.Write("<");
                if (direction == up) Console.Write("^");
                if (direction == down) Console.Write("v");

                if (snakeNewHead.col == food.col && snakeNewHead.row == food.row)
                {
                    do
                    {
                        food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth));
                    }
                    while (snakeEl.Contains(food) || obsticles.Contains(food));
                    lastFoodTime = Environment.TickCount;
                    Console.SetCursorPosition(food.col, food.row);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("@");
                    sleepTime--;

                    Position obsticle = new Position();
                    do
                    {
                        obsticle = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth));
                    }
                    while (snakeEl.Contains(obsticle) || obsticles.Contains(obsticle) || food.row != obsticle.row && food.col != food.col);
                    obsticles.Add(obsticle);
                    Console.SetCursorPosition(obsticle.col, obsticle.row);
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write("X");
                }
                else
                {
                    Position last = snakeEl.Dequeue();
                    Console.SetCursorPosition(last.col, last.row);
                    Console.Write(" ");
                }

                if (Environment.TickCount - lastFoodTime >= foodDissapearTime)
                {
                    negativePoints = negativePoints + 50;
                    Console.SetCursorPosition(food.col, food.row);
                    Console.Write(" ");
                    do
                    {
                        food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth));
                    }
                    while (snakeEl.Contains(food));
                    lastFoodTime = Environment.TickCount;
                }

                Console.SetCursorPosition(food.col, food.row);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("@");

                sleepTime -= 0.01;

                Thread.Sleep((int)sleepTime);

            }
        }
Example #8
0
            static void Main(string[] args)
            {

                Position[] directions = new Position[]{
                    new Position(0,1), //right
                    new Position(0,-1),//left
                    new Position(1,0), // down
                    new Position(-1,0), // up
                };
                int direction = 0; // 0 teku6tat posoka // saotvetno kat elementi na masiva positoin
                Random randomNumberGenerator = new Random();
                Console.BufferHeight = Console.WindowHeight; // consolata da nqma scrollbar i buffera da bude golqm kolkoto consolata

                Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto
                    randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata

                Queue<Position> snakeElements = new Queue<Position>(); // tova 6te e opa6kata TUKA SE PRAVQT ZMIQTA I QDENETO
                Position position1 = new Position();



                for (int i = 0; i <= 5; i++)
                {
                    snakeElements.Enqueue(new Position(0, i));
                }
                foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta
                {
                    Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona
                    Console.WriteLine('*');
                }

                while (true)
                {
                    if (Console.KeyAvailable) // proverqva dali potrebitelq e natisnal nqkakvo kop4e
                    {

                        ConsoleKeyInfo userInput = Console.ReadKey(); // pro4ita ne6to tam
                        if (userInput.Key == ConsoleKey.LeftArrow)
                        {
                            direction = 1;
                        }
                        if (userInput.Key == ConsoleKey.RightArrow)
                        {
                            direction = 0;
                        }
                        if (userInput.Key == ConsoleKey.UpArrow)
                        {
                            direction = 3;
                        }
                        if (userInput.Key == ConsoleKey.DownArrow)
                        {
                            direction = 2;
                        }

                    }
                    Position snakeHead = snakeElements.Last(); // tozi method vru6ta posledniq element ot opa6kata

                    Position nextDirection = directions[direction]; // s tozi red vzimame na kade da se dviji zmiqta kato directions e masiva a direction 0,1,2,3

                    //novata poziciq na zmiqta
                    Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);// purvo e X posle e Y kato za primer vzimame stariq direction na row i go
                    //Position newHead = snakeElements.Last();// tova ni vru6ta glavata na na6ta zmiq i q zapisva v promenlivata new head
                    snakeElements.Enqueue(snakeNewHead); // tozi kod slaga nova glava vseki put na novata poziciq
                    // tova proverqva dali glavata na zmiqta ne e izlezla ot ekrana bukvalno
                    if (snakeNewHead.row < 0 || snakeNewHead.col < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth) // row = red
                    {
                        Console.SetCursorPosition(0, 0);
                        Console.WriteLine();
                        Console.WriteLine("Game over");
                        Console.WriteLine("Your points are: {0}", (snakeElements.Count - 6) * 100); // tova vru6ta broq elementi na zmiqta
                        return; // prikliu4va izpulnenieto na teku6tiq method
                    }


                    //TOVA E KODA AKO ZMIQTA APNE QBALKATA
                    if (snakeNewHead.col == food.col && snakeNewHead.row == food.row) // tuka slagame statement koito proverqva dali glavata na zmiqta se zasi4a s tazi na xranata
                    {
                        food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto, no tozi put prosto vzima novi stoinosti za novata qbalka
                    randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata
                    }
                    else // samo kogato zmiqta ne udrq lqbalkata 6te se maxa edna zvezda ot opajkata
                    {
                        snakeElements.Dequeue();
                    }


                    // garanciqta 4e 6te imame nova qbalka i zmiq e 4e console clear e predi tqx a ne sled tqx


                    Console.Clear(); // 4isti konzolata

                    foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta
                    {
                        Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona
                        Console.WriteLine('*');
                    }
                    Console.SetCursorPosition(food.col, food.row); // S TOZI KOD RISUVAME QBALKATA NA RANDOM MESTO V KONZOLATA
                    Console.Write('@');



                    Thread.Sleep(100); // konzolata spira prosto bavi zmiqta s opredeleno vreme
                }
            }
Example #9
0
        static void Main()
        {
            byte right = 0;
            byte left = 1;
            byte down = 2;
            byte up = 3;

            double sleepTime = 100;
            int lastFoodTime = 0;
            int foodDissapearTime = 8000;
            int negativePoints = 0;

            Position[] directions = new Position[]
                {
                    new Position(0,1),//right
                    new Position(0,-1),//left
                    new Position(1,0),//down
                    new Position(-1,0)//up
                };

            Console.BufferHeight = Console.WindowHeight; //fix the problem with snake out from console

            List<Position> obstacles = new List<Position>()
            {
                new Position(12,12),
                new Position(14,40),
                new Position(7,7)
            };

            foreach (Position element in obstacles)
            {
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.SetCursorPosition(element.Y, element.X);
                Console.Write("!");
            }


            Random numberGenerator = new Random();
            Position food = new Position(numberGenerator.Next(0, Console.WindowHeight), numberGenerator.Next(0, Console.WindowWidth));

            Queue<Position> snakeElements = new Queue<Position>();

            for (int i = 0; i <= 5; i++)
            {
                snakeElements.Enqueue(new Position(0, i));
            }

            int currDirection = right;

            while (true)
            {
               negativePoints++;//when we move negative points up up 

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();

                    if (userInput.Key == ConsoleKey.RightArrow)
                    {

                        if (currDirection != left)
                        {
                            currDirection = right;

                        }
                    }

                    if (userInput.Key == ConsoleKey.LeftArrow)
                    {
                        if (currDirection != right)
                        {
                            currDirection = left;

                        }
                    }

                    if (userInput.Key == ConsoleKey.DownArrow)
                    {
                        if (currDirection != up)
                        {
                            currDirection = down;

                        }
                    }

                    if (userInput.Key == ConsoleKey.UpArrow)
                    {
                        if (currDirection != down)
                        {
                            currDirection = up;
                        }
                    }
                }

                Position snakeHead = snakeElements.Last();

                Position nextDirection = directions[currDirection];
                Position snakeNewHead = new Position(snakeHead.X + nextDirection.X, snakeHead.Y + nextDirection.Y);
                /*
                if (snakeNewHead.X < 0)
                {
                    snakeNewHead.X = Console.WindowHeight - 1;
                }

                if (snakeNewHead.Y < 0)
                {
                    snakeNewHead.Y = Console.WindowWidth - 1;
                }

                if (snakeNewHead.X >= Console.WindowHeight)
                {
                    snakeNewHead.X = 0;
                }

                if (snakeNewHead.Y >= Console.WindowWidth)
                {
                    snakeNewHead.Y = 0;
                }
                */
              //  /*
                                if (snakeNewHead.X < 0 || snakeNewHead.Y < 0 || snakeNewHead.X >= Console.WindowHeight || snakeNewHead.Y >= Console.WindowWidth)
                                {
                                    Console.SetCursorPosition(0, 0);
                                    Console.WriteLine("Game over!"); //TODO: middle position
                                    int userPoints = ((snakeElements.Count - 6) * 100) - negativePoints; //TODO:
                                    Console.WriteLine("Your points are {0}", userPoints);
                                    return;
                                }
                               // */
                if (snakeElements.Contains(snakeNewHead) || obstacles.Contains(snakeNewHead))
                {
                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine("Game over!"); //TODO: middle position
                    int userPoints = ((snakeElements.Count - 6) * 100) - negativePoints; //TODO:
                    userPoints = Math.Max(userPoints, 0);
                    Console.WriteLine("Your points are {0}", userPoints);
                    Console.WriteLine("Your snake cannot through his body. ");
                    return;
                }

                Console.SetCursorPosition(snakeHead.Y, snakeHead.X);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("*");

                snakeElements.Enqueue(snakeNewHead);
                Console.SetCursorPosition(snakeNewHead.Y, snakeNewHead.X); //draw new head
                Console.ForegroundColor = ConsoleColor.Green;
                if (currDirection == right) Console.Write(">");
                if (currDirection == left) Console.Write("<");
                if (currDirection == down) Console.Write("v");
                if (currDirection == up) Console.Write("^");

                if (snakeNewHead.X == food.X && snakeNewHead.Y == food.Y)
                {
                    //feeding the snake
                    do
                    {
                        food = new Position(numberGenerator.Next(0, Console.WindowHeight), numberGenerator.Next(0, Console.WindowWidth));

                    } while (snakeElements.Contains(food));

                    lastFoodTime = Environment.TickCount;

                    Console.SetCursorPosition(food.Y, food.X);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write('@');

                    sleepTime--;

                    Position obstacle = new Position();
                    do
                    {
                        obstacle = new Position(numberGenerator.Next(0, Console.WindowHeight), numberGenerator.Next(0, Console.WindowWidth));

                    } while (snakeElements.Contains(obstacle) || obstacles.Contains(obstacle) || (food.Y != obstacle.Y && food.X != obstacle.X));

                    obstacles.Add(obstacle);
                    Console.SetCursorPosition(obstacle.Y, obstacle.X);
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write("!");

                }
                else
                {
                    //move
                    Position last = snakeElements.Dequeue();
                    Console.SetCursorPosition(last.Y, last.X);//col,row
                    Console.Write(" ");
                }

                if (Environment.TickCount - lastFoodTime >= foodDissapearTime)
                {
                    negativePoints += 50;

                    Console.SetCursorPosition(food.Y, food.X);
                    Console.Write(" ");

                    do
                    {
                        food = new Position(numberGenerator.Next(0, Console.WindowHeight), numberGenerator.Next(0, Console.WindowWidth));

                    } while (snakeElements.Contains(food) || obstacles.Contains(food));

                    lastFoodTime = Environment.TickCount;
                }

                Console.SetCursorPosition(food.Y, food.X);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write('@');

                sleepTime -= 0.01;

                Thread.Sleep((int)sleepTime);
            }
        }
Example #10
0
        static void Main(string[] args)
        {
            int slp = 0, width;
            Console.BufferHeight = Console.WindowHeight;

            Console.Clear();
            Console.CursorVisible = false;
            width = Console.WindowWidth;
            for (int space = 0; space < 4; space++)
            {
                Console.WriteLine();
            }
            Console.WriteLine("{0,42}", "MENU");
            Console.WriteLine();
            Console.WriteLine("{0,44}", "P-Pause");
            Console.WriteLine();
            Console.WriteLine("{0,44}", "L-Level");
            Console.WriteLine();
            Console.WriteLine("{0,50}", "Press Enter to start");
            Console.ReadLine();
            Console.Clear();
            for (int space = 0; space < 4; space++)
            {
                Console.WriteLine();
            }
            Console.Write("{0,48}", "Input level number:");
            while (!int.TryParse(Console.ReadLine(), out slp))
            {
                Console.Clear();
                Console.Write("Input a valid number!");
            }
            slp = 100 / slp;

            Position[] directions = new Position[]
            {
                new Position(0,1),  //right
                new Position(0,-1), //left
                new Position(1,0),  //down
                new Position(-1,0), //up
            };

            int direction = 0;      //0

            Random randomNumberGenerator = new Random();

            Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), randomNumberGenerator.Next(0, Console.WindowWidth));

            Position antiFood = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), randomNumberGenerator.Next(0, Console.WindowWidth));

            Queue<Position> snakeElements = new Queue<Position>();
            for (int i = 38; i < 43; i++)
            {
                snakeElements.Enqueue(new Position(10, i));
            }

            foreach (Position position in snakeElements)
            {
                Console.SetCursorPosition(position.col, position.row);
                Console.Write("*");
            }

            while (true)
            {

                if (Console.KeyAvailable == true)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    if ((userInput.Key == ConsoleKey.LeftArrow || userInput.Key == ConsoleKey.A) && direction != 0)
                    {
                        direction = 1;
                    }
                    if ((userInput.Key == ConsoleKey.RightArrow || userInput.Key == ConsoleKey.D) && direction != 1)
                    {
                        direction = 0;
                    }
                    if ((userInput.Key == ConsoleKey.DownArrow || userInput.Key == ConsoleKey.S) && direction != 3)
                    {
                        direction = 2;
                    }
                    if ((userInput.Key == ConsoleKey.UpArrow || userInput.Key == ConsoleKey.W) && direction != 2)
                    {
                        direction = 3;
                    }
                    if (userInput.Key == ConsoleKey.D0)
                    {
                        break;
                    }
                    if (userInput.Key == ConsoleKey.P)
                    {
                        Console.Clear();
                        Console.SetCursorPosition(Console.WindowWidth / 2 - 3, Console.WindowHeight / 2);
                        Console.Write("PAUSE");
                        //Console.WriteLine("{0,42}","PAUSE");
                        //Console.WriteLine("{0,51}","Press Enter to continue");
                        Console.ReadLine();
                    }
                    if (userInput.Key == ConsoleKey.L)
                    {
                        Console.Clear();
                        Console.SetCursorPosition(Console.WindowWidth / 2 - 6, Console.WindowHeight / 2);
                        Console.Write("CHOOSE LEVEL:");
                        //Console.WriteLine("{0,42}","PAUSE");
                        //Console.WriteLine("{0,51}","Press Enter to continue");
                        slp = int.Parse(Console.ReadLine());
                        slp = 100 / slp;
                    }
                }

                Position snakeHead = snakeElements.Last();

                Position nextDirection = directions[direction];
                Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);

                //if(snakeNewHead.row<0 || snakeNewHead.col<0 ||
                //    snakeNewHead.row>=Console.WindowHeight ||
                //    snakeNewHead.col>=Console.WindowWidth)
                //{
                //    Console.Clear();
                //    Console.WriteLine("Game OVER");
                //    Console.WriteLine("Your points:{0}", snakeElements.Count-6);
                //    Console.ReadLine();
                //    direction = 0;
                //    snakeNewHead.row = 0;
                //    snakeNewHead.col = 0;

                //}
                if (snakeNewHead.row < 0)
                {
                    snakeNewHead.row = Console.WindowHeight - 1;
                }
                if (snakeNewHead.col < 0)
                {
                    snakeNewHead.col = Console.WindowWidth - 1;
                }
                if (snakeNewHead.row == Console.WindowHeight)
                {
                    snakeNewHead.row = 0;
                }
                if (snakeNewHead.col == Console.WindowWidth)
                {
                    snakeNewHead.col = 0;
                }

                if (snakeElements.Contains(snakeNewHead))
                {
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 17, Console.WindowHeight / 2);
                    Console.WriteLine("You tried to eat yourself! Game Over!");
                    Console.ReadLine();
                    Console.Clear();
                    break;
                }
                snakeElements.Enqueue(snakeNewHead);

                Console.Clear();
                Console.SetCursorPosition(food.col, food.row);
                Console.Write("@");

                Console.SetCursorPosition(antiFood.col, antiFood.row);
                Console.Write("X");

                if (snakeNewHead.col == food.col && snakeNewHead.row == food.row)
                {
                    food.col = randomNumberGenerator.Next(0, Console.WindowWidth);
                    food.row = randomNumberGenerator.Next(0, Console.WindowHeight);
                    Console.Beep(7000, 50);
                }
                else
                {
                    snakeElements.Dequeue();
                }

                if (snakeHead.col == antiFood.col && snakeHead.row == antiFood.row)
                {
                    snakeElements.Dequeue();
                    snakeElements.Dequeue();
                    snakeElements.Dequeue();
                    antiFood.col = randomNumberGenerator.Next(0, Console.WindowWidth);
                    antiFood.row = randomNumberGenerator.Next(0, Console.WindowHeight);
                    Console.Beep(1000, 50);
                }

                foreach (Position position in snakeElements)
                {
                    Console.SetCursorPosition(position.col, position.row);
                    Console.Write("*");
                }

                if (direction == 0)
                {
                    Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                    Console.Write("O");
                }
                if (direction == 1)
                {
                    Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                    Console.Write("O");
                }
                if (direction == 2)
                {
                    Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                    Console.Write("O");
                }
                if (direction == 3)
                {
                    Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
                    Console.Write("O");
                }

                Thread.Sleep(slp);

            }
        }
Example #11
0
        static void Main(string[] args)
        {
            Position[] directions = new Position[]
            {
                new Position (0, 1),//right
                new Position (0, -1), // left
                new Position (1, 0), // down
                new Position (-1, 0),// top

            };
            int direction = 0; //0

            Queue<Position> snakeElement = new Queue<Position>();
            for (int i = 0; i <= 5; i++)
            {
                 snakeElement.Enqueue(new Position(0, i));
            }

            foreach (Position position in snakeElement)
            {
                Console.SetCursorPosition(position.Col, position.Row);
                Console.Write("*");

            }

            while (true)
            {
                ConsoleKeyInfo userinput = Console.ReadKey();
                if (userinput.Key == ConsoleKey.LeftArrow)
                {
                    direction = 1;
                }
                if (userinput.Key == ConsoleKey.RightArrow)
                {
                    direction = 0;
                }
                if (userinput.Key == ConsoleKey.UpArrow)
                {
                    direction = 3;
                }
                if (userinput.Key == ConsoleKey.DownArrow)
                {
                    direction = 2;
                }

                snakeElement.Dequeue();

                Position snakeHead = snakeElement.Last();
                snakeElement.Dequeue();
                Position nextdirection  = directions[direction];
                Position smakenewhead = new Position(snakeHead.Row + nextdirection.Row, snakeHead.Col + nextdirection.Col);
                snakeElement.Enqueue(smakenewhead);

                foreach (Position position in snakeElement)
                {
                    Console.SetCursorPosition(position.Col, position.Row);
                    Console.Write("*");

                }

            }
        }
Example #12
0
 public void Teleport(Position position)
 {
     SnakePieces.Remove(Head);
     SnakePieces.Insert(0, position);
 }