Advertisement
Fleshian

Untitled

Mar 19th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace FallingStones
  9. {
  10.     struct Object
  11.     {
  12.         public int row;
  13.         public int col;
  14.         public string str;
  15.         public ConsoleColor color;    
  16.     }
  17.     class FallingRocks
  18.     {
  19.         static void PrintOnPosition(int col, int row, string objects, ConsoleColor color = ConsoleColor.Black)
  20.         {
  21.             Console.ForegroundColor = color;
  22.             Console.SetCursorPosition(col, row);
  23.             Console.Write(objects);
  24.         }
  25.         static void PrintCharOnPosition(int col, int row, char objects, ConsoleColor color = ConsoleColor.Black)
  26.         {
  27.             Console.ForegroundColor = color;
  28.             Console.SetCursorPosition(col, row);
  29.             Console.Write(objects);
  30.         }
  31.         static void Main()
  32.         {
  33.             Console.BufferHeight = Console.WindowHeight = 25;
  34.             Console.BufferWidth = Console.WindowWidth = 60;
  35.             Console.BackgroundColor = ConsoleColor.White;
  36.             Console.ForegroundColor = ConsoleColor.Black;
  37.  
  38.             int playerField = 20;
  39.             int livesCount = 2;
  40.             int playerPoints = 0;
  41.             int roundsCounter = 0;
  42.             int threadSpeed = 150;
  43.            
  44.             Random randomNumbers = new Random();
  45.            
  46.             Object dwarf = new Object();
  47.             dwarf.col = playerField / 2 - 1;
  48.             dwarf.row = Console.WindowHeight - 1;
  49.             dwarf.str = "(0)";
  50.             dwarf.color = ConsoleColor.Black;
  51.             PrintOnPosition(dwarf.col, dwarf.row, dwarf.str,dwarf.color);
  52.             List<Object> objects = new List<Object>();
  53.  
  54.             string[] enemys = { "@", "*", "+", "-", "^", "&", "%", "!", "#", "$", ".", ";", "++", "--" };
  55.             string[] colorNames = {"Black", "Cyan", "Green", "Magenta",  
  56.                                     "DarkBlue", "DarkCyan", "DarkGreen", "DarkMagenta", "DarkRed", "DarkYellow"} ;
  57.  
  58.             //Console.BufferHeight = 300;
  59.             //foreach (string color in colorNames)
  60.             //{
  61.             //    Console.WriteLine(color);    
  62.             //}
  63.             //Console.ReadLine();
  64.             while (true)
  65.             {
  66.  
  67.                 // stoneGenerator = -1 = empty row  stoneGenerator = 0-3 = 1 to 4 stones per row
  68.                 int stoneGenerator = randomNumbers.Next(-1, 3);
  69.                 // random col
  70.                 int colGenerator = randomNumbers.Next(0, playerField);
  71.                 // Choosing random color . it is -1 beacuse last color in the array is white and the console is white so the item will not be seen
  72.                 string colorName = colorNames[randomNumbers.Next(colorNames.Length)];
  73.                 // chance to get life or enemys
  74.                 int chance = randomNumbers.Next(0, 100);
  75.                 // if the row is not empty
  76.                 if (stoneGenerator != -1)                
  77.                 {
  78.                     // 3% chance to generate row with Life on it
  79.                     if (chance < 4)
  80.                     {
  81.                         Object livePlus = new Object();
  82.                         livePlus.col = colGenerator;
  83.                         livePlus.row = 0;
  84.                         livePlus.str = "L";
  85.                         livePlus.color = ConsoleColor.Red;
  86.                         objects.Add(livePlus);
  87.                     }
  88.                     else
  89.                     {
  90.                         for (int i = 0; i <= stoneGenerator; i++)
  91.                         {
  92.                             int enemysGenerator = randomNumbers.Next(enemys.Length);
  93.                             Object enemyObjects = new Object();
  94.                             enemyObjects.col = colGenerator;
  95.                             enemyObjects.row = 0;
  96.                             enemyObjects.str = enemys[enemysGenerator];
  97.                             enemyObjects.color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorName);
  98.                             objects.Add(enemyObjects);
  99.                         }
  100.                     }
  101.                 }
  102.                 // moving the dwarf
  103.                 while (Console.KeyAvailable)
  104.                 {
  105.                    
  106.                     ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  107.                     if (pressedKey.Key == ConsoleKey.LeftArrow)
  108.                     {
  109.                         if (dwarf.col - 1 >= 0)
  110.                         {
  111.                             dwarf.col = dwarf.col - 1;
  112.                             PrintOnPosition(dwarf.col, dwarf.row, dwarf.str, dwarf.color);
  113.                             PrintOnPosition(dwarf.col + 3, dwarf.row, " ");
  114.                         }
  115.                     }
  116.                     if (pressedKey.Key == ConsoleKey.RightArrow)
  117.                     {
  118.                         if (dwarf.col + 2 <= playerField)
  119.                         {
  120.                             dwarf.col = dwarf.col + 1;
  121.                             PrintOnPosition(dwarf.col, dwarf.row, dwarf.str, dwarf.color);
  122.                             PrintOnPosition(dwarf.col - 1, dwarf.row, " ");
  123.                         }
  124.                     }
  125.                 }
  126.                                                        
  127.                 List<Object> newObjects = new List<Object>();
  128.                 for (int i = 0; i < objects.Count; i++ )
  129.                 {
  130.                     Object oldObject = objects[i];
  131.                     Object newObject = new Object();
  132.                     newObject.col = oldObject.col;
  133.                     newObject.row = oldObject.row + 1;
  134.                     newObject.str = oldObject.str;
  135.                     newObject.color = oldObject.color;
  136.                     if (newObject.row == dwarf.row)  // Points
  137.                     {
  138.                         playerPoints += 10;
  139.                     }
  140.                     if (newObject.str == "L" && newObject.row == dwarf.row && (newObject.col >= dwarf.col && newObject.col <= dwarf.col + 2)) // lives++
  141.                     {
  142.                         livesCount++;
  143.                         if (livesCount >= 5) livesCount = 5;                        
  144.                     }
  145.                     else if ((newObject.row == dwarf.row && (newObject.col >= dwarf.col && newObject.col <= dwarf.col + 2)) || // check colision with single character stones
  146.                         ((newObject.str == "++" || newObject.str == "--") && (newObject.row == dwarf.row) &&                   // check colision with double character stones
  147.                         (newObject.col + 1 >= dwarf.col && newObject.col + 1 <= dwarf.col + 2)))
  148.                     {
  149.                         playerPoints -= 1000;
  150.                         if (playerPoints <= 0) playerPoints = 0;
  151.                         objects.Clear();
  152.                         livesCount--;
  153.                         if (livesCount == 0)
  154.                         {
  155.                             PrintOnPosition(25, 15, "GAME OVER", ConsoleColor.Red);
  156.                             PrintOnPosition(25, 10, "Lives :" + livesCount, ConsoleColor.Black);
  157.                             Console.SetCursorPosition(25,16);
  158.                             Console.ReadLine();
  159.                             return;
  160.                         }
  161.                     }
  162.                     if (newObject.row < Console.WindowHeight)
  163.                     {
  164.                         newObjects.Add(newObject);
  165.                     }
  166.                 }
  167.                 Console.Clear();
  168.                 objects = newObjects;
  169.  
  170.                 // print everything
  171.                 PrintOnPosition(dwarf.col, dwarf.row, dwarf.str, dwarf.color);
  172.                 foreach (Object obj in objects)
  173.                 {                  
  174.                     PrintOnPosition(obj.col, obj.row, obj.str, obj.color);                                            
  175.                 }
  176.  
  177.                 PrintOnPosition(25, 10, "Lives :" + livesCount, ConsoleColor.Black);
  178.                 PrintOnPosition(25, 11, "Points : " + playerPoints, ConsoleColor.Black);
  179.                    // Press button to reset speed
  180.  
  181.                 roundsCounter++;
  182.                 if (playerPoints % 1000 == 0)
  183.                 {
  184.                     threadSpeed -= 5;        
  185.                 }
  186.                 Thread.Sleep(threadSpeed);
  187.  
  188.             }
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement