Advertisement
milen8204

FallingRocks

Mar 23rd, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. /* Implement the "Falling Rocks" game in the text console.
  5.  * A small dwarf stays at the bottom of the screen and can move left and right (by the arrows keys).
  6.  * A number of rocks of different sizes and forms constantly fall down and you need to avoid a crash.
  7.  * Rocks are the symbols ^, @, *, &, +, %, $, #, !, ., ;, - distributed with appropriate density.
  8.  * The dwarf is (O). Ensure a constant game speed by Thread.Sleep(150).
  9.  * Implement collision detection and scoring system. */
  10. class Object
  11. {
  12.     public int x;
  13.     public int y;
  14.     public string c;
  15.     public ConsoleColor color;
  16. }
  17.  
  18. class FallingRocks
  19. {
  20.     static void PrintOnPosition(int x, int y, string str, ConsoleColor color = ConsoleColor.Gray)
  21.     {
  22.         Console.SetCursorPosition(x, y);
  23.         Console.ForegroundColor = color;
  24.         Console.Write(str);
  25.     }
  26.  
  27.     static void RockMover(Object rock)
  28.     {
  29.         rock.y++;
  30.     }
  31.  
  32.     static void Main()
  33.     {
  34.         int simplicity = 150;
  35.         int lives = 5;
  36.         int score = 0;
  37.         int level = 0;
  38.         Console.BufferWidth = Console.WindowWidth = 40;
  39.         Console.BufferHeight = Console.WindowHeight = 40;
  40.  
  41.         //creating dwarf`s left side "("
  42.         Object dwarfLeftSide = new Object();
  43.         dwarfLeftSide.x = (Console.WindowWidth / 2) - 1;
  44.         dwarfLeftSide.y = Console.WindowHeight - 1;
  45.         dwarfLeftSide.c = "(";
  46.         dwarfLeftSide.color = ConsoleColor.White;
  47.         //creating dwarf`s head "O"
  48.         Object dwarfHead = new Object();
  49.         dwarfHead.x = dwarfLeftSide.x + 1;
  50.         dwarfHead.y = Console.WindowHeight - 1;
  51.         dwarfHead.c = "O";
  52.         dwarfHead.color = ConsoleColor.White;
  53.         //creating dwarf`s right side ")"
  54.         Object dwarfRightSide = new Object();
  55.         dwarfRightSide.x = dwarfHead.x + 1;
  56.         dwarfRightSide.y = Console.WindowHeight - 1;
  57.         dwarfRightSide.c = ")";
  58.         dwarfRightSide.color = ConsoleColor.White;
  59.  
  60.         List<Object> rocksForPrint = new List<Object>();
  61.  
  62.         while (lives > 0)
  63.         {
  64.             bool collision = false;
  65.  
  66.             //printing player on the screen
  67.             PrintOnPosition(dwarfLeftSide.x, dwarfLeftSide.y, dwarfLeftSide.c, dwarfLeftSide.color);
  68.             PrintOnPosition(dwarfHead.x, dwarfHead.y, dwarfHead.c, dwarfHead.color);
  69.             PrintOnPosition(dwarfRightSide.x, dwarfRightSide.y, dwarfRightSide.c, dwarfRightSide.color);
  70.  
  71.             //moving player
  72.             while (Console.KeyAvailable)
  73.             {
  74.                 ConsoleKeyInfo pressedKey = Console.ReadKey();
  75.  
  76.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  77.                 {
  78.                     if (dwarfLeftSide.x >= 1)
  79.                     {
  80.                         dwarfLeftSide.x = dwarfLeftSide.x - 1;
  81.                         dwarfHead.x = dwarfHead.x - 1;
  82.                         dwarfRightSide.x = dwarfRightSide.x - 1;
  83.                     }
  84.                 }
  85.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  86.                 {
  87.                     if (dwarfRightSide.x < Console.WindowWidth - 1)
  88.                     {
  89.                         dwarfLeftSide.x = dwarfLeftSide.x + 1;
  90.                         dwarfHead.x = dwarfHead.x + 1;
  91.                         dwarfRightSide.x = dwarfRightSide.x + 1;
  92.                     }
  93.                 }
  94.             }
  95.  
  96.             //creating row whit rocks
  97.             string[] rocksString = { "^", "@", "*", "&", "+", "%", "$", "#", "!", ".", ";" };
  98.             ConsoleColor[] rocksColor = { ConsoleColor.White, ConsoleColor.Green, ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.Cyan, ConsoleColor.Magenta };
  99.             Random pickArock = new Random();
  100.             Random rockDensity = new Random();
  101.             Random pickXcoordinate = new Random();
  102.             Random pickRockColor = new Random();
  103.            
  104.             int density = rockDensity.Next(0, 5);
  105.            
  106.             for (int i = 0; i < density; i++)
  107.             {
  108.                 Object rock = new Object();
  109.                 rock.x = pickXcoordinate.Next(1, (Console.WindowWidth - 1));
  110.                 rock.y = 0;
  111.  
  112.                 //to prevent that random gives every time the same number in loop
  113.                 for (int j = 0; j <= 1; j++)
  114.                 {
  115.                     rock.c = rocksString[pickArock.Next(0, rocksString.Length)];
  116.                 }
  117.  
  118.                 //to prevent that random gives every time the same number in loop
  119.                 for (int k = 0; k <= 1; k++)
  120.                 {
  121.                     rock.color = rocksColor[pickRockColor.Next(0, rocksColor.Length)];
  122.                 }            
  123.                                  
  124.                 rocksForPrint.Add(rock);
  125.             }
  126.  
  127.             //moving all rocks down
  128.             foreach (var item in rocksForPrint)
  129.             {
  130.                 RockMover(item);
  131.             }
  132.  
  133.             //printing rocks on the screen
  134.             foreach (var rock in rocksForPrint)
  135.             {
  136.                 if (rock.y < Console.WindowHeight - 1)
  137.                 {
  138.                     PrintOnPosition(rock.x, rock.y, rock.c, rock.color);
  139.                 }
  140.             }
  141.  
  142.             //cuting the List of roks
  143.             if (rocksForPrint.Count > 250)
  144.             {
  145.                 for (int i = 0; i < 10; i++)
  146.                 {
  147.                     rocksForPrint.RemoveAt(0);
  148.                 }
  149.             }
  150.  
  151.             //cheks for that the dwarf had been hit by some of the rocks
  152.             for (int i = 0; i < rocksForPrint.Count; i++)
  153.             {
  154.                 if ((dwarfLeftSide.x == rocksForPrint[i].x || dwarfHead.x == rocksForPrint[i].x || dwarfRightSide.x == rocksForPrint[i].x) && dwarfHead.y == rocksForPrint[i].y)
  155.                 {
  156.                     collision = true;
  157.                 }
  158.             }
  159.  
  160.             if (collision)
  161.             {
  162.                 Console.Beep();
  163.                 //prints dead dwarf
  164.                 PrintOnPosition(dwarfLeftSide.x, dwarfLeftSide.y, "*", ConsoleColor.Red);
  165.                 PrintOnPosition(dwarfHead.x, dwarfHead.y, "*", ConsoleColor.Red);
  166.                 PrintOnPosition(dwarfRightSide.x, dwarfRightSide.y, "*", ConsoleColor.Red);
  167.                 lives--;
  168.             }
  169.  
  170.             score++;
  171.             Thread.Sleep(simplicity);
  172.             Console.Clear();
  173.  
  174.             //checks for difficulty
  175.             if (score % 100 == 0)
  176.             {
  177.                 simplicity = simplicity - 10;
  178.                 level++;
  179.             }
  180.         }
  181.  
  182.         Console.Clear();
  183.         Console.WriteLine("Game over");
  184.         Console.WriteLine("You died on level {0}. Your score is {1}", level, (score - 40));
  185.         Console.WriteLine("Press [Enter] to exit");        
  186.         Console.ReadLine();
  187.         Environment.Exit(0);
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement