Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _12.FallingRocks
- {
- using System;
- using System.Collections.Generic;
- using System.Threading;
- public struct Object
- {
- public int PosX;
- public int PosY;
- public string Str;
- public ConsoleColor Color;
- }
- public class GameFallingRocks
- {
- private const int HEIGHT = 25;
- private const int WIDTH = 43;
- private static Random randomGen = new Random();
- private static char[] symbs = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };
- public static void Main(string[] args)
- {
- checked
- {
- // Remove buffer and defining sizes of the console window!
- Console.BufferHeight = Console.WindowHeight = HEIGHT;
- Console.BufferWidth = Console.WindowWidth = WIDTH;
- // Defining size of playfield area:
- int playfieldWidth = 23;
- // Create the Dwarf:
- Object dwarf = new Object();
- dwarf.PosX = playfieldWidth / 2;
- dwarf.PosY = Console.WindowHeight - 1;
- dwarf.Str = "(0)";
- dwarf.Color = ConsoleColor.Yellow;
- List<Object> rocks = new List<Object>();
- int livesCount = 10;
- while (livesCount > 0)
- {
- // Create a next Rock on random Top position:
- Object nextRock = new Object();
- nextRock.PosX = randomGen.Next(0, playfieldWidth);
- nextRock.PosY = 0;
- nextRock.Str = GetRockRandomSymbol();
- nextRock.Color = GetRandomConsoleColor();
- rocks.Add(nextRock);
- // Move Dwarf (keys pressed)!
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo pressedKey = Console.ReadKey(true);
- while (Console.KeyAvailable)
- {
- Console.ReadKey();
- }
- if (pressedKey.Key == ConsoleKey.LeftArrow)
- {
- if (dwarf.PosX - 1 >= 0)
- {
- dwarf.PosX--;
- }
- }
- else if (pressedKey.Key == ConsoleKey.RightArrow)
- {
- if (dwarf.PosX + 1 < playfieldWidth)
- {
- dwarf.PosX++;
- }
- }
- }
- // Moving Rocks!
- // Needs to Create a new List of Rocks
- List<Object> newRockList = new List<Object>();
- for (int i = 0; i < rocks.Count; i++)
- {
- Object oldRock = rocks[i];
- Object newRock = new Object();
- newRock.PosX = oldRock.PosX;
- newRock.PosY = oldRock.PosY + 1;
- newRock.Str = oldRock.Str;
- newRock.Color = oldRock.Color;
- // Check if Rocks are hitting Dwarf!
- for (int j = 0; j < newRock.Str.Length; j++)
- {
- if ((newRock.PosX + j == dwarf.PosX || newRock.PosX + j == dwarf.PosX + 1 || newRock.PosX + j == dwarf.PosX + 2) && newRock.PosY == dwarf.PosY)
- {
- livesCount--;
- }
- }
- if (newRock.PosY < Console.WindowHeight)
- {
- newRockList.Add(newRock);
- }
- }
- rocks = newRockList;
- // Clear the console!
- Console.Clear();
- // Redraw playfield!
- PrintStringOnPosition(dwarf.PosX, dwarf.PosY, dwarf.Str, dwarf.Color);
- foreach (Object rock in rocks)
- {
- PrintStringOnPosition(rock.PosX, rock.PosY, rock.Str, rock.Color);
- }
- // Print info!
- PrintStringOnPosition(27, 8, "Lives: " + livesCount, ConsoleColor.White);
- if (livesCount <= 0)
- {
- PrintStringOnPosition(27, 10, "GAME OVER !!!", ConsoleColor.Red);
- }
- // Slow down program!
- Thread.Sleep(150);
- }
- }
- }
- private static string GetRockRandomSymbol()
- {
- int lengthSymbol = randomGen.Next(1, 4);
- string symbol = string.Empty;
- for (int i = 0; i < lengthSymbol; i++)
- {
- int index = randomGen.Next(0, symbs.Length);
- symbol += symbs[index];
- }
- return symbol;
- }
- private static ConsoleColor GetRandomConsoleColor()
- {
- var consoleColors = Enum.GetValues(typeof(ConsoleColor));
- return (ConsoleColor)consoleColors.GetValue(randomGen.Next(consoleColors.Length));
- }
- private static void PrintStringOnPosition(int x, int y, string str, ConsoleColor color = ConsoleColor.Gray)
- {
- checked
- {
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = color;
- Console.Write(str);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement