Advertisement
soxa

FleeingDwarf

Nov 13th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. class FallingRocks
  6. {
  7.     struct Dwarf
  8.     {
  9.         public int x;
  10.         public int y;
  11.         public string c;
  12.         public ConsoleColor color;
  13.     }
  14.  
  15.     struct Rock
  16.     {
  17.         public int x;
  18.         public int y;
  19.         public string c;
  20.         public ConsoleColor color;
  21.     }
  22.  
  23.     static void StickOnPosition(int x, int y, string c, ConsoleColor color)
  24.     {
  25.         Console.SetCursorPosition(x, y);
  26.         Console.ForegroundColor = color;
  27.         Console.Write(c);
  28.     }
  29.  
  30.     static void FallingRocksRain(int x, int y, string c, ConsoleColor color)
  31.     {
  32.         Console.SetCursorPosition(x, y);
  33.         Console.ForegroundColor = color;
  34.         Console.Write(c);
  35.     }
  36.  
  37.     static void stickLivesView(int x, int y, string txtLives, int live, string txtScore, int score)
  38.     {
  39.         Console.SetCursorPosition(x, y);
  40.         Console.ForegroundColor = ConsoleColor.Red;
  41.         Console.Write(txtLives);
  42.         Console.Write(live);
  43.         Console.SetCursorPosition(x, y + 1);
  44.         Console.Write(txtScore);
  45.         Console.Write(score);
  46.     }
  47.  
  48.     static void Main()
  49.     {
  50.         Console.BufferHeight = Console.WindowHeight = 20; // Vertical Y
  51.         Console.BufferWidth = Console.WindowWidth = 38; // Horizontal X
  52.         Dwarf stick = new Dwarf();
  53.         int playfieldWidth = 18;
  54.         int stickLives = 5;
  55.         int result = 0;
  56.         int time = 0;
  57.  
  58.         stick.x = 9;
  59.         stick.y = Console.WindowHeight - 1;
  60.         stick.color = ConsoleColor.Yellow;
  61.         stick.c = "(0)";
  62.  
  63.         Random rand = new Random();
  64.  
  65.         Rock newRocks = new Rock();
  66.         string[] element = { "^", "@", "&", "%", "*", "#", "!", ".", ";", "=" };
  67.         List<Rock> rocks = new List<Rock>();
  68.  
  69.         StickOnPosition(stick.x, stick.y, stick.c, ConsoleColor.Yellow);
  70.  
  71.         while (true)
  72.         {
  73.             {
  74.                 newRocks.y = 0;
  75.                 newRocks.x = rand.Next(1, playfieldWidth + 3);
  76.                 newRocks.color = ConsoleColor.Green;//FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
  77.                 newRocks.c = element[rand.Next(0, 10)];
  78.                 if (rand.Next(1, 115) < 30)
  79.                 {
  80.                     if (rand.Next(1, 115) < 13)
  81.                     {
  82.                         newRocks.color = ConsoleColor.Red;
  83.                         newRocks.c = "+";
  84.                     }
  85.                     else
  86.                     {
  87.                         newRocks.color = ConsoleColor.Yellow;
  88.                         newRocks.c = "$";
  89.                     }
  90.                 }
  91.                 rocks.Add(newRocks);
  92.             }
  93.             if (Console.KeyAvailable)
  94.             {
  95.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  96.                 while (Console.KeyAvailable) Console.ReadKey(true);
  97.  
  98.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  99.                 {
  100.                     if (stick.x - 1 >= 0)
  101.                     {
  102.                         stick.x -= 1;
  103.                     }
  104.                 }
  105.                 if (pressedKey.Key == ConsoleKey.RightArrow)
  106.                 {
  107.                     if (stick.x + 1 <= playfieldWidth)
  108.                     {
  109.                         stick.x += 1;
  110.                     }
  111.                 }
  112.             }
  113.             StickOnPosition(stick.x, stick.y, stick.c, ConsoleColor.Yellow);
  114.             if ((500 - time) > 150)
  115.             {
  116.                 Thread.Sleep(500 - time);
  117.             }
  118.             else
  119.             {
  120.                 Thread.Sleep(150);
  121.             }
  122.             Console.Clear();
  123.  
  124.             List<Rock> newList = new List<Rock>();
  125.             for (int i = 0; i < rocks.Count; i++)
  126.             {
  127.                 Rock oldRocks = rocks[i];
  128.                 Rock newRockses = new Rock();
  129.                 newRockses.x = oldRocks.x;
  130.                 newRockses.y = oldRocks.y + 1;
  131.                 newRockses.c = oldRocks.c;
  132.                 newRockses.color = oldRocks.color;
  133.  
  134.  
  135.                 if ((stick.x == newRockses.x && stick.y == newRockses.y) || (stick.x + 1 == newRockses.x && stick.y == newRockses.y) || (stick.x + 2 == newRockses.x && stick.y == newRockses.y))
  136.                 {
  137.                     if (newRockses.c == "$")
  138.                     {
  139.                         result++;
  140.                         if (result % 5 == 0)
  141.                         {
  142.                             time += 25;
  143.                         }
  144.                         continue;
  145.                     }
  146.                     if (newRockses.c == "+")
  147.                     {
  148.                         stickLives++;
  149.                         continue;
  150.                     }
  151.                     stickLives--;
  152.  
  153.                     for (int j = 0; j < 240; j++)
  154.                     {
  155.                         StickOnPosition(stick.x+1, stick.y, "X", ConsoleColor.Red);
  156.                     }
  157.                        
  158.                    
  159.                     if (stickLives == 0)
  160.                     {
  161.                         stickLivesView(25, 10, "Lives : ", stickLives, "Score : ", result);
  162.                         Console.SetCursorPosition(7, 13);
  163.                         Console.WriteLine("Press any key to exit !!");
  164.                         Console.ReadLine();
  165.                         return;
  166.                     }
  167.                      break;
  168.                 }
  169.                 if (newRockses.y < Console.WindowHeight)
  170.                 {
  171.                     newList.Add(newRockses);
  172.                 }
  173.             }
  174.             rocks = newList;
  175.             stickLivesView(25, 10, "Lives : ", stickLives, "Score : ", result);
  176.             foreach (Rock roc in rocks)
  177.             {
  178.                 FallingRocksRain(roc.x, roc.y, roc.c, roc.color);
  179.             }
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement