vonko1988

C#FallingRocksVersion1

Mar 14th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.20 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. struct Object //here we will keep info about the cars
  8. {
  9.     public int x;
  10.     public int y;
  11.     public ConsoleColor color;
  12.     public char c;
  13.     public string s;
  14. }
  15.  
  16. class JustCars
  17. {
  18.     static void SetConsoleProperties()
  19.     {
  20.         Console.WindowWidth = 30;
  21.         Console.WindowHeight = 25;
  22.         Console.BufferWidth = Console.WindowWidth;
  23.         Console.BufferHeight = Console.WindowHeight;
  24.         Console.ForegroundColor = ConsoleColor.Cyan;
  25.         Console.BackgroundColor = ConsoleColor.White;
  26.     }
  27.  
  28.     static void PrintOnPosition(int x, int y, char symbol, ConsoleColor color = ConsoleColor.Cyan)
  29.     {
  30.         Console.SetCursorPosition(x, y);
  31.         Console.ForegroundColor = color;
  32.         Console.Write(symbol);
  33.     }
  34.  
  35.     static void PrintStringOnPosition(int x, int y, string str, ConsoleColor color = ConsoleColor.Cyan)
  36.     {
  37.         Console.SetCursorPosition(x, y);
  38.         Console.ForegroundColor = color;
  39.         Console.Write(str);
  40.     }
  41.  
  42.  
  43.     static void DrawFieldBorder()
  44.     {
  45.         for (int i = 0; i < Console.WindowHeight; i++)
  46.         {
  47.             PrintOnPosition(playFieldWidth, i, '|', ConsoleColor.Black);
  48.         }
  49.     }
  50.  
  51.     static int playFieldWidth = 15;
  52.  
  53.     static void Main()
  54.     {
  55.         double acceleration = 0.1;
  56.         double currentSpeed = 150;
  57.         int currentSpeedInt = 0;
  58.         int lives = 10;
  59.         Object dwarf = new Object();
  60.         dwarf.x = playFieldWidth / 2;
  61.         dwarf.y = Console.WindowHeight - 1;
  62.         dwarf.c = '@';
  63.         dwarf.color = ConsoleColor.Magenta;
  64.         Random randomGenerator = new Random();
  65.         List<Object> objects = new List<Object>();
  66.  
  67.         while (true)
  68.         {
  69.             {
  70.                 Object newRock = new Object();
  71.                 int randomColor = randomGenerator.Next(0, 5);
  72.                 int randomRockType = randomGenerator.Next(0, 12);
  73.                 ConsoleColor rocksColor = ConsoleColor.Gray;
  74.  
  75.                 switch (randomColor)
  76.                 {
  77.                     case 0: rocksColor = ConsoleColor.Blue; break;
  78.                     case 1: rocksColor = ConsoleColor.Black; break;
  79.                     case 2: rocksColor = ConsoleColor.Green; break;
  80.                     case 3: rocksColor = ConsoleColor.DarkCyan; break;
  81.                     case 4: rocksColor = ConsoleColor.DarkMagenta; break;
  82.                 }
  83.  
  84.                 int chance = randomGenerator.Next(0, 100);
  85.                 if ((chance < 100) && (chance > 90))
  86.                 {
  87.                     newRock.color = rocksColor;
  88.                     newRock.x = randomGenerator.Next(0, playFieldWidth-2);
  89.                     newRock.y = 0;
  90.                     switch (randomRockType)
  91.                     {
  92.                         case 0: newRock.s = "^^^"; break;
  93.                         case 1: newRock.s = "***"; break;
  94.                         case 2: newRock.s = "&&&"; break;
  95.                         case 3: newRock.s = "+++"; break;
  96.                         case 4: newRock.s = "%%%"; break;
  97.                         case 5: newRock.s = "$$$"; break;
  98.                         case 6: newRock.s = "###"; break;
  99.                         case 7: newRock.s = "!!!"; break;
  100.                         case 8: newRock.s = "..."; break;
  101.                         case 9: newRock.s = ";;;"; break;
  102.                         case 10: newRock.s = "---"; break;
  103.                         case 11: newRock.s = "@@@"; break;
  104.                     }
  105.                     objects.Add(newRock);
  106.                 }
  107.                 else if ((chance <= 90) && (chance >= 70))
  108.                 {
  109.                     newRock.color = rocksColor;
  110.                     newRock.x = randomGenerator.Next(0, playFieldWidth - 2);
  111.                     newRock.y = 0;
  112.                     switch (randomRockType)
  113.                     {
  114.                         case 0: newRock.s = "^^"; break;
  115.                         case 1: newRock.s = "**"; break;
  116.                         case 2: newRock.s = "&&"; break;
  117.                         case 3: newRock.s = "++"; break;
  118.                         case 4: newRock.s = "%%"; break;
  119.                         case 5: newRock.s = "$$"; break;
  120.                         case 6: newRock.s = "##"; break;
  121.                         case 7: newRock.s = "!!"; break;
  122.                         case 8: newRock.s = ".."; break;
  123.                         case 9: newRock.s = ";;"; break;
  124.                         case 10: newRock.s = "--"; break;
  125.                         case 11: newRock.s = "@@"; break;
  126.                     }
  127.                     objects.Add(newRock);
  128.                 }
  129.                 else if ((chance < 70) && (chance > 5))
  130.                 {
  131.                     newRock.color = rocksColor;
  132.                     newRock.x = randomGenerator.Next(0, playFieldWidth - 2);
  133.                     newRock.y = 0;
  134.                     switch (randomRockType)
  135.                     {
  136.                         case 0: newRock.s = "^"; break;
  137.                         case 1: newRock.s = "*"; break;
  138.                         case 2: newRock.s = "&"; break;
  139.                         case 3: newRock.s = "+"; break;
  140.                         case 4: newRock.s = "%"; break;
  141.                         case 5: newRock.s = "$"; break;
  142.                         case 6: newRock.s = "#"; break;
  143.                         case 7: newRock.s = "!"; break;
  144.                         case 8: newRock.s = "."; break;
  145.                         case 9: newRock.s = ";"; break;
  146.                         case 10: newRock.s = "-"; break;
  147.                         case 11: newRock.s = "@"; break;
  148.                     }
  149.                     objects.Add(newRock);
  150.                 }
  151.                 else if (chance <= 5)
  152.                 {
  153.                     Object bonusObject = new Object();
  154.                     bonusObject.color = ConsoleColor.Red;
  155.                     bonusObject.x = randomGenerator.Next(0, playFieldWidth);
  156.                     bonusObject.y = 0;
  157.                     bonusObject.s = "5";
  158.                     objects.Add(bonusObject);
  159.                 }
  160.             }
  161.  
  162.  
  163.             if (Console.KeyAvailable)
  164.             {
  165.                 //while (Console.KeyAvailable) Console.ReadKey(true);
  166.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  167.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  168.                 {
  169.                     if (dwarf.x > 0)
  170.                     {
  171.                         dwarf.x--;
  172.                     }
  173.                 }
  174.                 if (pressedKey.Key == ConsoleKey.RightArrow)
  175.                 {
  176.                     if (dwarf.x < playFieldWidth - 3)
  177.                     {
  178.                         dwarf.x++;
  179.                     }
  180.                 }
  181.             }
  182.  
  183.             SetConsoleProperties();
  184.  
  185.             List<Object> newList = new List<Object>();
  186.  
  187.             for (int i = 0; i < objects.Count; i++)
  188.             {
  189.                 Object oldRock = objects[i];
  190.                 Object newObject = new Object();
  191.                 newObject.x = oldRock.x;
  192.                 newObject.y = oldRock.y + 1;
  193.                 newObject.s = oldRock.s;
  194.                 newObject.color = oldRock.color;
  195.                 if (newObject.y < Console.WindowHeight)
  196.                 {
  197.                     newList.Add(newObject);
  198.                     if ((newObject.s.Length == 3) && (newObject.y == dwarf.y) && ((newObject.x >= dwarf.x && newObject.x <= dwarf.x+2) ||
  199.                         (newObject.x >= dwarf.x-2) && (newObject.x < dwarf.x + 2)))
  200.                     {
  201.                         lives--;
  202.                         PrintStringOnPosition(dwarf.x+1, dwarf.y, "x", ConsoleColor.Red);
  203.                         Console.Beep(500, 500);
  204.                         Thread.Sleep(500);
  205.                         objects.Clear();
  206.                         dwarf.x = playFieldWidth / 2;
  207.  
  208.                         if (lives == 0)
  209.                         {
  210.                             Console.Clear();
  211.                             PrintStringOnPosition(Console.WindowWidth / 2 - 5, Console.WindowHeight / 2, "Game Over", ConsoleColor.Red);
  212.                             Thread.Sleep(2000);
  213.                             Main();
  214.                         }
  215.                     }
  216.                     else if ((newObject.s.Length == 2) && (newObject.y == dwarf.y) && ((newObject.x >= dwarf.x && newObject.x <= dwarf.x+2) ||
  217.                         (newObject.x >= dwarf.x - 1) && (newObject.x < dwarf.x + 1)))
  218.                     {
  219.                         lives--;
  220.                         PrintStringOnPosition(dwarf.x + 1, dwarf.y, "x", ConsoleColor.Red);
  221.                         Console.Beep(500, 500);
  222.                         Thread.Sleep(500);
  223.                         objects.Clear();
  224.                         dwarf.x = playFieldWidth / 2;
  225.  
  226.                         if (lives == 0)
  227.                         {
  228.                             Console.Clear();
  229.                             PrintStringOnPosition(Console.WindowWidth / 2 - 5, Console.WindowHeight / 2, "Game Over", ConsoleColor.Red);
  230.                             Thread.Sleep(2000);
  231.                             Main();
  232.                         }
  233.                     }
  234.                     else if ((newObject.s.Length == 1 && newObject.s != "5") && (newObject.y == dwarf.y) && (newObject.x >= dwarf.x && newObject.x <= dwarf.x+2))
  235.                     {
  236.                         lives--;
  237.                         PrintStringOnPosition(dwarf.x + 1, dwarf.y, "x", ConsoleColor.Red);
  238.                         Console.Beep(500, 500);
  239.                         Thread.Sleep(500);
  240.                         objects.Clear();
  241.                         dwarf.x = playFieldWidth / 2;
  242.  
  243.                         if (lives == 0)
  244.                         {
  245.                             Console.Clear();
  246.                             PrintStringOnPosition(Console.WindowWidth / 2 - 5, Console.WindowHeight / 2, "Game Over", ConsoleColor.Red);
  247.                             Thread.Sleep(2000);
  248.                             Main();
  249.                         }
  250.                     }
  251.                     else if ((newObject.s == "5") && (newObject.y == dwarf.y) && (newObject.x >= dwarf.x && newObject.x <= dwarf.x + 2))
  252.                     {
  253.                         currentSpeed += 5.0;
  254.                     }
  255.                 }
  256.             }
  257.  
  258.             objects = newList;
  259.  
  260.             Console.Clear();
  261.  
  262.             //Here we draw our dwarf
  263.             PrintStringOnPosition(dwarf.x, dwarf.y, "(0)", dwarf.color);
  264.  
  265.             foreach (Object rock in objects)
  266.             {
  267.                 PrintStringOnPosition(rock.x, rock.y, rock.s, rock.color);
  268.             }
  269.  
  270.             DrawFieldBorder();
  271.  
  272.             PrintStringOnPosition(playFieldWidth + 4, Console.WindowHeight / 2, "Lives: " + lives, ConsoleColor.Red);
  273.             PrintStringOnPosition(playFieldWidth + 1, Console.WindowHeight / 2 + 2, "Easiness:" + currentSpeedInt, ConsoleColor.Red);
  274.  
  275.             currentSpeed -= acceleration;
  276.             if (currentSpeed < 10)
  277.             {
  278.                 currentSpeed = 10;
  279.             }
  280.             currentSpeedInt = (int)currentSpeed;
  281.             Thread.Sleep((int)currentSpeed);
  282.         }
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment