Advertisement
salron3

Falling Rocks

Aug 18th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.74 KB | None | 0 0
  1. // * Implement the "Falling Rocks" game in the text console. A small dwarf stays at the bottom of the
  2. // screen and can move left and right (by the arrows keys). A number of rocks of different sizes and
  3. // forms constantly fall down and you need to avoid a crash.
  4. // Rocks are the symbols ^, @, *, &, +, %, $, #, !, ., ;, - distributed with appropriate density.
  5. // The dwarf is (O). Ensure a constant game speed by Thread.Sleep(150).
  6. // Implement collision detection and scoring system.
  7.  
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15.  
  16.  
  17. class FallingRocksSolo
  18. {
  19.     static void ResetBuffer()
  20.     {
  21.         Console.BufferHeight = Console.WindowHeight = 30;
  22.         Console.BufferWidth = Console.WindowWidth = 60;
  23.     }
  24.  
  25.     static void PrintAtPosition(int x, int y, char symbol, ConsoleColor color)
  26.     {
  27.         Console.SetCursorPosition(x, y);
  28.         Console.ForegroundColor = color;
  29.         Console.Write(symbol);
  30.     }
  31.  
  32.     static void PrintStringAtPosition(int x, int y, string text, ConsoleColor color)
  33.     {
  34.         Console.SetCursorPosition(x, y);
  35.         Console.ForegroundColor = color;
  36.         Console.Write(text);
  37.     }
  38.  
  39.     struct Unit
  40.     {
  41.         public int x;
  42.         public int y;
  43.         public ConsoleColor color;
  44.         public char symbol;
  45.     }
  46.  
  47.     static void Main()
  48.     {
  49.         ResetBuffer();
  50.         Random randomGenerator = new Random();
  51.         List<Unit> RocksList = new List<Unit>();
  52.         int livesCount = 1;
  53.         int score = 0;
  54.         char[] symbolList = { '^', '*', '&', '+', '%', '$', '#', '!', '.', ';' };
  55.         int speed = 0;
  56.  
  57.  
  58.         // Init Dwarf
  59.         Unit Dwarf = new Unit();
  60.         Dwarf.x = (Console.WindowWidth / 2) - 1;
  61.         Dwarf.y = Console.WindowHeight - 1;
  62.         Dwarf.color = ConsoleColor.White;
  63.         Dwarf.symbol = '@';
  64.  
  65.         while (true)
  66.         {
  67.             bool hit = false;
  68.  
  69.             int spawnBuffChance = randomGenerator.Next(0, 100);
  70.  
  71.             if (spawnBuffChance < 10)
  72.             {
  73.                 // Spawn buff
  74.                 Unit newBuff = new Unit();
  75.                 newBuff.x = randomGenerator.Next(0, Console.WindowWidth - 2);
  76.                 newBuff.y = 5;
  77.                 newBuff.color = ConsoleColor.Red; // We start from blue because black is not Good in our game :)
  78.                 // newInitRock.color = (ConsoleColor)randomGenerator.Next((int)ConsoleColor.Blue, (int)ConsoleColor.Yellow); // We start from blue because black is not Good in our game :)
  79.                 newBuff.symbol = '¤'; // TODO: Random
  80.                 RocksList.Add(newBuff);
  81.             }
  82.             else
  83.             {
  84.                 // Spawn Rock
  85.                 Unit newInitRock = new Unit();
  86.                 newInitRock.x = randomGenerator.Next(0, Console.WindowWidth - 2);
  87.                 newInitRock.y = 5;
  88.                 newInitRock.color = ConsoleColor.Cyan; // We start from blue because black is not Good in our game :)
  89.                 // newInitRock.color = (ConsoleColor)randomGenerator.Next((int)ConsoleColor.Blue, (int)ConsoleColor.Yellow); // We start from blue because black is not Good in our game :)
  90.                 newInitRock.symbol = symbolList[randomGenerator.Next(0, 9)]; // TODO: Random
  91.                 RocksList.Add(newInitRock);
  92.             }
  93.  
  94.             // Move Dwarf
  95.             if (Console.KeyAvailable)
  96.             {
  97.                 ConsoleKeyInfo keyPressed = Console.ReadKey(true);
  98.                 while (Console.KeyAvailable) { Console.ReadKey(true); }
  99.                 if (keyPressed.Key == ConsoleKey.LeftArrow)
  100.                 {
  101.                     if (Dwarf.x > 0)
  102.                     {
  103.                         Dwarf.x--;
  104.                     }
  105.                 }
  106.                 if (keyPressed.Key == ConsoleKey.RightArrow)
  107.                 {
  108.                     if (Dwarf.x < Console.WindowWidth - 2)
  109.                     {
  110.                         Dwarf.x++;
  111.                     }
  112.                 }
  113.             }
  114.  
  115.             // Move Rocks
  116.             List<Unit> newList = new List<Unit>();
  117.             for (int i = 0; i < RocksList.Count; i++)
  118.             {
  119.                 Unit oldRock = RocksList[i];
  120.                 Unit NewMovedRock = new Unit();
  121.                 NewMovedRock.x = oldRock.x;
  122.                 NewMovedRock.y = oldRock.y + 1;
  123.                 NewMovedRock.color = oldRock.color;
  124.                 NewMovedRock.symbol = oldRock.symbol;
  125.  
  126.                 // Buff Detection
  127.                 if (NewMovedRock.symbol == '¤' && NewMovedRock.x == Dwarf.x && NewMovedRock.y == Dwarf.y)
  128.                 {
  129.                     speed = speed - 50;
  130.                 }
  131.  
  132.                 // Collision Detection
  133.                 if (NewMovedRock.symbol != '¤' && NewMovedRock.x == Dwarf.x && NewMovedRock.y == Dwarf.y)
  134.                 {
  135.                     livesCount--;
  136.                     hit = true;
  137.                     speed = 0;
  138.                     if (livesCount <= 0)
  139.                     {
  140.                         PrintStringAtPosition(42, 2, "GAME OVER", ConsoleColor.Red);
  141.                         PrintStringAtPosition(33, 3, "Press [enter] to continue", ConsoleColor.Red);
  142.                         Console.ReadLine();
  143.                     }
  144.                 }
  145.                 if (NewMovedRock.y < Console.WindowHeight)
  146.                 {
  147.                     newList.Add(NewMovedRock);
  148.                 }
  149.                 else
  150.                 {
  151.                     score++;
  152.                 }
  153.             }
  154.             RocksList = newList;
  155.  
  156.             // Clear All
  157.             Console.Clear();
  158.  
  159.             // Draw Dwarf
  160.             if (hit)
  161.             {
  162.                 PrintAtPosition(Dwarf.x, Dwarf.y, 'X', ConsoleColor.Red);
  163.                 RocksList.Clear();
  164.             }
  165.             else
  166.             {
  167.                 PrintAtPosition(Dwarf.x, Dwarf.y, Dwarf.symbol, Dwarf.color);
  168.             }
  169.  
  170.             // Draw Rocks
  171.             foreach (Unit rock in RocksList)
  172.             {
  173.                 PrintAtPosition(rock.x, rock.y, rock.symbol, rock.color);
  174.             }
  175.  
  176.             // Draw Score and lives
  177.             for (int i = 0; i < Console.WindowWidth; i++) // Score Divider
  178.             {
  179.                 PrintAtPosition(i, 5, '-', ConsoleColor.Gray);
  180.             }
  181.             PrintStringAtPosition(10, 2, "Lives: " + livesCount, ConsoleColor.Green);
  182.             PrintStringAtPosition(20, 2, "Score: " + score, ConsoleColor.Green);
  183.             PrintStringAtPosition(20, 3, "Speed: " + speed, ConsoleColor.Green);
  184.  
  185.             // Slow the game down
  186.             if (speed < 170)
  187.             {
  188.                 speed++;
  189.             }
  190.             Thread.Sleep(250 - speed);
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement