lmarkov

Falling Rocks

Dec 2nd, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.07 KB | None | 0 0
  1. /*
  2.  * * Implement the "Falling Rocks" game in the text console. A small dwarf stays at the bottom of the screen and can move left and right (by the arrows keys). A number of rocks of different sizes and forms constantly fall down and you need to avoid a crash.
  3.  *
  4.  * The game is based on JustCars by Niki's demo
  5.  *
  6.  */
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13.  
  14. struct Object
  15. {
  16.     public int x;
  17.     public int y;
  18.     public char c;
  19.     public ConsoleColor color;
  20. }
  21.  
  22. class FallingRocks
  23. {
  24.     static void PrintOnPosition(int x, int y, char c,
  25.         ConsoleColor color = ConsoleColor.Gray)
  26.     {
  27.         Console.SetCursorPosition(x, y);
  28.         Console.ForegroundColor = color;
  29.         Console.Write(c);
  30.     }
  31.     static void PrintStringOnPosition(int x, int y, string str,
  32.         ConsoleColor color = ConsoleColor.Gray)
  33.     {
  34.         Console.SetCursorPosition(x, y);
  35.         Console.ForegroundColor = color;
  36.         Console.Write(str);
  37.     }
  38.     static void Main()
  39.     {
  40.         long scores = 0;
  41.         int speed = 100;
  42.         int acceleration = 1;
  43.         //byte level = 1;
  44.         int playGroundWidht = 30;
  45.         int livesCount = 3;
  46.         Console.BufferHeight = Console.WindowHeight = 20;
  47.         Console.BufferWidth = Console.WindowWidth = 60;
  48.         Object dwarf = new Object();
  49.         dwarf.x = 15;
  50.         dwarf.y = Console.WindowHeight - 1;
  51.         dwarf.c = 'O';
  52.         dwarf.color = ConsoleColor.Cyan;
  53.         char[] rockSymbol = new char[11];
  54.         rockSymbol[1] = '@';
  55.         rockSymbol[2] = '*';
  56.         rockSymbol[3] = '&';
  57.         rockSymbol[4] = '+';
  58.         rockSymbol[5] = '%';
  59.         rockSymbol[6] = '$';
  60.         rockSymbol[7] = '#';
  61.         rockSymbol[8] = '!';
  62.         rockSymbol[9] = '.';
  63.         rockSymbol[10] = ';';
  64.  
  65.         Random randomGenerator = new Random();
  66.         List<Object> objects = new List<Object>();
  67.         while (true)
  68.         {
  69.             scores = scores + 1;
  70.             if(scores <= 1000 && scores <= 2000)
  71.             {
  72.                 acceleration = 1;
  73.             }
  74.             else if (scores < 2000 && scores <= 5000)
  75.             {
  76.                 acceleration = 2;
  77.             }
  78.             else if (scores < 5000 && scores <= 10000)
  79.             {
  80.                 acceleration = 3;
  81.             }
  82.             else if (scores < 10000 && scores <= 20000)
  83.             {
  84.                 acceleration = 4;
  85.             }
  86.             else
  87.             {
  88.                 acceleration = 5;
  89.             }            
  90.  
  91.             speed += acceleration;
  92.             if (speed > 500)
  93.             {
  94.                 speed = 500;
  95.             }
  96.  
  97.             bool hitted = false;
  98.             {
  99.                 int chance = randomGenerator.Next(0, 100);
  100.                 if (chance < 5)
  101.                 {
  102.                     Object newObject = new Object();
  103.                     newObject.color = ConsoleColor.DarkGreen;
  104.                     newObject.c = 'L'; // 1 live bonus
  105.                     newObject.x = randomGenerator.Next(0, playGroundWidht);
  106.                     newObject.y = 0;
  107.                     objects.Add(newObject);
  108.                 }                
  109.                 else if (chance < 20)
  110.                 {
  111.                     Object newObject = new Object();
  112.                     newObject.color = ConsoleColor.Green;
  113.                     newObject.c = 'S';   // speed bonus
  114.                     newObject.x = randomGenerator.Next(0, playGroundWidht);
  115.                     newObject.y = 0;
  116.                     objects.Add(newObject);
  117.                 }
  118.                 else if (chance < 25)
  119.                 {
  120.                     Object newObject = new Object();
  121.                     newObject.color = ConsoleColor.Blue;
  122.                     newObject.c = 'D'; // scores * 2
  123.                     newObject.x = randomGenerator.Next(0, playGroundWidht);
  124.                     newObject.y = 0;
  125.                     objects.Add(newObject);
  126.                 }
  127.                 else if (chance < 30)
  128.                 {
  129.                     Object newObject = new Object();
  130.                     newObject.color = ConsoleColor.DarkYellow;
  131.                     newObject.c = 'B'; // bonus scores
  132.                     newObject.x = randomGenerator.Next(0, playGroundWidht);
  133.                     newObject.y = 0;
  134.                     objects.Add(newObject);
  135.                 }
  136.                 else
  137.                 {                    
  138.                     Object newCar = new Object();
  139.                     newCar.color = ConsoleColor.Red;
  140.                     newCar.x = randomGenerator.Next(0, playGroundWidht);
  141.                     newCar.y = 0;
  142.                     newCar.c = rockSymbol[randomGenerator.Next(1, 10)];    // different rocks                                    
  143.                     objects.Add(newCar);
  144.                 }
  145.             }
  146.  
  147.             while (Console.KeyAvailable)
  148.             {
  149.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  150.                
  151.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  152.                 {
  153.                     if (dwarf.x - 1 >= 0)
  154.                     {
  155.                         dwarf.x = dwarf.x - 1;
  156.                     }
  157.                 }
  158.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  159.                 {
  160.                     if (dwarf.x + 1 < playGroundWidht)
  161.                     {
  162.                         dwarf.x = dwarf.x + 1;
  163.                     }
  164.                 }
  165.             }
  166.             List<Object> newList = new List<Object>();
  167.             for (int i = 0; i < objects.Count; i++)
  168.             {
  169.                 Object oldRock = objects[i];
  170.                 Object newRock = new Object();
  171.                 newRock.x = oldRock.x;
  172.                 newRock.y = oldRock.y + 1;
  173.                 newRock.c = oldRock.c;
  174.                 newRock.color = oldRock.color;
  175.                 if (newRock.c == 'S' && newRock.y == dwarf.y && newRock.x == dwarf.x)
  176.                 {
  177.                     speed -= 50;
  178.                     if (speed < 100)
  179.                     {
  180.                         speed = 100;
  181.                     }
  182.                 }
  183.                 if (newRock.c == 'L' && newRock.y == dwarf.y && newRock.x == dwarf.x)
  184.                 {
  185.                     livesCount++;
  186.                 }
  187.                 if (newRock.c == 'B' && newRock.y == dwarf.y && newRock.x == dwarf.x)
  188.                 {
  189.                     scores = scores + 10;
  190.                 }
  191.                 if (newRock.c == 'D' && newRock.y == dwarf.y && newRock.x == dwarf.x)
  192.                 {
  193.                     scores = scores + 20;
  194.                 }
  195.                 if ((newRock.c == '@' || newRock.c == '*' || newRock.c == '&' || newRock.c == '+' || newRock.c == '%' || newRock.c == '$' || newRock.c == '#' || newRock.c == '!' || newRock.c == '.' || newRock.c == ';') && newRock.y == dwarf.y && newRock.x == dwarf.x)
  196.                 {
  197.                     //Console.Beep();
  198.                     livesCount--;
  199.                     hitted = true;
  200.                    
  201.                     while(livesCount <= 0)
  202.                     {
  203.                         PrintStringOnPosition(40, 10, "GAME OVER!!!", ConsoleColor.Red);
  204.                         PrintStringOnPosition(40, 12, "New game -> N", ConsoleColor.Blue);
  205.                         PrintStringOnPosition(40, 14, "Exit -> [esc]", ConsoleColor.Red);
  206.                         ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  207.                         if (pressedKey.Key == ConsoleKey.Escape)
  208.                         {
  209.                             Environment.Exit(0);
  210.                         }
  211.                         if (pressedKey.Key == ConsoleKey.N)
  212.                         {
  213.                             Main();
  214.                         }
  215.                     }
  216.                 }
  217.                 if (newRock.y < Console.WindowHeight)
  218.                 {
  219.                     newList.Add(newRock);
  220.                 }
  221.             }
  222.             objects = newList;
  223.             Console.Clear();
  224.             if (hitted)
  225.             {
  226.                 objects.Clear();
  227.                 if (speed > 200)
  228.                 {
  229.                     speed = 150;
  230.                 }
  231.                 PrintOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
  232.             }
  233.             else
  234.             {
  235.                 PrintOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
  236.             }
  237.             foreach (Object car in objects)
  238.             {
  239.                 PrintOnPosition(car.x, car.y, car.c, car.color);
  240.             }
  241.  
  242.             PrintStringOnPosition(40, 4, "Scores: " + scores, ConsoleColor.White);
  243.             PrintStringOnPosition(40, 5, "Lives: " + livesCount, ConsoleColor.White);
  244.             PrintStringOnPosition(40, 6, "Speed: " + speed, ConsoleColor.White);
  245.             PrintStringOnPosition(40, 7, "Acceleration: " + acceleration + "/5", ConsoleColor.White);
  246.             //PrintStringOnPosition(40, 8, "Level: " + level + "/10", ConsoleColor.White);
  247.            
  248.             Thread.Sleep((int)(600 - speed));
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment