Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Collections.Generic;
- class FallingRocks
- {
- static Random randomGenerator = new Random();
- static int score = 0;
- static void Main()
- {
- // Settings
- int windowHeight = 20;
- int windowWidth = 50;
- int speedRock = 200;
- int densityRocks = 2;
- Console.CursorVisible = false;
- Console.WindowHeight = windowHeight;
- Console.WindowWidth = windowWidth;
- Console.BufferWidth = Console.WindowWidth;
- Console.BufferHeight = Console.WindowHeight + 1;
- //conteiner for rocks
- List<Rock> rocks = new List<Rock>();
- Dwarf dwarf = new Dwarf(windowWidth / 2 - 1, windowHeight - 2);
- bool isPlay = true;
- while (isPlay)
- {
- Console.Clear();
- RocksGenerator(rocks, densityRocks);
- dwarf.Print();
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo pressedKey = Console.ReadKey();
- if (pressedKey.Key == ConsoleKey.LeftArrow)
- dwarf.MoveLeft(0);
- if (pressedKey.Key == ConsoleKey.RightArrow)
- dwarf.MoveRight(windowWidth);
- }
- for (int i = 0; i < rocks.Count; i++)
- {
- rocks[i].Print();
- rocks[i].Move();
- if (rocks[i].Y == dwarf.Y)
- {
- int rockStart = rocks[i].X;
- int rockEnd = rockStart + rocks[i].Size - 1;
- int dwarfStart = dwarf.X;
- int dwarfEnd = dwarf.X + 2;
- isPlay = rockEnd < dwarfStart || rockStart > dwarfEnd;
- }
- }
- RemoveRocksOutOfWindow(rocks);
- PrintScore(5, windowHeight - 1);
- Thread.Sleep(speedRock);
- }// end while
- Console.Clear();
- Console.WriteLine("Game Over!!!");
- Console.WriteLine("Your score: {0}", score);
- Console.ReadLine();
- }
- static void RocksGenerator(List<Rock> rocks, int densityRocks)
- {
- int randomNumber = randomGenerator.Next(1, densityRocks);
- for (int i = 0; i < randomNumber; i++)
- {
- int randomXPosition = randomGenerator.Next(1, Console.WindowWidth);
- rocks.Add(new Rock(randomXPosition));
- }
- }
- static void RemoveRocksOutOfWindow(List<Rock> rocks)
- {
- score++;
- for (int i = 0; i < rocks.Count; i++)
- {
- if (rocks[i].Y >= Console.WindowHeight - 1)
- {
- rocks.RemoveAt(i);
- }
- }
- }
- static void PrintScore(int x, int y)
- {
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("Score: {0}", score.ToString().PadLeft(5, '0'));
- }
- }
- class Rock
- {
- int x;
- int y;
- string view;
- ConsoleColor color;
- static Random randomGenerator = new Random();
- static ConsoleColor[] colors = { ConsoleColor.Cyan, ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.White };
- public Rock(int x = 0)
- {
- this.x = x;
- this.y = 0;
- string rockSymbols = "^@*&+%$#!.;-";
- int symbolPosition = randomGenerator.Next(0, rockSymbols.Length - 1);
- int size = randomGenerator.Next(1, 4);
- this.view = new String(rockSymbols[symbolPosition], size);
- color = colors[randomGenerator.Next(0, 3)];
- }
- public void Print()
- {
- Console.SetCursorPosition(this.x, this.y);
- Console.ForegroundColor = color;
- Console.Write(this.view);
- }
- public void Move()
- {
- this.y++;
- }
- public int X
- {
- get
- {
- return this.x;
- }
- set
- {
- this.x = value;
- }
- }
- public int Y
- {
- get
- {
- return this.y;
- }
- set
- {
- this.y = value;
- }
- }
- public int Size
- {
- get
- {
- return this.view.Length;
- }
- }
- }
- class Dwarf
- {
- int x;
- int y;
- string view;
- ConsoleColor color = ConsoleColor.White;
- public Dwarf(int x, int y)
- {
- this.x = x;
- this.y = y;
- this.view = "(O)";
- }
- public void Print()
- {
- Console.SetCursorPosition(this.x, this.y);
- Console.ForegroundColor = color;
- Console.Write(this.view);
- }
- public void MoveLeft(int minX)
- {
- if (this.x > minX)
- {
- this.x--;
- }
- }
- public void MoveRight(int maxX)
- {
- if (this.x < maxX - this.view.Length)
- {
- this.x++;
- }
- }
- public int X
- {
- get
- {
- return this.x;
- }
- set
- {
- this.x = value;
- }
- }
- public int Y
- {
- get
- {
- return this.y;
- }
- set
- {
- this.y = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement