Advertisement
coasterka

#4FallingRocksGameFinal

Mar 24th, 2014
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.31 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5.  
  6. struct Dwarf
  7. {
  8.     public int x;
  9.     public int y;
  10.     public string s;
  11.     public ConsoleColor color;
  12. }
  13.  
  14. struct Objects
  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 PrintDwarfOnPosition(int x, int y, string s, ConsoleColor color = ConsoleColor.Gray)
  25.     {
  26.         Console.SetCursorPosition(x, y);
  27.         Console.ForegroundColor = color;        
  28.         Console.Write(s);
  29.     }
  30.     static void PrintRockOnPosition(int x, int y, char c, ConsoleColor color = ConsoleColor.Gray)
  31.     {
  32.         Console.SetCursorPosition(x, y);
  33.         Console.ForegroundColor = color;
  34.         Console.Write(c);
  35.     }
  36.     static void PrintStringOnPosition(int x, int y, string s, ConsoleColor color = ConsoleColor.Gray)
  37.     {
  38.         Console.SetCursorPosition(x, y);
  39.         Console.ForegroundColor = color;
  40.         Console.Write(s);
  41.     }
  42.     static void Main()
  43.     {
  44.         //char[] rocksArray = { '!', '@', '#', '$', '%', '&', '^', '+', '-' };
  45.         string dwarfSymbol = "(0)";
  46.         double speed = 100.0;        
  47.         int playfieldWidth = 16;
  48.         int livesCount = 5;
  49.         Console.BufferHeight = Console.WindowHeight = 20;
  50.         Console.BufferWidth = Console.WindowWidth = 50;
  51.  
  52.         Dwarf dwarf = new Dwarf();
  53.         {
  54.             dwarf.x = 8;
  55.             dwarf.y = Console.WindowHeight - 1;
  56.             dwarf.s = dwarfSymbol;
  57.             dwarf.color = ConsoleColor.Yellow;
  58.         }
  59.         Random randomGenerator = new Random();
  60.         //int randomStone = randomGenerator.Next(rocksArray.Length);
  61.         List<Objects> objects = new List<Objects>();
  62.  
  63.         while (true)
  64.         {
  65.             speed = speed + 5;
  66.             if (speed > 400)
  67.             {
  68.                 speed = 400;                
  69.             }
  70.             bool isHit = false;            
  71.             {
  72.                 // adding bonus objects
  73.                 int chance = randomGenerator.Next(0, 100);
  74.                 if (chance < 10)
  75.                 {
  76.                     Objects bonus = new Objects();
  77.                     bonus.color = ConsoleColor.Blue;
  78.                     bonus.c = '@';
  79.                     bonus.x = randomGenerator.Next(0, playfieldWidth);
  80.                     bonus.y = 0;
  81.                     objects.Add(bonus);
  82.                 }
  83.                 else if (chance < 20)
  84.                 {                    
  85.                     Objects bonus = new Objects();
  86.                     bonus.color = ConsoleColor.DarkMagenta;
  87.                     bonus.c = '%';
  88.                     bonus.x = randomGenerator.Next(0, playfieldWidth);
  89.                     bonus.y = 0;
  90.                     objects.Add(bonus);
  91.                 }
  92.                 else
  93.                 {
  94.                     Objects newRock = new Objects();
  95.                     newRock.color = ConsoleColor.Green;
  96.                     newRock.x = randomGenerator.Next(0, playfieldWidth);
  97.                     newRock.y = 0;
  98.                     int num = randomGenerator.Next(0, 10);
  99.                     //newRock.c = '#';
  100.                     //char[] rocksArray = { '!', '@', '#', '$', '%', '&', '^', '+', '-' }; //no use of this
  101.                     newRock.c = (char)('#' + num);
  102.                     //char[] chars = "$#*?^&".ToCharArray();
  103.                     //Random randomChar = new Random();
  104.                     //int num = randomChar.Next(chars.Length);
  105.                     //Console.WriteLine(chars[num]);
  106.                     objects.Add(newRock);                    
  107.                 }
  108.  
  109.             }
  110.  
  111.             // move the dwarf (key pressed)
  112.             while (Console.KeyAvailable)
  113.             {
  114.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  115.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  116.                 {
  117.                     if (dwarf.x - 1 >= 0)
  118.                     {
  119.                         dwarf.x = dwarf.x - 1;
  120.                     }
  121.                 }
  122.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  123.                 {
  124.                     if (dwarf.x + 1 < playfieldWidth)
  125.                     {
  126.                         dwarf.x = dwarf.x + 1;
  127.                     }
  128.                 }
  129.             }            
  130.  
  131.             //creating new list of rocks
  132.             List<Objects> newListOfRocks = new List<Objects>();
  133.  
  134.             // move rocks
  135.             for (int i = 0; i < objects.Count; i++)
  136.             {
  137.                 Objects oldRock = objects[i];
  138.                 Objects newObject = new Objects();
  139.                 newObject.x = oldRock.x;
  140.                 newObject.y = oldRock.y + 1;
  141.                 newObject.c = oldRock.c;
  142.                 //newObject.c = rocksArray[randomStone];
  143.                 newObject.color = oldRock.color;
  144.  
  145.                 // check if rocks are hitting the dwarf
  146.                 //if (newObject.c == '%' && newObject.y == dwarf.y && newObject.x == dwarf.x)
  147.                 if (newObject.c == '%' && newObject.y == dwarf.y && newObject.x >= dwarf.x && newObject.x <= dwarf.x + 2)
  148.                 {
  149.                     speed -= 100;
  150.                     if (speed < 150)
  151.                     {
  152.                         speed = 150;
  153.                     }
  154.                 }
  155.                 if (newObject.c == '@' && newObject.y == dwarf.y && newObject.x >= dwarf.x && newObject.x <= dwarf.x + 2)
  156.                 {
  157.                     livesCount++;                    
  158.                 }
  159.                 if(newObject.color == ConsoleColor.Green)
  160.                 {
  161.                     if (newObject.y == dwarf.y && newObject.x >= dwarf.x && newObject.x <= dwarf.x + 2)
  162.                     {
  163.                         if (newObject.c == '!' || newObject.c == '&' || newObject.c == '#' || newObject.c == '$' ||
  164.                             newObject.c == '^' || newObject.c == '+' || newObject.c == '-' || newObject.c == '(' ||
  165.                             newObject.c == ')' || newObject.c == '\'' || newObject.c == ',' || newObject.c == '*' ||
  166.                             newObject.c == '%' || newObject.c == '@')
  167.                         {
  168.                             livesCount--;
  169.                             isHit = true;
  170.                             speed = 100;
  171.                             if (livesCount <= 0)
  172.                             {
  173.                                 PrintStringOnPosition(playfieldWidth + 3, 10, "!!!GAME OVER!!!", ConsoleColor.Red);
  174.                                 PrintStringOnPosition(playfieldWidth + 3, 12, "Press [ENTER] to exit...", ConsoleColor.Gray);
  175.                                 Console.ReadLine();
  176.                                 Environment.Exit(0);
  177.                             }
  178.                         }
  179.                     }
  180.                 }
  181.                 if (newObject.y < Console.WindowHeight)
  182.                 {
  183.                     newListOfRocks.Add(newObject);
  184.                 }
  185.             }
  186.             objects = newListOfRocks;
  187.  
  188.             // clear the console
  189.             Console.Clear();
  190.  
  191.             // redraw playfield
  192.             if (isHit)
  193.             {
  194.                 Console.Beep();
  195.                 objects.Clear();
  196.                 PrintRockOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
  197.             }
  198.             else
  199.             {
  200.                 PrintDwarfOnPosition(dwarf.x, dwarf.y, dwarf.s, dwarf.color);
  201.             }
  202.             foreach (Objects rock in objects)
  203.             {
  204.                 PrintRockOnPosition(rock.x, rock.y, rock.c, rock.color);
  205.             }
  206.  
  207.             // draw info
  208.             PrintStringOnPosition(playfieldWidth + 3, 4, "Lives: " + livesCount, ConsoleColor.White);
  209.             PrintStringOnPosition(playfieldWidth + 3, 5, "Speed: " + speed, ConsoleColor.White);
  210.             PrintStringOnPosition(playfieldWidth + 3, 6, "% decreases the speed", ConsoleColor.DarkMagenta);
  211.             PrintStringOnPosition(playfieldWidth + 3, 7, "@ gives you a new life", ConsoleColor.Blue);
  212.             PrintStringOnPosition(playfieldWidth + 3, 8, "Beware of green objects!", ConsoleColor.Green);
  213.                        
  214.             // slow down program
  215.             int maxSpeed = (int)(600 - speed);
  216.             Thread.Sleep(maxSpeed);
  217.  
  218.             //exiting the program? how?
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement