-
-
Save StrikerXx798/f0ec2e685856f725fbf5036a040b7a1e to your computer and use it in GitHub Desktop.
ДЗ: Brave new world
This file contains 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; | |
using System.Text; | |
namespace ConsoleApp1 | |
{ | |
internal class Program | |
{ | |
const ConsoleKey MoveUpKey = ConsoleKey.W; | |
const ConsoleKey MoveDownKey = ConsoleKey.S; | |
const ConsoleKey MoveLeftKey = ConsoleKey.A; | |
const ConsoleKey MoveRightKey = ConsoleKey.D; | |
const ConsoleKey ExitKey = ConsoleKey.Q; | |
static void Main(string[] args) | |
{ | |
Console.InputEncoding = Encoding.Unicode; | |
Console.OutputEncoding = Encoding.Unicode; | |
char[,] map = { | |
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, | |
{ '#', '.', '.', '.', '*', '.', '.', '.', '.', '#' }, | |
{ '#', '.', '#', '#', '#', '.', '#', '#', '.', '#' }, | |
{ '#', '.', '.', '*', '#', '.', '.', '#', '.', '#' }, | |
{ '#', '#', '#', '.', '#', '#', '.', '#', '.', '#' }, | |
{ '#', '.', '.', '.', '.', '*', '.', '#', '.', '#' }, | |
{ '#', '.', '#', '#', '#', '#', '.', '#', '.', '#' }, | |
{ '#', '.', '.', '.', '.', '.', '.', '*', '.', '#' }, | |
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } | |
}; | |
int playerX = 1; | |
int playerY = 1; | |
int itemCount = 0; | |
bool isRunning = true; | |
while (isRunning) | |
{ | |
Console.Clear(); | |
DrawMap(map); | |
DrawPlayer(playerX, playerY); | |
DrawPlayerInfo(itemCount); | |
GetDirection(out int directionX, out int directionY, ref isRunning); | |
int nextX = playerX + directionX; | |
int nextY = playerY + directionY; | |
if (map[nextX, nextY] != '#') | |
{ | |
playerX = nextX; | |
playerY = nextY; | |
} | |
TryPickItem(map, playerX, playerY, ref itemCount); | |
} | |
Console.WriteLine("Вы вышли из игры."); | |
} | |
static void DrawMap(char[,] map) | |
{ | |
Console.SetCursorPosition(0, 0); | |
for (int i = 0; i < map.GetLength(0); i++) | |
{ | |
for (int j = 0; j < map.GetLength(1); j++) | |
{ | |
Console.ForegroundColor = GetColor(map[i, j]); | |
Console.Write(map[i, j]); | |
} | |
Console.WriteLine(); | |
} | |
Console.ResetColor(); | |
} | |
static void DrawPlayer(int playerX, int playerY) | |
{ | |
Console.SetCursorPosition(playerY, playerX); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.Write('@'); | |
Console.ResetColor(); | |
} | |
static void DrawPlayerInfo(int itemCount) | |
{ | |
int infoStartY = 12; | |
Console.SetCursorPosition(0, infoStartY); | |
Console.WriteLine($"Собрано предметов: {itemCount}"); | |
Console.WriteLine($"Управление: {MoveUpKey} (вверх), {MoveLeftKey} (влево), {MoveDownKey} (вниз), {MoveRightKey} (вправо), {ExitKey} (выход)"); | |
} | |
static ConsoleColor GetColor(char symbol) | |
{ | |
switch (symbol) | |
{ | |
case '#': | |
return ConsoleColor.Gray; | |
case '*': | |
return ConsoleColor.Yellow; | |
case '.': | |
case '_': | |
default: | |
return ConsoleColor.White; | |
} | |
} | |
static void GetDirection(out int directionX, out int directionY, ref bool isRunning) | |
{ | |
directionX = 0; | |
directionY = 0; | |
ConsoleKey key = Console.ReadKey(true).Key; | |
switch (key) | |
{ | |
case MoveUpKey: | |
directionX = -1; | |
break; | |
case MoveDownKey: | |
directionX = 1; | |
break; | |
case MoveLeftKey: | |
directionY = -1; | |
break; | |
case MoveRightKey: | |
directionY = 1; | |
break; | |
case ExitKey: | |
isRunning = false; | |
break; | |
default: | |
break; | |
} | |
} | |
static void TryPickItem(char[,] map, int playerX, int playerY, ref int itemCount) | |
{ | |
if (map[playerX, playerY] == '*') | |
{ | |
itemCount++; | |
map[playerX, playerY] = '.'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment