Advertisement
SSkrev

Falling rocks

Mar 19th, 2014
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.54 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. class FallingRocks
  5. {
  6.     struct Rocks
  7.     {
  8.         public int x;
  9.         public int y;
  10.         public string ch;
  11.         public ConsoleColor color;
  12.     }
  13.     static void Position(int x, int y, string c, ConsoleColor color = ConsoleColor.Green)
  14.     {
  15.         Console.SetCursorPosition(x, y);
  16.         Console.ForegroundColor = color;
  17.         Console.Write(c);
  18.     }
  19.     static void StringPosition(int x, int y, string c, ConsoleColor color = ConsoleColor.Green)
  20.     {
  21.         Console.SetCursorPosition(x, y);
  22.         Console.ForegroundColor = color;
  23.         Console.Write(c);
  24.     }
  25.     static void Main()
  26.     {
  27.         int playfiledwidth = 61;
  28.         int score =0;
  29.         Console.BufferHeight = Console.WindowHeight = 20;
  30.         Console.BufferWidth = Console.WindowWidth = 61;
  31.         Rocks dwarf = new Rocks();
  32.         dwarf.x = 30;
  33.         dwarf.y = Console.WindowHeight - 1;
  34.         dwarf.ch="(.)Y(.)";
  35.         dwarf.color = ConsoleColor.Magenta;
  36.  
  37.         List<Rocks> rock = new List<Rocks>();
  38.         Random randomGenerator = new Random();
  39.         while (true)
  40.         {
  41.             {
  42.                 Rocks newRock = new Rocks();
  43.                 switch (randomGenerator.Next(1, 11))
  44.                 {
  45.                     case 0: newRock.color = ConsoleColor.DarkRed;
  46.                         break;
  47.                     case 1: newRock.color = ConsoleColor.Gray;
  48.                         break;
  49.                     case 2: newRock.color = ConsoleColor.Green;
  50.                         break;
  51.                     case 3: newRock.color = ConsoleColor.Yellow;
  52.                         break;
  53.                     case 4: newRock.color = ConsoleColor.DarkGray;
  54.                         break;
  55.                     case 5: newRock.color = ConsoleColor.Blue;
  56.                         break;
  57.                     case 6: newRock.color = ConsoleColor.DarkBlue;
  58.                         break;
  59.                     case 7: newRock.color = ConsoleColor.DarkGreen;
  60.                         break;
  61.                     case 8: newRock.color = ConsoleColor.DarkRed;
  62.                         break;
  63.                     case 9: newRock.color = ConsoleColor.White;
  64.                         break;
  65.                     case 10: newRock.color = ConsoleColor.DarkCyan;
  66.                         break;
  67.                 }
  68.                 newRock.x = randomGenerator.Next(0, playfiledwidth);
  69.                 newRock.y = 2;
  70.                 switch (randomGenerator.Next(1,11))
  71.                 {
  72.                     case 0: newRock.ch = "^";
  73.                         break;
  74.                     case 1: newRock.ch = "@";
  75.                         break;
  76.                     case 2: newRock.ch = "*";
  77.                         break;
  78.                     case 3: newRock.ch = "&";
  79.                         break;
  80.                     case 4: newRock.ch = "+";
  81.                         break;
  82.                     case 5: newRock.ch = "%";
  83.                         break;
  84.                     case 6: newRock.ch = "$";
  85.                         break;
  86.                     case 7: newRock.ch = "#";
  87.                         break;
  88.                     case 8: newRock.ch = "!";
  89.                         break;
  90.                     case 9: newRock.ch = ".";
  91.                         break;
  92.                     case 10: newRock.ch = ";";
  93.                         break;
  94.                 }
  95.                 rock.Add(newRock);
  96.             }
  97.             //---------------------------------------------------
  98.             if (Console.KeyAvailable)
  99.             {
  100.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  101.                 while (Console.KeyAvailable)
  102.                 {
  103.                     Console.ReadKey();
  104.                 }
  105.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  106.                 {
  107.                     if (dwarf.x >= 1)
  108.                     {
  109.                         dwarf.x = dwarf.x - 1;
  110.                     }
  111.                 }
  112.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  113.                 {
  114.                     if (dwarf.x <= 59-dwarf.ch.Length)
  115.                     {
  116.                         dwarf.x = dwarf.x + 1;
  117.                     }
  118.                 }
  119.  
  120.             }
  121.             List<Rocks> newList = new List<Rocks>();
  122.             for (int i = 0; i < rock.Count; i++)
  123.             {
  124.                 Rocks oldRocks = rock[i];
  125.                 Rocks newRocks = new Rocks();
  126.                 newRocks.x = oldRocks.x;
  127.                 newRocks.y = oldRocks.y + 1;
  128.                 newRocks.ch = oldRocks.ch;
  129.                 newRocks.color = oldRocks.color;
  130.  
  131.                 if (newRocks.y == dwarf.y && newRocks.x == dwarf.x+3)
  132.                 {
  133.                     Position(dwarf.x+2, dwarf.y, "_!_", ConsoleColor.Red);
  134.                     StringPosition(1, 2, "Game Over!!", ConsoleColor.Red);
  135.                     Console.WriteLine();
  136.                     return;
  137.                 }
  138.                 if (newRocks.y < Console.WindowHeight)
  139.                 {
  140.                     newList.Add(newRocks);
  141.                 }
  142.             }            
  143.             rock = newList;
  144.             Console.Clear();
  145.             Position(dwarf.x, dwarf.y, dwarf.ch, dwarf.color);
  146.             foreach (var rocks in rock)
  147.             {
  148.                 Position(rocks.x, rocks.y, rocks.ch, rocks.color);
  149.             }
  150.             StringPosition(1, 1, "Score:" + score, ConsoleColor.Gray);
  151.            
  152.             Thread.Sleep(150);
  153.             score++;
  154.         }  
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement