Advertisement
tockata

Falling Rocks

Jun 24th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. class FallingRocks
  6. {
  7.     static int lives = 5;
  8.     static int score = 10;
  9.  
  10.     struct Figure
  11.     {
  12.         public int x;
  13.         public int y;
  14.         public string s;
  15.         public ConsoleColor color;
  16.     }
  17.  
  18.     static void Main()
  19.     {
  20.         SetPlayfield();
  21.         List<char> rocksChar = new List<char>() { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };
  22.         List<ConsoleColor> colors = new List<ConsoleColor> { ConsoleColor.DarkYellow, ConsoleColor.Blue, ConsoleColor.Magenta,
  23.             ConsoleColor.Red, ConsoleColor.White, ConsoleColor.Yellow, ConsoleColor.Gray };
  24.         List<Figure> rocks = new List<Figure>();
  25.         Random randomGenerator = new Random();
  26.  
  27.         Figure playfieldBorder = new Figure();
  28.         playfieldBorder.x = 0;
  29.         playfieldBorder.y = 3;
  30.         playfieldBorder.s = new string('-', Console.WindowWidth - 1);
  31.         playfieldBorder.color = ConsoleColor.Gray;
  32.  
  33.         Figure dwarf = new Figure();
  34.         dwarf.x = Console.WindowWidth / 2;
  35.         dwarf.y = Console.WindowHeight - 1;
  36.         dwarf.s = "(0)";
  37.         dwarf.color = ConsoleColor.Green;
  38.  
  39.         while (true)
  40.         {
  41.             CreateFigure(rocksChar, colors, rocks, randomGenerator);
  42.             dwarf = MoveDwarf(dwarf);
  43.             rocks = MoveRocks(rocks);
  44.             Console.Clear();
  45.             PrintOnPosition(playfieldBorder.x, playfieldBorder.y, playfieldBorder.s, playfieldBorder.color);
  46.  
  47.             foreach (Figure currentRock in rocks)
  48.             {
  49.                 PrintOnPosition(currentRock.x, currentRock.y, currentRock.s, currentRock.color);
  50.             }
  51.  
  52.             PrintOnPosition(dwarf.x, dwarf.y, dwarf.s, dwarf.color);
  53.             PrintOnPosition(2, 1, "Lives: " + lives, ConsoleColor.Green);
  54.             PrintOnPosition(21, 1, "Score: " + score, ConsoleColor.Green);
  55.             score += 10;
  56.             CheckDwarfIsHit(rocks, dwarf);
  57.             Thread.Sleep(300);
  58.         }
  59.     }
  60.  
  61.     private static Figure MoveDwarf(Figure dwarf)
  62.     {
  63.         while (Console.KeyAvailable)
  64.         {
  65.             ConsoleKeyInfo pressedKey = Console.ReadKey();
  66.             if (pressedKey.Key == ConsoleKey.LeftArrow)
  67.             {
  68.                 if (dwarf.x - 1 >= 0)
  69.                 {
  70.                     dwarf.x--;
  71.                 }
  72.             }
  73.             else if (pressedKey.Key == ConsoleKey.RightArrow)
  74.             {
  75.                 if (dwarf.x + 2 < Console.WindowWidth - 2)
  76.                 {
  77.                     dwarf.x++;
  78.                 }
  79.             }
  80.         }
  81.         return dwarf;
  82.     }
  83.  
  84.     private static List<Figure> MoveRocks(List<Figure> rocks)
  85.     {
  86.         for (int i = 0; i < rocks.Count; i++)
  87.         {
  88.             if (rocks[i].y < Console.WindowHeight - 1)
  89.             {
  90.                 Figure tempRock = new Figure();
  91.                 tempRock.x = rocks[i].x;
  92.                 tempRock.y = rocks[i].y + 1;
  93.                 tempRock.s = rocks[i].s;
  94.                 tempRock.color = rocks[i].color;
  95.                 rocks[i] = tempRock;
  96.             }
  97.             else if (rocks[i].y == Console.WindowHeight - 1)
  98.             {
  99.                 rocks.RemoveAt(i);
  100.             }
  101.         }
  102.         return rocks;
  103.     }
  104.  
  105.     private static void CheckDwarfIsHit(List<Figure> rocks, Figure dwarf)
  106.     {
  107.         foreach (Figure rock in rocks)
  108.         {
  109.             if (rock.s.Length == 1)
  110.             {
  111.                 if (rock.y == dwarf.y && (rock.x == dwarf.x || rock.x == dwarf.x + 1 || rock.x == dwarf.x + 2))
  112.                 {
  113.                     PrintOnPosition(dwarf.x, dwarf.y, dwarf.s, ConsoleColor.Red);
  114.                     lives--;
  115.                     if (lives == 0)
  116.                     {
  117.                         Console.Clear();
  118.                         PrintOnPosition(15, 10, "Game Over!", ConsoleColor.Red);
  119.                         PrintOnPosition(8, 11, "Press any key to exit.", ConsoleColor.Red);
  120.                         Console.ReadLine();
  121.                         Environment.Exit(0);
  122.                     }
  123.                 }
  124.             }
  125.             //else if (rock.s.Length == 2)
  126.             //{
  127.             //    if (rock.y == dwarf.y && (rock.x == dwarf.x || rock.x == dwarf.x + 1 || rock.x == dwarf.x + 2
  128.             //    || rock.x + 1 == dwarf.x || rock.x + 1 == dwarf.x + 1 || rock.x + 1 == dwarf.x + 2))
  129.             //    {
  130.             //        PrintOnPosition(dwarf.x, dwarf.y, dwarf.s, ConsoleColor.Red);
  131.             //        lives--;
  132.             //        if (lives == 0)
  133.             //        {
  134.             //            Console.Clear();
  135.             //            PrintOnPosition(15, 10, "Game Over!", ConsoleColor.Red);
  136.             //            PrintOnPosition(8, 11, "Press any key to exit.", ConsoleColor.Red);
  137.             //            Console.ReadLine();
  138.             //            Environment.Exit(0);
  139.             //        }
  140.             //    }
  141.             //}
  142.             //else if (rock.s.Length == 3)
  143.             //{
  144.             //    if (rock.y == dwarf.y && (rock.x == dwarf.x || rock.x == dwarf.x + 1 || rock.x == dwarf.x + 2
  145.             //    || rock.x + 1 == dwarf.x || rock.x + 1 == dwarf.x + 1 || rock.x + 1 == dwarf.x + 2
  146.             //    || rock.x + 2 == dwarf.x || rock.x + 2 == dwarf.x + 2 || rock.x + 1 == dwarf.x + 2))
  147.             //    {
  148.             //        lives--;
  149.             //        if (lives == 0)
  150.             //        {
  151.             //            Console.Clear();
  152.             //            PrintOnPosition(15, 10, "Game Over!", ConsoleColor.Red);
  153.             //            PrintOnPosition(8, 11, "Press any key to exit.", ConsoleColor.Red);
  154.             //            Console.ReadLine();
  155.             //            Environment.Exit(0);
  156.             //        }
  157.             //    }
  158.             //}
  159.         }
  160.     }
  161.  
  162.     private static void SetPlayfield()
  163.     {
  164.         Console.Title = "Falling rocks";
  165.         Console.WindowWidth = 40;
  166.         Console.BufferWidth = Console.WindowWidth;
  167.         Console.WindowHeight = 20;
  168.         Console.BufferHeight = Console.WindowHeight;
  169.         Console.CursorVisible = false;
  170.     }
  171.  
  172.     private static void CreateFigure(List<char> rocksChar, List<ConsoleColor> colors, List<Figure> rocks, Random randomGenerator)
  173.     {
  174.         Figure rock = new Figure();
  175.         rock.x = randomGenerator.Next(0, Console.WindowWidth);
  176.         rock.y = 4;
  177.         char newRockChar = rocksChar[randomGenerator.Next(0, rocksChar.Count)];
  178.         string newFigure = new string(newRockChar, randomGenerator.Next(1, 4));
  179.         rock.s = newFigure;
  180.         rock.color = colors[randomGenerator.Next(0, colors.Count)];
  181.         rocks.Add(rock);
  182.     }
  183.  
  184.     static void PrintOnPosition(int x, int y, string s, ConsoleColor color = ConsoleColor.Red)
  185.     {
  186.         Console.SetCursorPosition(x, y);
  187.         Console.ForegroundColor = color;
  188.         Console.Write(s);
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement