コード例 #1
0
 static void Main()
 {
     Random rnd = new Random();
     string[] rockTypes = { "!", "@", "#", "$", "%", "^", "&", "*", ".", ";" };
     List<Item> rocks = new List<Item>();
     long score = 0;
     double SleepTime = 200;
     Console.CursorVisible = false;
     Console.BufferHeight = Console.WindowHeight;
     Console.WriteLine("Falling rocks game!");
     Console.WriteLine("Use the arrow keys to move the dwarf (0) at the bottom.");
     Console.WriteLine("Try to avoid being hit!");
     Console.WriteLine("Press one of the arrows to start...");
     ConsoleKeyInfo pressedKey = Console.ReadKey();
     if (pressedKey.Key == ConsoleKey.LeftArrow ||
     pressedKey.Key == ConsoleKey.RightArrow)
     {
         Dwarf dwarf = new Dwarf();
         dwarf.Display();
         while (true)
         {
             for (int i = rnd.Next(0, 3); i < 4; i++) //randomly generate 1-4 new rocks
             {
                 Item rock = new Item(rnd.Next(0, Console.WindowWidth - 1),
                 0,
                 (ConsoleColor)rnd.Next((int)ConsoleColor.Blue, (int)ConsoleColor.White),
                 rockTypes[rnd.Next(rockTypes.Length)]);
                 rock.Display(); //displaying the rocks
                 rocks.Add(rock); //adding the rocks to a list
             }
             foreach (Item i in rocks) //generating falling of rocks
             {
                 i.Clear();
                 i.Y++;
                 if (i.Y < (Console.WindowHeight)) i.Display(); //showing the fallen rock only in the field
                 else score++; //adding score for each fallen rock
                 if ((i.X >= dwarf.X) &&
                 (i.X <= dwarf.X + 2) &&
                 (i.Y >= dwarf.Y) &&
                 (i.Y <= dwarf.Y + 2))
                 {
                     Console.Clear();
                     Console.SetCursorPosition(0, 0);
                     Console.ForegroundColor = ConsoleColor.White;
                     Console.WriteLine("Game over!!!");
                     Console.WriteLine("Your score is {0} points!", score);
                     return;
                 }
             }
             rocks.RemoveAll(i => i.Y >= (Console.WindowHeight));
             if (Console.KeyAvailable)
             {
                 dwarf.Move(Console.ReadKey());
             }
             Thread.Sleep((int)SleepTime);
             SleepTime -= 0.05;
         }
     }
 }
コード例 #2
0
ファイル: FallingRocks.cs プロジェクト: krasi070/FallingRocks
        public static void Main()
        {
            ResetBuffer();
            Console.CursorVisible = false;

            Dwarf dwarf = new Dwarf(Console.WindowWidth / 2 - 2, Console.WindowHeight - 1);
            GameBoard board = new GameBoard();
            IEngine engine = new FallingRocksEngine(dwarf, board);

            engine.Run();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: BobbyBorisov/FallingRocks
 private static void DetermineCollision()
 {
     for (int i = 0; i < _rocks.Count; i++)
     {
         if ((_dwarf.x == _rocks[i].BoundaryX && _dwarf.y == _rocks[i].BoundaryY + 1)
         || (_dwarf.x + 1 == _rocks[i].BoundaryX && _dwarf.y == _rocks[i].BoundaryY + 1)
         || (_dwarf.x - 1 == _rocks[i].BoundaryX && _dwarf.y == _rocks[i].BoundaryY + 1))
         {
             _dwarf.livesCount--;
             _dwarf.isCollision = true;
             break;
         }
     }
     if (_dwarf.isCollision)
     {
         _rocks.Clear();
         Console.Clear();
         GenerateNewRocks();
         _dwarf.Draw();
         System.Threading.Thread.Sleep(1000);
         _dwarf = new Dwarf(WindowWidth-GameMenuWidth, WindowHeight, _dwarf.livesCount);
         _dwarf.isCollision = false;
     }
 }
コード例 #4
0
ファイル: FallingRocks.cs プロジェクト: unbelt/SoftUni
        private static void Control(Dwarf dwarf)
        {
            // Left / Right movement
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo userInput = Console.ReadKey();

                if (userInput.Key == ConsoleKey.LeftArrow)
                {
                    if (dwarf.XPos > 1)
                    {
                        dwarf.XPos--;
                    }

                    Console.SetCursorPosition(dwarf.XPos + 1, dwarf.YPos);
                    Console.Write(' ');
                }

                if (userInput.Key == ConsoleKey.RightArrow)
                {
                    if (dwarf.XPos <= gameWidth / 2 - 1)
                    {
                        dwarf.XPos++;
                    }

                    Console.SetCursorPosition(dwarf.XPos - 1, dwarf.YPos);
                    Console.Write(' ');
                }

                if (userInput.Key == ConsoleKey.Spacebar)
                {
                    // Pause game
                    Console.SetCursorPosition(Console.WindowWidth - 47, (Console.WindowHeight / 2) - 6);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(" -= GAME PUSED =- ");
                    Console.SetCursorPosition(Console.WindowWidth - 48, (Console.WindowHeight / 2) - 4);
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("Press Space to resume");
                    Console.ReadKey();

                    // Return to game
                    if (userInput.Key == ConsoleKey.Spacebar)
                    {
                        Console.Clear();
                        Control(dwarf);
                    }
                }

                // Exit game / Main menu /
                if (userInput.Key == ConsoleKey.Escape)
                {
                    Console.Clear();
                    Menu();
                }
            }
        }
コード例 #5
0
        static void Main()
        {
            Console.CursorVisible = false;
            Console.BufferHeight = Console.WindowHeight = 18;
            Console.WindowWidth = 20;
            Console.BufferWidth = 20;

            Random randomGenerator = new Random();
            double sleeptime = 200;
            double score = 0;
            int lives = 5;

            Dwarf newDwarf = new Dwarf();
            newDwarf.col = 9;
            newDwarf.row = Console.WindowHeight - 1;
            newDwarf.color = ConsoleColor.Gray;

            List<Rock> Rocks = new List<Rock>();

            PrintOnPosition(0, 5, " The aim of the game is to avoid all the rocks. Good luck!", ConsoleColor.Yellow);
            PrintOnPosition(0, 9, "Pick", ConsoleColor.Yellow);
            PrintOnPosition(5, 9, (char)3, ConsoleColor.Red);
            PrintOnPosition(7, 9, "for lives", ConsoleColor.Yellow);
            PrintOnPosition(0, 10, "Pick", ConsoleColor.Yellow);
            PrintOnPosition(5, 10, '$', ConsoleColor.Green);
            PrintOnPosition(7, 10, "for money", ConsoleColor.Yellow);
            PrintOnPosition(5, 13, "Good luck!", ConsoleColor.Red);
            PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
            Console.ReadLine();
            Console.Clear();

            for (int i = 0; i <= Console.WindowWidth - 1; i++)
            {
                PrintOnPosition(i, 4, '_', ConsoleColor.White);
            }

            while (true)
            {
                HitPosition newHitPosition = HitPosition.None;

                int chance = randomGenerator.Next(0, 100);

                {
                    Rock newRock = new Rock();
                    newRock.col = randomGenerator.Next(0, 19);
                    newRock.row = 5;

                    if (chance < 1)
                    {
                        newRock.shape = (char)RockShape.Heart;
                        newRock.color = ConsoleColor.Red;
                    }
                    else if (chance < 10)
                    {
                        newRock.shape = (char)RockShape.Money;
                        newRock.color = ConsoleColor.Green;
                    }
                    else if (chance < 100)
                    {
                        newRock.shape = (char)RockShape.Rock;
                        newRock.color = ConsoleColor.Cyan;
                    }
                    Rocks.Add(newRock);
                }

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                    }
                    if (userInput.Key == ConsoleKey.LeftArrow && newDwarf.col > 0)
                    {
                        PrintOnPosition(newDwarf.col--, newDwarf.row, "   ", newDwarf.color);
                    }
                    if (userInput.Key == ConsoleKey.RightArrow && newDwarf.col < Console.WindowWidth - 2)
                    {
                        PrintOnPosition(newDwarf.col++, newDwarf.row, "   ", newDwarf.color);
                    }
                }
                PrintOnPosition(newDwarf.col, newDwarf.row, newDwarf.shape, newDwarf.color);

                List<Rock> newList = new List<Rock>();
                for (int i = 0; i < Rocks.Count; i++)
                {
                    Rock oldRock = Rocks[i];
                    Rock newRock = new Rock();
                    newRock.col = oldRock.col;
                    newRock.row = oldRock.row + 1;
                    newRock.shape = oldRock.shape;
                    newRock.color = oldRock.color;

                    if ((newRock.col == newDwarf.col || newRock.col == newDwarf.col + 1 || newRock.col == newDwarf.col + 2) && newRock.row == newDwarf.row)
                    {
                        switch (newRock.shape)
                        {
                            case '@':
                                newHitPosition = (HitPosition)Enum.Parse(typeof(HitPosition), ((newRock.col - newDwarf.col) + 1).ToString());
                                break;
                            case '$':
                                score += 500;
                                break;
                            case (char)3:
                                lives += 1;
                                break;
                        }
                    }
                    if (newRock.row < Console.WindowHeight)
                    {
                        newList.Add(newRock);
                    }
                }

                if (newHitPosition != HitPosition.None)
                {
                    PrintOnPosition(newDwarf.col, newDwarf.row, hitshapes[(int)newHitPosition], ConsoleColor.Red);
                    if (lives < 2)
                    {
                        PrintOnPosition(0, 0, "GAME OVER!", ConsoleColor.Red);
                        PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
                        Console.ReadLine();
                        PrintOnPosition(0, 0, @"           ", ConsoleColor.Red);
                        return;
                    }

                    PrintOnPosition(0, 0, "Press enter", ConsoleColor.Red);
                    PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
                    Console.ReadLine();
                    PrintOnPosition(0, 0, @"           ", ConsoleColor.Red);
                    foreach (Rock rock in Rocks)
                    {
                        PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black);
                    }
                    Rocks.Clear();
                    newList.Clear();
                    lives--;
                    sleeptime += 20;
                }

                foreach (Rock rock in Rocks)
                {
                    PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black);
                }
                Rocks = newList;

                foreach (Rock rock in Rocks)
                {
                    PrintOnPosition(rock.col, rock.row, rock.shape, rock.color);
                }

                PrintOnPosition(7, 2, "Score: " + (int)score, ConsoleColor.Cyan);
                PrintOnPosition(7, 3, "Lives: " + lives, ConsoleColor.Yellow);

                score += 14.66;
                sleeptime -= 0.5;
                if (sleeptime < 100)
                {
                    sleeptime = 100;
                }

                if (sleeptime > 200)
                {
                    sleeptime = 200;
                }

                Thread.Sleep((int)sleeptime);
            }
        }
コード例 #6
0
ファイル: FallingRocks.cs プロジェクト: cvet-/Telerik_Academy
        static void Main()
        {
            // Some properties and variables to be used
            Console.CursorVisible = false;
            Console.BufferHeight = Console.WindowHeight = 18;
            Console.WindowWidth = 20;
            Console.BufferWidth = 20;

            Random randomGenerator = new Random();
            double sleeptime = 200;
            double score = 0;
            int lives = 5;

            // constructing the dwarf elements "(O)"
            Dwarf newDwarf = new Dwarf();
            newDwarf.col = 9;
            newDwarf.row = Console.WindowHeight - 1;
            newDwarf.color = ConsoleColor.Gray;

            List<Rock> Rocks = new List<Rock>();

            // Writing the instructions at the start of the game
            PrintOnPosition(0, 5, " The aim of the game is to avoid all the rocks. Good luck!", ConsoleColor.Yellow);
            PrintOnPosition(0, 9, "Pick", ConsoleColor.Yellow);
            PrintOnPosition(5, 9, (char)3, ConsoleColor.Red);
            PrintOnPosition(7, 9, "for lives", ConsoleColor.Yellow);
            PrintOnPosition(0, 10, "Pick", ConsoleColor.Yellow);
            PrintOnPosition(5, 10, '$', ConsoleColor.Green);
            PrintOnPosition(7, 10, "for money", ConsoleColor.Yellow);
            PrintOnPosition(5, 13, "Good luck!", ConsoleColor.Red);
            PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
            Console.ReadLine();
            Console.Clear();

            // making the lines of play
            for (int i = 0; i <= Console.WindowWidth - 1; i++)
            {
                PrintOnPosition(i, 4, '_', ConsoleColor.White);
            }

            while (true)
            {
                // The bool values in case we get hit. They are different for the parts of the dwarf elements
                HitPosition newHitPosition = HitPosition.None;

                // the chance is for making bonuses from time to time
                int chance = randomGenerator.Next(0, 100);

                {
                    Rock newRock = new Rock();
                    newRock.col = randomGenerator.Next(0, 19);
                    newRock.row = 5;


                    if (chance < 1)
                    {
                        // It's a heart character
                        newRock.shape = (char)RockShape.Heart;
                        newRock.color = ConsoleColor.Red;
                    }
                    else if (chance < 10)
                    {
                        newRock.shape = (char)RockShape.Money;
                        newRock.color = ConsoleColor.Green;
                    }
                    else if (chance < 100)
                    {
                        newRock.shape = (char)RockShape.Rock;
                        newRock.color = ConsoleColor.Cyan;
                    }
                    Rocks.Add(newRock);
                }

                //moving the dwarf
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    // In order to avoid the moving bug (If numerous keys are pressed, the program will execute each one)
                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                    }
                    if (userInput.Key == ConsoleKey.LeftArrow && newDwarf.col > 0)
                    {
                        PrintOnPosition(newDwarf.col--, newDwarf.row, "   ", newDwarf.color);
                    }
                    if (userInput.Key == ConsoleKey.RightArrow && newDwarf.col < Console.WindowWidth - 2)
                    {
                        PrintOnPosition(newDwarf.col++, newDwarf.row, "   ", newDwarf.color);
                    }
                }
                PrintOnPosition(newDwarf.col, newDwarf.row, newDwarf.shape, newDwarf.color);

                // The new list is made in order to add the next position of the rock which is y + 1. Thus making it fall
                List<Rock> newList = new List<Rock>();
                for (int i = 0; i < Rocks.Count; i++)
                {
                    Rock oldRock = Rocks[i];
                    Rock newRock = new Rock();
                    newRock.col = oldRock.col;
                    newRock.row = oldRock.row + 1;
                    newRock.shape = oldRock.shape;
                    newRock.color = oldRock.color;

                    // check if we get hit
                    if ((newRock.col == newDwarf.col || newRock.col == newDwarf.col + 1 || newRock.col == newDwarf.col + 2) && newRock.row == newDwarf.row)
                    {
                        switch (newRock.shape)
                        {
                            case '@':
                                newHitPosition = (HitPosition)Enum.Parse(typeof(HitPosition), ((newRock.col - newDwarf.col) + 1).ToString());
                                break;
                            case '$':
                                score += 500;
                                break;
                            case (char)3:
                                lives += 1;
                                break;
                        }
                    }
                    // Add the new object in the newList until it reaches the end of the screen
                    if (newRock.row < Console.WindowHeight)
                    {
                        newList.Add(newRock);
                    }
                }

                // consequences for being hit
                if (newHitPosition != HitPosition.None)
                {
                    PrintOnPosition(newDwarf.col, newDwarf.row, hitshapes[(int)newHitPosition], ConsoleColor.Red);
                    // checks if you have 1 live left. Else the game will continue until you reach -1 lives
                    if (lives < 2)
                    {
                        PrintOnPosition(0, 0, "GAME OVER!", ConsoleColor.Red);
                        // We print the X in order to see the place of casualty                       
                        PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
                        Console.ReadLine();
                        PrintOnPosition(0, 0, @"           ", ConsoleColor.Red);
                        return;
                    }

                    // If we still have lives, the game continues
                    PrintOnPosition(0, 0, "Press enter", ConsoleColor.Red);
                    // this is made in order to avoid a bug in which when you press some letter, it prints it on the console
                    // on the last position the cursor was
                    PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
                    Console.ReadLine();
                    // Clear the positions of the old objects
                    PrintOnPosition(0, 0, @"           ", ConsoleColor.Red);
                    foreach (Rock rock in Rocks)
                    {
                        PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black);
                    }
                    Rocks.Clear();
                    newList.Clear();
                    // Consequences
                    lives--;
                    sleeptime += 20;
                }

                // This basically makes the rocks move. The idea is - Clear the old rocks and make the new ones
                foreach (Rock rock in Rocks)
                {
                    PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black);
                }
                // The object Rocks takes the value of the new list which contains the positions of the new rocks
                Rocks = newList;

                foreach (Rock rock in Rocks)
                {
                    PrintOnPosition(rock.col, rock.row, rock.shape, rock.color);
                }

                // The score and lives
                PrintOnPosition(7, 2, "Score: " + (int)score, ConsoleColor.Cyan);
                PrintOnPosition(7, 3, "Lives: " + lives, ConsoleColor.Yellow);

                // Making the score increase and the speed of the game increase constantly
                score += 14.66;
              /*  sleeptime -= 0.5;
                if (sleeptime < 100)
                {
                    sleeptime = 100;
                }

                if (sleeptime > 200)
                {
                    sleeptime = 200;
                }*/

                // Set the speed of the program
                Thread.Sleep(150);
            }
        }
コード例 #7
0
        static void Main()
        {
            Console.CursorVisible = false;
            Console.BufferHeight  = Console.WindowHeight = 18;
            Console.WindowWidth   = 20;
            Console.BufferWidth   = 20;

            Random randomGenerator = new Random();
            double sleeptime       = 200;
            double score           = 0;
            int    lives           = 5;

            Dwarf newDwarf = new Dwarf();

            newDwarf.col   = 9;
            newDwarf.row   = Console.WindowHeight - 1;
            newDwarf.color = ConsoleColor.Gray;

            List <Rock> Rocks = new List <Rock>();

            PrintOnPosition(0, 5, " The aim of the game is to avoid all the rocks. Good luck!", ConsoleColor.Yellow);
            PrintOnPosition(0, 9, "Pick", ConsoleColor.Yellow);
            PrintOnPosition(5, 9, (char)3, ConsoleColor.Red);
            PrintOnPosition(7, 9, "for lives", ConsoleColor.Yellow);
            PrintOnPosition(0, 10, "Pick", ConsoleColor.Yellow);
            PrintOnPosition(5, 10, '$', ConsoleColor.Green);
            PrintOnPosition(7, 10, "for money", ConsoleColor.Yellow);
            PrintOnPosition(5, 13, "Good luck!", ConsoleColor.Red);
            PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
            Console.ReadLine();
            Console.Clear();

            for (int i = 0; i <= Console.WindowWidth - 1; i++)
            {
                PrintOnPosition(i, 4, '_', ConsoleColor.White);
            }

            while (true)
            {
                HitPosition newHitPosition = HitPosition.None;

                int chance = randomGenerator.Next(0, 100);

                {
                    Rock newRock = new Rock();
                    newRock.col = randomGenerator.Next(0, 19);
                    newRock.row = 5;


                    if (chance < 1)
                    {
                        newRock.shape = (char)RockShape.Heart;
                        newRock.color = ConsoleColor.Red;
                    }
                    else if (chance < 10)
                    {
                        newRock.shape = (char)RockShape.Money;
                        newRock.color = ConsoleColor.Green;
                    }
                    else if (chance < 100)
                    {
                        newRock.shape = (char)RockShape.Rock;
                        newRock.color = ConsoleColor.Cyan;
                    }
                    Rocks.Add(newRock);
                }

                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                    }
                    if (userInput.Key == ConsoleKey.LeftArrow && newDwarf.col > 0)
                    {
                        PrintOnPosition(newDwarf.col--, newDwarf.row, "   ", newDwarf.color);
                    }
                    if (userInput.Key == ConsoleKey.RightArrow && newDwarf.col < Console.WindowWidth - 2)
                    {
                        PrintOnPosition(newDwarf.col++, newDwarf.row, "   ", newDwarf.color);
                    }
                }
                PrintOnPosition(newDwarf.col, newDwarf.row, newDwarf.shape, newDwarf.color);

                List <Rock> newList = new List <Rock>();
                for (int i = 0; i < Rocks.Count; i++)
                {
                    Rock oldRock = Rocks[i];
                    Rock newRock = new Rock();
                    newRock.col   = oldRock.col;
                    newRock.row   = oldRock.row + 1;
                    newRock.shape = oldRock.shape;
                    newRock.color = oldRock.color;

                    if ((newRock.col == newDwarf.col || newRock.col == newDwarf.col + 1 || newRock.col == newDwarf.col + 2) && newRock.row == newDwarf.row)
                    {
                        switch (newRock.shape)
                        {
                        case '@':
                            newHitPosition = (HitPosition)Enum.Parse(typeof(HitPosition), ((newRock.col - newDwarf.col) + 1).ToString());
                            break;

                        case '$':
                            score += 500;
                            break;

                        case (char)3:
                            lives += 1;
                            break;
                        }
                    }
                    if (newRock.row < Console.WindowHeight)
                    {
                        newList.Add(newRock);
                    }
                }

                if (newHitPosition != HitPosition.None)
                {
                    PrintOnPosition(newDwarf.col, newDwarf.row, hitshapes[(int)newHitPosition], ConsoleColor.Red);
                    if (lives < 2)
                    {
                        PrintOnPosition(0, 0, "GAME OVER!", ConsoleColor.Red);
                        PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
                        Console.ReadLine();
                        PrintOnPosition(0, 0, @"           ", ConsoleColor.Red);
                        return;
                    }

                    PrintOnPosition(0, 0, "Press enter", ConsoleColor.Red);
                    PrintOnPosition(17, 0, ' ', ConsoleColor.Black);
                    Console.ReadLine();
                    PrintOnPosition(0, 0, @"           ", ConsoleColor.Red);
                    foreach (Rock rock in Rocks)
                    {
                        PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black);
                    }
                    Rocks.Clear();
                    newList.Clear();
                    lives--;
                    sleeptime += 20;
                }

                foreach (Rock rock in Rocks)
                {
                    PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black);
                }
                Rocks = newList;

                foreach (Rock rock in Rocks)
                {
                    PrintOnPosition(rock.col, rock.row, rock.shape, rock.color);
                }

                PrintOnPosition(7, 2, "Score: " + (int)score, ConsoleColor.Cyan);
                PrintOnPosition(7, 3, "Lives: " + lives, ConsoleColor.Yellow);

                score     += 14.66;
                sleeptime -= 0.5;
                if (sleeptime < 100)
                {
                    sleeptime = 100;
                }

                if (sleeptime > 200)
                {
                    sleeptime = 200;
                }

                Thread.Sleep((int)sleeptime);
            }
        }
コード例 #8
0
ファイル: FallingRocks.cs プロジェクト: androidejl/Telerik
 private static void RestartGame()
 {
     player = new Dwarf(WindowWidth - GameMenuWidth, WindowHeight);
     gameSpeed = 0d;
     gameScore = 0;
 }
コード例 #9
0
        static void Main()
        {
            Initialisation();
            Dwarf          dwarf = Dwarf.GetInstance();
            List <Rock>    removeObsoletes;
            bool           hit = false;
            ConsoleKeyInfo pressedKey;

            while (true)
            {
                for (int i = 0, count = 1; i < count; i++)
                {
                    rocks.Add(new Rock());
                }

                while (Console.KeyAvailable)
                {
                    pressedKey = Console.ReadKey(true);
                    if (pressedKey.Key == ConsoleKey.LeftArrow)
                    {
                        dwarf.MoveLeft();
                    }
                    if (pressedKey.Key == ConsoleKey.RightArrow)
                    {
                        dwarf.MoveRight();
                    }
                }
                removeObsoletes = new List <Rock>();
                hit             = false;
                foreach (Rock rock in rocks)
                {
                    if (!rock.MoveDown())
                    {
                        removeObsoletes.Add(rock);
                    }
                    if (dwarf.Overlap(rock))
                    {
                        hit = true;
                        Console.Beep();

                        Console.Out.WriteLine("GAME OVER !!!");
                        Environment.Exit(0);
                    }
                }
                Console.Clear();
                foreach (Rock old in removeObsoletes)
                {
                    rocks.Remove(old);
                }
                if (hit)
                {
                    rocks.Clear();
                }

                foreach (Rock rock in rocks)
                {
                    rock.Print();
                }
                dwarf.Print(hit);

                Thread.Sleep(150);
            }
        }
コード例 #10
0
ファイル: FallingRocks.cs プロジェクト: unbelt/SoftUni
        private static void Collisions(List<Rocks> rocks, Dwarf dwarf)
        {
            // Collisions with Dwarf
            for (int i = 0; i < rocks.Count; i++)
            {
                if (dwarf.IsHittedBy(rocks[i]))
                {
                    Helper.DrawElement((Console.BufferWidth / 2) + 13 + lives - 1, 4, '\u2665'.ToString(), ConsoleColor.Black);
                    lives--; // loses lives only if hitted by rock

                    rocks.Remove(rocks[i]);

                    if (lives == 0) // You are dead
                    {
                        break;
                    }
                }

                rocks[i].Print();

                // When rock hit the ground
                if (rocks[i].YPos == gameHeight - 1)
                {
                    score += 1;
                    rocks[i].Clear();
                    rocks.RemoveAt(i);
                }

                if (score > 1000) // Winning score
                {
                    Info.YouWin(score);
                }
            }
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: stefansum/TelerikAcademy
        static void Main(string[] args)
        {
            Console.WindowHeight = 40;
            Console.WindowWidth  = 40;
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth  = Console.BufferWidth;
            List <GameObject> allObject = new List <GameObject>();
            List <Rock>       rocks     = new List <Rock>();
            List <Bullet>     bullets   = new List <Bullet>();
            Random            random    = new Random();
            Dwarf             dwarf     = new Dwarf(new Position(20, 39));

            allObject.Add(dwarf);
            Draw(allObject);



            while (true)
            {
                Thread.Sleep(150);
                Console.Clear();

                Console.WriteLine("point = " + point);
                Rock rock = new Rock(new Position(random.Next(39), 1), new Position(0, 1));

                allObject.Add(rock);
                rocks.Add(rock);
                point += CollisionDetection.BulletDetection(bullets, rocks);

                if (CollisionDetection.RockSmash(dwarf, rocks))
                {
                    EndGame();
                }



                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    if (userInput.Key == ConsoleKey.LeftArrow)
                    {
                        if (dwarf.Position.Row > 0)
                        {
                            dwarf.MoveLeft();
                        }
                    }
                    if (userInput.Key == ConsoleKey.RightArrow)
                    {
                        if (dwarf.Position.Row < Console.WindowWidth - 2)
                        {
                            dwarf.MoveRight();
                        }
                    }
                    if (userInput.Key == ConsoleKey.Spacebar)
                    {
                        Bullet bullet = new Bullet(new Position(dwarf.Position.Row, dwarf.Position.Col - 1), new Position(0, -1));
                        bullets.Add(bullet);
                        allObject.Add(bullet);
                    }
                }
                Draw(allObject);
            }
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: BobbyBorisov/FallingRocks
 static void Main()
 {
     Console.BufferWidth = Console.WindowWidth = WindowWidth;
     Console.BufferHeight = Console.WindowHeight = WindowHeight;
     Console.Title = "Falling Rocks - Team Edmos";
     Console.CursorVisible = false;
     PrintIntro();
     ConsoleKeyInfo pressAnyKey = Console.ReadKey();
     if ((pressAnyKey.Key == ConsoleKey.Enter) || (pressAnyKey.Key != ConsoleKey.Enter))
     {
         Console.Clear();
         _dwarf = new Dwarf(WindowWidth - GameMenuWidth, WindowHeight, 5);
         GenerateNewRocks();
         while (true)
         {
             System.Threading.Thread.Sleep(500 - 3 * (int)gameSpeed);
             GetUserInput();
             MoveRocks();
             DetermineCollision();
             Repaint();
             gameSpeed = Math.Min(gameSpeed + Acceleration, MaximumGameSpeed);
         }
     }
 }
コード例 #13
0
        static void Main()
        {
            Battlefield battlefield = new Battlefield();
            Instructions();
            Dwarf hero = new Dwarf();
            Rock rock = new Rock();
            DateTime start = DateTime.Now;
            int score = 0;

            while (true)
            {
                hero.MoveDwarf();
                if (rock.ReignChaos(hero.X))
                {
                    Thread.Sleep(1500);
                    Console.Clear();
                    break;
                }
                Thread.Sleep(150);
            }
            DateTime end = DateTime.Now;
            score = hero.TrackScore(start, end);

            Console.SetCursorPosition(7, 9);
            Console.WriteLine("G A M E  O V E R");
            Console.SetCursorPosition(7, 10);
            Console.WriteLine("Your score: {0}", score);
        }
コード例 #14
0
ファイル: FallingRocks.cs プロジェクト: unbelt/SoftUni
        // V6 Supercharged
        private static void Engine(List<Rocks> rocks, Dwarf dwarf)
        {
            while (lives > 0)
            {
                // Control the asteroid creation
                Rocks newRock = new Rocks(UI.PickUpChar(), ConsoleColor.Cyan);
                rocks.Add(newRock);

                // Move the asteroids
                for (int index = 0; index < rocks.Count; index++)
                {
                    rocks[index].Clear();
                    rocks[index].Move();
                }

                Collisions(rocks, dwarf);
                Control(dwarf);
                dwarf.Draw();

                UI.DrawInterface(lives, score); // draw score and lives in separated cells

                // Slow down the game
                Thread.Sleep(gameSpeed);
            }
        }
コード例 #15
0
 public static Dwarf GetInstance()
 {
     if (instance == null)
     {
         instance = new Dwarf();
     }
     return instance;
 }
コード例 #16
0
ファイル: FallingRocks.cs プロジェクト: unbelt/SoftUni
        private static void GameSettings()
        {
            Dwarf dwarf = new Dwarf('\u2588', gameWidth / 4, gameHeight - 2, ConsoleColor.White);

            Engine(rocks, dwarf); // Start the game
            Info.GameOver(score);
            GameSettings();
        }
コード例 #17
0
ファイル: FallingRocks.cs プロジェクト: dhija/Telerik
 private static void RestartGame()
 {
     player    = new Dwarf(WindowWidth - GameMenuWidth, WindowHeight);
     gameSpeed = 0d;
     gameScore = 0;
 }
コード例 #18
0
        public static void Main()
        {
            // hide scrollbar and set console width and height
            Console.BufferHeight = Console.WindowHeight = WindowHeight;
            Console.BufferWidth  = Console.WindowWidth = WindowWidth;

            ManualResetEvent resetEvent = new ManualResetEvent(false);

            int  livesCount = LivesCount;
            int  score      = 0;
            int  chance;
            bool hit;

            char[] rockSymbols = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };

            // create a new instance of a dwarf
            Dwarf dwarf = new Dwarf(PlayFieldWidth / 2, WindowHeight - 1);

            List <Rock> rocks = new List <Rock>();

            // game loop
            while (true)
            {
                hit = false;

                // if key is pressed, get key and move car
                while (Console.KeyAvailable)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey(true);

                    switch (pressedKey.Key)
                    {
                    case ConsoleKey.LeftArrow:
                        if (dwarf.X - 1 >= 0)
                        {
                            dwarf.X--;
                        }

                        break;

                    case ConsoleKey.RightArrow:
                        if (dwarf.X + 1 < PlayFieldWidth)
                        {
                            dwarf.X++;
                        }

                        break;

                    case ConsoleKey.Spacebar:
                        resetEvent.Reset();
                        PrintStringOnPosition((WindowWidth / 2 - GameOverMessage.Length / 2), (WindowHeight / 2 - 1), PausedMessage, ConsoleColor.White);
                        Console.ReadKey();
                        resetEvent.Set();
                        break;
                    }
                }

                // create an instance of a rock and add it to the list of rocks - chance added to ensure the number of rocks is not too high
                chance = random.Next(0, 100);
                if (chance > 20)
                {
                    Rock rock = new Rock();
                    rock.X      = random.Next(0, PlayFieldWidth);
                    rock.Y      = 0;
                    rock.Symbol = rockSymbols[random.Next(0, rockSymbols.Length)];
                    rock.Color  = GetRandomLightConsoleColor();
                    rocks.Add(rock);
                }

                // move rocks
                List <Rock> newListOfRocks = new List <Rock>();
                for (int i = 0; i < rocks.Count; i++)
                {
                    Rock oldRock = rocks[i];
                    Rock newRock = new Rock(oldRock.X, oldRock.Y + 1, oldRock.Symbol, oldRock.Color);

                    // if rock collides with dwarf
                    if (newRock.X >= dwarf.X && newRock.X <= dwarf.X + 2 && newRock.Y == dwarf.Y)
                    {
                        hit = true;

                        livesCount--;

                        // game over
                        if (livesCount == 0)
                        {
                            // end game
                            PrintStringOnPosition((WindowWidth / 2 - GameOverMessage.Length / 2 - 1), (WindowHeight / 2 - 1), GameOverMessage, ConsoleColor.White);
                            // reset score
                            score = 0;
                            Console.ReadLine();
                            return;
                        }
                    }

                    if (newRock.Y < WindowHeight)
                    {
                        newListOfRocks.Add(newRock);
                    }
                    else
                    {
                        score += 10;
                    }
                }

                rocks = newListOfRocks;

                // clear the console
                Console.Clear();

                // draw dwarf
                if (hit)
                {
                    rocks.Clear();
                    PrintOnPosition(dwarf.X + 1, dwarf.Y, 'x', ConsoleColor.Red);
                }
                else
                {
                    PrintStringOnPosition(dwarf.X, dwarf.Y, dwarf.Body, dwarf.Color);
                }

                // draw rocks
                foreach (var r in rocks)
                {
                    PrintOnPosition(r.X, r.Y, r.Symbol, r.Color);
                }

                // print game info
                PrintStringOnPosition(WindowWidth - 8, 0, "Lives: " + livesCount, ConsoleColor.White);
                PrintStringOnPosition(0, 0, "Score: " + score, ConsoleColor.White);

                // slow game down
                Thread.Sleep(150);
            }
        }
コード例 #19
0
        static void Main()
        {
            Random rnd = new Random();

            string[]    rockTypes = { "!", "@", "#", "$", "%", "^", "&", "*", ".", ";" };
            List <Item> rocks     = new List <Item>();
            long        score     = 0;
            double      SleepTime = 200;

            Console.CursorVisible = false;
            Console.BufferHeight  = Console.WindowHeight;
            Console.WriteLine("Falling rocks game!");
            Console.WriteLine("Use the arrow keys to move the dwarf (0) at the bottom.");
            Console.WriteLine("Try to avoid being hit!");
            Console.WriteLine("Press one of the arrows to start...");
            ConsoleKeyInfo pressedKey = Console.ReadKey();

            if (pressedKey.Key == ConsoleKey.LeftArrow ||
                pressedKey.Key == ConsoleKey.RightArrow)
            {
                Dwarf dwarf = new Dwarf();
                dwarf.Display();
                while (true)
                {
                    for (int i = rnd.Next(0, 3); i < 4; i++) //randomly generate 1-4 new rocks
                    {
                        Item rock = new Item(rnd.Next(0, Console.WindowWidth - 1),
                                             0,
                                             (ConsoleColor)rnd.Next((int)ConsoleColor.Blue, (int)ConsoleColor.White),
                                             rockTypes[rnd.Next(rockTypes.Length)]);
                        rock.Display();       //displaying the rocks
                        rocks.Add(rock);      //adding the rocks to a list
                    }
                    foreach (Item i in rocks) //generating falling of rocks
                    {
                        i.Clear();
                        i.Y++;
                        if (i.Y < (Console.WindowHeight))
                        {
                            i.Display();                               //showing the fallen rock only in the field
                        }
                        else
                        {
                            score++;  //adding score for each fallen rock
                        }
                        if ((i.X >= dwarf.X) &&
                            (i.X <= dwarf.X + 2) &&
                            (i.Y >= dwarf.Y) &&
                            (i.Y <= dwarf.Y + 2))
                        {
                            Console.Clear();
                            Console.SetCursorPosition(0, 0);
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.WriteLine("Game over!!!");
                            Console.WriteLine("Your score is {0} points!", score);
                            return;
                        }
                    }
                    rocks.RemoveAll(i => i.Y >= (Console.WindowHeight));
                    if (Console.KeyAvailable)
                    {
                        dwarf.Move(Console.ReadKey());
                    }
                    Thread.Sleep((int)SleepTime);
                    SleepTime -= 0.05;
                }
            }
        }