coasterka

#4FallingRocksGameBeta2.0

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