ntodorova

11_FallingRocks

Nov 24th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. class FallingRocks
  7. {
  8.     static int playerPadSize = 3;
  9.     static int playfieldWidth = 40;
  10.     static int livesCount = 10;
  11.     static int pointsCount = 0;
  12.     static string[] rocksSymbols = { "^^", "@@", "*", "+++", "&", ";", "%", "$$", "#", "!", ".." };
  13.     static int indexRocksSymbols;
  14.  
  15.     struct Object
  16.     {
  17.         public int x;
  18.         public int y;
  19.         public string c;
  20.         public ConsoleColor color;
  21.     }
  22.  
  23.     static void RemoveScrollBars()
  24.     {
  25.         Console.ForegroundColor = ConsoleColor.Gray;
  26.         Console.BufferHeight = Console.WindowHeight = 20;
  27.         Console.BufferWidth = Console.WindowWidth = 60;
  28.     }
  29.  
  30.     static void PrintOnPosition(int x, int y, char c, ConsoleColor color = ConsoleColor.Gray)
  31.     {
  32.         Console.SetCursorPosition(x, y);
  33.         Console.ForegroundColor = color;
  34.         Console.Write(c);
  35.     }
  36.  
  37.     static void PrintStringOnPosition(int x, int y, string str, ConsoleColor color = ConsoleColor.Gray)
  38.     {
  39.         Console.SetCursorPosition(x, y);
  40.         Console.ForegroundColor = color;
  41.         Console.Write(str);
  42.     }
  43.  
  44.     static void Main()
  45.     {
  46.         RemoveScrollBars();
  47.  
  48.         Object dwarf = new Object();
  49.         dwarf.x = playfieldWidth / 2 - playerPadSize / 2;
  50.         dwarf.y = Console.WindowHeight - 1;
  51.         dwarf.c = "(0)";
  52.         dwarf.color = ConsoleColor.Yellow;
  53.  
  54.         Random randomGenerator = new Random();
  55.         List<Object> rocks = new List<Object>();
  56.  
  57.         // Get a string array with the names of ConsoleColor enumeration members.
  58.         string[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
  59.  
  60.         while (true)
  61.         {
  62.             bool hitted = false;
  63.             indexRocksSymbols = randomGenerator.Next(0, rocksSymbols.Length);
  64.             ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[randomGenerator.Next(0, colorNames.Length)]);
  65.  
  66.             Object newRock = new Object();
  67.             newRock.color = color;
  68.             newRock.c = rocksSymbols[indexRocksSymbols];
  69.             newRock.x = randomGenerator.Next(0, playfieldWidth);
  70.             newRock.y = 0;
  71.             rocks.Add(newRock);
  72.  
  73.             if (Console.KeyAvailable)
  74.             {
  75.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  76.                 while (Console.KeyAvailable) Console.ReadKey(true);
  77.  
  78.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  79.                 {
  80.                     if (dwarf.x - 1 >= 0)
  81.                     {
  82.                         dwarf.x = dwarf.x - 1;
  83.                     }
  84.                 }
  85.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  86.                 {
  87.                     if (dwarf.x + 1 < playfieldWidth)
  88.                     {
  89.                         dwarf.x = dwarf.x + 1;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             for (int i = 0; i < rocks.Count; i++)
  95.             {
  96.                 Object oldObject = rocks[i];
  97.                 Object newObject = new Object();
  98.                 newObject.x = oldObject.x;
  99.                 newObject.y = oldObject.y + 1;
  100.                 newObject.c = oldObject.c;
  101.                 newObject.color = oldObject.color;
  102.                 rocks.Remove(oldObject);
  103.  
  104.                 if (newObject.y == Console.WindowHeight)
  105.                 {
  106.                     pointsCount++;
  107.                 }
  108.  
  109.                 if ((newObject.y == dwarf.y && newObject.x == dwarf.x)
  110.                     || (newObject.y == dwarf.y && newObject.x == dwarf.x + 1)
  111.                     || (newObject.y == dwarf.y && newObject.x == dwarf.x + 2))
  112.                 {
  113.                     livesCount--;
  114.                     hitted = true;
  115.  
  116.                     if (livesCount <= 0)
  117.                     {
  118.                         PrintStringOnPosition(15, 7, "GAME OVER", ConsoleColor.Red);
  119.                         PrintStringOnPosition(15, 9, "Press Enter to continue", ConsoleColor.Red);
  120.                         Console.ReadLine();
  121.                         Environment.Exit(0);
  122.                     }
  123.                 }
  124.  
  125.                 if (newObject.y < Console.WindowHeight)
  126.                 {
  127.                     rocks.Add(newObject);
  128.                 }
  129.             }
  130.  
  131.             Console.Clear();
  132.  
  133.             // Print the dwarf
  134.             if (hitted)
  135.             {
  136.                 rocks.Clear();
  137.                 PrintStringOnPosition(dwarf.x, dwarf.y, "XXX", ConsoleColor.Red);
  138.             }
  139.             else
  140.             {
  141.                 PrintStringOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
  142.             }
  143.  
  144.             // Print the rocks
  145.             foreach (Object rock in rocks)
  146.             {
  147.                 PrintStringOnPosition(rock.x, rock.y, rock.c, rock.color);
  148.             }
  149.  
  150.             PrintStringOnPosition(41, 5, "Lives: " + livesCount, ConsoleColor.White);
  151.             PrintStringOnPosition(41, 7, "Score: " + pointsCount, ConsoleColor.White);
  152.  
  153.             Thread.Sleep(300);
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment