Advertisement
Guest User

Problem 12 FallingRocks

a guest
Mar 23rd, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. class FallingRocks
  6. {
  7.     struct Unit
  8.     {
  9.         public int x;
  10.         public int y;
  11.         public ConsoleColor color;
  12.         public char symbol;
  13.     }
  14.     static void Main()
  15.     {
  16.         Console.SetWindowSize(60, 30);
  17.         Console.SetBufferSize(60, 30);
  18.         char[] symbols = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';' };
  19.         String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
  20.         int numColors = colorNames.Length;
  21.         List<Unit> RocksList = new List<Unit>();
  22.         string player = "(0)";
  23.         int playerPosX = Console.WindowWidth / 2;
  24.         int playerPosY = 26;
  25.         ulong score = 0;
  26.         byte lives = 3;
  27.         Random rnd = new Random();
  28.         ConsoleKey rightArrow = ConsoleKey.RightArrow;
  29.         ConsoleKey leftArrow = ConsoleKey.LeftArrow;
  30.         while (true)
  31.         {
  32.             Thread.Sleep(150);
  33.             Console.Clear();
  34.             for (int i = 0; i < rnd.Next(1, 3); i++)
  35.             {
  36.                 Unit newInitRock = new Unit();
  37.                 newInitRock.x = rnd.Next(59);
  38.                 newInitRock.y = 0;
  39.                 newInitRock.color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[rnd.Next(numColors)]);
  40.                 if (newInitRock.color == ConsoleColor.Black)
  41.                 {
  42.                     newInitRock.color = ConsoleColor.Green;
  43.                 }
  44.                 else
  45.                 {
  46.                     newInitRock.color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[rnd.Next(numColors)]);
  47.                 }
  48.                 newInitRock.symbol = symbols[rnd.Next(symbols.Length)];
  49.                 RocksList.Add(newInitRock);
  50.             }
  51.             List<Unit> newList = new List<Unit>();
  52.             for (int i = 0; i < RocksList.Count; i++)
  53.             {
  54.                 Unit oldRock = RocksList[i];
  55.                 Unit NewMovedRock = new Unit();
  56.                 NewMovedRock.x = oldRock.x;
  57.                 NewMovedRock.y = oldRock.y + 1;
  58.                 NewMovedRock.color = oldRock.color;
  59.                 NewMovedRock.symbol = oldRock.symbol;
  60.                 if (NewMovedRock.x >= playerPosX && NewMovedRock.x < playerPosX + 3 && NewMovedRock.y == playerPosY && lives > 0)
  61.                 {
  62.                     lives--;
  63.                 }
  64.                 if (NewMovedRock.y < 27)
  65.                 {
  66.                     newList.Add(NewMovedRock);
  67.                 }
  68.             }
  69.             RocksList = newList;
  70.             foreach (Unit rock in RocksList)
  71.             {
  72.                 Console.SetCursorPosition(rock.x, rock.y);
  73.                 Console.ForegroundColor = rock.color;
  74.                 Console.Write(rock.symbol);
  75.             }
  76.             Console.ForegroundColor = ConsoleColor.Green;
  77.             Console.SetCursorPosition(playerPosX, playerPosY);
  78.             Console.Write(player);
  79.             Console.SetCursorPosition(0, 27);
  80.             Console.Write(new String('-', 60));
  81.             Console.SetCursorPosition(0, 28);
  82.             Console.Write("lives : " + lives);
  83.             Console.SetCursorPosition(0, 29);
  84.             Console.Write("score : " + score);
  85.             if (Console.KeyAvailable && playerPosX <= 57 && playerPosX >= 0)
  86.             {
  87.                 ConsoleKeyInfo key = Console.ReadKey(true);
  88.                 while (Console.KeyAvailable) { Console.ReadKey(true); }
  89.                 if (key.Key == rightArrow && playerPosX < 57)
  90.                 {
  91.                     playerPosX += 1;
  92.                 }
  93.                 if (key.Key == leftArrow && playerPosX > 0)
  94.                 {
  95.                     playerPosX -= 1;
  96.                 }
  97.             }
  98.             if (lives == 0)
  99.             {
  100.                 break;
  101.             }
  102.             score++;
  103.         }
  104.         Console.ForegroundColor = ConsoleColor.Red;
  105.         Console.SetCursorPosition(0, 28);
  106.         Console.WriteLine("Game Over");
  107.         Console.WriteLine("Final score : " + score);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement