Advertisement
anilak

C#1_4_11

May 18th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.52 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.         GameObject dwarf = new GameObject();
  51.         dwarf.x = playfield / 2 + 1;
  52.         dwarf.y = Console.WindowHeight - 1;
  53.         dwarf.c = 'O';
  54.         dwarf.color = ConsoleColor.Green;
  55.         List<GameObject> rocks = new List<GameObject>();
  56.         while (true)
  57.         {
  58.             bool detectCollision = false;
  59.             bool getBonus = false;
  60.             int chance = randomGen.Next(0, 100);
  61.             int rockX = randomGen.Next(0, playfield);
  62.             char rockChar;
  63.             if (chance < 3)
  64.             {
  65.                 rockChar = (char)3;
  66.             }
  67.             else
  68.             {
  69.                 rockChar = shapes[randomGen.Next(0, shapes.Length)];
  70.             }
  71.             ConsoleColor rockCol = colors[randomGen.Next(0, colors.Length)];
  72.             for (int i = 1; i <= randomGen.Next(1, 4); i++)
  73.             {
  74.                 GameObject newRock = new GameObject();
  75.                 newRock.y = 0;
  76.                
  77.                 if (newRock.x >= playfield) newRock.x = playfield - 1;
  78.                 newRock.c = rockChar;
  79.                 if (newRock.c == (char)3)
  80.                 {
  81.                     newRock.color = ConsoleColor.Red;
  82.                     newRock.x = rockX;
  83.                 }
  84.                 else
  85.                 {
  86.                     newRock.color = rockCol;
  87.                     newRock.x = rockX + i - 1;
  88.                 }
  89.                 rocks.Add(newRock);
  90.             }
  91.             while (Console.KeyAvailable)            //Move the dwarf if arrow key is pressed;
  92.             {
  93.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  94.                 if(pressedKey.Key == ConsoleKey.RightArrow)
  95.                     if ((dwarf.x + 2) < playfield)
  96.                     {
  97.                         dwarf.x++;
  98.                     }
  99.                 if(pressedKey.Key == ConsoleKey.LeftArrow)        
  100.                     if ((dwarf.x - 2) >= 0)
  101.                     {
  102.                         dwarf.x--;
  103.                     }
  104.             }
  105.             List<GameObject> currentRocks = new List<GameObject>();
  106.             for (int i = 0; i < rocks.Count; i++)                  //Move the rocks;
  107.             {
  108.                 GameObject oldRock = rocks[i];
  109.                 GameObject currentRock = new GameObject();
  110.                 currentRock.x = oldRock.x;
  111.                 currentRock.y = oldRock.y + 1;
  112.                 currentRock.c = oldRock.c;
  113.                 currentRock.color = oldRock.color;
  114.                 if (currentRock.y < Console.WindowHeight)
  115.                 {
  116.                     currentRocks.Add(currentRock);
  117.                 }
  118.                 else
  119.                 {
  120.                     score++;
  121.                     if(level < 5)   level = score / 50;
  122.                 }
  123.             }
  124.             rocks = currentRocks;
  125.             foreach (GameObject rock in rocks)                          //Check for collision;
  126.             {
  127.                 if (((rock.x == dwarf.x - 1)||
  128.                     (rock.x == dwarf.x) ||
  129.                     (rock.x == dwarf.x + 1)) && (rock.y == dwarf.y))
  130.                 {
  131.                     if (rock.c == (char)3)
  132.                     {
  133.                         getBonus = true;
  134.                     }
  135.                     else
  136.                     {
  137.                         detectCollision = true;
  138.                     }
  139.                    
  140.                 }
  141.             }            
  142.             Console.Clear();                                            //Clear the console;
  143.             if (getBonus)
  144.             {
  145.                 lifes++;
  146.             }
  147.             if (detectCollision)                                        //Redraw the field;
  148.             {
  149.                 lifes--;
  150.                 if (lifes <= 0)
  151.                 {
  152.                     DrawStringOnPosition(playfield + 6, 12, "Lives: " + lifes, ConsoleColor.White);
  153.                     DrawStringOnPosition(playfield + 6, 11, "Score: " + score, ConsoleColor.White);
  154.                     DrawObjectOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
  155.                     DrawStringOnPosition(playfield + 1, 15, "GAME OVER!!!", ConsoleColor.Red);
  156.                     DrawStringOnPosition(playfield + 1, 16, "Press [enter] to exit.", ConsoleColor.Red);
  157.                     Console.Beep(500, 200);
  158.                     Console.Beep(600, 200);
  159.                     Console.Beep(700, 800);
  160.                     Console.ReadLine();
  161.                     return;
  162.                 }
  163.                 rocks.Clear();
  164.                 DrawObjectOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
  165.                 Console.Beep(500, 200);
  166.                 Console.Beep(600, 200);
  167.                 Console.Beep(700, 800);
  168.             }
  169.             else
  170.             {
  171.                 DrawObjectOnPosition(dwarf.x - 1, dwarf.y, '(', dwarf.color);
  172.                 DrawObjectOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
  173.                 DrawObjectOnPosition(dwarf.x + 1, dwarf.y, ')', dwarf.color);
  174.             }
  175.             foreach (GameObject rock in rocks)
  176.             {
  177.                 DrawObjectOnPosition(rock.x, rock.y, rock.c, rock.color);
  178.             }
  179.             for (int i = 0; i < Console.WindowHeight; i++)
  180.             DrawObjectOnPosition(playfield, i,'|', ConsoleColor.White);
  181.             DrawStringOnPosition(playfield + 6, 10, "Level: " + level, ConsoleColor.White);//Draw info;
  182.             DrawStringOnPosition(playfield + 6, 11, "Score: " + score, ConsoleColor.White);
  183.             DrawStringOnPosition(playfield + 6, 12, "Lives: " + lifes, ConsoleColor.White);
  184.             Console.Beep();
  185.             Thread.Sleep(600 - level*100);                                              //Slow the program;
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement