Advertisement
Fleshian

Fallin Stones2

Mar 21st, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.08 KB | None | 0 0
  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 = 25;
  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.             while (true)
  59.             {
  60.  
  61.                 // stoneGenerator = -1 = empty row  stoneGenerator = 0-3 = 1 to 4 stones per row
  62.                 int stoneGenerator = randomNumbers.Next(-1, 3);
  63.                 // random col
  64.                 int colGenerator = randomNumbers.Next(0, playerField);
  65.                 // Choosing random color
  66.                 string colorName = colorNames[randomNumbers.Next(colorNames.Length)];
  67.                 // chance to get life or enemys
  68.                 int chance = randomNumbers.Next(0, 100);
  69.                 // if the row is not empty
  70.                 if (stoneGenerator != -1)                
  71.                 {
  72.                     // 2% chance to generate row with Life on it
  73.                     if (chance < 3)
  74.                     {
  75.                         Object livePlus = new Object();
  76.                         livePlus.col = colGenerator;
  77.                         livePlus.row = 0;
  78.                         livePlus.str = "L";
  79.                         livePlus.color = ConsoleColor.Red;
  80.                         objects.Add(livePlus);
  81.                     }
  82.                     else
  83.                     {
  84.                         for (int i = 0; i <= stoneGenerator; i++)
  85.                         {
  86.                             int enemysGenerator = randomNumbers.Next(enemys.Length);
  87.                             Object enemyObjects = new Object();
  88.                             enemyObjects.col = colGenerator;
  89.                             enemyObjects.row = 0;
  90.                             enemyObjects.str = enemys[enemysGenerator];
  91.                             enemyObjects.color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorName);
  92.                             objects.Add(enemyObjects);
  93.                         }
  94.                     }
  95.                 }
  96.                 // moving the dwarf
  97.                 while (Console.KeyAvailable)
  98.                 {
  99.                    
  100.                     ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  101.                     if (pressedKey.Key == ConsoleKey.LeftArrow)
  102.                     {
  103.                         if (dwarf.col - 1 >= 0)
  104.                         {
  105.                             dwarf.col = dwarf.col - 1;
  106.                             PrintOnPosition(dwarf.col, dwarf.row, dwarf.str, dwarf.color);
  107.                             PrintOnPosition(dwarf.col + 3, dwarf.row, " ");
  108.                         }
  109.                     }
  110.                     if (pressedKey.Key == ConsoleKey.RightArrow)
  111.                     {
  112.                         if (dwarf.col + 2 <= playerField)
  113.                         {
  114.                             dwarf.col = dwarf.col + 1;
  115.                             PrintOnPosition(dwarf.col, dwarf.row, dwarf.str, dwarf.color);
  116.                             PrintOnPosition(dwarf.col - 1, dwarf.row, " ");
  117.                         }
  118.                     }
  119.                 }
  120.                                                        
  121.                 List<Object> newObjects = new List<Object>();
  122.                 for (int i = 0; i < objects.Count; i++ )
  123.                 {
  124.                     Object oldObject = objects[i];
  125.                     Object newObject = new Object();
  126.                     newObject.col = oldObject.col;
  127.                     newObject.row = oldObject.row + 1;
  128.                     newObject.str = oldObject.str;
  129.                     newObject.color = oldObject.color;
  130.                     if (newObject.row == dwarf.row)  // Points
  131.                     {
  132.                         playerPoints += 10;
  133.                     }
  134.                     if (newObject.str == "L" && newObject.row == dwarf.row && (newObject.col >= dwarf.col && newObject.col <= dwarf.col + 2)) // lives++
  135.                     {
  136.                         livesCount++;
  137.                         if (livesCount >= 5) livesCount = 5;                        
  138.                     }
  139.                     else if ((newObject.row == dwarf.row && (newObject.col >= dwarf.col && newObject.col <= dwarf.col + 2)) || // check colision with single character stones
  140.                         ((newObject.str == "++" || newObject.str == "--") && (newObject.row == dwarf.row) &&                   // check colision with double character stones
  141.                         (newObject.col + 1 >= dwarf.col && newObject.col + 1 <= dwarf.col + 2)))
  142.                     {
  143.                         playerPoints -= 1000;
  144.                         if (playerPoints <= 0) playerPoints = 0;
  145.                         objects.Clear();
  146.                         livesCount--;
  147.                         threadSpeed = 150;
  148.                         if (livesCount == 0)
  149.                         {
  150.                             PrintOnPosition(25, 15, "GAME OVER", ConsoleColor.Red);
  151.                             PrintOnPosition(25, 10, "Lives :" + livesCount, ConsoleColor.Black);
  152.                             Console.SetCursorPosition(25,16);
  153.                             Console.ReadLine();
  154.                             return;
  155.                         }
  156.                     }
  157.                     if (newObject.row < Console.WindowHeight)
  158.                     {
  159.                         newObjects.Add(newObject);
  160.                     }
  161.                 }
  162.                 Console.Clear();
  163.                 objects = newObjects;
  164.  
  165.                 // print everything
  166.                 PrintOnPosition(dwarf.col, dwarf.row, dwarf.str, dwarf.color);
  167.                 foreach (Object obj in objects)
  168.                 {                  
  169.                     PrintOnPosition(obj.col, obj.row, obj.str, obj.color);                                            
  170.                 }
  171.  
  172.                 PrintOnPosition(25, 10, "Lives :" + livesCount, ConsoleColor.Black);
  173.                 PrintOnPosition(25, 11, "Points : " + playerPoints, ConsoleColor.Black);
  174.                    // Press button to reset speed
  175.  
  176.                 roundsCounter++;
  177.                 if (playerPoints % 1000 == 0 && playerPoints > 0)
  178.                 {
  179.                     threadSpeed -= 5;
  180.                 }
  181.                 Thread.Sleep(threadSpeed);
  182.  
  183.             }
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement