Advertisement
dimipan80

Game Falling Rocks (Not Fully Completed!)

Jun 22nd, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.81 KB | None | 0 0
  1. namespace _12.FallingRocks
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Threading;
  6.  
  7.     public struct Object
  8.     {
  9.         public int PosX;
  10.         public int PosY;
  11.         public string Str;
  12.         public ConsoleColor Color;
  13.     }    
  14.  
  15.     public class GameFallingRocks
  16.     {
  17.         private const int HEIGHT = 25;
  18.         private const int WIDTH = 43;
  19.  
  20.         private static Random randomGen = new Random();
  21.         private static char[] symbs = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };
  22.        
  23.         public static void Main(string[] args)
  24.         {
  25.             checked
  26.             {
  27.                 // Remove buffer and defining sizes of the console window!
  28.                 Console.BufferHeight = Console.WindowHeight = HEIGHT;
  29.                 Console.BufferWidth = Console.WindowWidth = WIDTH;
  30.  
  31.                 // Defining size of playfield area:
  32.                 int playfieldWidth = 23;
  33.  
  34.                 // Create the Dwarf:
  35.                 Object dwarf = new Object();
  36.                 dwarf.PosX = playfieldWidth / 2;
  37.                 dwarf.PosY = Console.WindowHeight - 1;
  38.                 dwarf.Str = "(0)";
  39.                 dwarf.Color = ConsoleColor.Yellow;
  40.                
  41.                 List<Object> rocks = new List<Object>();
  42.                 int livesCount = 10;
  43.  
  44.                 while (livesCount > 0)
  45.                 {
  46.                     // Create a next Rock on random Top position:
  47.                     Object nextRock = new Object();
  48.                     nextRock.PosX = randomGen.Next(0, playfieldWidth);
  49.                     nextRock.PosY = 0;
  50.                     nextRock.Str = GetRockRandomSymbol();
  51.                     nextRock.Color = GetRandomConsoleColor();
  52.                     rocks.Add(nextRock);
  53.  
  54.                     // Move Dwarf (keys pressed)!
  55.                     if (Console.KeyAvailable)
  56.                     {
  57.                         ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  58.                         while (Console.KeyAvailable)
  59.                         {
  60.                             Console.ReadKey();
  61.                         }
  62.  
  63.                         if (pressedKey.Key == ConsoleKey.LeftArrow)
  64.                         {
  65.                             if (dwarf.PosX - 1 >= 0)
  66.                             {
  67.                                 dwarf.PosX--;
  68.                             }
  69.                         }
  70.                         else if (pressedKey.Key == ConsoleKey.RightArrow)
  71.                         {
  72.                             if (dwarf.PosX + 1 < playfieldWidth)
  73.                             {
  74.                                 dwarf.PosX++;
  75.                             }
  76.                         }                        
  77.                     }
  78.  
  79.                     // Moving Rocks!
  80.                     // Needs to Create a new List of Rocks
  81.                     List<Object> newRockList = new List<Object>();
  82.                     for (int i = 0; i < rocks.Count; i++)
  83.                     {
  84.                         Object oldRock = rocks[i];
  85.                         Object newRock = new Object();
  86.                         newRock.PosX = oldRock.PosX;
  87.                         newRock.PosY = oldRock.PosY + 1;
  88.                         newRock.Str = oldRock.Str;
  89.                         newRock.Color = oldRock.Color;
  90.  
  91.                         // Check if Rocks are hitting Dwarf!
  92.                         for (int j = 0; j < newRock.Str.Length; j++)
  93.                         {
  94.                             if ((newRock.PosX + j == dwarf.PosX || newRock.PosX + j == dwarf.PosX + 1 || newRock.PosX + j == dwarf.PosX + 2) && newRock.PosY == dwarf.PosY)
  95.                             {
  96.                                 livesCount--;
  97.                             }
  98.                         }
  99.  
  100.                         if (newRock.PosY < Console.WindowHeight)
  101.                         {
  102.                             newRockList.Add(newRock);
  103.                         }
  104.                     }
  105.  
  106.                     rocks = newRockList;
  107.  
  108.                     // Clear the console!
  109.                     Console.Clear();
  110.  
  111.                     // Redraw playfield!
  112.                     PrintStringOnPosition(dwarf.PosX, dwarf.PosY, dwarf.Str, dwarf.Color);
  113.                     foreach (Object rock in rocks)
  114.                     {
  115.                         PrintStringOnPosition(rock.PosX, rock.PosY, rock.Str, rock.Color);
  116.                     }
  117.  
  118.                     // Print info!
  119.                     PrintStringOnPosition(27, 8, "Lives: " + livesCount, ConsoleColor.White);
  120.                     if (livesCount <= 0)
  121.                     {
  122.                         PrintStringOnPosition(27, 10, "GAME OVER !!!", ConsoleColor.Red);
  123.                     }
  124.  
  125.                     // Slow down program!
  126.                     Thread.Sleep(150);
  127.                 }
  128.             }
  129.         }
  130.  
  131.         private static string GetRockRandomSymbol()
  132.         {
  133.             int lengthSymbol = randomGen.Next(1, 4);
  134.             string symbol = string.Empty;
  135.             for (int i = 0; i < lengthSymbol; i++)
  136.             {
  137.                 int index = randomGen.Next(0, symbs.Length);
  138.                 symbol += symbs[index];
  139.             }
  140.  
  141.             return symbol;
  142.         }
  143.  
  144.         private static ConsoleColor GetRandomConsoleColor()
  145.         {
  146.             var consoleColors = Enum.GetValues(typeof(ConsoleColor));
  147.             return (ConsoleColor)consoleColors.GetValue(randomGen.Next(consoleColors.Length));
  148.         }
  149.  
  150.         private static void PrintStringOnPosition(int x, int y, string str, ConsoleColor color = ConsoleColor.Gray)
  151.         {
  152.             checked
  153.             {
  154.                 Console.SetCursorPosition(x, y);
  155.                 Console.ForegroundColor = color;
  156.                 Console.Write(str);
  157.             }
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement