示例#1
0
        static void Main()
        {
            string[] visualization = new string[] { "*", "@", "%", "$", "^", "&", "#", "+", ".", "+++", "++", "--", "!" };

            ConsoleColor[] colors = new ConsoleColor[] { ConsoleColor.Red, ConsoleColor.Magenta, ConsoleColor.Green, ConsoleColor.Yellow, ConsoleColor.Cyan };

            RemoveScrollBars();
            Player myplayer = new Player("(0)", Console.WindowHeight - 1);
            myplayer.Draw();

            // create rocks
            Rock[] rocks = new Rock[15];

            Random random = new Random();

            for (int createrRock = 0; createrRock < rocks.Length; createrRock++)
            {
                rocks[createrRock] = new Rock(visualization[random.Next(0, visualization.Length - 1)], colors[random.Next(0, colors.Length - 1)], createrRock * 5 + random.Next(0, 3), random.Next(3, 10));
            }

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key == ConsoleKey.LeftArrow)
                    {
                        myplayer.MoveLeft();
                    }
                    if (keyInfo.Key == ConsoleKey.RightArrow)
                    {
                        myplayer.MoveRight();
                    }
                    if (keyInfo.Key == ConsoleKey.Escape)
                    {
                        break;
                    }
                }

                Console.Clear();
                myplayer.Draw();

                //Draw rocks
                for (int counterRocks = 0; counterRocks < rocks.Length; counterRocks++)
                {
                    rocks[counterRocks].Move();
                    rocks[counterRocks].Draw();
                }

                if (myplayer.IsDead(rocks) == true)
                {
                    Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
                    Console.WriteLine("GAME OVER");
                    break;
                }
                Thread.Sleep(50); // Delay the program
            }
        }
        static void Main(string[] args)
        {
            Console.BufferHeight = Console.WindowHeight = 20;
            Console.BufferWidth = Console.WindowWidth = 45;
            Console.BackgroundColor = ConsoleColor.Gray;

            Player player = new Player(Console.WindowWidth / 3, Console.WindowHeight - 1, 
                'O', ConsoleColor.DarkRed);


            GameInfo[] stats = new GameInfo[2];
            stats[0] = new GameInfo((Console.WindowWidth / 3) * 2, 10, "LIVES", 5, ConsoleColor.Black);
            stats[1] = new GameInfo((Console.WindowWidth / 3) * 2, 4, "POINTS", 0, ConsoleColor.Black);

            GameInfo gameOver = new GameInfo(Console.WindowWidth / 2, Console.WindowHeight / 2, "GAME OVER", 0, ConsoleColor.Red);

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

            while (true)
            {
                //Move player
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo pressed = Console.ReadKey(true);
                    while (Console.KeyAvailable) Console.ReadKey(true);

                    if (pressed.Key == ConsoleKey.LeftArrow)
                    {
                        if (player.X - 1 >= 0)
                            player.X -= 1;
                    }
                    else if (pressed.Key == ConsoleKey.RightArrow)
                    {
                        if (player.X + 1 < (Console.WindowWidth / 3) * 2)
                            player.X += 1;
                    }
                }

                //Create random rock
                Rock newRock = new Rock();
                newRock.SetRandom((Console.WindowWidth / 3) * 2);
                rocks.Add(newRock);
                //Move rocks
                for (int rock = 0; rock < rocks.Count; rock++)
                {
                    rocks[rock].Y += 1;
                    if (rocks[rock].Y >= Console.WindowHeight)
                    {
                        rocks.Remove(rocks[rock]);
                        stats[1].Amount += 1;
                    }

                    //Check if collision
                    if (rocks[rock].X == player.X && rocks[rock].Y == player.Y)
                    {
                        player.Symbol = 'X';
                        Console.Write((char)7);
                        stats[0].Amount -= 1;
                        rock--;
                        if (stats[0].Amount <= 0)
                        {
                            Console.Clear();
                            gameOver.Print();
                            Console.ReadKey();
                            return;
                        }
                    }
                }
                //Clear console
                Console.Clear();
                //Redraw field
                player.Print();
                if (player.Symbol == 'X') player.Symbol = 'O';

                foreach (var rock in rocks)
                {
                    rock.Print();
                }

                foreach (var stat in stats)
                {
                    stat.Print();
                }
                //Draw Info

                Thread.Sleep(220);
            }
        }