Advertisement
nikolov_k

Untitled

Dec 2nd, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. class FallingRocks
  6. {
  7.     struct Entity
  8.     {
  9.         public int x;
  10.         public int y;
  11.         public string content;
  12.         public ConsoleColor color;
  13.     }
  14.  
  15.     // Print rocks or dwarf
  16.     static void PrintObject(int x, int y, string str, ConsoleColor color = ConsoleColor.White)
  17.     {
  18.         Console.SetCursorPosition(x, y);
  19.         Console.ForegroundColor = color;
  20.         Console.Write(str);
  21.     }
  22.  
  23.     // Build Rock
  24.     static string BuildRock(int chance)
  25.     {
  26.         char[] rockTypes = new char[12] { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };
  27.         string rockType = rockTypes[(chance % 12)].ToString();
  28.         string rock = rockType;
  29.         if (chance < 4)
  30.         {
  31.             rock = rockType + rockType + rockType;
  32.         }
  33.         else if (chance < 15)
  34.         {
  35.             rock = rockType + rockType;
  36.         }
  37.         return rock;
  38.     }
  39.  
  40.     // Choose Color
  41.     static ConsoleColor ChooseColor(int chance)
  42.     {
  43.         ConsoleColor color = ConsoleColor.White;
  44.         switch (chance % 3)
  45.         {
  46.             case 0:
  47.                 color = ConsoleColor.Blue;
  48.                 break;
  49.             case 1:
  50.                 color = ConsoleColor.Yellow;
  51.                 break;
  52.             case 2:
  53.                 color = ConsoleColor.DarkCyan;
  54.                 break;
  55.             default:
  56.                 color = ConsoleColor.White;
  57.                 break;
  58.         }
  59.         return color;
  60.     }
  61.  
  62.     static void Main()
  63.     {
  64.         int playfieldWidth = 40;
  65.         int livesCount = 5;
  66.         int scoreCount = 0;
  67.         Console.BufferHeight = Console.WindowHeight = 20;
  68.         Console.BufferWidth = Console.WindowWidth = 60;
  69.         Entity dwarf = new Entity();
  70.         dwarf.x = 18;
  71.         dwarf.y = 19;
  72.         dwarf.color = ConsoleColor.White;
  73.         dwarf.content = "(0)";
  74.         Random randomGenerator = new Random();
  75.         List<Entity> rocks = new List<Entity>();
  76.         while (true)
  77.         {
  78.             bool hitted = false;
  79.             int firstChance = randomGenerator.Next(0, 100);
  80.             // Choose number of rocks in line
  81.             int rocksInLine = firstChance / 20;
  82.             // Move rocks
  83.             List<Entity> newRocks = new List<Entity>();
  84.             for (int i = 0; i < rocks.Count; i++)
  85.             {
  86.                 Entity oldRock = rocks[i];
  87.                 Entity newRock = new Entity();
  88.                 newRock.x = oldRock.x;
  89.                 newRock.y = oldRock.y + 1;
  90.                 newRock.content = oldRock.content;
  91.                 newRock.color = oldRock.color;
  92.                 if (newRock.y < Console.WindowHeight)
  93.                 {
  94.                     newRocks.Add(newRock);
  95.                 }
  96.                 else
  97.                 {
  98.                     scoreCount += oldRock.content.Length;
  99.                 }
  100.                 if (dwarf.x > (newRock.x - 3) && dwarf.x < (newRock.x + newRock.content.Length) && newRock.y == 19)
  101.                 {
  102.                     hitted = true;
  103.                 }
  104.             }
  105.             rocks = newRocks;
  106.  
  107.             // Generate rocks in top line
  108.             for (int i = 0; i < rocksInLine; i++)
  109.             {
  110.                 int secondChance = randomGenerator.Next(0, playfieldWidth);
  111.                 Entity newRock = new Entity();
  112.                 newRock.content = BuildRock(secondChance);
  113.                 newRock.x = secondChance;
  114.                 newRock.y = 0;
  115.                 newRock.color = ChooseColor(secondChance);
  116.                 rocks.Add(newRock);
  117.             }
  118.             // Take user input - move dwarf right or left
  119.             while (Console.KeyAvailable)
  120.             {
  121.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  122.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  123.                 {
  124.                     if (dwarf.x >= 1)
  125.                     {
  126.                         dwarf.x = dwarf.x - 1;
  127.                     }
  128.                 }
  129.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  130.                 {
  131.                     if (dwarf.x + 3 < playfieldWidth)
  132.                     {
  133.                         dwarf.x = dwarf.x + 1;
  134.                     }
  135.                 }
  136.             }
  137.             // Print current content
  138.             Console.Clear();
  139.             foreach (Entity rock in rocks)
  140.             {
  141.                 PrintObject(rock.x, rock.y, rock.content, rock.color);
  142.             }
  143.             PrintObject(42, 5, "SCORE: " + scoreCount, ConsoleColor.DarkGreen);
  144.             PrintObject(42, 7, "LIVES: " + livesCount, ConsoleColor.DarkGreen);
  145.             if (hitted)
  146.             {
  147.                 for (int i = 0; i < 3; i++)
  148.                 {
  149.                     PrintObject(dwarf.x, dwarf.y, "BOOM", ConsoleColor.Red);
  150.                     Thread.Sleep(300);
  151.                     PrintObject(dwarf.x, dwarf.y, "BOOM", ConsoleColor.White);
  152.                     Thread.Sleep(300);
  153.                 }
  154.                 livesCount--;
  155.                 rocks.Clear();
  156.                 Console.Clear();
  157.             }
  158.             else
  159.             {
  160.                 PrintObject(dwarf.x, dwarf.y, dwarf.content, dwarf.color);
  161.             }
  162.             if (livesCount < 1)
  163.             {
  164.                 PrintObject(20, 5, "GAME OVER!", ConsoleColor.DarkGreen);
  165.                 PrintObject(20, 7, "YOUR SCORE: " + scoreCount, ConsoleColor.DarkGreen);
  166.                 PrintObject(20, 9, "Press [enter] to exit", ConsoleColor.DarkGreen);
  167.                 Console.ReadLine();
  168.                 Environment.Exit(0);
  169.             }
  170.             Thread.Sleep(150);
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement