Advertisement
wnvko

C#.Part1.OperarorsAndExpressions.14

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