Created
December 19, 2015 17:40
-
-
Save 21region/2b49a479241487d2fb98 to your computer and use it in GitHub Desktop.
The starting point to develop the console version of the game 'Labyrinth'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace UniumConsole | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
InitializeTheGame(); | |
StartTheGame(); | |
} | |
static void InitializeTheGame() | |
{ | |
CreateWalls(); | |
DrawWalls(); | |
DrawPlayer(); | |
} | |
static void StartTheGame() | |
{ | |
bool exitGame = false; | |
while (!exitGame) | |
{ | |
ConsoleKeyInfo keyInfo = Console.ReadKey(); | |
exitGame = GameLogic(keyInfo.Key); | |
} | |
} | |
//////// | |
static void CreateLeftWall() | |
{ | |
int leftColumn = 0; | |
for (int y = 0; y < ScreenHeight; y++) | |
Wall[leftColumn + y * ScreenWidth] = true; | |
} | |
static void CreateRightWall() | |
{ | |
int rightColumn = ScreenWidth - 1; | |
for (int y = 0; y < ScreenHeight; y++) | |
Wall[rightColumn + y * ScreenWidth] = true; | |
} | |
static void CreateVerticalWalls() | |
{ | |
CreateLeftWall(); | |
CreateRightWall(); | |
} | |
static void CreateWalls() | |
{ | |
CreateVerticalWalls(); | |
// CreateHorizontalWalls(); | |
} | |
//////// | |
static void DrawWalls() | |
{ | |
for (int x = 0; x < ScreenWidth; x++) | |
{ | |
for (int y = 0; y < ScreenHeight; y++) | |
{ | |
Console.SetCursorPosition(x, y); | |
if (ThereIsWall(x, y)) | |
DrawWall(x, y); | |
} | |
} | |
} | |
static bool GameLogic(ConsoleKey key) | |
{ | |
RemovePlayer(); | |
if (key == ConsoleKey.RightArrow || key == ConsoleKey.D) | |
MovePlayerRight(); | |
// else if (key == ConsoleKey.LeftArrow || key == ConsoleKey.A) | |
// MovePlayerLeft(); | |
DrawPlayer(); | |
bool gameShouldExit = (key == ConsoleKey.Escape); | |
return gameShouldExit; | |
} | |
static void RemovePlayer() | |
{ | |
Console.SetCursorPosition(_playerCurrentPositionX, _playerCurrentPositionY); | |
Console.Write(" "); | |
Console.SetCursorPosition(_playerCurrentPositionX, _playerCurrentPositionY); | |
} | |
static void DrawPlayer() | |
{ | |
Console.SetCursorPosition(_playerCurrentPositionX, _playerCurrentPositionY); | |
Console.Write(PlayerCharacter); | |
Console.SetCursorPosition(_playerCurrentPositionX, _playerCurrentPositionY); | |
} | |
static void DrawWall(int wallPositionX, int wallPositionY) | |
{ | |
Console.SetCursorPosition(wallPositionX, wallPositionY); | |
Console.Write(WallImage); | |
Console.SetCursorPosition(wallPositionX, wallPositionY); | |
} | |
static void MovePlayerRight() | |
{ | |
if (!ThereIsWall(_playerCurrentPositionX + 1, _playerCurrentPositionY)) | |
_playerCurrentPositionX++; | |
} | |
static bool ThereIsWall(int x, int y) | |
{ | |
return Wall[x + y * ScreenWidth]; | |
} | |
const int ScreenWidth = 80; | |
const int ScreenHeight = 25; | |
static readonly bool[] Wall = new bool[ScreenWidth * ScreenHeight]; | |
const string WallImage = "▓"; | |
const string PlayerCharacter = "☻"; | |
static int _playerCurrentPositionX = 0; | |
static int _playerCurrentPositionY = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx