Advertisement
anilak

Falling Rocks

Jul 31st, 2013
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.57 KB | None | 0 0
  1. // Implement the "Falling Rocks" game in the text console.
  2. // A small dwarf stays at the bottom of the screen and can
  3. //move left and right (by the arrows keys). A number of
  4. //rocks of different sizes and forms constantly fall down
  5. //and you need to avoid a crash.
  6. //Rocks are the symbols ^, @, *, &, +, %, $, #, !, ., ;,
  7. // - distributed with appropriate density. The dwarf is (O).
  8. // Ensure a constant game speed by Thread.Sleep(150).
  9. // Implement collision detection and scoring system.
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Threading;
  14.  
  15. class FallingRocks
  16. {
  17.     struct GameObject
  18.     {
  19.         public int x;
  20.         public int y;
  21.         public char c;
  22.         public ConsoleColor color;
  23.     }
  24.  
  25.     public static void DrawObjectOnPosition (int x, int y, char c, ConsoleColor color)
  26.     {
  27.         Console.SetCursorPosition(x, y);
  28.         Console.ForegroundColor = color;
  29.         Console.Write(c);
  30.     }
  31.  
  32.     public static void DrawStringOnPosition(int x, int y, string s, ConsoleColor color)
  33.     {
  34.         Console.SetCursorPosition(x, y);
  35.         Console.ForegroundColor = color;
  36.         Console.Write(s);
  37.     }
  38.  
  39.     static void Main()                      //Game speed depends on the level, a heart is a bonus which gives an extra life
  40.     {
  41.         int playfield = 18;                  //Define the playfield;
  42.         int lifes = 3;
  43.         int score = 0;
  44.         int level = 0;
  45.         char[] shapes = {'!','@','#','$','%','^','&','*','+','.',';'};
  46.         ConsoleColor[] colors = {ConsoleColor.Gray, ConsoleColor.Cyan, ConsoleColor.Magenta, ConsoleColor.Yellow, ConsoleColor.Yellow};
  47.         Random randomGen = new Random();
  48.         Console.BufferHeight = Console.WindowHeight = 20;    //clear the left scroll bar
  49.         Console.BufferHeight = Console.WindowWidth = 60;     //clear the down scroll bar
  50.         Console.Title = "Falling Rocks";
  51.         GameObject dwarf = new GameObject();
  52.         dwarf.x = playfield / 2 + 1;
  53.         dwarf.y = Console.WindowHeight - 1;
  54.         dwarf.c = 'O';
  55.         dwarf.color = ConsoleColor.Green;
  56.         List<GameObject> rocks = new List<GameObject>();
  57.         while (true)
  58.         {
  59.             bool detectCollision = false;
  60.             bool getBonus = false;
  61.             int chance = randomGen.Next(0, 100);
  62.             int rockX = randomGen.Next(0, playfield - 1);
  63.             char rockChar;
  64.             if (chance < 3)
  65.             {
  66.                 rockChar = (char)3;
  67.             }
  68.             else
  69.             {
  70.                 rockChar = shapes[randomGen.Next(0, shapes.Length)];
  71.             }
  72.             ConsoleColor rockCol = colors[randomGen.Next(0, colors.Length)];
  73.             for (int i = 1; i <= randomGen.Next(1, 4); i++)
  74.             {
  75.                 GameObject newRock = new GameObject();
  76.                 newRock.y = 0;
  77.                
  78.                 if (newRock.x >= playfield) newRock.x = playfield - 1;
  79.                 newRock.c = rockChar;
  80.                 if (newRock.c == (char)3)
  81.                 {
  82.                     newRock.color = ConsoleColor.Red;
  83.                     newRock.x = rockX;
  84.                 }
  85.                 else
  86.                 {
  87.                     newRock.color = rockCol;
  88.                     newRock.x = rockX + i - 1;
  89.                 }
  90.                 rocks.Add(newRock);
  91.             }
  92.             while (Console.KeyAvailable)            //Move the dwarf if arrow key is pressed;
  93.             {
  94.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  95.                 if(pressedKey.Key == ConsoleKey.RightArrow)
  96.                     if ((dwarf.x + 2) < playfield)
  97.                     {
  98.                         dwarf.x++;
  99.                     }
  100.                 if(pressedKey.Key == ConsoleKey.LeftArrow)        
  101.                     if ((dwarf.x - 2) >= 0)
  102.                     {
  103.                         dwarf.x--;
  104.                     }
  105.             }
  106.             List<GameObject> currentRocks = new List<GameObject>();
  107.             for (int i = 0; i < rocks.Count; i++)                  //Move the rocks;
  108.             {
  109.                 GameObject oldRock = rocks[i];
  110.                 GameObject currentRock = new GameObject();
  111.                 currentRock.x = oldRock.x;
  112.                 currentRock.y = oldRock.y + 1;
  113.                 currentRock.c = oldRock.c;
  114.                 currentRock.color = oldRock.color;
  115.                 if (currentRock.y < Console.WindowHeight)
  116.                 {
  117.                     currentRocks.Add(currentRock);
  118.                 }
  119.                 else
  120.                 {
  121.                     score++;
  122.                     if(level < 5)   level = score / 50;
  123.                 }
  124.             }
  125.             rocks = currentRocks;
  126.             foreach (GameObject rock in rocks)                          //Check for collision;
  127.             {
  128.                 if (((rock.x == dwarf.x - 1)||
  129.                     (rock.x == dwarf.x) ||
  130.                     (rock.x == dwarf.x + 1)) && (rock.y == dwarf.y))
  131.                 {
  132.                     if (rock.c == (char)3)
  133.                     {
  134.                         getBonus = true;
  135.                     }
  136.                     else
  137.                     {
  138.                         detectCollision = true;
  139.                     }
  140.                    
  141.                 }
  142.             }            
  143.             Console.Clear();                                            //Clear the console;
  144.             if (getBonus)
  145.             {
  146.                 lifes++;
  147.             }
  148.             if (detectCollision)                                        //Redraw the field;
  149.             {
  150.                 lifes--;
  151.                 if (lifes <= 0)
  152.                 {
  153.                     DrawStringOnPosition(playfield + 6, 12, "Lives: " + lifes, ConsoleColor.White);
  154.                     DrawStringOnPosition(playfield + 6, 11, "Score: " + score, ConsoleColor.White);
  155.                     DrawObjectOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
  156.                     DrawStringOnPosition(playfield + 1, 15, "GAME OVER!!!", ConsoleColor.Red);
  157.                     DrawStringOnPosition(playfield + 1, 16, "Press [enter] to exit.", ConsoleColor.Red);
  158.                     Console.Beep(500, 200);
  159.                     Console.Beep(600, 200);
  160.                     Console.Beep(700, 800);
  161.                     Console.ReadLine();
  162.                     return;
  163.                 }
  164.                 DrawObjectOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
  165.                 rocks.Clear();
  166.                 Console.Beep(500, 200);
  167.                 Console.Beep(600, 200);
  168.                 Console.Beep(700, 800);
  169.             }
  170.             else
  171.             {
  172.                 DrawObjectOnPosition(dwarf.x - 1, dwarf.y, '(', dwarf.color);
  173.                 DrawObjectOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
  174.                 DrawObjectOnPosition(dwarf.x + 1, dwarf.y, ')', dwarf.color);
  175.             }
  176.             foreach (GameObject rock in rocks)
  177.             {
  178.                 DrawObjectOnPosition(rock.x, rock.y, rock.c, rock.color);
  179.             }
  180.             for (int i = 0; i < Console.WindowHeight; i++)
  181.             DrawObjectOnPosition(playfield, i,'|', ConsoleColor.White);
  182.             DrawStringOnPosition(playfield + 6, 10, "Level: " + level, ConsoleColor.White);//Draw info;
  183.             DrawStringOnPosition(playfield + 6, 11, "Score: " + score, ConsoleColor.White);
  184.             DrawStringOnPosition(playfield + 6, 12, "Lives: " + lifes, ConsoleColor.White);
  185.             Console.Beep();
  186.             Thread.Sleep(600 - level*100);                                              //Slow the program;
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement