Advertisement
AnitaN

04.ConsoleInputOutputHomework/12.FallingRocks

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