Advertisement
TzvetanIG

FollingRocks

Mar 20th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4.  
  5. class FallingRocks
  6. {
  7.     static Random randomGenerator = new Random();
  8.     static int score = 0;
  9.  
  10.  
  11.     static void Main()
  12.     {
  13.         // Settings
  14.         int windowHeight = 20;
  15.         int windowWidth = 50;
  16.         int speedRock = 200;
  17.         int densityRocks = 2;
  18.  
  19.  
  20.         Console.CursorVisible = false;
  21.         Console.WindowHeight = windowHeight;
  22.         Console.WindowWidth = windowWidth;
  23.         Console.BufferWidth = Console.WindowWidth;
  24.         Console.BufferHeight = Console.WindowHeight + 1;
  25.  
  26.         //conteiner for rocks
  27.         List<Rock> rocks = new List<Rock>();
  28.  
  29.         Dwarf dwarf = new Dwarf(windowWidth / 2 - 1, windowHeight - 2);
  30.  
  31.         bool isPlay = true;
  32.  
  33.         while (isPlay)
  34.         {
  35.             Console.Clear();
  36.             RocksGenerator(rocks, densityRocks);
  37.             dwarf.Print();
  38.  
  39.             if (Console.KeyAvailable)
  40.             {
  41.                 ConsoleKeyInfo pressedKey = Console.ReadKey();
  42.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  43.                     dwarf.MoveLeft(0);
  44.                 if (pressedKey.Key == ConsoleKey.RightArrow)
  45.                     dwarf.MoveRight(windowWidth);
  46.             }
  47.  
  48.             for (int i = 0; i < rocks.Count; i++)
  49.             {
  50.                 rocks[i].Print();
  51.                 rocks[i].Move();
  52.                 if (rocks[i].Y == dwarf.Y)
  53.                 {
  54.                     int rockStart = rocks[i].X;
  55.                     int rockEnd = rockStart + rocks[i].Size - 1;
  56.                     int dwarfStart = dwarf.X;
  57.                     int dwarfEnd = dwarf.X + 2;
  58.                     isPlay = rockEnd < dwarfStart || rockStart > dwarfEnd;
  59.                 }
  60.             }
  61.  
  62.             RemoveRocksOutOfWindow(rocks);
  63.             PrintScore(5, windowHeight - 1);
  64.             Thread.Sleep(speedRock);
  65.         }// end while
  66.  
  67.         Console.Clear();
  68.         Console.WriteLine("Game Over!!!");
  69.         Console.WriteLine("Your score: {0}", score);
  70.         Console.ReadLine();
  71.     }
  72.  
  73.     static void RocksGenerator(List<Rock> rocks, int densityRocks)
  74.     {
  75.         int randomNumber = randomGenerator.Next(1, densityRocks);
  76.  
  77.         for (int i = 0; i < randomNumber; i++)
  78.         {
  79.             int randomXPosition = randomGenerator.Next(1, Console.WindowWidth);
  80.             rocks.Add(new Rock(randomXPosition));
  81.         }
  82.     }
  83.  
  84.     static void RemoveRocksOutOfWindow(List<Rock> rocks)
  85.     {
  86.         score++;
  87.         for (int i = 0; i < rocks.Count; i++)
  88.         {
  89.             if (rocks[i].Y >= Console.WindowHeight - 1)
  90.             {
  91.                 rocks.RemoveAt(i);
  92.             }
  93.         }
  94.     }
  95.  
  96.     static void PrintScore(int x, int y)
  97.     {
  98.         Console.SetCursorPosition(x, y);
  99.         Console.ForegroundColor = ConsoleColor.White;
  100.         Console.Write("Score: {0}", score.ToString().PadLeft(5, '0'));
  101.     }
  102. }
  103.  
  104. class Rock
  105. {
  106.     int x;
  107.     int y;
  108.     string view;
  109.     ConsoleColor color;
  110.     static Random randomGenerator = new Random();
  111.     static ConsoleColor[] colors = { ConsoleColor.Cyan, ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.White };
  112.  
  113.     public Rock(int x = 0)
  114.     {
  115.         this.x = x;
  116.         this.y = 0;
  117.         string rockSymbols = "^@*&+%$#!.;-";
  118.         int symbolPosition = randomGenerator.Next(0, rockSymbols.Length - 1);
  119.         int size = randomGenerator.Next(1, 4);
  120.         this.view = new String(rockSymbols[symbolPosition], size);
  121.         color = colors[randomGenerator.Next(0, 3)];
  122.     }
  123.  
  124.     public void Print()
  125.     {
  126.         Console.SetCursorPosition(this.x, this.y);
  127.         Console.ForegroundColor = color;
  128.         Console.Write(this.view);
  129.     }
  130.  
  131.     public void Move()
  132.     {
  133.         this.y++;
  134.     }
  135.  
  136.     public int X
  137.     {
  138.         get
  139.         {
  140.             return this.x;
  141.         }
  142.  
  143.         set
  144.         {
  145.             this.x = value;
  146.         }
  147.     }
  148.  
  149.     public int Y
  150.     {
  151.         get
  152.         {
  153.             return this.y;
  154.         }
  155.  
  156.         set
  157.         {
  158.             this.y = value;
  159.         }
  160.     }
  161.  
  162.     public int Size
  163.     {
  164.         get
  165.         {
  166.             return this.view.Length;
  167.         }
  168.     }
  169.  
  170. }
  171.  
  172. class Dwarf
  173. {
  174.     int x;
  175.     int y;
  176.     string view;
  177.     ConsoleColor color = ConsoleColor.White;
  178.  
  179.     public Dwarf(int x, int y)
  180.     {
  181.         this.x = x;
  182.         this.y = y;
  183.         this.view = "(O)";
  184.     }
  185.  
  186.     public void Print()
  187.     {
  188.         Console.SetCursorPosition(this.x, this.y);
  189.         Console.ForegroundColor = color;
  190.         Console.Write(this.view);
  191.     }
  192.  
  193.     public void MoveLeft(int minX)
  194.     {
  195.         if (this.x > minX)
  196.         {
  197.             this.x--;
  198.         }
  199.     }
  200.  
  201.     public void MoveRight(int maxX)
  202.     {
  203.         if (this.x < maxX - this.view.Length)
  204.         {
  205.             this.x++;
  206.         }
  207.     }
  208.  
  209.     public int X
  210.     {
  211.         get
  212.         {
  213.             return this.x;
  214.         }
  215.  
  216.         set
  217.         {
  218.             this.x = value;
  219.         }
  220.     }
  221.  
  222.     public int Y
  223.     {
  224.         get
  225.         {
  226.             return this.y;
  227.         }
  228.  
  229.         set
  230.         {
  231.             this.y = value;
  232.         }
  233.     }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement