Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- struct Objects
- {
- public int col;
- public int row;
- public string c;
- public ConsoleColor color;
- }
- class FallingROcks
- {
- static void PrintCharOnPosition(int col, int row, string c, ConsoleColor color = ConsoleColor.White)
- {
- Console.SetCursorPosition(col, row);
- Console.ForegroundColor = color;
- Console.Write(c);
- }
- static void Main()
- {
- Console.BufferHeight = Console.WindowHeight = 40;
- Console.BufferWidth = Console.WindowWidth = 60;
- int playGround = 20;
- int lives = 3;
- int rounds = 1;
- Objects myRock = new Objects();
- myRock.col = playGround / 2 - 1;
- myRock.row = Console.WindowHeight - 1;
- myRock.c = "(0)";
- myRock.color = ConsoleColor.White;
- Random randomNumber = new Random();
- string[] rocksChar = { "*", "&", "$", "#", "@", "%" };
- ConsoleColor[] rocksColors = { ConsoleColor.Cyan, ConsoleColor.Blue, ConsoleColor.Yellow, ConsoleColor.White, ConsoleColor.Red };
- List<Objects> objectList = new List<Objects>();
- while (true)
- {
- bool isHitted = false;
- int rocksGenerator = randomNumber.Next(0, 5);
- if (lives == 0)
- {
- PrintCharOnPosition(15, 10, "GAME OVER !!!", ConsoleColor.Red);
- Console.WriteLine();
- return;
- }
- string displayLives = String.Format("LIVES: {0}", lives);
- string displayRounds = String.Format("ROUNDS: {0}", rounds);
- PrintCharOnPosition(30, 10, displayLives, ConsoleColor.Yellow);
- PrintCharOnPosition(30, 8, displayRounds, ConsoleColor.Green);
- for (int i = 0; i < rocksGenerator; i++)
- {
- Objects FallingRocks = new Objects();
- FallingRocks.col = randomNumber.Next(0, playGround + 1);
- FallingRocks.row = 0;
- FallingRocks.c = rocksChar[randomNumber.Next(0, rocksChar.Length)];
- FallingRocks.color = rocksColors[randomNumber.Next(0, rocksColors.Length)];
- objectList.Add(FallingRocks);
- }
- while (Console.KeyAvailable)
- {
- ConsoleKeyInfo pressedKey = Console.ReadKey(true);
- if (pressedKey.Key == ConsoleKey.LeftArrow)
- {
- if (myRock.col - 1 >= 0)
- {
- myRock.col = myRock.col - 1;
- }
- }
- else if (pressedKey.Key == ConsoleKey.RightArrow)
- {
- if (myRock.col + 2 <= playGround - 1)
- {
- myRock.col = myRock.col + 1;
- }
- }
- }
- List<Objects> newObjectList = new List<Objects>();
- for (int i = 0; i < objectList.Count; i++)
- {
- Objects oldRock = objectList[i];
- Objects newFallingRocks = new Objects();
- newFallingRocks.col = oldRock.col;
- newFallingRocks.row = oldRock.row + 1;
- newFallingRocks.c = oldRock.c;
- newFallingRocks.color = oldRock.color;
- if (newFallingRocks.row == myRock.row && ((newFallingRocks.col == myRock.col) || newFallingRocks.col == myRock.col + 1 || newFallingRocks.col == myRock.col + 2))
- {
- isHitted = true;
- }
- if (newFallingRocks.row < Console.WindowHeight)
- {
- newObjectList.Add(newFallingRocks);
- }
- }
- objectList = newObjectList;
- for (int i = 0; i < Console.WindowHeight; i++)
- {
- PrintCharOnPosition(playGround + 1, i, "|", ConsoleColor.Red);
- }
- foreach (Objects rock in objectList)
- {
- PrintCharOnPosition(rock.col, rock.row, rock.c, rock.color);
- }
- PrintCharOnPosition(myRock.col, myRock.row, myRock.c, myRock.color);
- if (isHitted)
- {
- objectList.Clear();
- PrintCharOnPosition(myRock.col, myRock.row, "***", ConsoleColor.Red);
- Thread.Sleep(50);
- lives--;
- rounds++;
- }
- Thread.Sleep(200);
- Console.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment