Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.76 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Security.Cryptography;
  4.  
  5. /*
  6.  * Implement the "Falling Rocks" game in the text console.
  7.  * A small dwarf stays at the bottom of the screen and can move left and right (by the arrows keys).
  8.  * A number of rocks of different sizes and forms constantly fall down and you need to avoid a crash.
  9.  * Rocks are the symbols  ^, @, *, &, +, %, $, #, !, ., ;, -  distributed with appropriate density. The dwarf is  (O) .
  10.  * Ensure a constant game speed by  Thread.Sleep(150) .
  11.  * Implement collision detection and scoring system.
  12. */
  13.  
  14. /*       How to Play    */
  15. /*
  16.  * Left arrow - move dwarf left
  17.  * Right arrow - move dwarf right
  18.  * Down arrow - decrease game speed
  19.  * Up arrow - increase game speed
  20.  *
  21.  * If game over click ENTER to start new game
  22. */
  23.  
  24. class FallingRocks
  25. {
  26.     //config
  27.     static int consoleWidth = 50;
  28.     static int consoleHeight = 30;
  29.     static int gameInfoHeight = 5;
  30.  
  31.     static int rocksPerRowMin = 0;
  32.     static int rocksPerRowMax = 2;
  33.     static int rocksMaxOnField = 30;
  34.  
  35.     static int defaultGameSpeed = 150;
  36.  
  37.     static ConsoleColor dwarfColor = ConsoleColor.DarkYellow;
  38.     static int dwarfArmor = 5;
  39.  
  40.     static string formatGameInfo = "\nSpeed: {0,-10}\nArmor: {1}\nScore: {2,-10}";
  41.     static string strGameOver = "G A M E  O V E R !\n";
  42.     //////////////////////////////////
  43.  
  44.     //generating real random numbers
  45.     static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
  46.  
  47.     //speed info
  48.     static int gameSpeed = defaultGameSpeed;
  49.  
  50.     //score info
  51.     static int score = 0;
  52.     static int armor = dwarfArmor;
  53.  
  54.     //rocks info
  55.     struct __Rock{
  56.  
  57.         public char rockSymbol;
  58.         public ConsoleColor rockColor;
  59.     };
  60.  
  61.     static int rocksCurrentCount = 0;
  62.     static __Rock[,] Rocks = new __Rock[consoleHeight, consoleWidth];
  63.     static char[] rockForms = { '^','@','*','&','+','%','$','#','!','.',';','-'};
  64.     static ConsoleColor[] rockColors = { ConsoleColor.Blue, ConsoleColor.Green, ConsoleColor.Yellow, ConsoleColor.Magenta, ConsoleColor.Cyan, ConsoleColor.DarkRed };
  65.  
  66.     //Dwarf info
  67.     static string dwarf = "(0)";
  68.     static int dwarfX = ((consoleWidth-1)/2);
  69.     static int dwarfY = consoleHeight-1;
  70.  
  71.     static void Main()
  72.     {
  73.  
  74.         Console.CursorVisible = false;
  75.         Console.WindowHeight = consoleHeight+gameInfoHeight;
  76.         Console.WindowWidth = consoleWidth;
  77.  
  78.         while(true)
  79.         {
  80.  
  81.             moveRocksDown();
  82.             drawNewRow();
  83.             drawRocks();
  84.  
  85.             showGameInfo();
  86.  
  87.             //check if dwarf is being hit by rock
  88.             if (dwarfCollision() == true)
  89.             {
  90.                 gameOver();
  91.                 continue;
  92.             }
  93.  
  94.             if(Console.KeyAvailable==true)
  95.             {
  96.                 ConsoleKeyInfo key = Console.ReadKey();
  97.  
  98.                 switch(key.Key)
  99.                 {
  100.                     case ConsoleKey.RightArrow: //move right
  101.                         {
  102.                             if (dwarfX < consoleWidth - 1) { dwarfX++; }
  103.                             break;
  104.                         }
  105.  
  106.                     case ConsoleKey.LeftArrow: //move left
  107.                         {
  108.                             if (dwarfX > 0) { dwarfX--; }
  109.                             break;
  110.                         }
  111.                     case ConsoleKey.UpArrow: //increase game speed
  112.                         {
  113.                             if (gameSpeed > 100) {
  114.  
  115.                                 gameSpeed -= 100;
  116.  
  117.                             } else if(gameSpeed > 10)
  118.                             {
  119.                                 gameSpeed -= 10;
  120.                             }
  121.  
  122.                             break;
  123.                         }
  124.                     case ConsoleKey.DownArrow: //slow down game speed
  125.                         {
  126.                             gameSpeed += 100;
  127.                             break;
  128.                         }
  129.                     default:
  130.                         {
  131.                             break;
  132.                         }
  133.                 }
  134.             }
  135.  
  136.             //check if dwarf steps on position where is rock
  137.             if (dwarfCollision() == true)
  138.             {
  139.                 gameOver();
  140.                 continue;
  141.             }
  142.  
  143.             drawDwarf();
  144.  
  145.             Thread.Sleep(gameSpeed);
  146.         }
  147.     }
  148.  
  149.     static void showGameInfo() //show score, armor, game speed
  150.     {
  151.         string strGameInfo = String.Format(formatGameInfo, gameSpeed, armor, score);
  152.         string strLine = "";
  153.         strLine = strLine.PadLeft(consoleWidth, '*');
  154.  
  155.         ConsoleWriteEx(ref strLine, '\0', 0, consoleHeight, ConsoleColor.White);
  156.         ConsoleWriteEx(ref strGameInfo, '\0', 0, consoleHeight+1, ConsoleColor.Gray);
  157.     }
  158.  
  159.     static void gameOver() //prints game over message on the screen
  160.     {
  161.         int x = (consoleWidth / 2) - (strGameOver.Length / 2);
  162.         int y = consoleHeight / 2;
  163.  
  164.         ConsoleWriteEx(ref strGameOver, '\0', x, y, ConsoleColor.Red);
  165.  
  166.         ConsoleKeyInfo key;
  167.         do{
  168.  
  169.         key = Console.ReadKey();
  170.  
  171.         }while(key.Key!=ConsoleKey.Enter);
  172.  
  173.         resetGame();
  174.     }
  175.  
  176.     static void resetGame() //after game over we reset the game field for new game :)
  177.     {
  178.         for(int y = 0;y < consoleHeight;y++)
  179.         {
  180.             for(int x = 0;x < consoleWidth;x++)
  181.             {
  182.                 Rocks[y, x].rockSymbol = '\0';
  183.             }
  184.         }
  185.  
  186.         dwarfX = ((consoleWidth-1)/2);
  187.         dwarfY = consoleHeight-1;
  188.  
  189.         rocksCurrentCount = 0;
  190.  
  191.         gameSpeed = defaultGameSpeed;
  192.         armor = dwarfArmor;
  193.         score = 0;
  194.     }
  195.  
  196.     static bool dwarfCollision() //check if dwarf is being hit by an rock
  197.     {
  198.         bool bCollision = false;
  199.  
  200.         for (int y = consoleHeight - 1, x = dwarfX;x < (dwarfX + dwarf.Length);x++)
  201.         {
  202.             if (Rocks[y,x].rockSymbol != '\0') //if dwarf is hit, we remove his armor by one
  203.             {
  204.                 if (armor == 0) { bCollision = true; } //if dwarf has no armor, then game over :(
  205.                 armor--;
  206.                 Rocks[y, x].rockSymbol = '\0';
  207.  
  208.                 break;
  209.             }
  210.         }
  211.  
  212.         return bCollision;
  213.     }
  214.  
  215.     static void drawDwarf() //draws the dwarf on the screen
  216.     {
  217.         ConsoleWriteEx(ref dwarf, '\0', dwarfX, dwarfY, dwarfColor);
  218.     }
  219.  
  220.     static void drawRocks() //draws all rocks on the screen
  221.     {
  222.         string strNull = null;
  223.  
  224.         for (int y = 0; y < consoleHeight; y++)
  225.         {
  226.             for (int x = 0; x < consoleWidth; x++)
  227.             {
  228.                 ConsoleWriteEx(ref strNull, Rocks[y, x].rockSymbol, x, y, Rocks[y, x].rockColor);
  229.             }
  230.         }
  231.     }
  232.  
  233.  
  234.     static void moveRocksDown() //Move rocks one row down
  235.     {
  236.         //Before losing the last down row, check how many rocks we remove from the field
  237.         //so we know how many new rocks we can spawn and also increase score
  238.         for (int y = consoleHeight - 1, x = 0; x < consoleWidth; x++)
  239.         {
  240.             if (Rocks[y, x].rockSymbol != '\0')
  241.             {
  242.  
  243.                 rocksCurrentCount--;
  244.                 score += Rocks[y, x].rockSymbol;
  245.             }
  246.         }
  247.  
  248.         //Move rocks one row down
  249.         for (int y = consoleHeight - 1; y > 0; y--)
  250.         {
  251.             for (int x = 0; x < consoleWidth; x++)
  252.             {
  253.                 Rocks[y, x] = Rocks[y - 1, x];
  254.             }
  255.         }
  256.     }
  257.  
  258.     static void drawNewRow() //draws the top row of rocks that is coming from above
  259.     {
  260.         int y = 0,x = 0;
  261.         int countRocks = RandRange(rocksPerRowMin,rocksPerRowMax); //random number of rocks that will attempt to be drawn on the row
  262.  
  263.         //Clear the top row
  264.         for (x = 0; x < consoleWidth; x++)
  265.         {
  266.             Rocks[0, x].rockSymbol = '\0';
  267.         }
  268.  
  269.         while(countRocks>0)
  270.         {
  271.             if (rocksCurrentCount > rocksMaxOnField) { break; } //if we reached the limit of allowed rocks on the game field, then stop adding rocks
  272.  
  273.             do{
  274.  
  275.                 x = RandRange(0, consoleWidth-1);
  276.  
  277.             } while(Rocks[y,x].rockSymbol!=0x0);
  278.  
  279.             Rocks[y,x].rockSymbol = rockForms[RandRange(0,rockForms.Length-1)];
  280.             Rocks[y,x].rockColor = rockColors[RandRange(0, rockColors.Length - 1)];
  281.  
  282.             rocksCurrentCount++;
  283.             countRocks--;
  284.         }
  285.     }
  286.  
  287.     static void ConsoleWriteEx(ref string str, char symbol, int x, int y, ConsoleColor color) //ConsoleWriteExtended, to save some lines of code
  288.     {
  289.         Console.SetCursorPosition(x, y);
  290.         Console.ForegroundColor = color;
  291.  
  292.         if (str != null)
  293.         {
  294.             Console.Write(str);
  295.         }
  296.         else
  297.         {
  298.             Console.Write(symbol);
  299.         }
  300.  
  301.         Console.ResetColor();
  302.     }
  303.  
  304.     static int RandRange(int minimumValue, int maximumValue) //Random number generator, Random() is not enough random ;)
  305.     {
  306.         byte[] randomNumber = new byte[1];
  307.  
  308.         rand.GetBytes(randomNumber);
  309.  
  310.         double asciiValueOfRandomCharacter = Convert.ToDouble(randomNumber[0]);
  311.  
  312.         // We are using Math.Max, and substracting 0.00000000001,
  313.         // to ensure "multiplier" will always be between 0.0 and .99999999999
  314.         // Otherwise, it's possible for it to be "1", which causes problems in our rounding.
  315.  
  316.         double multiplier = Math.Max(0, (asciiValueOfRandomCharacter / 255d) - 0.00000000001d);
  317.  
  318.         // We need to add one to the range, to allow for the rounding done with Math.Floor
  319.  
  320.         int range = maximumValue - minimumValue + 1;
  321.         double randomValueInRange = Math.Floor(multiplier * range);
  322.  
  323.         return (int)(minimumValue + randomValueInRange);
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement