Advertisement
syssboxx

Falling Rocks

Jun 3rd, 2013
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.78 KB | None | 0 0
  1. //Implement the "Falling Rocks" game in the text console. A small dwarf stays at the bottom of the screen and //can move left and right (by the arrows keys). A number of rocks of different sizes and forms constantly //fall down and you need to avoid a crash
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. struct Object
  10. {
  11.     public int x;
  12.     public int y;
  13.     public string str;
  14.     public ConsoleColor color;
  15. }
  16. class FallingRocks
  17. {
  18.     static void PrintOnPosition(int x, int y, char c, ConsoleColor color)
  19.     {
  20.         Console.SetCursorPosition(x, y);
  21.         Console.ForegroundColor = color;
  22.         Console.WriteLine(c);
  23.     }
  24.  
  25.     static void PrintStringOnPosition(int x, int y, string str, ConsoleColor color)
  26.     {
  27.         Console.SetCursorPosition(x, y);
  28.         Console.ForegroundColor = color;
  29.         Console.WriteLine(str);
  30.     }
  31.  
  32.     static void Main()
  33.     {
  34.         int livesCounter = 5;
  35.         int bonus = 0;
  36.         double speed = 300.0;
  37.         double acceleration = 0.5;
  38.  
  39.         Console.BufferHeight = Console.WindowHeight = 50;
  40.         Console.BufferWidth = Console.WindowWidth = 60;
  41.  
  42.         int playFieldStart = 9;
  43.         int playFieldHeightEnd = Console.WindowHeight - 1;
  44.         int playFieldWidthtEnd = 55;
  45.         Random randomGenerator = new Random();
  46.  
  47.         string[] rocksSymbols = { "^", "@", "*", "++", "&", ";", "%", "$", "#", "!", "..","+++" };
  48.         int indexSymbols;
  49.         string[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
  50.  
  51.         //create the dwarf
  52.         Object dwarf = new Object();
  53.         dwarf.x = 20;
  54.         dwarf.y = playFieldHeightEnd - 1;
  55.         dwarf.color = ConsoleColor.Red;
  56.         dwarf.str = "(0)";
  57.  
  58.         //PrintStringOnPosition(dwarf.x, dwarf.y, dwarf.str, dwarf.color);
  59.  
  60.         //create a list of the rocks
  61.         List<Object> rocks = new List<Object>();
  62.  
  63.         while (true)
  64.         {
  65.             indexSymbols = randomGenerator.Next(0, rocksSymbols.Length);
  66.             ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[randomGenerator.Next(0, colorNames.Length)]);
  67.  
  68.             speed += acceleration;
  69.  
  70.             if (speed > 300)
  71.             {
  72.                 speed = 300;
  73.             }
  74.             bool hasCollision = false;
  75.             bool hasBonus = false;
  76.             {
  77.                 int chance = randomGenerator.Next(0, playFieldWidthtEnd);
  78.                 if (chance < 5)
  79.                 {
  80.                     Object rockBonus = new Object();
  81.                     rockBonus.x = randomGenerator.Next(0, Console.WindowHeight); ;
  82.                     rockBonus.y = playFieldStart;
  83.                     rockBonus.color = ConsoleColor.White;
  84.                     rockBonus.str = "O";
  85.                     rocks.Add(rockBonus);
  86.                 }
  87.                 else
  88.                 {
  89.                     Object rock = new Object();
  90.                     rock.x = randomGenerator.Next(0, Console.WindowHeight); ;
  91.                     rock.y = playFieldStart;
  92.                     rock.color = color;
  93.                     rock.str = rocksSymbols[indexSymbols]; ;
  94.                     rocks.Add(rock);
  95.                 }
  96.  
  97.             }
  98.             //move dwarf
  99.             while (Console.KeyAvailable)
  100.             {
  101.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  102.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  103.                 {
  104.                     if (dwarf.x - 1 >= 0)
  105.                     {
  106.                         dwarf.x = dwarf.x - 1;
  107.                     }
  108.                 }
  109.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  110.                 {
  111.                     if (dwarf.x + 1 < playFieldWidthtEnd)
  112.                     {
  113.                         dwarf.x = dwarf.x + 1;
  114.                     }
  115.                 }
  116.             }
  117.  
  118.             //move the rocks
  119.             //create a new list and copy the old coordiantes of the rocks into the new rocks, than clear the old rocks
  120.             List<Object> newRocks = new List<Object>();
  121.             for (int i = 0; i < rocks.Count; i++)
  122.             {
  123.                 Object oldRock = rocks[i];
  124.                 Object newRock = new Object();
  125.                 newRock.x = oldRock.x;
  126.                 newRock.y = oldRock.y + 1;
  127.                 newRock.str = oldRock.str;
  128.                 newRock.color = oldRock.color;
  129.                 //rocks.Remove(oldRock);
  130.  
  131.                 //collision detection
  132.                 if ((newRock.y == dwarf.y && newRock.x == dwarf.x) || (newRock.y == dwarf.y && newRock.x == dwarf.x + 1)
  133.                             || (newRock.y == dwarf.y && newRock.x == dwarf.x + 2))
  134.                 {
  135.                     if (newRock.str == "O")
  136.                     {
  137.                         hasBonus = true;
  138.                         bonus++;
  139.                         livesCounter++;
  140.                         if (bonus==50)
  141.                         {
  142.                             Console.Clear();
  143.                             PrintStringOnPosition(20, 4, "YOU WIN!!!", ConsoleColor.White);
  144.                             PrintStringOnPosition(15, 5, "Press [enter] to exit", ConsoleColor.White);
  145.                             Console.ReadLine();
  146.                             Environment.Exit(0);
  147.                         }
  148.                     }
  149.                     else
  150.                     {
  151.                         hasCollision = true;
  152.                         livesCounter--;
  153.                         speed += 50;
  154.                         if (speed > 400)
  155.                         {
  156.                             speed = 400;
  157.                         }
  158.                         if (livesCounter <= 0)
  159.                         {
  160.                             Console.Clear();
  161.                             PrintStringOnPosition(20, 4, "GAME OVER!!!", ConsoleColor.White);
  162.                             PrintStringOnPosition(15, 5, "Press [enter] to exit", ConsoleColor.White);
  163.                             Console.ReadLine();
  164.                             Environment.Exit(0);
  165.                         }
  166.                     }
  167.                 }
  168.  
  169.                 if (newRock.y < playFieldHeightEnd - 1)
  170.                 {
  171.                     newRocks.Add(newRock);
  172.                 }
  173.             }
  174.             rocks = newRocks;
  175.             Console.Clear();
  176.  
  177.             if (hasCollision)
  178.             {
  179.                 newRocks.Clear();
  180.                 PrintStringOnPosition(dwarf.x, dwarf.y, "XXX", ConsoleColor.Red);
  181.             }
  182.             else if (hasBonus)
  183.             {
  184.                 PrintStringOnPosition(dwarf.x, dwarf.y, "+1", ConsoleColor.Yellow);
  185.             }
  186.             else
  187.             {
  188.  
  189.                 PrintStringOnPosition(dwarf.x, dwarf.y, dwarf.str, ConsoleColor.Red);
  190.             }
  191.             foreach (Object rock in rocks)
  192.             {
  193.                 PrintStringOnPosition(rock.x, rock.y, rock.str, rock.color);
  194.             }
  195.  
  196.  
  197.  
  198.  
  199.             PrintStringOnPosition(6, 2, "-----> You are playing Falling Rocks <-----", ConsoleColor.Green);
  200.             PrintStringOnPosition(4, 4, "Avoid the falling rock or catch O for +1 live", ConsoleColor.White);
  201.             PrintStringOnPosition(0, 10, "-------------------------------------------------------", ConsoleColor.Red);
  202.             PrintStringOnPosition(0, playFieldHeightEnd, "-------------------------------------------------------", ConsoleColor.Red);
  203.  
  204.  
  205.             //draw scores info
  206.             PrintStringOnPosition(20, 5, "Remaining lives : " + livesCounter, ConsoleColor.Green);
  207.             PrintStringOnPosition(20, 6, "Bonuses : " + bonus, ConsoleColor.Green);
  208.             PrintStringOnPosition(20, 7, "Speed : " + speed, ConsoleColor.Green);
  209.  
  210.             Thread.Sleep((int)(600 - speed));
  211.            
  212.         }
  213.  
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement