static void GenerateLineOfRocks() { char[] rocks = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';' }; int sizeOfRocks, posOfRock; int numOfRocks = rnd.Next(1, difficulty); for (int i = 0; i < numOfRocks; i++) { Stone stone = new Stone(); sizeOfRocks = rnd.Next(1, 4); stone.color = rnd.Next(0, 4); stone.sign = rocks[rnd.Next(0, rocks.GetLength(0) - 1)]; posOfRock = rnd.Next(0, 77); for (int j = 0; j < sizeOfRocks; j++) { array[0, posOfRock] = stone; writeStoneAtPos(stone, 0, posOfRock); posOfRock++; } } }
static int Game(int rockCount, int sleepTime) { char[] rockSymbols = new char[10] { '#', '+', '&', '!', '=', '$', '~', '^', '?', '%' }; // symbols for the rocks, can be changed sbyte rocksSpeed = 1; double score = 0; int finalScore = 0; Random randomGenerator = new Random(); Stone[] rocks = new Stone[rockCount]; Stone dwarf = new Stone(); dwarf.X = Console.WindowWidth / 2; dwarf.Y = Console.WindowHeight - 1; for (int i = 0; i < rockCount; i++) { rocks[i].X = randomGenerator.Next(1, (Console.WindowWidth - 1)); rocks[i].Y = randomGenerator.Next(1, (Console.WindowHeight - 5)); rocks[i].Color = (ConsoleColor)randomGenerator.Next(10, 16); rocks[i].Symbol = rockSymbols[randomGenerator.Next(1, rockSymbols.Length)]; } while (true) { if (rocksSpeed % 23 == 0) { for (int i = 0; i < rockCount; i++) { if (rocks[i].Y == (Console.WindowHeight - 1)) { Console.SetCursorPosition(rocks[i].X, rocks[i].Y); Console.Write(" "); rocks[i].X = randomGenerator.Next(1, (Console.WindowWidth - 1)); rocks[i].Y = randomGenerator.Next(0, 3); rocks[i].Color = (ConsoleColor)randomGenerator.Next(10, 16); rocks[i].Symbol = rockSymbols[randomGenerator.Next(1, rockSymbols.Length)]; } else { Console.SetCursorPosition(rocks[i].X, rocks[i].Y); Console.Write(" "); rocks[i].Y++; } Console.SetCursorPosition(rocks[i].X, rocks[i].Y); Console.ForegroundColor = rocks[i].Color; Console.Write(rocks[i].Symbol); } } if (Console.KeyAvailable) { Console.SetCursorPosition(dwarf.X - 1, dwarf.Y); Console.Write(" "); ConsoleKeyInfo pressedKey = Console.ReadKey(); if (pressedKey.Key == ConsoleKey.RightArrow) dwarf.X++; if (pressedKey.Key == ConsoleKey.LeftArrow) dwarf.X--; } if ((dwarf.X == Console.WindowWidth - 2) || (dwarf.X < 1)) { return (finalScore * 10); } for (int i = 0; i < rockCount; i++) { if (((dwarf.X == rocks[i].X) && (dwarf.Y == rocks[i].Y)) || ((dwarf.X == rocks[i].X - 1) && (dwarf.Y == rocks[i].Y)) || ((dwarf.X == rocks[i].X + 1) && (dwarf.Y == rocks[i].Y))) { Console.ForegroundColor = ConsoleColor.Red; Console.SetCursorPosition(dwarf.X - 1, dwarf.Y); Console.Write("\\@/"); return (finalScore * 10); } } Console.ForegroundColor = ConsoleColor.Cyan; Console.SetCursorPosition(dwarf.X - 1, dwarf.Y); Console.Write("\\☺/"); Thread.Sleep(sleepTime); rocksSpeed++; score += 0.02; finalScore = (int)score; } }
static void writeStoneAtPos(Stone stone, int x, int y) { try { if (stone.color == 0) Console.ForegroundColor = ConsoleColor.White; else if (stone.color == 1) Console.ForegroundColor = ConsoleColor.Yellow; else if (stone.color == 2) Console.ForegroundColor = ConsoleColor.Gray; else if (stone.color == 3) Console.ForegroundColor = ConsoleColor.Cyan; else if (stone.color == 4) Console.ForegroundColor = ConsoleColor.Green; Console.SetCursorPosition(y, x); Console.Write(stone.sign); } catch (ArgumentOutOfRangeException e) { Console.Clear(); Console.WriteLine(e.Message); } }
static void MoveRocksDown() { Stone[,] temp = new Stone[25, 80]; for (int i = 0; i < array.GetLength(0); i++) for (int j = 0; j < array.GetLength(1); j++) if (i != 24) temp[i + 1, j] = array[i, j]; array = temp; Console.MoveBufferArea(0, 0, y, 24, 0, 1); //Move rocks down a line Console.MoveBufferArea(y + 3, 0, 80 - (y + 3), 24, y + 3, 1); Console.MoveBufferArea(y, 0, 3, 23, y, 1); }