Skip to content

Instantly share code, notes, and snippets.

@21region 21region/Program.cs
Created Dec 19, 2015

Embed
What would you like to do?
The starting point to develop the console version of the game 'Labyrinth'
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;
}
}
@Will0376

This comment has been minimized.

Copy link

commented Dec 19, 2015

thx

@P4K1TO

This comment has been minimized.

Copy link

commented Dec 20, 2015

Спасибо

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.