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 = "#";
- static void Main(string[] args)
- {
- int playerPosition = 1;
- Random generatorRandom = new Random();
- bool isStruck = 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--;
- }
- }
- }
- //Collision check
- int nearestObstaclePosition = board[board.Length - 2].IndexOf(OBSTACLE);
- if (playerPosition == nearestObstaclePosition)
- {
- isStruck = true;
- }
- //Creating a new obstacle
- int obstaclePosition = generatorRandom.Next(3);
- string obstacle = SetObstacle(obstaclePosition);
- //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();
- Thread.Sleep(600);
- }
- Console.Clear();
- Console.WriteLine("GAME OVER");
- 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 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]);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment