Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * * Implement the "Falling Rocks" game in the text console. A small dwarf stays at the bottom of the screen and can move left and right (by the arrows keys). A number of rocks of different sizes and forms constantly fall down and you need to avoid a crash.
- *
- * The game is based on JustCars by Niki's demo
- *
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- struct Object
- {
- public int x;
- public int y;
- public char c;
- public ConsoleColor color;
- }
- class FallingRocks
- {
- static void PrintOnPosition(int x, int y, char c,
- ConsoleColor color = ConsoleColor.Gray)
- {
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = color;
- Console.Write(c);
- }
- static void PrintStringOnPosition(int x, int y, string str,
- ConsoleColor color = ConsoleColor.Gray)
- {
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = color;
- Console.Write(str);
- }
- static void Main()
- {
- long scores = 0;
- int speed = 100;
- int acceleration = 1;
- //byte level = 1;
- int playGroundWidht = 30;
- int livesCount = 3;
- Console.BufferHeight = Console.WindowHeight = 20;
- Console.BufferWidth = Console.WindowWidth = 60;
- Object dwarf = new Object();
- dwarf.x = 15;
- dwarf.y = Console.WindowHeight - 1;
- dwarf.c = 'O';
- dwarf.color = ConsoleColor.Cyan;
- char[] rockSymbol = new char[11];
- rockSymbol[1] = '@';
- rockSymbol[2] = '*';
- rockSymbol[3] = '&';
- rockSymbol[4] = '+';
- rockSymbol[5] = '%';
- rockSymbol[6] = '$';
- rockSymbol[7] = '#';
- rockSymbol[8] = '!';
- rockSymbol[9] = '.';
- rockSymbol[10] = ';';
- Random randomGenerator = new Random();
- List<Object> objects = new List<Object>();
- while (true)
- {
- scores = scores + 1;
- if(scores <= 1000 && scores <= 2000)
- {
- acceleration = 1;
- }
- else if (scores < 2000 && scores <= 5000)
- {
- acceleration = 2;
- }
- else if (scores < 5000 && scores <= 10000)
- {
- acceleration = 3;
- }
- else if (scores < 10000 && scores <= 20000)
- {
- acceleration = 4;
- }
- else
- {
- acceleration = 5;
- }
- speed += acceleration;
- if (speed > 500)
- {
- speed = 500;
- }
- bool hitted = false;
- {
- int chance = randomGenerator.Next(0, 100);
- if (chance < 5)
- {
- Object newObject = new Object();
- newObject.color = ConsoleColor.DarkGreen;
- newObject.c = 'L'; // 1 live bonus
- newObject.x = randomGenerator.Next(0, playGroundWidht);
- newObject.y = 0;
- objects.Add(newObject);
- }
- else if (chance < 20)
- {
- Object newObject = new Object();
- newObject.color = ConsoleColor.Green;
- newObject.c = 'S'; // speed bonus
- newObject.x = randomGenerator.Next(0, playGroundWidht);
- newObject.y = 0;
- objects.Add(newObject);
- }
- else if (chance < 25)
- {
- Object newObject = new Object();
- newObject.color = ConsoleColor.Blue;
- newObject.c = 'D'; // scores * 2
- newObject.x = randomGenerator.Next(0, playGroundWidht);
- newObject.y = 0;
- objects.Add(newObject);
- }
- else if (chance < 30)
- {
- Object newObject = new Object();
- newObject.color = ConsoleColor.DarkYellow;
- newObject.c = 'B'; // bonus scores
- newObject.x = randomGenerator.Next(0, playGroundWidht);
- newObject.y = 0;
- objects.Add(newObject);
- }
- else
- {
- Object newCar = new Object();
- newCar.color = ConsoleColor.Red;
- newCar.x = randomGenerator.Next(0, playGroundWidht);
- newCar.y = 0;
- newCar.c = rockSymbol[randomGenerator.Next(1, 10)]; // different rocks
- objects.Add(newCar);
- }
- }
- while (Console.KeyAvailable)
- {
- ConsoleKeyInfo pressedKey = Console.ReadKey(true);
- if (pressedKey.Key == ConsoleKey.LeftArrow)
- {
- if (dwarf.x - 1 >= 0)
- {
- dwarf.x = dwarf.x - 1;
- }
- }
- else if (pressedKey.Key == ConsoleKey.RightArrow)
- {
- if (dwarf.x + 1 < playGroundWidht)
- {
- dwarf.x = dwarf.x + 1;
- }
- }
- }
- List<Object> newList = new List<Object>();
- for (int i = 0; i < objects.Count; i++)
- {
- Object oldRock = objects[i];
- Object newRock = new Object();
- newRock.x = oldRock.x;
- newRock.y = oldRock.y + 1;
- newRock.c = oldRock.c;
- newRock.color = oldRock.color;
- if (newRock.c == 'S' && newRock.y == dwarf.y && newRock.x == dwarf.x)
- {
- speed -= 50;
- if (speed < 100)
- {
- speed = 100;
- }
- }
- if (newRock.c == 'L' && newRock.y == dwarf.y && newRock.x == dwarf.x)
- {
- livesCount++;
- }
- if (newRock.c == 'B' && newRock.y == dwarf.y && newRock.x == dwarf.x)
- {
- scores = scores + 10;
- }
- if (newRock.c == 'D' && newRock.y == dwarf.y && newRock.x == dwarf.x)
- {
- scores = scores + 20;
- }
- if ((newRock.c == '@' || newRock.c == '*' || newRock.c == '&' || newRock.c == '+' || newRock.c == '%' || newRock.c == '$' || newRock.c == '#' || newRock.c == '!' || newRock.c == '.' || newRock.c == ';') && newRock.y == dwarf.y && newRock.x == dwarf.x)
- {
- //Console.Beep();
- livesCount--;
- hitted = true;
- while(livesCount <= 0)
- {
- PrintStringOnPosition(40, 10, "GAME OVER!!!", ConsoleColor.Red);
- PrintStringOnPosition(40, 12, "New game -> N", ConsoleColor.Blue);
- PrintStringOnPosition(40, 14, "Exit -> [esc]", ConsoleColor.Red);
- ConsoleKeyInfo pressedKey = Console.ReadKey(true);
- if (pressedKey.Key == ConsoleKey.Escape)
- {
- Environment.Exit(0);
- }
- if (pressedKey.Key == ConsoleKey.N)
- {
- Main();
- }
- }
- }
- if (newRock.y < Console.WindowHeight)
- {
- newList.Add(newRock);
- }
- }
- objects = newList;
- Console.Clear();
- if (hitted)
- {
- objects.Clear();
- if (speed > 200)
- {
- speed = 150;
- }
- PrintOnPosition(dwarf.x, dwarf.y, 'X', ConsoleColor.Red);
- }
- else
- {
- PrintOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
- }
- foreach (Object car in objects)
- {
- PrintOnPosition(car.x, car.y, car.c, car.color);
- }
- PrintStringOnPosition(40, 4, "Scores: " + scores, ConsoleColor.White);
- PrintStringOnPosition(40, 5, "Lives: " + livesCount, ConsoleColor.White);
- PrintStringOnPosition(40, 6, "Speed: " + speed, ConsoleColor.White);
- PrintStringOnPosition(40, 7, "Acceleration: " + acceleration + "/5", ConsoleColor.White);
- //PrintStringOnPosition(40, 8, "Level: " + level + "/10", ConsoleColor.White);
- Thread.Sleep((int)(600 - speed));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment