Advertisement
SvetlozarDraganov

Falling Rocks

Jun 2nd, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.12 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.  
  7. namespace _11_Falling_Rocks
  8. {
  9.     class Program
  10.     {
  11.         static int shipXposition = Console.WindowWidth / 2; //Starting Ship Position
  12.         static int shipYposition = Console.WindowHeight - 1;
  13.         static string ship = "(0)"; //Ship Appereance
  14.         static Random generator = new Random(); //Random generator
  15.         static List<List<int>> rocksXposition = new List<List<int>>(); //Store information about the X-coordinate of each rock
  16.         static int rocksYposition = 0; //Counter for Y-coordinate of rocks when they are less than console.windowheight
  17.         static String[] colorNames = { "Blue", "Green", "Cyan", "Red", "Magenta", "Yellow", "White" }; //List of the rocks-colors
  18.         static List<List<string>> rocksColors = new List<List<string>>(); //Store information about the color of each rock
  19.         static string symbols = "!@#$%^&*"; //Symbols used as rocks in the game
  20.         static List<List<char>> rocksSymbols = new List<List<char>>(); //Store information about the symbol of each rock
  21.         static int minSymbolsPerRow = 1;
  22.         static int maxSymbolsPerRow = 1;
  23.         static int minXrockPosition = 1;
  24.         static int maxXrockPosition = Console.WindowWidth - 1;
  25.         static int points = 0;
  26.         static int gameSpeed = 350;
  27.         static int gameLevelHolder = 0;
  28.  
  29.         static void SetInitialValues()
  30.         {
  31.             Console.BufferHeight = Console.WindowHeight;
  32.             Console.BufferWidth = Console.WindowWidth;
  33.         }
  34.  
  35.         static void GenerateNewRocks()
  36.         {
  37.             rocksXposition.Add(new List<int>());
  38.             rocksColors.Add(new List<string>());
  39.             rocksSymbols.Add(new List<char>());
  40.  
  41.             int numberOfSymbols = generator.Next(minSymbolsPerRow, maxSymbolsPerRow);
  42.             for (int i = 0; i < numberOfSymbols; i++)
  43.             {
  44.                 rocksXposition[rocksXposition.Count-1].Add(generator.Next(minXrockPosition, maxXrockPosition)); //Store each rock-X-coordinate
  45.                 string randomColor = colorNames[generator.Next(colorNames.Length)]; //Generate random rock-color name
  46.                 rocksColors[rocksColors.Count - 1].Add(randomColor); //Store each rock-color
  47.                 int randomSymbol = generator.Next(symbols.Length); // Generate random rock-symbol
  48.                 rocksSymbols[rocksSymbols.Count - 1].Add(symbols[randomSymbol]); //Store each rock symbol
  49.             }
  50.         }
  51.  
  52.         static void RemoveFirstRowOfRocks()
  53.         {
  54.             points += rocksXposition[0].Count;
  55.             rocksXposition.RemoveAt(0);
  56.             rocksColors.RemoveAt(0);
  57.             rocksSymbols.RemoveAt(0);
  58.             GenerateNewRocks();
  59.         }
  60.  
  61.         static void PrintShip()
  62.         {
  63.             Console.ResetColor();
  64.             Console.SetCursorPosition(shipXposition, shipYposition);
  65.             Console.WriteLine(ship);
  66.         }
  67.  
  68.         static void MoveShipRight()
  69.         {
  70.             if (shipXposition < Console.WindowWidth - 4)
  71.             {
  72.                 shipXposition++;
  73.             }
  74.         }
  75.  
  76.         static void MoveShipLeft()
  77.         {
  78.             if (shipXposition > 1)
  79.             {
  80.                 shipXposition--;
  81.             }
  82.         }
  83.  
  84.         static void PrintRocks()
  85.         {
  86.             if (rocksYposition < shipYposition)
  87.             {
  88.                 GenerateNewRocks();
  89.                 for (int row = 0; row <= rocksYposition; row++)
  90.                 {
  91.                     for (int col = 0; col < rocksXposition[row].Count; col++)
  92.                     {
  93.                         Console.SetCursorPosition(rocksXposition[row][col], rocksYposition - row);
  94.                         Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), rocksColors[row][col]); //Set Random Foreground color
  95.                         Console.WriteLine("{0}",rocksSymbols[row][col]);
  96.                     }
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 RemoveFirstRowOfRocks();
  102.                 bool rockHitShip = false;
  103.                 for (int row = 0; row < Console.WindowHeight-1; row++)//same code is used
  104.                 {
  105.                     for (int col = 0; col < rocksXposition[row].Count; col++)
  106.                     {
  107.                         Console.SetCursorPosition(rocksXposition[row][col], Console.WindowHeight - 2 - row);
  108.                         Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), rocksColors[row][col]); //Set Random Foreground color
  109.                         Console.WriteLine("{0}",rocksSymbols[row][col]);
  110.                         if (row == 0 && (rocksXposition[0][col] == shipXposition || rocksXposition[0][col] == shipXposition + 1
  111.                             || rocksXposition[0][col] == shipXposition + 2))
  112.                         {
  113.                             rockHitShip = true;
  114.                         }
  115.                     }
  116.                 }
  117.                 if (rockHitShip == true)
  118.                 {
  119.                     Console.SetCursorPosition(Console.WindowWidth / 2-20, Console.WindowHeight / 2);
  120.                     Console.ResetColor();
  121.                     Console.Beep();
  122.                     Console.ForegroundColor = ConsoleColor.Green;
  123.                     Console.BackgroundColor = ConsoleColor.Red;
  124.                     Console.WriteLine("YOU HAVE CRASHED INTO A ROCK WITH {0} POINTS", points);
  125.                     Console.SetCursorPosition(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2+1);
  126.                     Console.WriteLine("PRESS ENTER/SPACE TO START OVER");
  127.                     points = 0;
  128.                     gameLevelHolder = 0;
  129.                     minSymbolsPerRow = 1;
  130.                     maxSymbolsPerRow = 2;
  131.                     gameSpeed = 350;
  132.                     rockHitShip = false;
  133.                     ConsoleKeyInfo pressEnterSpace;
  134.                     do
  135.                     {
  136.                         Console.ResetColor();
  137.                         Console.SetCursorPosition(0, 0);
  138.                         pressEnterSpace = Console.ReadKey();
  139.                        
  140.                     } while (pressEnterSpace.Key != ConsoleKey.Enter && pressEnterSpace.Key != ConsoleKey.Spacebar);
  141.                     rocksYposition = -1;
  142.                     rocksXposition.Clear();
  143.                     rocksColors.Clear();
  144.                     rocksSymbols.Clear();
  145.                 }
  146.             }
  147.         }
  148.  
  149.         static void MoveRocksDown()
  150.         {
  151.             rocksYposition++;
  152.         }
  153.  
  154.         static void GameLevel()
  155.         {
  156.             gameLevelHolder = points / 100;
  157.             if (minSymbolsPerRow < 5) minSymbolsPerRow = 1+ points/500;
  158.             if (maxSymbolsPerRow <= 10) maxSymbolsPerRow = 1 + points/250;
  159.             if (gameSpeed > 100) gameSpeed = 350 - points / 10;
  160.  
  161.         }
  162.  
  163.         static void PrintPoints()
  164.         {
  165.             Console.ForegroundColor = ConsoleColor.Green;
  166.             Console.BackgroundColor = ConsoleColor.Red;
  167.             Console.SetCursorPosition(Console.WindowWidth - 16 - (points.ToString()).Length, 0);
  168.             Console.WriteLine("LEVEL-{0}/POINTS={1}",gameLevelHolder,points);
  169.             Console.ResetColor();
  170.         }
  171.  
  172.         static void Main()
  173.         {
  174.             SetInitialValues();
  175.  
  176.             while (true)
  177.             {
  178.                 Console.Clear();
  179.                 if (Console.KeyAvailable)
  180.                 {
  181.                     ConsoleKeyInfo keyPressed = Console.ReadKey();
  182.                     if (keyPressed.Key == ConsoleKey.RightArrow)
  183.                     {
  184.                         MoveShipRight();
  185.                     }
  186.                     else if (keyPressed.Key == ConsoleKey.LeftArrow)
  187.                     {
  188.                         MoveShipLeft();
  189.                     }
  190.                 }
  191.                 PrintShip();
  192.                 PrintRocks();
  193.                 PrintPoints();
  194.                 MoveRocksDown();
  195.                 GameLevel();
  196.                 System.Threading.Thread.Sleep(gameSpeed);
  197.             }
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement