Advertisement
wnvko

C#Part1.ConsoleInputOutput.11.FallingRocksGame

Nov 9th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.45 KB | None | 0 0
  1. /*
  2.  * Implement the "Falling Rocks" game in the text console. A small dwarf stays at
  3.  * the bottom of the screen and can move left and right (by the arrows keys).
  4.  * A number of rocks of different sizes and forms constantly fall down and you need to avoid a crash.
  5.  * Rocks are the symbols ^, @, *, &, +, %, $, #, !, ., ;, - distributed with appropriate density.
  6.  * The dwarf is (O). Ensure a constant game speed by Thread.Sleep(150).
  7.  * Implement collision detection and scoring system.
  8.  */
  9.  
  10. namespace FallingRocksGame
  11. {
  12.     using System;
  13.     using System.Threading;
  14.  
  15.     class FallingRocksGame
  16.     {
  17.         //create new type for rocks. This will help trace rock easyer
  18.         struct Rock
  19.         {
  20.             public int X;
  21.             public int Y;
  22.             public char typeOfRock;
  23.         };
  24.  
  25.         //create random number
  26.         static int RandomNumber(int inputValue)
  27.         {
  28.             Random randomNumber = new Random();
  29.             int output = randomNumber.Next(0, inputValue);
  30.             Thread.Sleep(10);
  31.             return output;
  32.         }
  33.  
  34.         //creat new rock method
  35.         static Rock InitializeNewRock()
  36.         {
  37.             Rock newRock;
  38.             newRock.X = RandomNumber(Console.WindowWidth - 1);
  39.             newRock.Y = RandomNumber(Console.WindowHeight - 20);
  40.             char rockSymbol = '*';
  41.             int randomRock;
  42.             randomRock = RandomNumber(11);
  43.             switch (randomRock)
  44.             {
  45.                 case 0: rockSymbol = '^'; break;
  46.                 case 1: rockSymbol = '@'; break;
  47.                 case 2: rockSymbol = '*'; break;
  48.                 case 3: rockSymbol = '&'; break;
  49.                 case 4: rockSymbol = '+'; break;
  50.                 case 5: rockSymbol = '%'; break;
  51.                 case 6: rockSymbol = '$'; break;
  52.                 case 7: rockSymbol = '#'; break;
  53.                 case 8: rockSymbol = '!'; break;
  54.                 case 9: rockSymbol = '.'; break;
  55.                 case 10: rockSymbol = ';'; break;
  56.                 case 11: rockSymbol = '-'; break;
  57.             }
  58.             newRock.typeOfRock = rockSymbol;
  59.             return newRock;
  60.         }
  61.  
  62.         //move rocks
  63.         static Rock MoveRock(Rock rockToMove)
  64.         {
  65.             Console.SetCursorPosition(rockToMove.X, rockToMove.Y);
  66.             Console.Write(" ");
  67.             int newY = rockToMove.Y + 1;
  68.             Rock newRock = rockToMove;
  69.             if (newY > Console.WindowHeight - 1)
  70.             {
  71.                 newRock = InitializeNewRock();
  72.                 return newRock;
  73.             }
  74.             else
  75.             {
  76.                 Console.SetCursorPosition(rockToMove.X, newY);
  77.                 Console.Write(rockToMove.typeOfRock);
  78.                 newRock.Y++;
  79.                 return newRock;
  80.             }
  81.         }
  82.  
  83.         //writes the dwarf
  84.         private static void WriteDwarf(int x, int y)
  85.         {
  86.             Console.SetCursorPosition(x, y);
  87.             Console.Write(" (O) ");
  88.         }
  89.  
  90.         //writes dead dwarf
  91.         private static void WriteDeadDwarf(int x, int y)
  92.         {
  93.             Console.SetCursorPosition(x, y);
  94.             Console.Write(" _o_ ");
  95.         }
  96.  
  97.         static void Main(string[] args)
  98.         {
  99.             //set the gamefield parameters
  100.             Console.WindowHeight = 25;
  101.             Console.WindowWidth = 80;
  102.             int maxCurrentRock = 10;
  103.             DateTime startGameTime = DateTime.Now;
  104.             int oldscore = 0;
  105.             int score = 0;
  106.             int gamespeed = 200;
  107.  
  108.             //put first ten rocks on the field
  109.             Rock[] newRock = new Rock[100];
  110.             for (int i = 0; i < maxCurrentRock; i++)
  111.             {
  112.                 newRock[i] = InitializeNewRock();
  113.             }
  114.  
  115.             //put the dwarf on the field
  116.             int dwarfXPosition = (Console.WindowWidth / 2) - 2;
  117.             int dwarfYPosition = Console.WindowHeight - 1;
  118.             WriteDwarf(dwarfXPosition, dwarfYPosition);
  119.  
  120.             while (true)
  121.             {
  122.                 //sets game speed
  123.                 Thread.Sleep(gamespeed);
  124.  
  125.                 //check if dwarf is moving
  126.                 if (Console.KeyAvailable)
  127.                 {
  128.                     ConsoleKeyInfo userInput = Console.ReadKey();
  129.                     if (userInput.Key == ConsoleKey.RightArrow)
  130.                     {
  131.                         if (dwarfXPosition + 6 < Console.WindowWidth)
  132.                         {
  133.                             dwarfXPosition++;
  134.                             WriteDwarf(dwarfXPosition, dwarfYPosition);
  135.                         }
  136.                     }
  137.                     if (userInput.Key == ConsoleKey.LeftArrow)
  138.                     {
  139.                         if (dwarfXPosition > 0)
  140.                         {
  141.                             dwarfXPosition--;
  142.                             WriteDwarf(dwarfXPosition, dwarfYPosition);
  143.                         }
  144.                     }
  145.                 }
  146.  
  147.                 //checks for collision
  148.                 for (int i = 0; i < maxCurrentRock; i++)
  149.                 {
  150.                     if (newRock[i].Y == Console.WindowHeight - 1)
  151.                     {
  152.                         if ((newRock[i].X > dwarfXPosition) && (newRock[i].X < (dwarfXPosition + 4)))
  153.                         {
  154.                             WriteDeadDwarf(dwarfXPosition, dwarfYPosition);
  155.                             Console.SetCursorPosition(0, 0);
  156.                             Console.WriteLine("\aGame over");
  157.                             Thread.Sleep(250);
  158.                             Console.WriteLine("\aYour score is {0}", score);
  159.                             Console.ReadKey();
  160.                             return;
  161.                         }
  162.                     }
  163.                 }
  164.  
  165.                 //start moving rocks
  166.                 for (int i = 0; i < maxCurrentRock; i++)
  167.                 {
  168.                     newRock[i] = MoveRock(newRock[i]);
  169.                 }
  170.  
  171.                 //calculates score and increas dificulty
  172.                 score = (int)(DateTime.Now - startGameTime).TotalSeconds;
  173.                 if ((score - oldscore) > 10)
  174.                 {
  175.                     oldscore = score;
  176.                     maxCurrentRock++;
  177.                     newRock[maxCurrentRock] = InitializeNewRock();
  178.                 }
  179.                 if ((score - oldscore) > 25)
  180.                 {
  181.                     gamespeed -= 20;
  182.                 }
  183.             }
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement