Advertisement
coasterka

#4FallingRocksGameBeta

Mar 20th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.35 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Collections.Generic;
  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 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 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.         double speed = 100.0;
  45.         int playfieldWidth = 15;
  46.         int livesCount = 5;
  47.         Console.BufferHeight = Console.WindowHeight = 20;
  48.         Console.BufferWidth = Console.WindowWidth = 50;
  49.  
  50.         Dwarf dwarf = new Dwarf();
  51.         {
  52.             dwarf.x = 2;
  53.             dwarf.y = Console.WindowHeight - 1;
  54.             dwarf.s = "(0)";
  55.             dwarf.color = ConsoleColor.Yellow;
  56.         }
  57.         Random randomGenerator = new Random();
  58.         List<Object> objects = new List<Object>();
  59.  
  60.         while (true)
  61.         {
  62.             speed = speed + 5;
  63.             //How can I print the messages bellow? They're not showing if I put them in these if-loops
  64.             //while (speed < 200)
  65.             //{
  66.             //    PrintStringOnPosition(playfieldWidth + 3, 6, "Level 1", ConsoleColor.White);
  67.             //}
  68.             //if (speed >= 200 && speed < 300)
  69.             //{
  70.             //    PrintStringOnPosition(playfieldWidth + 3, 6, "Level 2", ConsoleColor.White);
  71.             //}
  72.             //if (speed >= 300 && speed < 400)
  73.             //{
  74.             //    PrintStringOnPosition(playfieldWidth + 3, 6, "Level 3", ConsoleColor.White);
  75.             //}
  76.             if (speed > 400)
  77.             {                
  78.                 //PrintStringOnPosition(playfieldWidth + 3, 6, "Max Level reached", ConsoleColor.White);
  79.                 speed = 400;
  80.             }
  81.             bool isHit = false;
  82.             //bool getsBonus = false;
  83.             {
  84.                 // adding bonus objects
  85.                 int chance = randomGenerator.Next(0, 100);
  86.                 if (chance < 10)
  87.                 {
  88.                     Object bonus = new Object();
  89.                     bonus.color = ConsoleColor.Blue;
  90.                     bonus.c = '@';
  91.                     bonus.x = randomGenerator.Next(0, playfieldWidth);
  92.                     bonus.y = 0;
  93.                     objects.Add(bonus);
  94.                 }
  95.                 else if (chance < 20)
  96.                 {                    
  97.                     Object bonus = new Object();
  98.                     bonus.color = ConsoleColor.DarkMagenta;
  99.                     bonus.c = '%';
  100.                     bonus.x = randomGenerator.Next(0, playfieldWidth);
  101.                     bonus.y = 0;
  102.                     objects.Add(bonus);
  103.                 }
  104.                 else
  105.                 {
  106.                     Object newRock = new Object();
  107.                     newRock.color = ConsoleColor.Green;
  108.                     newRock.x = randomGenerator.Next(0, playfieldWidth);
  109.                     newRock.y = 0;
  110.                     newRock.c = '#';
  111.                     objects.Add(newRock);
  112.                 }
  113.             }
  114.  
  115.             // move the dwarf (key pressed)
  116.             while (Console.KeyAvailable)
  117.             {
  118.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  119.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  120.                 {
  121.                     if (dwarf.x - 1 >= 0)
  122.                     {
  123.                         dwarf.x = dwarf.x - 1;
  124.                     }
  125.                 }
  126.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  127.                 {
  128.                     if (dwarf.x + 1 < playfieldWidth)
  129.                     {
  130.                         dwarf.x = dwarf.x + 1;
  131.                     }
  132.                 }
  133.             }
  134.  
  135.             //creating new list of rocks
  136.             List<Object> newListOfRocks = new List<Object>();
  137.  
  138.             // move rocks
  139.             for (int i = 0; i < objects.Count; i++)
  140.             {
  141.                 Object oldRock = objects[i];
  142.                 Object newObject = new Object();
  143.                 newObject.x = oldRock.x;
  144.                 newObject.y = oldRock.y + 1;
  145.                 newObject.c = oldRock.c;
  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.                 {
  151.                     speed -= 50;
  152.                 }
  153.                 if (newObject.c == '@' && newObject.y == dwarf.y && newObject.x == dwarf.x)
  154.                 {
  155.                     livesCount++;
  156.                 }
  157.                 if (newObject.c == '#' && newObject.y == dwarf.y && newObject.x == dwarf.x)
  158.                 {
  159.                     livesCount--;
  160.                     isHit = true;
  161.                     speed = 100;
  162.                     if (livesCount <= 0)
  163.                     {
  164.                         PrintStringOnPosition(playfieldWidth + 3, 10, "!!!GAME OVER!!!", ConsoleColor.Red);
  165.                         PrintStringOnPosition(playfieldWidth + 3, 12, "Press [ENTER] to exit...", ConsoleColor.Gray);
  166.                         Console.ReadLine();
  167.                         Environment.Exit(0);
  168.                     }
  169.                 }
  170.                 if (newObject.y < Console.WindowHeight)
  171.                 {
  172.                     newListOfRocks.Add(newObject);
  173.                 }
  174.             }
  175.             objects = newListOfRocks;
  176.  
  177.             // clear the console
  178.             Console.Clear();
  179.  
  180.             // redraw playfield            
  181.             if (isHit)
  182.             {
  183.                 Console.Beep();
  184.                 objects.Clear();
  185.                 PrintRockOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
  186.             }
  187.             else
  188.             {
  189.                 PrintDwarfOnPosition(dwarf.x, dwarf.y, dwarf.s, dwarf.color);
  190.             }
  191.  
  192.             //if (getsBonus)
  193.             //{                
  194.             //    PrintRockOnPosition(dwarf.x, dwarf.y, 'B', ConsoleColor.White);
  195.             //    PrintRockOnPosition(dwarf.x, dwarf.y, 'B', ConsoleColor.White);
  196.             //}
  197.             //else
  198.             //{
  199.             //    PrintDwarfOnPosition(dwarf.x, dwarf.y, dwarf.s, dwarf.color);
  200.             //}
  201.  
  202.             foreach (Object 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.                        
  211.             // slow down program
  212.             Thread.Sleep((int)(600 - speed));
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement