Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using System.Text;
- using System.Threading;
- class Exercise11FallingRocks
- {
- public struct Position
- {
- public int row;
- public int col;
- public char randomChar;
- public ConsoleColor color;
- public Position(int row, int col, ConsoleColor color, char randomChar)
- {
- this.row = row;
- this.col = col;
- this.color = color;
- this.randomChar = randomChar;
- }
- }
- public static char[] rocksChar = { '@', '*', '&', '+', '%', '$', '#', '!', '.', ';' };
- public static string[] colors = { "Cyan", "Green", "Blue", "DarkCyan", "DarkGreen", "DarkYellow" };
- public static Random randomNum = new Random();
- public static List<Position> rocks = new List<Position>();
- public static List<Position> gun = new List<Position>();
- public static ConsoleColor color;
- public static int width;
- public static int height;
- public static int rockDensity;
- static void AddNewRow(int tempRow)
- {
- for (int y = 0; y < width; y++)
- {
- if (randomNum.Next(rockDensity) < 1)
- {
- if (randomNum.Next(30) < 1)
- {
- if (randomNum.Next(2) < 1)
- {
- rocks.Add(new Position(tempRow, y, ConsoleColor.Yellow, '\u2660'));
- }
- else
- {
- rocks.Add(new Position(tempRow, y, ConsoleColor.Red, (char)3));
- }
- }
- else
- {
- color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colors[randomNum.Next(colors.Length)]);
- rocks.Add(new Position(tempRow, y, color, rocksChar[randomNum.Next(rocksChar.Length)]));
- // add duplicate rocks
- if (randomNum.Next(9) < 1 && y > 1 && y < width - 1)
- {
- y = y + 1;
- rocks.Add(new Position(tempRow, y, rocks[rocks.Count - 1].color, rocks[rocks.Count - 1].randomChar));
- if (randomNum.Next(12) < 1 && y > 1 && y < width - 1)
- {
- y = y + 1;
- rocks.Add(new Position(tempRow, y, rocks[rocks.Count - 1].color, rocks[rocks.Count - 1].randomChar));
- }
- }
- }
- }
- }
- }
- static void ShowCollision(ConsoleColor tempColor)
- {
- for (int u = height; u > 8; u -= 1)
- {
- Console.BackgroundColor = tempColor;
- Console.SetCursorPosition(0, u);
- Console.Write("".PadRight(width, ' '));
- Thread.Sleep(1);
- Console.BackgroundColor = ConsoleColor.Black;
- Console.SetCursorPosition(0, u);
- Console.Write("".PadRight(width, ' '));
- }
- }
- static void DrawHelp()
- {
- for (int i = 0; i <= height + 1; i++)
- {
- Console.SetCursorPosition(width, i);
- Console.Write("|");
- }
- int infoRow = 2;
- Console.SetCursorPosition(width + 10, infoRow++);
- Console.Write("Falling rocks");
- Console.SetCursorPosition(width + 7, infoRow++); infoRow++;
- Console.Write("by Konstantin Dikov");
- Console.SetCursorPosition(width + 2, infoRow++); infoRow++;
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("Keys:");
- Console.ForegroundColor = ConsoleColor.DarkMagenta;
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("Left - Left arrow");
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("Right - Right arrow");
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("Gun - Spacebar");
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("Blast - Enter");
- Console.ForegroundColor = ConsoleColor.DarkMagenta;
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("Up arrow - Increase density");
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("Down arrow - Decrease density"); infoRow++;
- Console.SetCursorPosition(width + 2, infoRow++); infoRow++;
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("Tips:");
- Console.ForegroundColor = ConsoleColor.DarkMagenta;
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("- Higher density = more points");
- Console.SetCursorPosition(width + 2, infoRow++);
- Console.Write("- Blast points x 4");
- }
- static void Main()
- {
- Console.CursorVisible = false;
- Console.Title = "Falling Rocks - by Konstantin Dikov";
- ConsoleKey keyPressed;
- Console.BufferWidth = Console.WindowWidth = 82;
- Console.BufferHeight = Console.WindowHeight = 25;
- width = 48;
- height = 23;
- int offset = 5;
- int currentRow = 0;
- rockDensity = 39;
- int score = 0;
- int highScore = 0;
- int lifes = 5;
- int blasts = 5;
- int blastPosition = 0;
- bool gameOver = false;
- int scoreFormula = 0;
- string spaces = new string(' ', width);
- Position dwarf = new Position();
- dwarf.col = width / 2;
- dwarf.row = height;
- dwarf.color = ConsoleColor.White;
- string dwarfString = "(0)";
- //draw ground and ------
- Console.SetCursorPosition(0, 3);
- Console.Write("".PadRight(width, '-'));
- Console.SetCursorPosition(0, height + 1);
- Console.BackgroundColor = ConsoleColor.DarkRed;
- Console.Write("".PadRight(width, ' '));
- Console.BackgroundColor = ConsoleColor.Black;
- //
- DrawHelp();
- AddNewRow(currentRow);
- //check if file with highscore exist
- if (!File.Exists("highscore.txt"))
- {
- StreamWriter saveScoreFile = new StreamWriter("highscore.txt");
- saveScoreFile.WriteLine("0");
- saveScoreFile.Close();
- }
- else
- {
- StreamReader readHighScore = new StreamReader("highscore.txt");
- highScore = int.Parse(readHighScore.ReadLine());
- readHighScore.Close();
- }
- //
- double timeDelay = Environment.TickCount;
- double gunDelay = Environment.TickCount;
- while (!gameOver)
- {
- if (Environment.TickCount - timeDelay > 150)
- {
- //clear last row
- Console.SetCursorPosition(0, dwarf.row);
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(spaces);
- //draw dwarf just to prevent dwarf blinking
- Console.SetCursorPosition(dwarf.col, dwarf.row);
- Console.ForegroundColor = (dwarf.color);
- Console.Write(dwarfString);
- for (int i = 0; i < rocks.Count; i++)
- {
- if (offset - rocks[i].row == dwarf.row && rocks[i].col >= dwarf.col &&
- rocks[i].col <= dwarf.col + 2)
- {
- if (rocks[i].randomChar == '\u2660')
- {
- if (blasts < 6)
- {
- ShowCollision(ConsoleColor.Yellow);
- blasts = blasts + 1;
- }
- }
- else if (rocks[i].randomChar == (char)3)
- {
- if (lifes < 6)
- {
- ShowCollision(ConsoleColor.Green);
- lifes = lifes + 1;
- }
- }
- else
- {
- lifes = lifes - 1;
- if (lifes == 0)
- {
- gameOver = true;
- break;
- }
- else
- {
- ShowCollision(ConsoleColor.DarkRed);
- }
- }
- }
- // clear rocks
- Console.SetCursorPosition(rocks[i].col, offset - rocks[i].row - 1);
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(" ");
- // draw rocks
- Console.SetCursorPosition(rocks[i].col, offset - rocks[i].row);
- Console.ForegroundColor = (rocks[i].color);
- Console.Write(rocks[i].randomChar);
- }
- timeDelay = Environment.TickCount;
- scoreFormula = (109 - rockDensity) / 9;
- score = score + rocks.RemoveAll(r => r.row == offset - height) * scoreFormula;
- offset = offset + 1;
- currentRow = currentRow + 1;
- AddNewRow(currentRow);
- //draw info board: score, lifes and blasts
- Console.BackgroundColor = ConsoleColor.DarkRed;
- Console.ForegroundColor = ConsoleColor.White;
- Console.SetCursorPosition(0, 0);
- Console.WriteLine(" Score {0:D6} | High score {1:D6} | Density: {2:D2}%", score, highScore, 109 - rockDensity);
- Console.BackgroundColor = ConsoleColor.DarkMagenta;
- Console.SetCursorPosition(0, 1);
- Console.Write(" Lifes: ");
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("".PadRight(lifes, (char)3) + "".PadRight(6 - lifes, ' '));
- Console.ForegroundColor = ConsoleColor.White;
- Console.SetCursorPosition(13, 1);
- Console.Write(" | Gun: ");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Infinite ");
- Console.Write("Blasts: ");
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write("".PadRight(blasts, '\u2660') + "".PadRight(6 - blasts, ' '));
- Console.BackgroundColor = ConsoleColor.Black;
- if (gameOver) break;
- }
- if (Console.KeyAvailable)
- {
- keyPressed = Console.ReadKey(true).Key;
- if (keyPressed == ConsoleKey.LeftArrow && dwarf.col > 0)
- {
- //draw speed trail
- Console.SetCursorPosition(dwarf.col + 2, dwarf.row);
- Console.ForegroundColor = ConsoleColor.DarkGray;
- Console.Write(">");
- dwarf.col = dwarf.col - 1;
- }
- else if (keyPressed == ConsoleKey.RightArrow && dwarf.col < width - 3)
- {
- //draw speed trail
- Console.SetCursorPosition(dwarf.col, dwarf.row);
- Console.ForegroundColor = ConsoleColor.DarkGray;
- Console.Write("<");
- dwarf.col = dwarf.col + 1;
- }
- else if (keyPressed == ConsoleKey.DownArrow && rockDensity < 100)
- {
- rockDensity = rockDensity + 1;
- }
- else if (keyPressed == ConsoleKey.UpArrow && rockDensity > 10)
- {
- rockDensity = rockDensity - 1;
- }
- else if (keyPressed == ConsoleKey.Enter && blasts > 0)
- {
- for (int z = 0; z < 15; z++)
- {
- blastPosition = dwarf.col - z;
- if (dwarf.col >= width - z - 1)
- blastPosition = width - 2 * (z + 1);
- if (dwarf.col <= z)
- blastPosition = 0;
- Console.BackgroundColor = ConsoleColor.Yellow;
- Console.SetCursorPosition(blastPosition, dwarf.row - 1 - z);
- Console.Write(" ".PadRight(2 * (z + 1), ' '));
- score = score + scoreFormula * 4 * rocks.RemoveAll(r => (r.row == offset - (height - z)) &&
- (r.col >= blastPosition) &&
- (r.col < blastPosition + 2 * (z + 1)));
- Thread.Sleep(20);
- Console.BackgroundColor = ConsoleColor.Black;
- Console.SetCursorPosition(blastPosition, dwarf.row - 1 - z);
- Console.Write(" ".PadRight(2 * (z + 1), ' '));
- }
- blasts = blasts - 1;
- Console.BackgroundColor = ConsoleColor.Black;
- }
- else if (keyPressed == ConsoleKey.Spacebar)
- {
- gun.Add(new Position(dwarf.row - 1, dwarf.col + 1, ConsoleColor.Green, 'o'));
- Console.SetCursorPosition(dwarf.col + 1, dwarf.row - 1);
- Console.ForegroundColor = gun[gun.Count - 1].color;
- Console.Write(gun[gun.Count - 1].randomChar);
- }
- }
- //move bullets
- if (Environment.TickCount - gunDelay > 50)
- {
- for (int i = 0; i < gun.Count; i++)
- {
- if (rocks.RemoveAll(nn => (nn.row == offset - gun[i].row - 1) && (nn.col == gun[i].col)) > 0)
- {
- score = score + scoreFormula * 2;
- Console.SetCursorPosition(gun[i].col, gun[i].row);
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(" ");
- gun.RemoveAt(i);
- i--;
- }
- else if (rocks.RemoveAll(nn => (nn.row == offset - gun[i].row) && (nn.col == gun[i].col)) > 0)
- {
- score = score + scoreFormula * 2;
- Console.SetCursorPosition(gun[i].col, gun[i].row - 1);
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(" ");
- Console.SetCursorPosition(gun[i].col, gun[i].row);
- Console.Write(" ");
- gun.RemoveAt(i);
- i--;
- }
- else
- {
- Console.MoveBufferArea(gun[i].col, gun[i].row, 1, 1, gun[i].col, gun[i].row - 1);
- if (gun[i].row > 5)
- {
- gun[i] = new Position(gun[i].row - 1, gun[i].col, gun[i].color, gun[i].randomChar);
- }
- else
- {
- Console.SetCursorPosition(gun[i].col, gun[i].row - 1);
- Console.ForegroundColor = ConsoleColor.Black;
- Console.Write(" ");
- gun.RemoveAt(i);
- i--;
- }
- }
- }
- gunDelay = Environment.TickCount;
- }
- // draw dwarf
- Console.SetCursorPosition(dwarf.col, dwarf.row);
- Console.ForegroundColor = (dwarf.color);
- Console.Write(dwarfString);
- }
- Console.SetCursorPosition(width / 2 - 5, height / 2);
- Console.ForegroundColor = ConsoleColor.White;
- Console.BackgroundColor = ConsoleColor.Red;
- Console.WriteLine(" GAME OVER ");
- if (score > highScore)
- {
- Console.SetCursorPosition(width / 2 - 7, height / 2 + 1);
- Console.ForegroundColor = ConsoleColor.White;
- Console.BackgroundColor = ConsoleColor.DarkGreen;
- Console.WriteLine(" NEW HIGH SCORE ");
- }
- Console.SetCursorPosition(5, height / 2 + 3);
- Console.ForegroundColor = ConsoleColor.White;
- Console.BackgroundColor = ConsoleColor.Black;
- Console.Write("Press \"Y\" to play again or \"N\" to quit");
- while (true)
- {
- if (score > highScore)
- {
- StreamWriter saveScoreFile = new StreamWriter("highscore.txt");
- saveScoreFile.WriteLine(score);
- saveScoreFile.Close();
- }
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo newGame = Console.ReadKey(true);
- if (newGame.Key == ConsoleKey.Y)
- {
- rocks.RemoveAll(c => c.row >= 0);
- Console.Clear();
- Main();
- }
- if (newGame.Key == ConsoleKey.N)
- {
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement