alexmitev

FallingRocks

Nov 7th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8.  
  9.  
  10. struct Objects
  11. {
  12.     public int col;
  13.     public int row;
  14.     public string c;
  15.     public ConsoleColor color;
  16. }
  17.  
  18. class FallingROcks
  19. {
  20.     static void PrintCharOnPosition(int col, int row, string c, ConsoleColor color = ConsoleColor.White)
  21.     {
  22.         Console.SetCursorPosition(col, row);
  23.         Console.ForegroundColor = color;
  24.         Console.Write(c);
  25.     }
  26.     static void Main()
  27.     {
  28.         Console.BufferHeight = Console.WindowHeight = 40;
  29.         Console.BufferWidth = Console.WindowWidth = 60;
  30.         int playGround = 20;
  31.         int lives = 3;
  32.         int rounds = 1;
  33.         Objects myRock = new Objects();
  34.         myRock.col = playGround / 2 - 1;
  35.         myRock.row = Console.WindowHeight - 1;
  36.         myRock.c = "(0)";
  37.         myRock.color = ConsoleColor.White;
  38.         Random randomNumber = new Random();
  39.  
  40.         string[] rocksChar = { "*", "&", "$", "#", "@", "%" };
  41.         ConsoleColor[] rocksColors = { ConsoleColor.Cyan, ConsoleColor.Blue, ConsoleColor.Yellow, ConsoleColor.White, ConsoleColor.Red };
  42.         List<Objects> objectList = new List<Objects>();
  43.  
  44.         while (true)
  45.         {
  46.             bool isHitted = false;
  47.             int rocksGenerator = randomNumber.Next(0, 5);
  48.  
  49.             if (lives == 0)
  50.             {
  51.                 PrintCharOnPosition(15, 10, "GAME OVER !!!", ConsoleColor.Red);
  52.                 Console.WriteLine();
  53.                 return;
  54.             }
  55.  
  56.             string displayLives = String.Format("LIVES:  {0}", lives);
  57.             string displayRounds = String.Format("ROUNDS: {0}", rounds);
  58.  
  59.             PrintCharOnPosition(30, 10, displayLives, ConsoleColor.Yellow);
  60.             PrintCharOnPosition(30, 8, displayRounds, ConsoleColor.Green);
  61.  
  62.             for (int i = 0; i < rocksGenerator; i++)
  63.             {
  64.  
  65.                 Objects FallingRocks = new Objects();
  66.                 FallingRocks.col = randomNumber.Next(0, playGround + 1);
  67.                 FallingRocks.row = 0;
  68.                 FallingRocks.c = rocksChar[randomNumber.Next(0, rocksChar.Length)];
  69.                 FallingRocks.color = rocksColors[randomNumber.Next(0, rocksColors.Length)];
  70.                 objectList.Add(FallingRocks);
  71.             }
  72.  
  73.             while (Console.KeyAvailable)
  74.             {
  75.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  76.  
  77.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  78.                 {
  79.  
  80.                     if (myRock.col - 1 >= 0)
  81.                     {
  82.                         myRock.col = myRock.col - 1;
  83.                     }
  84.                 }
  85.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  86.                 {
  87.                     if (myRock.col + 2 <= playGround - 1)
  88.                     {
  89.                         myRock.col = myRock.col + 1;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             List<Objects> newObjectList = new List<Objects>();
  95.  
  96.             for (int i = 0; i < objectList.Count; i++)
  97.             {
  98.                 Objects oldRock = objectList[i];
  99.                 Objects newFallingRocks = new Objects();
  100.                 newFallingRocks.col = oldRock.col;
  101.                 newFallingRocks.row = oldRock.row + 1;
  102.                 newFallingRocks.c = oldRock.c;
  103.                 newFallingRocks.color = oldRock.color;
  104.                 if (newFallingRocks.row == myRock.row && ((newFallingRocks.col == myRock.col) || newFallingRocks.col == myRock.col + 1 || newFallingRocks.col == myRock.col + 2))
  105.                 {
  106.                     isHitted = true;
  107.                 }
  108.                 if (newFallingRocks.row < Console.WindowHeight)
  109.                 {
  110.                     newObjectList.Add(newFallingRocks);
  111.                 }
  112.             }
  113.             objectList = newObjectList;
  114.  
  115.             for (int i = 0; i < Console.WindowHeight; i++)
  116.             {
  117.                 PrintCharOnPosition(playGround + 1, i, "|", ConsoleColor.Red);
  118.             }
  119.             foreach (Objects rock in objectList)
  120.             {
  121.                 PrintCharOnPosition(rock.col, rock.row, rock.c, rock.color);
  122.  
  123.             }
  124.             PrintCharOnPosition(myRock.col, myRock.row, myRock.c, myRock.color);
  125.  
  126.             if (isHitted)
  127.             {
  128.                 objectList.Clear();
  129.                
  130.                 PrintCharOnPosition(myRock.col, myRock.row, "***", ConsoleColor.Red);
  131.                 Thread.Sleep(50);
  132.                 lives--;
  133.                 rounds++;            
  134.             }
  135.            
  136.             Thread.Sleep(200);
  137.             Console.Clear();
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment