Skip to content

Instantly share code, notes, and snippets.

@21region
Created December 19, 2015 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 21region/2b49a479241487d2fb98 to your computer and use it in GitHub Desktop.
Save 21region/2b49a479241487d2fb98 to your computer and use it in GitHub Desktop.
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
Copy link

thx

@P4K1TO
Copy link

P4K1TO commented Dec 20, 2015

Спасибо

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment