Advertisement
kdikov

Falling rocks

Nov 18th, 2012
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. class Exercise11FallingRocks
  9. {
  10.     public struct Position
  11.     {
  12.         public int row;
  13.         public int col;
  14.         public char randomChar;
  15.         public ConsoleColor color;
  16.  
  17.         public Position(int row, int col, ConsoleColor color, char randomChar)
  18.         {
  19.             this.row = row;
  20.             this.col = col;
  21.             this.color = color;
  22.             this.randomChar = randomChar;
  23.         }
  24.     }
  25.  
  26.     public static char[] rocksChar = { '@', '*', '&', '+', '%', '$', '#', '!', '.', ';' };
  27.     public static string[] colors = { "Cyan", "Green", "Blue", "DarkCyan", "DarkGreen", "DarkYellow" };
  28.  
  29.     public static Random randomNum = new Random();
  30.  
  31.     public static List<Position> rocks = new List<Position>();
  32.     public static List<Position> gun = new List<Position>();
  33.  
  34.     public static ConsoleColor color;
  35.     public static int width;
  36.     public static int height;
  37.     public static int rockDensity;
  38.  
  39.     static void AddNewRow(int tempRow)
  40.     {
  41.         for (int y = 0; y < width; y++)
  42.         {
  43.             if (randomNum.Next(rockDensity) < 1)
  44.             {
  45.                 if (randomNum.Next(30) < 1)
  46.                 {
  47.                     if (randomNum.Next(2) < 1)
  48.                     {
  49.                         rocks.Add(new Position(tempRow, y, ConsoleColor.Yellow, '\u2660'));
  50.                     }
  51.                     else
  52.                     {
  53.                         rocks.Add(new Position(tempRow, y, ConsoleColor.Red, (char)3));
  54.                     }
  55.                 }
  56.  
  57.                 else
  58.                 {
  59.                     color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colors[randomNum.Next(colors.Length)]);
  60.                     rocks.Add(new Position(tempRow, y, color, rocksChar[randomNum.Next(rocksChar.Length)]));
  61.  
  62.                     // add duplicate rocks
  63.                     if (randomNum.Next(9) < 1 && y > 1 && y < width - 1)
  64.                     {
  65.                         y = y + 1;
  66.                         rocks.Add(new Position(tempRow, y, rocks[rocks.Count - 1].color, rocks[rocks.Count - 1].randomChar));
  67.  
  68.                         if (randomNum.Next(12) < 1 && y > 1 && y < width - 1)
  69.                         {
  70.                             y = y + 1;
  71.                             rocks.Add(new Position(tempRow, y, rocks[rocks.Count - 1].color, rocks[rocks.Count - 1].randomChar));
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.     }
  78.  
  79.     static void ShowCollision(ConsoleColor tempColor)
  80.     {
  81.         for (int u = height; u > 8; u -= 1)
  82.         {
  83.             Console.BackgroundColor = tempColor;
  84.             Console.SetCursorPosition(0, u);
  85.             Console.Write("".PadRight(width, ' '));
  86.             Thread.Sleep(1);
  87.             Console.BackgroundColor = ConsoleColor.Black;
  88.             Console.SetCursorPosition(0, u);
  89.             Console.Write("".PadRight(width, ' '));
  90.         }
  91.     }
  92.  
  93.     static void DrawHelp()
  94.     {
  95.         for (int i = 0; i <= height + 1; i++)
  96.         {
  97.             Console.SetCursorPosition(width, i);
  98.             Console.Write("|");
  99.         }
  100.         int infoRow = 2;
  101.  
  102.         Console.SetCursorPosition(width + 10, infoRow++);
  103.         Console.Write("Falling rocks");
  104.         Console.SetCursorPosition(width + 7, infoRow++); infoRow++;
  105.         Console.Write("by Konstantin Dikov");
  106.         Console.SetCursorPosition(width + 2, infoRow++); infoRow++;
  107.         Console.ForegroundColor = ConsoleColor.White;
  108.         Console.Write("Keys:");
  109.         Console.ForegroundColor = ConsoleColor.DarkMagenta;
  110.         Console.SetCursorPosition(width + 2, infoRow++);
  111.         Console.Write("Left - Left arrow");
  112.         Console.SetCursorPosition(width + 2, infoRow++);
  113.         Console.Write("Right - Right arrow");
  114.         Console.ForegroundColor = ConsoleColor.Magenta;
  115.         Console.SetCursorPosition(width + 2, infoRow++);
  116.         Console.Write("Gun - Spacebar");
  117.         Console.SetCursorPosition(width + 2, infoRow++);
  118.         Console.Write("Blast - Enter");
  119.         Console.ForegroundColor = ConsoleColor.DarkMagenta;
  120.         Console.SetCursorPosition(width + 2, infoRow++);
  121.         Console.Write("Up arrow - Increase density");
  122.         Console.SetCursorPosition(width + 2, infoRow++);
  123.         Console.Write("Down arrow - Decrease density"); infoRow++;
  124.         Console.SetCursorPosition(width + 2, infoRow++); infoRow++;
  125.         Console.ForegroundColor = ConsoleColor.White;
  126.         Console.Write("Tips:");
  127.         Console.ForegroundColor = ConsoleColor.DarkMagenta;
  128.         Console.SetCursorPosition(width + 2, infoRow++);
  129.         Console.Write("- Higher density = more points");
  130.         Console.SetCursorPosition(width + 2, infoRow++);
  131.         Console.Write("- Blast points x 4");
  132.     }
  133.  
  134.     static void Main()
  135.     {
  136.         Console.CursorVisible = false;
  137.         Console.Title = "Falling Rocks - by Konstantin Dikov";
  138.         ConsoleKey keyPressed;
  139.         Console.BufferWidth = Console.WindowWidth = 82;
  140.         Console.BufferHeight = Console.WindowHeight = 25;
  141.         width = 48;
  142.         height = 23;
  143.         int offset = 5;
  144.         int currentRow = 0;
  145.         rockDensity = 39;
  146.         int score = 0;
  147.         int highScore = 0;
  148.         int lifes = 5;
  149.         int blasts = 5;
  150.         int blastPosition = 0;
  151.         bool gameOver = false;
  152.         int scoreFormula = 0;
  153.         string spaces = new string(' ', width);
  154.  
  155.         Position dwarf = new Position();
  156.         dwarf.col = width / 2;
  157.         dwarf.row = height;
  158.         dwarf.color = ConsoleColor.White;
  159.  
  160.         string dwarfString = "(0)";
  161.  
  162.         //draw ground and ------
  163.         Console.SetCursorPosition(0, 3);
  164.         Console.Write("".PadRight(width, '-'));
  165.         Console.SetCursorPosition(0, height + 1);
  166.         Console.BackgroundColor = ConsoleColor.DarkRed;
  167.         Console.Write("".PadRight(width, ' '));
  168.         Console.BackgroundColor = ConsoleColor.Black;
  169.         //
  170.         DrawHelp();
  171.  
  172.         AddNewRow(currentRow);
  173.  
  174.         //check if file with highscore exist
  175.         if (!File.Exists("highscore.txt"))
  176.         {
  177.             StreamWriter saveScoreFile = new StreamWriter("highscore.txt");
  178.             saveScoreFile.WriteLine("0");
  179.             saveScoreFile.Close();
  180.         }
  181.         else
  182.         {
  183.             StreamReader readHighScore = new StreamReader("highscore.txt");
  184.             highScore = int.Parse(readHighScore.ReadLine());
  185.             readHighScore.Close();
  186.         }
  187.         //
  188.         double timeDelay = Environment.TickCount;
  189.         double gunDelay = Environment.TickCount;
  190.         while (!gameOver)
  191.         {
  192.             if (Environment.TickCount - timeDelay > 150)
  193.             {
  194.                 //clear last row
  195.                 Console.SetCursorPosition(0, dwarf.row);
  196.                 Console.ForegroundColor = ConsoleColor.Black;
  197.                 Console.Write(spaces);
  198.  
  199.                 //draw dwarf just to prevent dwarf blinking
  200.                 Console.SetCursorPosition(dwarf.col, dwarf.row);
  201.                 Console.ForegroundColor = (dwarf.color);
  202.                 Console.Write(dwarfString);
  203.  
  204.                 for (int i = 0; i < rocks.Count; i++)
  205.                 {
  206.                     if (offset - rocks[i].row == dwarf.row && rocks[i].col >= dwarf.col &&
  207.                         rocks[i].col <= dwarf.col + 2)
  208.                     {
  209.                         if (rocks[i].randomChar == '\u2660')
  210.                         {
  211.                             if (blasts < 6)
  212.                             {
  213.                                 ShowCollision(ConsoleColor.Yellow);
  214.                                 blasts = blasts + 1;
  215.                             }
  216.                         }
  217.                         else if (rocks[i].randomChar == (char)3)
  218.                         {
  219.                             if (lifes < 6)
  220.                             {
  221.                                 ShowCollision(ConsoleColor.Green);
  222.                                 lifes = lifes + 1;
  223.                             }
  224.                         }
  225.                         else
  226.                         {
  227.                             lifes = lifes - 1;
  228.                             if (lifes == 0)
  229.                             {
  230.                                 gameOver = true;
  231.                                 break;
  232.                             }
  233.                             else
  234.                             {
  235.                                 ShowCollision(ConsoleColor.DarkRed);
  236.                             }
  237.                         }
  238.  
  239.                     }
  240.                     // clear rocks
  241.                     Console.SetCursorPosition(rocks[i].col, offset - rocks[i].row - 1);
  242.                     Console.ForegroundColor = ConsoleColor.Black;
  243.                     Console.Write(" ");
  244.                     // draw rocks
  245.                     Console.SetCursorPosition(rocks[i].col, offset - rocks[i].row);
  246.                     Console.ForegroundColor = (rocks[i].color);
  247.                     Console.Write(rocks[i].randomChar);
  248.                 }
  249.  
  250.                 timeDelay = Environment.TickCount;
  251.  
  252.                 scoreFormula = (109 - rockDensity) / 9;
  253.                 score = score + rocks.RemoveAll(r => r.row == offset - height) * scoreFormula;
  254.  
  255.                 offset = offset + 1;
  256.                 currentRow = currentRow + 1;
  257.                 AddNewRow(currentRow);
  258.  
  259.                 //draw info board: score, lifes and blasts
  260.                 Console.BackgroundColor = ConsoleColor.DarkRed;
  261.                 Console.ForegroundColor = ConsoleColor.White;
  262.                 Console.SetCursorPosition(0, 0);
  263.                 Console.WriteLine(" Score {0:D6} | High score {1:D6} | Density: {2:D2}%", score, highScore, 109 - rockDensity);
  264.  
  265.                 Console.BackgroundColor = ConsoleColor.DarkMagenta;
  266.                 Console.SetCursorPosition(0, 1);
  267.                 Console.Write(" Lifes: ");
  268.                 Console.ForegroundColor = ConsoleColor.Red;
  269.                 Console.WriteLine("".PadRight(lifes, (char)3) + "".PadRight(6 - lifes, ' '));
  270.  
  271.                 Console.ForegroundColor = ConsoleColor.White;
  272.                 Console.SetCursorPosition(13, 1);
  273.                 Console.Write(" | Gun: ");
  274.                 Console.ForegroundColor = ConsoleColor.White;
  275.                 Console.WriteLine("Infinite ");
  276.                 Console.Write("Blasts: ");
  277.                 Console.ForegroundColor = ConsoleColor.Yellow;
  278.                 Console.Write("".PadRight(blasts, '\u2660') + "".PadRight(6 - blasts, ' '));
  279.  
  280.                 Console.BackgroundColor = ConsoleColor.Black;
  281.  
  282.                 if (gameOver) break;
  283.             }
  284.  
  285.             if (Console.KeyAvailable)
  286.             {
  287.                 keyPressed = Console.ReadKey(true).Key;
  288.  
  289.                 if (keyPressed == ConsoleKey.LeftArrow && dwarf.col > 0)
  290.                 {
  291.                     //draw speed trail
  292.                     Console.SetCursorPosition(dwarf.col + 2, dwarf.row);
  293.                     Console.ForegroundColor = ConsoleColor.DarkGray;
  294.                     Console.Write(">");
  295.  
  296.                     dwarf.col = dwarf.col - 1;
  297.                 }
  298.                 else if (keyPressed == ConsoleKey.RightArrow && dwarf.col < width - 3)
  299.                 {
  300.                     //draw speed trail
  301.                     Console.SetCursorPosition(dwarf.col, dwarf.row);
  302.                     Console.ForegroundColor = ConsoleColor.DarkGray;
  303.                     Console.Write("<");
  304.  
  305.                     dwarf.col = dwarf.col + 1;
  306.                 }
  307.                 else if (keyPressed == ConsoleKey.DownArrow && rockDensity < 100)
  308.                 {
  309.                     rockDensity = rockDensity + 1;
  310.                 }
  311.                 else if (keyPressed == ConsoleKey.UpArrow && rockDensity > 10)
  312.                 {
  313.                     rockDensity = rockDensity - 1;
  314.                 }
  315.                 else if (keyPressed == ConsoleKey.Enter && blasts > 0)
  316.                 {
  317.                     for (int z = 0; z < 15; z++)
  318.                     {
  319.                         blastPosition = dwarf.col - z;
  320.                         if (dwarf.col >= width - z - 1)
  321.                             blastPosition = width - 2 * (z + 1);
  322.                         if (dwarf.col <= z)
  323.                             blastPosition = 0;
  324.  
  325.                         Console.BackgroundColor = ConsoleColor.Yellow;
  326.                         Console.SetCursorPosition(blastPosition, dwarf.row - 1 - z);
  327.                         Console.Write("   ".PadRight(2 * (z + 1), ' '));
  328.                         score = score + scoreFormula * 4 * rocks.RemoveAll(r => (r.row == offset - (height - z)) &&
  329.                                      (r.col >= blastPosition) &&
  330.                                      (r.col < blastPosition + 2 * (z + 1)));
  331.  
  332.                         Thread.Sleep(20);
  333.  
  334.                         Console.BackgroundColor = ConsoleColor.Black;
  335.                         Console.SetCursorPosition(blastPosition, dwarf.row - 1 - z);
  336.                         Console.Write("   ".PadRight(2 * (z + 1), ' '));
  337.  
  338.                     }
  339.  
  340.                     blasts = blasts - 1;
  341.                     Console.BackgroundColor = ConsoleColor.Black;
  342.  
  343.                 }
  344.  
  345.                 else if (keyPressed == ConsoleKey.Spacebar)
  346.                 {
  347.                     gun.Add(new Position(dwarf.row - 1, dwarf.col + 1, ConsoleColor.Green, 'o'));
  348.  
  349.                     Console.SetCursorPosition(dwarf.col + 1, dwarf.row - 1);
  350.                     Console.ForegroundColor = gun[gun.Count - 1].color;
  351.                     Console.Write(gun[gun.Count - 1].randomChar);
  352.                 }
  353.             }
  354.  
  355.             //move bullets
  356.             if (Environment.TickCount - gunDelay > 50)
  357.             {
  358.                 for (int i = 0; i < gun.Count; i++)
  359.                 {
  360.                    
  361.  
  362.                     if (rocks.RemoveAll(nn => (nn.row == offset - gun[i].row - 1) && (nn.col == gun[i].col)) > 0)
  363.                     {
  364.                         score = score + scoreFormula * 2;
  365.                        
  366.                         Console.SetCursorPosition(gun[i].col, gun[i].row);
  367.                         Console.ForegroundColor = ConsoleColor.Black;
  368.                         Console.Write(" ");
  369.                         gun.RemoveAt(i);
  370.                         i--;
  371.                     }
  372.                     else if (rocks.RemoveAll(nn => (nn.row == offset - gun[i].row) && (nn.col == gun[i].col)) > 0)
  373.                     {
  374.                         score = score + scoreFormula * 2;
  375.  
  376.                         Console.SetCursorPosition(gun[i].col, gun[i].row - 1);
  377.                         Console.ForegroundColor = ConsoleColor.Black;
  378.                         Console.Write(" ");
  379.                         Console.SetCursorPosition(gun[i].col, gun[i].row);
  380.                         Console.Write(" ");
  381.                         gun.RemoveAt(i);
  382.                         i--;
  383.                     }
  384.                     else
  385.                     {
  386.                         Console.MoveBufferArea(gun[i].col, gun[i].row, 1, 1, gun[i].col, gun[i].row - 1);
  387.                         if (gun[i].row > 5)
  388.                         {
  389.                             gun[i] = new Position(gun[i].row - 1, gun[i].col, gun[i].color, gun[i].randomChar);
  390.                         }
  391.                         else
  392.                         {
  393.                             Console.SetCursorPosition(gun[i].col, gun[i].row - 1);
  394.                             Console.ForegroundColor = ConsoleColor.Black;
  395.                             Console.Write(" ");
  396.                             gun.RemoveAt(i);
  397.                             i--;
  398.                         }
  399.                        
  400.                     }
  401.                 }
  402.                 gunDelay = Environment.TickCount;
  403.             }
  404.  
  405.             // draw dwarf
  406.             Console.SetCursorPosition(dwarf.col, dwarf.row);
  407.             Console.ForegroundColor = (dwarf.color);
  408.             Console.Write(dwarfString);
  409.  
  410.         }
  411.  
  412.         Console.SetCursorPosition(width / 2 - 5, height / 2);
  413.         Console.ForegroundColor = ConsoleColor.White;
  414.         Console.BackgroundColor = ConsoleColor.Red;
  415.         Console.WriteLine(" GAME OVER ");
  416.  
  417.         if (score > highScore)
  418.         {
  419.             Console.SetCursorPosition(width / 2 - 7, height / 2 + 1);
  420.             Console.ForegroundColor = ConsoleColor.White;
  421.             Console.BackgroundColor = ConsoleColor.DarkGreen;
  422.             Console.WriteLine(" NEW HIGH SCORE ");
  423.         }
  424.  
  425.         Console.SetCursorPosition(5, height / 2 + 3);
  426.         Console.ForegroundColor = ConsoleColor.White;
  427.         Console.BackgroundColor = ConsoleColor.Black;
  428.  
  429.         Console.Write("Press \"Y\" to play again or \"N\" to quit");
  430.  
  431.         while (true)
  432.         {
  433.             if (score > highScore)
  434.             {
  435.                 StreamWriter saveScoreFile = new StreamWriter("highscore.txt");
  436.                 saveScoreFile.WriteLine(score);
  437.                 saveScoreFile.Close();
  438.             }
  439.  
  440.             if (Console.KeyAvailable)
  441.             {
  442.                 ConsoleKeyInfo newGame = Console.ReadKey(true);
  443.                 if (newGame.Key == ConsoleKey.Y)
  444.                 {
  445.                     rocks.RemoveAll(c => c.row >= 0);
  446.                     Console.Clear();
  447.                     Main();
  448.                 }
  449.                 if (newGame.Key == ConsoleKey.N)
  450.                 {
  451.                     break;
  452.                 }
  453.             }
  454.         }
  455.     }
  456. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement