psotirov

Falling Rocks

Nov 29th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. class FallingRocks
  6. {
  7.     enum RockShape {Dot, vLine, hLine, Square, Crest}
  8.     public const int fieldWidth = 40;
  9.     public const int fieldHeight = 30;
  10.     public const int bonusStep = 500;
  11.  
  12.     class Rock
  13.     {
  14.         static int maxSize = 5;
  15.         static string allowedChars  = "*@&+%$#!.;";
  16.         int posX;
  17.         int posY;
  18.         char symbol;
  19.         RockShape shape;
  20.         int length;
  21.         ConsoleColor color;
  22.  
  23.         public Rock()
  24.         {
  25.             Random innerRockGenerator = new Random();
  26.             symbol = allowedChars[innerRockGenerator.Next(allowedChars.Length)];
  27.             length = innerRockGenerator.Next(1, maxSize+1);
  28.             color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), Convert.ToString(innerRockGenerator.Next(1, 16))); // selects one of the 16 foregroud colors
  29.             shape = (RockShape)Enum.Parse(typeof(RockShape), Convert.ToString(innerRockGenerator.Next((int)RockShape.Crest + 1))); // selects some shape
  30.             posY = 0;
  31.             posX = innerRockGenerator.Next(0, fieldWidth - length);
  32.         }
  33.  
  34.         public uint GetRockLength()
  35.         {
  36.             return (uint)length;
  37.         }
  38.  
  39.         public void Draw()
  40.         {
  41.             Console.ForegroundColor = color;
  42.             switch (shape)
  43.             {
  44.                 case RockShape.Dot:
  45.                     Console.SetCursorPosition(posX, posY);
  46.                     Console.Write(symbol);
  47.                     break;
  48.                 case RockShape.vLine:
  49.                     for (int i = 0; (i < length) && (posY + i < fieldHeight); i++)
  50.                     {
  51.                         Console.SetCursorPosition(posX, posY+i);
  52.                         Console.Write(symbol);
  53.                     }
  54.                     break;
  55.                 case RockShape.hLine:
  56.                     Console.SetCursorPosition(posX, posY);
  57.                     Console.Write(new string(symbol, length));
  58.                     break;
  59.                 case RockShape.Square:
  60.                     string rockRow = new string(symbol, length);
  61.                     for (int i = 0; (i < length) && (posY + i < fieldHeight); i++)
  62.                     {
  63.                         Console.SetCursorPosition(posX, posY+i);
  64.                         Console.Write(rockRow);
  65.                     }
  66.                     break;
  67.                 case RockShape.Crest:
  68.                     for (int i = 0; (i < length) && (posY+i < fieldHeight); i++)
  69.                     {
  70.                         Console.SetCursorPosition(posX+length/2, posY+i);
  71.                         Console.Write(symbol);
  72.                     }
  73.                     if ((posY + length / 2) < fieldHeight)
  74.                     {
  75.                         Console.SetCursorPosition(posX, posY + length / 2);
  76.                         Console.Write(new string(symbol, length));
  77.                     }
  78.                     break;
  79.                 default:
  80.                     break;
  81.             }
  82.         }
  83.  
  84.         public bool Down()
  85.         {
  86.             posY++;
  87.             return (posY < fieldHeight); // if it is outside the boundary returns false, otherwise if OK returns true
  88.         }
  89.  
  90.         public bool DwarfCollisionCheck(int dwarfPosition)
  91.         {
  92.             int rockHeight = ((shape == RockShape.Dot) || (shape == RockShape.hLine)) ? 1 : length;
  93.             if ((posY + rockHeight) >= fieldHeight)
  94.             {
  95.                 int hLength = length;
  96.                 int tempPosX = posX;
  97.                 if (shape == RockShape.vLine || shape == RockShape.Dot) hLength = 1; // the width of "vertical line" and "dot" rock is always 1
  98.                 if ((shape == RockShape.Crest) && ((posY + length / 2 + 1) != fieldHeight))
  99.                 {
  100.                     hLength = 1; // the width of "crest"rock is always 1 except of its middle vertical line
  101.                     tempPosX += length / 2; // and vertical line of the crest is shifted at the middle of the figure
  102.                 }
  103.  
  104.                 if ((dwarfPosition > (tempPosX - 3)) && (dwarfPosition < (tempPosX+hLength))) return true; //Checks if position of dwarf (including 3 chars width) is within the rock's current line
  105.             }
  106.  
  107.             return false;
  108.         }
  109.     }
  110.  
  111.     static class Dwarf
  112.     {
  113.         static string dwarfBody;
  114.         static int posX;
  115.         static bool hasDamage;
  116.         static int damageStatusCounter;
  117.         static ConsoleColor color;
  118.  
  119.         static Dwarf()
  120.         {
  121.             dwarfBody = "(0)";
  122.             posX = fieldWidth / 2 - 1;
  123.             color = ConsoleColor.Green;
  124.             hasDamage = false;
  125.             damageStatusCounter = 0;
  126.         }
  127.  
  128.         public static void Draw()
  129.         {
  130.             Console.SetCursorPosition(posX, Console.WindowHeight - 1);
  131.             Console.ForegroundColor = (hasDamage)?ConsoleColor.Red:color;
  132.             Console.Write(dwarfBody);
  133.             if (hasDamage && (--damageStatusCounter == 0)) hasDamage=false;
  134.         }
  135.  
  136.         public static void Clear()
  137.         {
  138.             Console.SetCursorPosition(posX, Console.WindowHeight - 1);
  139.             Console.ForegroundColor = color;
  140.             Console.Write(new string(' ',dwarfBody.Length));
  141.         }
  142.  
  143.         public static void Left()
  144.         {
  145.             if (posX == 0) return;
  146.             Clear();
  147.             posX--;
  148.             Draw();
  149.         }
  150.  
  151.         public static void Right()
  152.         {
  153.             if (posX == fieldWidth) return;
  154.             Clear();
  155.             posX++;
  156.             Draw();
  157.         }
  158.  
  159.         public static void SetDamage()
  160.         {
  161.             hasDamage = true;
  162.             damageStatusCounter = 4;
  163.         }
  164.  
  165.         public static bool GetDamage()
  166.         {
  167.             return (hasDamage && damageStatusCounter==4);
  168.         }
  169.  
  170.         public static int GetDwarfPosition()
  171.         {
  172.             return posX;
  173.         }
  174.     }
  175.  
  176.     static void Main()
  177.     {
  178.         Console.BufferWidth = Console.WindowWidth = fieldWidth + 15;
  179.         Console.BufferHeight = Console.WindowHeight = fieldHeight;
  180.         uint dwarfScore = 0;
  181.         uint dwarfLives = 5;
  182.         Random randGenerator = new Random();
  183.         List<Rock> allRocks = new List<Rock>();
  184.         allRocks.Add(new Rock());
  185.  
  186.         while (true)
  187.         {
  188.             // Clear console
  189.             Console.Clear();
  190.             // Move our dwarf on key presssed
  191.             if (Console.KeyAvailable)
  192.             {
  193.                 while (Console.KeyAvailable) // you can press control keys multiple times per loop in order to move faster
  194.                 {
  195.                     ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  196.                     if (pressedKey.Key == ConsoleKey.LeftArrow) Dwarf.Left(); // moves dwarf left
  197.                     if (pressedKey.Key == ConsoleKey.RightArrow) Dwarf.Right(); // moves dwarf right
  198.                     if (pressedKey.Key == ConsoleKey.Escape)
  199.                     {
  200.                         Console.ResetColor();
  201.                         Console.WriteLine("GOOD BYE\n\n");
  202.                         Console.ResetColor();
  203.                         Console.ReadKey(true);
  204.                         return; // Exits the game
  205.                     }
  206.                 }
  207.             }
  208.             // Move rocks
  209.             int RocksIndex = 0;
  210.             while (RocksIndex < allRocks.Count)
  211.             {
  212.                 if (allRocks[RocksIndex].Down()) //Moves down the current rock
  213.                 {
  214.                     allRocks[RocksIndex].Draw();
  215.                     if (allRocks[RocksIndex].DwarfCollisionCheck(Dwarf.GetDwarfPosition())) Dwarf.SetDamage(); // Checks for collision with the Dwarf
  216.                     RocksIndex++;
  217.                 }
  218.                 else
  219.                 {
  220.                     uint plusScore = allRocks[RocksIndex].GetRockLength();
  221.                     bool hasBonus = (dwarfScore / bonusStep) != ((dwarfScore + plusScore) / bonusStep);
  222.                     if (hasBonus) dwarfLives++; // BONUS: for each "bonusStep" points, the player wins one additional spare life
  223.                     dwarfScore += plusScore;
  224.                     allRocks.Remove(allRocks[RocksIndex]); // removes item if it is outside the boundary
  225.                 }
  226.             }
  227.  
  228.             // Add new rock with 20% possibility
  229.             if (randGenerator.Next(5) == 1) allRocks.Add(new Rock());
  230.  
  231.             // Check for hit
  232.             if (Dwarf.GetDamage())
  233.             {
  234.                 dwarfLives--;
  235.                 allRocks.Clear();
  236.                 if (dwarfLives == 0)
  237.                 {
  238.                     Console.SetCursorPosition(5, fieldHeight / 2);
  239.                     Console.BackgroundColor = ConsoleColor.Red;
  240.                     Console.ForegroundColor = ConsoleColor.White;
  241.                     Console.WriteLine("  !!!  GAME OVER  !!!   ");
  242.                     Console.ResetColor();
  243.                     Console.ReadKey(true);
  244.                     return;
  245.                 }
  246.             }
  247.  
  248.  
  249.             // Redraw field
  250.             Dwarf.Draw();
  251.            
  252.             // Draw info
  253.             Console.SetCursorPosition(fieldWidth + 5, 2);
  254.             Console.ForegroundColor = ConsoleColor.White;
  255.             Console.Write("SCORE:");
  256.             Console.SetCursorPosition(fieldWidth + 2, 3);
  257.             Console.ForegroundColor = ConsoleColor.Green;
  258.             Console.Write("{0,10}", dwarfScore);
  259.             Console.SetCursorPosition(fieldWidth + 5, 5);
  260.             Console.ForegroundColor = ConsoleColor.White;
  261.             Console.Write("LIVES:");
  262.             Console.SetCursorPosition(fieldWidth + 2, 6);
  263.             Console.ForegroundColor = ConsoleColor.Green;
  264.             Console.Write("{0,10}", dwarfLives);
  265.            
  266.             // Slowdown the program
  267.             Thread.Sleep(150);
  268.         }
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment