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;
- namespace BrickGame
- {
- class Program
- {
- static string[] board;
- const string PLAYER = "^";
- const string OBSTACLE = "#";
- const string NITRO = "N";
- const string POINTS = "P";
- static int points = 0;
- static int highestScore = 0;
- static void Main(string[] args)
- {
- string reset;
- do
- {
- int playerPosition = 1;
- Random generatorRandom = new Random();
- bool isStruck = false;
- int speed = 0;
- int nitroTime = -1;
- bool isNitroFuel = false;
- NewBoard(10);
- SetPlayer(playerPosition);
- ShowBoard();
- //Loop
- while (!isStruck)
- {
- //controls
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo keyPressed = Console.ReadKey(true);
- if (keyPressed.Key == ConsoleKey.RightArrow)
- {
- if (playerPosition < 2)
- {
- playerPosition++;
- }
- }
- if (keyPressed.Key == ConsoleKey.LeftArrow)
- {
- if (playerPosition > 0)
- {
- playerPosition--;
- }
- }
- if (keyPressed.Key == ConsoleKey.Spacebar && isNitroFuel)
- {
- if (nitroTime == -1)
- {
- nitroTime = 10;
- isNitroFuel = false;
- }
- }
- while (Console.KeyAvailable)
- {
- Console.ReadKey(false);
- }
- }
- if (nitroTime == 10)
- {
- speed += 200;
- nitroTime--;
- }
- else if (nitroTime > 0)
- {
- nitroTime--;
- }
- else if (nitroTime == 0)
- {
- speed -= 200;
- nitroTime = -1;
- }
- //Collision check
- int nearestObstaclePosition = board[board.Length - 2].IndexOf(OBSTACLE);
- if (playerPosition == nearestObstaclePosition)
- {
- isStruck = true;
- }
- else
- {
- points++;
- }
- int nearestNitroPosition = board[board.Length - 2].IndexOf(NITRO);
- if (playerPosition == nearestNitroPosition)
- {
- isNitroFuel = true;
- }
- int nearestPointPosition = board[board.Length - 2].IndexOf(POINTS);
- if (playerPosition == nearestPointPosition)
- {
- points += 10;
- }
- //Creating a new obstacle
- int obstaclePosition = generatorRandom.Next(3);
- string obstacle = SetObstacle(obstaclePosition);
- if (generatorRandom.Next(20) == 0)
- {
- int nitroPosition = generatorRandom.Next(3);
- obstacle = SetNitro(nitroPosition, obstacle);
- }
- if (generatorRandom.Next(20) == 0)
- {
- int pointPosition = generatorRandom.Next(3);
- obstacle = SetPoints(pointPosition, obstacle);
- }
- //Moving the game board down
- for (int i = board.Length - 2; i > 0; i--)
- {
- board[i] = board[i - 1];
- }
- board[0] = obstacle;
- SetPlayer(playerPosition);
- ShowBoard();
- Console.WriteLine($"Nitro: {isNitroFuel}");
- speed++;
- if (speed > 600)
- {
- Thread.Sleep(1);
- }
- else
- {
- Thread.Sleep(600 - speed);
- }
- }
- Console.Clear();
- Console.WriteLine("GAME OVER");
- Console.WriteLine($"You have earned {points} points");
- if(points > highestScore)
- {
- Console.WriteLine("NEW HIGHEST SCORE!!!");
- highestScore = points;
- }
- Thread.Sleep(2000);
- Console.WriteLine("Do you want to play again?");
- reset = Console.ReadLine();
- points = 0;
- } while (reset == "yes" || reset == "Yes");
- Console.ReadKey();
- }
- private static void NewBoard(int boardSize)
- {
- board = new string[boardSize];
- for (int i = 0; i < board.Length; i++)
- {
- board[i] = "";
- }
- }
- private static string SetObstacle(int position)
- {
- string line = " ";
- line = line.Insert(position, OBSTACLE);
- return line;
- }
- private static string SetNitro(int position, string line)
- {
- line = line.Remove(position,1).Insert(position, NITRO);
- return line;
- }
- private static string SetPoints(int position, string line)
- {
- line = line.Insert(position, POINTS);
- return line;
- }
- private static void SetPlayer(int position)
- {
- string line = " "; //in quotation marks we insert 3 spaces
- line = line.Insert(position, PLAYER);
- board[board.Length - 1] = line;
- }
- private static void ShowBoard()
- {
- Console.Clear();
- for (int i = 0; i < board.Length; i++)
- {
- Console.WriteLine(board[i]);
- }
- Console.WriteLine($"Points: {points}");
- }
- }
- }
Add Comment
Please, Sign In to add comment