Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- class FallingRocks
- {
- static int lives = 5;
- static int score = 10;
- struct Figure
- {
- public int x;
- public int y;
- public string s;
- public ConsoleColor color;
- }
- static void Main()
- {
- SetPlayfield();
- List<char> rocksChar = new List<char>() { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };
- List<ConsoleColor> colors = new List<ConsoleColor> { ConsoleColor.DarkYellow, ConsoleColor.Blue, ConsoleColor.Magenta,
- ConsoleColor.Red, ConsoleColor.White, ConsoleColor.Yellow, ConsoleColor.Gray };
- List<Figure> rocks = new List<Figure>();
- Random randomGenerator = new Random();
- Figure playfieldBorder = new Figure();
- playfieldBorder.x = 0;
- playfieldBorder.y = 3;
- playfieldBorder.s = new string('-', Console.WindowWidth - 1);
- playfieldBorder.color = ConsoleColor.Gray;
- Figure dwarf = new Figure();
- dwarf.x = Console.WindowWidth / 2;
- dwarf.y = Console.WindowHeight - 1;
- dwarf.s = "(0)";
- dwarf.color = ConsoleColor.Green;
- while (true)
- {
- CreateFigure(rocksChar, colors, rocks, randomGenerator);
- dwarf = MoveDwarf(dwarf);
- rocks = MoveRocks(rocks);
- Console.Clear();
- PrintOnPosition(playfieldBorder.x, playfieldBorder.y, playfieldBorder.s, playfieldBorder.color);
- foreach (Figure currentRock in rocks)
- {
- PrintOnPosition(currentRock.x, currentRock.y, currentRock.s, currentRock.color);
- }
- PrintOnPosition(dwarf.x, dwarf.y, dwarf.s, dwarf.color);
- PrintOnPosition(2, 1, "Lives: " + lives, ConsoleColor.Green);
- PrintOnPosition(21, 1, "Score: " + score, ConsoleColor.Green);
- score += 10;
- CheckDwarfIsHit(rocks, dwarf);
- Thread.Sleep(300);
- }
- }
- private static Figure MoveDwarf(Figure dwarf)
- {
- while (Console.KeyAvailable)
- {
- ConsoleKeyInfo pressedKey = Console.ReadKey();
- if (pressedKey.Key == ConsoleKey.LeftArrow)
- {
- if (dwarf.x - 1 >= 0)
- {
- dwarf.x--;
- }
- }
- else if (pressedKey.Key == ConsoleKey.RightArrow)
- {
- if (dwarf.x + 2 < Console.WindowWidth - 2)
- {
- dwarf.x++;
- }
- }
- }
- return dwarf;
- }
- private static List<Figure> MoveRocks(List<Figure> rocks)
- {
- for (int i = 0; i < rocks.Count; i++)
- {
- if (rocks[i].y < Console.WindowHeight - 1)
- {
- Figure tempRock = new Figure();
- tempRock.x = rocks[i].x;
- tempRock.y = rocks[i].y + 1;
- tempRock.s = rocks[i].s;
- tempRock.color = rocks[i].color;
- rocks[i] = tempRock;
- }
- else if (rocks[i].y == Console.WindowHeight - 1)
- {
- rocks.RemoveAt(i);
- }
- }
- return rocks;
- }
- private static void CheckDwarfIsHit(List<Figure> rocks, Figure dwarf)
- {
- foreach (Figure rock in rocks)
- {
- if (rock.s.Length == 1)
- {
- if (rock.y == dwarf.y && (rock.x == dwarf.x || rock.x == dwarf.x + 1 || rock.x == dwarf.x + 2))
- {
- PrintOnPosition(dwarf.x, dwarf.y, dwarf.s, ConsoleColor.Red);
- lives--;
- if (lives == 0)
- {
- Console.Clear();
- PrintOnPosition(15, 10, "Game Over!", ConsoleColor.Red);
- PrintOnPosition(8, 11, "Press any key to exit.", ConsoleColor.Red);
- Console.ReadLine();
- Environment.Exit(0);
- }
- }
- }
- //else if (rock.s.Length == 2)
- //{
- // if (rock.y == dwarf.y && (rock.x == dwarf.x || rock.x == dwarf.x + 1 || rock.x == dwarf.x + 2
- // || rock.x + 1 == dwarf.x || rock.x + 1 == dwarf.x + 1 || rock.x + 1 == dwarf.x + 2))
- // {
- // PrintOnPosition(dwarf.x, dwarf.y, dwarf.s, ConsoleColor.Red);
- // lives--;
- // if (lives == 0)
- // {
- // Console.Clear();
- // PrintOnPosition(15, 10, "Game Over!", ConsoleColor.Red);
- // PrintOnPosition(8, 11, "Press any key to exit.", ConsoleColor.Red);
- // Console.ReadLine();
- // Environment.Exit(0);
- // }
- // }
- //}
- //else if (rock.s.Length == 3)
- //{
- // if (rock.y == dwarf.y && (rock.x == dwarf.x || rock.x == dwarf.x + 1 || rock.x == dwarf.x + 2
- // || rock.x + 1 == dwarf.x || rock.x + 1 == dwarf.x + 1 || rock.x + 1 == dwarf.x + 2
- // || rock.x + 2 == dwarf.x || rock.x + 2 == dwarf.x + 2 || rock.x + 1 == dwarf.x + 2))
- // {
- // lives--;
- // if (lives == 0)
- // {
- // Console.Clear();
- // PrintOnPosition(15, 10, "Game Over!", ConsoleColor.Red);
- // PrintOnPosition(8, 11, "Press any key to exit.", ConsoleColor.Red);
- // Console.ReadLine();
- // Environment.Exit(0);
- // }
- // }
- //}
- }
- }
- private static void SetPlayfield()
- {
- Console.Title = "Falling rocks";
- Console.WindowWidth = 40;
- Console.BufferWidth = Console.WindowWidth;
- Console.WindowHeight = 20;
- Console.BufferHeight = Console.WindowHeight;
- Console.CursorVisible = false;
- }
- private static void CreateFigure(List<char> rocksChar, List<ConsoleColor> colors, List<Figure> rocks, Random randomGenerator)
- {
- Figure rock = new Figure();
- rock.x = randomGenerator.Next(0, Console.WindowWidth);
- rock.y = 4;
- char newRockChar = rocksChar[randomGenerator.Next(0, rocksChar.Count)];
- string newFigure = new string(newRockChar, randomGenerator.Next(1, 4));
- rock.s = newFigure;
- rock.color = colors[randomGenerator.Next(0, colors.Count)];
- rocks.Add(rock);
- }
- static void PrintOnPosition(int x, int y, string s, ConsoleColor color = ConsoleColor.Red)
- {
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = color;
- Console.Write(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement