Advertisement
Teodor92

FallingDown

Nov 29th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. class FallingRocks
  8. {
  9.     static Random randomGenerator = new Random();
  10.     // scoring and leveling system
  11.     static int score = 0;
  12.     static int currentLevel = 0;
  13.     static int levelReached = 1;
  14.     static decimal speed = 400;
  15.     // array with different types of rocks
  16.     static char[] rockChars = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';',};
  17.     // array with different types of rock colors
  18.     static ConsoleColor[] color =
  19.     {
  20.         ConsoleColor.Blue, ConsoleColor.Cyan,
  21.         ConsoleColor.DarkBlue, ConsoleColor.DarkCyan,
  22.         ConsoleColor.DarkGray, ConsoleColor.DarkGreen,
  23.         ConsoleColor.DarkMagenta, ConsoleColor.DarkYellow,
  24.         ConsoleColor.Gray, ConsoleColor.Green,
  25.         ConsoleColor.Magenta, ConsoleColor.Yellow
  26.     };
  27.     static bool Pause = false;
  28.     struct Object
  29.     {
  30.         public int x;
  31.         public int y;
  32.         public int objLen;
  33.         public string c;
  34.         public ConsoleColor color;
  35.     }
  36.     static void RemoveScrollBars()
  37.     {
  38.         Console.BufferHeight = Console.WindowHeight;
  39.         Console.BufferWidth = Console.WindowWidth;
  40.     }
  41.     static void StringPrinter(int x, int y, string PrintString, ConsoleColor color)
  42.     {
  43.         Console.SetCursorPosition(x, y);
  44.         Console.ForegroundColor = color;
  45.         Console.Write(PrintString);
  46.     }
  47.     static void ScoringSystem()
  48.     {
  49.         Console.ForegroundColor = ConsoleColor.White;
  50.         Console.Write("Your points: {0}", score);
  51.         Console.Write("\t");
  52.         Console.ForegroundColor = ConsoleColor.Red;
  53.         Console.Write(" Your speed is: {0}", (int)speed);
  54.     }
  55.     static void ColisoionDetect()
  56.     { }
  57.     static void Main()
  58.     {
  59.         // dwarf object
  60.         Object dwarf = new Object();
  61.         dwarf.x = Console.WindowWidth / 2 - 1;
  62.         dwarf.y = Console.WindowHeight - 3;
  63.         dwarf.c = "(0)";
  64.         dwarf.color = ConsoleColor.White;
  65.         //rock area list
  66.         List<Object> rocks = new List<Object>();
  67.         RemoveScrollBars();
  68.         while (true)
  69.         {
  70.             bool isDead = false;
  71.             //rock object
  72.             Object startRock = new Object();
  73.             startRock.x = randomGenerator.Next(0, Console.WindowWidth);
  74.             startRock.y = 1;
  75.             startRock.objLen = randomGenerator.Next(1,5);
  76.             startRock.c = new string(rockChars[randomGenerator.Next(1, 10)], startRock.objLen);
  77.             startRock.color = color[randomGenerator.Next(1, 12)];
  78.             rocks.Add(startRock);
  79.             // Top row scoring system
  80.             ScoringSystem();
  81.             // Rock area movement
  82.                 // Fixing the lag problem with a second list
  83.             List<Object> newList = new List<Object>();
  84.             for (int i = 0; i < rocks.Count; i++)
  85.             {
  86.                 Object oldRock = rocks[i];
  87.                 Object newRock = new Object();
  88.                 newRock.x = oldRock.x;
  89.                 newRock.y = oldRock.y + 1;
  90.                 newRock.objLen = oldRock.objLen;
  91.                 newRock.c = oldRock.c;
  92.                 newRock.color = oldRock.color;
  93.                 //collison detect
  94.                 if (newRock.y == dwarf.y)
  95.                 {
  96.                     for (int j = 0; j < newRock.objLen; j++)
  97.                     {
  98.                         if ((newRock.x + j == dwarf.x) || (newRock.x + j == dwarf.x + 1) || (newRock.x + j == dwarf.x + 2))
  99.                         {
  100.                             isDead = true;
  101.                             break;
  102.                         }
  103.                     }
  104.                 }
  105.                 else
  106.                 {
  107.                     //point and level system
  108.                     if (speed > 0)
  109.                     {
  110.                         speed = speed - 0.01M;  
  111.                     }
  112.                     score++;
  113.                 }
  114.                 if (newRock.y < Console.WindowHeight)
  115.                 {
  116.                     newList.Add(newRock);
  117.                 }
  118.             }
  119.             rocks = newList;
  120.             //Dwarf movement and some other key functions
  121.             if (Console.KeyAvailable)
  122.             {
  123.                 ConsoleKeyInfo keyPressed = Console.ReadKey();
  124.                 if (keyPressed.Key == ConsoleKey.LeftArrow)
  125.                 {
  126.                     if (dwarf.x > 0)
  127.                     {
  128.                         dwarf.x--;  
  129.                     }
  130.                 }
  131.                 if (keyPressed.Key == ConsoleKey.RightArrow)
  132.                 {
  133.                     if (dwarf.x < Console.WindowWidth - 3)
  134.                     {
  135.                         dwarf.x++;                        
  136.                     }
  137.                 }
  138.                 while (Console.KeyAvailable)
  139.                 {
  140.                     Console.ReadKey(true);
  141.                 }
  142.             }
  143.             //dwarf printer
  144.             if (isDead)
  145.             {
  146.                 Console.ResetColor();
  147.                 Console.Clear();
  148.                 Console.WriteLine("Game Over !");
  149.                 Console.WriteLine("Your score is {0} pt", score);
  150.                 Environment.Exit(0);
  151.             }
  152.             else
  153.             {
  154.                 StringPrinter(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
  155.             }
  156.             //rock printer
  157.             foreach (Object rock in rocks)
  158.             {
  159.                 StringPrinter(rock.x, rock.y, rock.c, rock.color);
  160.             }
  161.             Thread.Sleep((int)speed);
  162.             //Clear the console
  163.             Console.Clear();
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement