private static void GenerateNewRocks() { //75% change of generating new rock and the number of rocks doens't exceed the maximum value. if (random.Next(0, 4) != 0 && rocks.Count() < MaximumNumberOfRocks) { //33% chance of generating new cluster. if (random.Next(0, 100) < 33) { Rock newRock = new Rock(WindowWidth - GameMenuWidth, WindowHeight); int clusterSize = random.Next(0, Math.Min(newRock.X, MaximumClusterSize)) + 1; if (clusterSize >= 2) { rocks.Add(newRock); for (int i = 1; i < clusterSize; ++i) { rocks.Add(new Rock(WindowWidth - GameMenuWidth, WindowHeight, newRock.X - i, newRock.Symbol, newRock.Color)); } } } //generate single block. else { rocks.Add(new Rock(WindowWidth - GameMenuWidth, WindowHeight)); } } }
//Rock copy; public Rock(Rock x) { Col = x.Col; Row = x.Row; RockType = x.RockType; Clr = x.Clr; }
private static void DrawRock(Rock r) { Console.ForegroundColor = r.Color; Console.SetCursorPosition(r.X, r.Y); Console.WriteLine(r.Type.ToString()); r.IsActive = true; r.Y--; }
static void Main() { string[] visualization = new string[] { "*", "@", "%", "$", "^", "&", "#", "+", ".", "+++", "++", "--", "!" }; ConsoleColor[] colors = new ConsoleColor[] { ConsoleColor.Red, ConsoleColor.Magenta, ConsoleColor.Green, ConsoleColor.Yellow, ConsoleColor.Cyan }; RemoveScrollBars(); Player myplayer = new Player("(0)", Console.WindowHeight - 1); myplayer.Draw(); // create rocks Rock[] rocks = new Rock[15]; Random random = new Random(); for (int createrRock = 0; createrRock < rocks.Length; createrRock++) { rocks[createrRock] = new Rock(visualization[random.Next(0, visualization.Length - 1)], colors[random.Next(0, colors.Length - 1)], createrRock * 5 + random.Next(0, 3), random.Next(3, 10)); } while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.LeftArrow) { myplayer.MoveLeft(); } if (keyInfo.Key == ConsoleKey.RightArrow) { myplayer.MoveRight(); } if (keyInfo.Key == ConsoleKey.Escape) { break; } } Console.Clear(); myplayer.Draw(); //Draw rocks for (int counterRocks = 0; counterRocks < rocks.Length; counterRocks++) { rocks[counterRocks].Move(); rocks[counterRocks].Draw(); } if (myplayer.IsDead(rocks) == true) { Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2); Console.WriteLine("GAME OVER"); break; } Thread.Sleep(50); // Delay the program } }
private static void AddNewRock() { count = rocks.Count; for (int i = 0; i < gameLevel - count; i++) { Rock r = new Rock() { X = Helpers.RandomNumber(0, Console.BufferWidth - 1), Y = Helpers.RandomNumber(-10, 1) }; rocks.Add(r); } }
/// <summary> /// Gives random rock and random powerups based on chance /// </summary> /// <returns>Returns a Rock</returns> public static Rock randomRock() { int chance = Game.rnd.Next(0, 301); Rock x = new Rock(6, Game.rnd.Next(1, Draw.GameWidth), GameObjects.rocksChar[Game.rnd.Next(0, GameObjects.rocksChar.Length)], (ConsoleColor)Game.rnd.Next(2, 16)); if (chance < 15) { x.Clr = ConsoleColor.Yellow; x.RockType = "\u21AC"; //Speed up power up } else if (chance > 15 && chance < 30) { x.Clr = ConsoleColor.Blue; x.RockType = "\u21EA"; //Multiplier powerup; } else if (chance > 30 && chance < 33) { x.Clr = ConsoleColor.Red; x.RockType = "\u2665"; //Life; } else if (chance > 33 && chance < 36) { x.Clr = ConsoleColor.DarkYellow; x.RockType = "\u273E"; //Invincibility; } else if (chance > 36 && chance < 38) { x.Clr = ConsoleColor.Black; x.RockType = "\u2620"; //Death (-3 lifes) } else if (chance > 38 && chance < 48) { x.Clr = ConsoleColor.Yellow; x.RockType = "\u21AB"; //Slow down } else if (chance > 48 && chance < 58) { x.Clr = ConsoleColor.Cyan; x.RockType = "\u2744"; //Freeze } else if (chance > 190 && chance < 199) { x.Clr = ConsoleColor.Magenta; x.RockType = "\u2622"; //Clear Rocks } else if (chance > 299) { x.Clr = ConsoleColor.Red; x.RockType = "\u2661"; //Better heart } return x; }
public bool IsDead(Rock[] rocks) { for (int counter = 0; counter < rocks.Length; counter++) { if (PositionY == rocks[counter].PositionY) { int a = PositionX; int b = PositionX + Visualization.Length - 1; int c = rocks[counter].PositionX; int d = c + (rocks[counter].Visualization.Length - 1); if ((a >= c && b <= d) || (a <= c && b >= c) || (a <= d && b >= d)) { return true; } } } return false; }
private static void GenerateRock(Rock rock, char symbol, ConsoleColor color) { Console.SetCursorPosition(rock.BoundaryX, rock.BoundaryY); Console.ForegroundColor = color; Console.Write(symbol); }
public RockRow(int length) { row = new Rock[length]; rowLength = length; }
static void Main() { // Some properties and variables to be used Console.CursorVisible = false; Console.BufferHeight = Console.WindowHeight = 18; Console.WindowWidth = 20; Console.BufferWidth = 20; Random randomGenerator = new Random(); double sleeptime = 200; double score = 0; int lives = 5; // constructing the dwarf elements "(O)" Dwarf newDwarf = new Dwarf(); newDwarf.col = 9; newDwarf.row = Console.WindowHeight - 1; newDwarf.color = ConsoleColor.Gray; List<Rock> Rocks = new List<Rock>(); // Writing the instructions at the start of the game PrintOnPosition(0, 5, " The aim of the game is to avoid all the rocks. Good luck!", ConsoleColor.Yellow); PrintOnPosition(0, 9, "Pick", ConsoleColor.Yellow); PrintOnPosition(5, 9, (char)3, ConsoleColor.Red); PrintOnPosition(7, 9, "for lives", ConsoleColor.Yellow); PrintOnPosition(0, 10, "Pick", ConsoleColor.Yellow); PrintOnPosition(5, 10, '$', ConsoleColor.Green); PrintOnPosition(7, 10, "for money", ConsoleColor.Yellow); PrintOnPosition(5, 13, "Good luck!", ConsoleColor.Red); PrintOnPosition(17, 0, ' ', ConsoleColor.Black); Console.ReadLine(); Console.Clear(); // making the lines of play for (int i = 0; i <= Console.WindowWidth - 1; i++) { PrintOnPosition(i, 4, '_', ConsoleColor.White); } while (true) { // The bool values in case we get hit. They are different for the parts of the dwarf elements HitPosition newHitPosition = HitPosition.None; // the chance is for making bonuses from time to time int chance = randomGenerator.Next(0, 100); { Rock newRock = new Rock(); newRock.col = randomGenerator.Next(0, 19); newRock.row = 5; if (chance < 1) { // It's a heart character newRock.shape = (char)RockShape.Heart; newRock.color = ConsoleColor.Red; } else if (chance < 10) { newRock.shape = (char)RockShape.Money; newRock.color = ConsoleColor.Green; } else if (chance < 100) { newRock.shape = (char)RockShape.Rock; newRock.color = ConsoleColor.Cyan; } Rocks.Add(newRock); } //moving the dwarf if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); // In order to avoid the moving bug (If numerous keys are pressed, the program will execute each one) while (Console.KeyAvailable) { Console.ReadKey(true); } if (userInput.Key == ConsoleKey.LeftArrow && newDwarf.col > 0) { PrintOnPosition(newDwarf.col--, newDwarf.row, " ", newDwarf.color); } if (userInput.Key == ConsoleKey.RightArrow && newDwarf.col < Console.WindowWidth - 2) { PrintOnPosition(newDwarf.col++, newDwarf.row, " ", newDwarf.color); } } PrintOnPosition(newDwarf.col, newDwarf.row, newDwarf.shape, newDwarf.color); // The new list is made in order to add the next position of the rock which is y + 1. Thus making it fall List<Rock> newList = new List<Rock>(); for (int i = 0; i < Rocks.Count; i++) { Rock oldRock = Rocks[i]; Rock newRock = new Rock(); newRock.col = oldRock.col; newRock.row = oldRock.row + 1; newRock.shape = oldRock.shape; newRock.color = oldRock.color; // check if we get hit if ((newRock.col == newDwarf.col || newRock.col == newDwarf.col + 1 || newRock.col == newDwarf.col + 2) && newRock.row == newDwarf.row) { switch (newRock.shape) { case '@': newHitPosition = (HitPosition)Enum.Parse(typeof(HitPosition), ((newRock.col - newDwarf.col) + 1).ToString()); break; case '$': score += 500; break; case (char)3: lives += 1; break; } } // Add the new object in the newList until it reaches the end of the screen if (newRock.row < Console.WindowHeight) { newList.Add(newRock); } } // consequences for being hit if (newHitPosition != HitPosition.None) { PrintOnPosition(newDwarf.col, newDwarf.row, hitshapes[(int)newHitPosition], ConsoleColor.Red); // checks if you have 1 live left. Else the game will continue until you reach -1 lives if (lives < 2) { PrintOnPosition(0, 0, "GAME OVER!", ConsoleColor.Red); // We print the X in order to see the place of casualty PrintOnPosition(17, 0, ' ', ConsoleColor.Black); Console.ReadLine(); PrintOnPosition(0, 0, @" ", ConsoleColor.Red); return; } // If we still have lives, the game continues PrintOnPosition(0, 0, "Press enter", ConsoleColor.Red); // this is made in order to avoid a bug in which when you press some letter, it prints it on the console // on the last position the cursor was PrintOnPosition(17, 0, ' ', ConsoleColor.Black); Console.ReadLine(); // Clear the positions of the old objects PrintOnPosition(0, 0, @" ", ConsoleColor.Red); foreach (Rock rock in Rocks) { PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black); } Rocks.Clear(); newList.Clear(); // Consequences lives--; sleeptime += 20; } // This basically makes the rocks move. The idea is - Clear the old rocks and make the new ones foreach (Rock rock in Rocks) { PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black); } // The object Rocks takes the value of the new list which contains the positions of the new rocks Rocks = newList; foreach (Rock rock in Rocks) { PrintOnPosition(rock.col, rock.row, rock.shape, rock.color); } // The score and lives PrintOnPosition(7, 2, "Score: " + (int)score, ConsoleColor.Cyan); PrintOnPosition(7, 3, "Lives: " + lives, ConsoleColor.Yellow); // Making the score increase and the speed of the game increase constantly score += 14.66; /* sleeptime -= 0.5; if (sleeptime < 100) { sleeptime = 100; } if (sleeptime > 200) { sleeptime = 200; }*/ // Set the speed of the program Thread.Sleep(150); } }
private static void ResetRock(Rock r) { r.X = Helpers.RandomNumber(0, Console.BufferWidth - 1); r.Y = 0; r.IsActive = false; points++; }
public RockRow() { row = new Rock[5]; rowLength = 0; }
static void Main() { Console.Title = "Falling Rocks"; Console.WindowWidth = GAME_WIDTH; Console.BufferWidth = GAME_WIDTH; Console.WindowHeight = GAME_HEIGHT + 1; Console.BufferHeight = GAME_HEIGHT + 1; Console.CursorVisible = false; List <Rock> rocks = new List <Rock>(); while (true) { Console.SetCursorPosition(1, 1); Console.WriteLine("SCORE:{0} ", SCORE); if (rocks.Any(rock => (rock.X >= PLAYER_X && rock.X <= PLAYER_X + PLAYER.Length && rock.Y == PLAYER_Y))) { Console.SetCursorPosition(GAME_WIDTH / 2 - 20, GAME_HEIGHT / 2); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("GAME OVER - SOCRE: {0}", SCORE); return; } foreach (var rock in rocks) { Console.ForegroundColor = rock.elColor; Console.SetCursorPosition(rock.X, rock.Y); Console.WriteLine(rock.symbol); } Console.SetCursorPosition(PLAYER_X, PLAYER_Y); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(PLAYER); if (Console.KeyAvailable) { if (Console.ReadKey().Key == ConsoleKey.LeftArrow) { if (PLAYER_X > 0) { PLAYER_X--; } } else { if (PLAYER_X < GAME_WIDTH - 4) { PLAYER_X++; } } } rocks.Add(Rock.CreateRandomRock(GAME_WIDTH, GAME_HEIGHT)); for (int i = 0; i < rocks.Count; i++) { if (rocks[i].Y == GAME_HEIGHT) { rocks.Remove(rocks[i]); } rocks[i].FallTheRock(); } Thread.Sleep(150); Console.Clear(); SCORE += 10; } }
static Rock CreateRock() { Rock r = new Rock(); //TODO: make it random sign int sign = randomGenerator.Next(1, 12); switch (sign) { case 1: r.Sign = '^'; break; case 2: r.Sign = '@'; break; case 3: r.Sign = '*'; break; case 4: r.Sign = '&'; break; case 5: r.Sign = '+'; break; case 6: r.Sign = '%'; break; case 7: r.Sign = '$'; break; case 8: r.Sign = '#'; break; case 9: r.Sign = '!'; break; case 10: r.Sign = '.'; break; case 11: r.Sign = ';'; break; } r.Width = randomGenerator.Next(1, 5); r.Left = randomGenerator.Next(0, WIDTH - r.Width + 1); int color = randomGenerator.Next(1, 6); switch (color) { case 1: r.Color = ConsoleColor.Blue; break; case 2: r.Color = ConsoleColor.Yellow; break; case 3: r.Color = ConsoleColor.Magenta; break; case 4: r.Color = ConsoleColor.Green; break; case 5: r.Color = ConsoleColor.Cyan; break; } return r; }
static void Main(string[] args) { Console.BufferHeight = Console.WindowHeight = 20; Console.BufferWidth = Console.WindowWidth = 45; Console.BackgroundColor = ConsoleColor.Gray; Player player = new Player(Console.WindowWidth / 3, Console.WindowHeight - 1, 'O', ConsoleColor.DarkRed); GameInfo[] stats = new GameInfo[2]; stats[0] = new GameInfo((Console.WindowWidth / 3) * 2, 10, "LIVES", 5, ConsoleColor.Black); stats[1] = new GameInfo((Console.WindowWidth / 3) * 2, 4, "POINTS", 0, ConsoleColor.Black); GameInfo gameOver = new GameInfo(Console.WindowWidth / 2, Console.WindowHeight / 2, "GAME OVER", 0, ConsoleColor.Red); List <Rock> rocks = new List <Rock>(); while (true) { //Move player if (Console.KeyAvailable) { ConsoleKeyInfo pressed = Console.ReadKey(true); while (Console.KeyAvailable) { Console.ReadKey(true); } if (pressed.Key == ConsoleKey.LeftArrow) { if (player.X - 1 >= 0) { player.X -= 1; } } else if (pressed.Key == ConsoleKey.RightArrow) { if (player.X + 1 < (Console.WindowWidth / 3) * 2) { player.X += 1; } } } //Create random rock Rock newRock = new Rock(); newRock.SetRandom((Console.WindowWidth / 3) * 2); rocks.Add(newRock); //Move rocks for (int rock = 0; rock < rocks.Count; rock++) { rocks[rock].Y += 1; if (rocks[rock].Y >= Console.WindowHeight) { rocks.Remove(rocks[rock]); stats[1].Amount += 1; } //Check if collision if (rocks[rock].X == player.X && rocks[rock].Y == player.Y) { player.Symbol = 'X'; Console.Write((char)7); stats[0].Amount -= 1; rock--; if (stats[0].Amount <= 0) { Console.Clear(); gameOver.Print(); Console.ReadKey(); return; } } } //Clear console Console.Clear(); //Redraw field player.Print(); if (player.Symbol == 'X') { player.Symbol = 'O'; } foreach (var rock in rocks) { rock.Print(); } foreach (var stat in stats) { stat.Print(); } //Draw Info Thread.Sleep(220); } }
public static bool checkGameState(Rock rock, int heroPosition) { bool gameOver = false; if (rock.y == 18 // Collision where Hero is && rock.x == heroPosition) { gameOver = true; } return gameOver; }
static void Main() { Battlefield battlefield = new Battlefield(); Instructions(); Dwarf hero = new Dwarf(); Rock rock = new Rock(); DateTime start = DateTime.Now; int score = 0; while (true) { hero.MoveDwarf(); if (rock.ReignChaos(hero.X)) { Thread.Sleep(1500); Console.Clear(); break; } Thread.Sleep(150); } DateTime end = DateTime.Now; score = hero.TrackScore(start, end); Console.SetCursorPosition(7, 9); Console.WriteLine("G A M E O V E R"); Console.SetCursorPosition(7, 10); Console.WriteLine("Your score: {0}", score); }
private static void ClearRock(Rock rock, char ch = ' ', ConsoleColor color = ConsoleColor.Black) { Console.SetCursorPosition(rock.BoundaryX, rock.BoundaryY); Console.ForegroundColor = color; Console.Write(ch); }
private static void MoveRocks() { for (int p = 0; p < _rocks.Count; p++) { int check1 = _rocks[p].BoundaryY; int check = _rocks[p].BoundaryY + 4; if (_rocks[p].BoundaryY < 0) { _rocks[p] = new Rock(_rocks[p].BoundaryX, _rocks[p].BoundaryY + 4); } else if (_rocks[p].BoundaryY == WindowHeight - 1) { ClearRock(_rocks[p]); _rocks[p] = new Rock(random.Next(0, WindowWidth - GameMenuWidth), 0); colors[p] = _possibleColors[random.Next(0, _possibleColors.Count)]; chars[p] = _possibleSymbols[random.Next(0, _possibleSymbols.Count)]; } else if (check > WindowHeight - 1) { ClearRock(_rocks[p]); _rocks[p] = new Rock(_rocks[p].x, WindowHeight - 1); } else { ClearRock(_rocks[p]); _rocks[p] = new Rock(_rocks[p].BoundaryX, _rocks[p].BoundaryY + 4); GenerateRock(_rocks[p], chars[p], colors[p]); } } gameScore = gameScore + 9; }
static void Main(string[] args) { Console.BufferHeight = Console.WindowHeight = 20; Console.BufferWidth = Console.WindowWidth = 45; Console.BackgroundColor = ConsoleColor.Gray; Player player = new Player(Console.WindowWidth / 3, Console.WindowHeight - 1, 'O', ConsoleColor.DarkRed); GameInfo[] stats = new GameInfo[2]; stats[0] = new GameInfo((Console.WindowWidth / 3) * 2, 10, "LIVES", 5, ConsoleColor.Black); stats[1] = new GameInfo((Console.WindowWidth / 3) * 2, 4, "POINTS", 0, ConsoleColor.Black); GameInfo gameOver = new GameInfo(Console.WindowWidth / 2, Console.WindowHeight / 2, "GAME OVER", 0, ConsoleColor.Red); List<Rock> rocks = new List<Rock>(); while (true) { //Move player if (Console.KeyAvailable) { ConsoleKeyInfo pressed = Console.ReadKey(true); while (Console.KeyAvailable) Console.ReadKey(true); if (pressed.Key == ConsoleKey.LeftArrow) { if (player.X - 1 >= 0) player.X -= 1; } else if (pressed.Key == ConsoleKey.RightArrow) { if (player.X + 1 < (Console.WindowWidth / 3) * 2) player.X += 1; } } //Create random rock Rock newRock = new Rock(); newRock.SetRandom((Console.WindowWidth / 3) * 2); rocks.Add(newRock); //Move rocks for (int rock = 0; rock < rocks.Count; rock++) { rocks[rock].Y += 1; if (rocks[rock].Y >= Console.WindowHeight) { rocks.Remove(rocks[rock]); stats[1].Amount += 1; } //Check if collision if (rocks[rock].X == player.X && rocks[rock].Y == player.Y) { player.Symbol = 'X'; Console.Write((char)7); stats[0].Amount -= 1; rock--; if (stats[0].Amount <= 0) { Console.Clear(); gameOver.Print(); Console.ReadKey(); return; } } } //Clear console Console.Clear(); //Redraw field player.Print(); if (player.Symbol == 'X') player.Symbol = 'O'; foreach (var rock in rocks) { rock.Print(); } foreach (var stat in stats) { stat.Print(); } //Draw Info Thread.Sleep(220); } }
static void Main() { Console.CursorVisible = false; Console.BufferHeight = Console.WindowHeight = 18; Console.WindowWidth = 20; Console.BufferWidth = 20; Random randomGenerator = new Random(); double sleeptime = 200; double score = 0; int lives = 5; Dwarf newDwarf = new Dwarf(); newDwarf.col = 9; newDwarf.row = Console.WindowHeight - 1; newDwarf.color = ConsoleColor.Gray; List<Rock> Rocks = new List<Rock>(); PrintOnPosition(0, 5, " The aim of the game is to avoid all the rocks. Good luck!", ConsoleColor.Yellow); PrintOnPosition(0, 9, "Pick", ConsoleColor.Yellow); PrintOnPosition(5, 9, (char)3, ConsoleColor.Red); PrintOnPosition(7, 9, "for lives", ConsoleColor.Yellow); PrintOnPosition(0, 10, "Pick", ConsoleColor.Yellow); PrintOnPosition(5, 10, '$', ConsoleColor.Green); PrintOnPosition(7, 10, "for money", ConsoleColor.Yellow); PrintOnPosition(5, 13, "Good luck!", ConsoleColor.Red); PrintOnPosition(17, 0, ' ', ConsoleColor.Black); Console.ReadLine(); Console.Clear(); for (int i = 0; i <= Console.WindowWidth - 1; i++) { PrintOnPosition(i, 4, '_', ConsoleColor.White); } while (true) { HitPosition newHitPosition = HitPosition.None; int chance = randomGenerator.Next(0, 100); { Rock newRock = new Rock(); newRock.col = randomGenerator.Next(0, 19); newRock.row = 5; if (chance < 1) { newRock.shape = (char)RockShape.Heart; newRock.color = ConsoleColor.Red; } else if (chance < 10) { newRock.shape = (char)RockShape.Money; newRock.color = ConsoleColor.Green; } else if (chance < 100) { newRock.shape = (char)RockShape.Rock; newRock.color = ConsoleColor.Cyan; } Rocks.Add(newRock); } if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); while (Console.KeyAvailable) { Console.ReadKey(true); } if (userInput.Key == ConsoleKey.LeftArrow && newDwarf.col > 0) { PrintOnPosition(newDwarf.col--, newDwarf.row, " ", newDwarf.color); } if (userInput.Key == ConsoleKey.RightArrow && newDwarf.col < Console.WindowWidth - 2) { PrintOnPosition(newDwarf.col++, newDwarf.row, " ", newDwarf.color); } } PrintOnPosition(newDwarf.col, newDwarf.row, newDwarf.shape, newDwarf.color); List<Rock> newList = new List<Rock>(); for (int i = 0; i < Rocks.Count; i++) { Rock oldRock = Rocks[i]; Rock newRock = new Rock(); newRock.col = oldRock.col; newRock.row = oldRock.row + 1; newRock.shape = oldRock.shape; newRock.color = oldRock.color; if ((newRock.col == newDwarf.col || newRock.col == newDwarf.col + 1 || newRock.col == newDwarf.col + 2) && newRock.row == newDwarf.row) { switch (newRock.shape) { case '@': newHitPosition = (HitPosition)Enum.Parse(typeof(HitPosition), ((newRock.col - newDwarf.col) + 1).ToString()); break; case '$': score += 500; break; case (char)3: lives += 1; break; } } if (newRock.row < Console.WindowHeight) { newList.Add(newRock); } } if (newHitPosition != HitPosition.None) { PrintOnPosition(newDwarf.col, newDwarf.row, hitshapes[(int)newHitPosition], ConsoleColor.Red); if (lives < 2) { PrintOnPosition(0, 0, "GAME OVER!", ConsoleColor.Red); PrintOnPosition(17, 0, ' ', ConsoleColor.Black); Console.ReadLine(); PrintOnPosition(0, 0, @" ", ConsoleColor.Red); return; } PrintOnPosition(0, 0, "Press enter", ConsoleColor.Red); PrintOnPosition(17, 0, ' ', ConsoleColor.Black); Console.ReadLine(); PrintOnPosition(0, 0, @" ", ConsoleColor.Red); foreach (Rock rock in Rocks) { PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black); } Rocks.Clear(); newList.Clear(); lives--; sleeptime += 20; } foreach (Rock rock in Rocks) { PrintOnPosition(rock.col, rock.row, ' ', ConsoleColor.Black); } Rocks = newList; foreach (Rock rock in Rocks) { PrintOnPosition(rock.col, rock.row, rock.shape, rock.color); } PrintOnPosition(7, 2, "Score: " + (int)score, ConsoleColor.Cyan); PrintOnPosition(7, 3, "Lives: " + lives, ConsoleColor.Yellow); score += 14.66; sleeptime -= 0.5; if (sleeptime < 100) { sleeptime = 100; } if (sleeptime > 200) { sleeptime = 200; } Thread.Sleep((int)sleeptime); } }
public bool Overlap(Rock r) { if (Console.WindowHeight - 1 == r.y) { if (x == r.x || x + 1 == r.x || x + 2 == r.x) { return true; } } return false; }
private static void StartGame() { Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.WindowWidth; Random randomColumn = new Random(); int rowInConsole = 0; int columnInConsole = 0; // Initialize Player InitializePlayerAndRocks initializePlayerAndRocks = new InitializePlayerAndRocks(); Player player = initializePlayerAndRocks.InitializePlayer((Console.WindowHeight - 1), (Console.WindowWidth / 2)); FallingRocksGameManager fallingRockManager = new FallingRocksGameManager(); Painter drawPlayerAndRocks = new Painter(); UserControl userControl = new UserControl(); Rock rock = new Rock(); List<Rock> rocks = new List<Rock>(); while (true) { player.Position = userControl.PlayerControlsListener(player); drawPlayerAndRocks.DrawPlayerOnScreen(player); // Create rock and give it random position on screen columnInConsole = randomColumn.Next(0, Console.WindowWidth); rock = initializePlayerAndRocks.InitializeRock(columnInConsole, rowInConsole); rocks.Add(rock); bool isGameOver = fallingRockManager.IsGameOver(player, rocks); if (isGameOver) { Console.SetCursorPosition(0, 0); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Game over!"); break; } // If the rock reach to the lowest part of the screen it remove! for (int i = 0; i < rocks.Count; i++) { if (rocks[i].Position.Y == (Console.WindowHeight - 1)) { Console.SetCursorPosition(rocks[i].Position.X, rocks[i].Position.Y); Console.Clear(); //redraw player drawPlayerAndRocks.DrawPlayerOnScreen(player); rocks.RemoveAt(i); } } drawPlayerAndRocks.EraseTailOfRock(rocks); fallingRockManager.SimulateFalling(rocks); drawPlayerAndRocks.DrawAllRocksOnScreen(rocks); Thread.Sleep(SPEED); } }