Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace ConsoleApp2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- bool haveItem = false;
- int playerX, playerY;
- char[,] map = CreateMap("map", out playerX, out playerY);
- PaintMap(map);
- while (true)
- {
- Console.SetCursorPosition(playerX, playerY);
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- SelectoinActoinByKeyPressed(key, ref playerX, ref playerY, ref haveItem, map);
- }
- }
- }
- static void SelectoinActoinByKeyPressed(ConsoleKeyInfo key, ref int playerX, ref int playerY, ref bool haveItem, char[,] map)
- {
- Dictionary<char, int[]> MoveKey = new Dictionary<char, int[]>
- {
- ['w'] = new int[] { 0, -1 },
- ['s'] = new int[] { 0, 1 },
- ['d'] = new int[] { 1, 0 },
- ['a'] = new int[] { -1, 0 }
- };
- if (key.KeyChar != ' ' & MoveKey.TryGetValue(key.KeyChar,out int[] value))
- {
- if (map[playerY + MoveKey[key.KeyChar][1], playerX + MoveKey[key.KeyChar][0]] != '#')
- {
- Move(ref playerX, ref playerY, MoveKey[key.KeyChar], '$');
- SymvolOnLastPozition(playerX, playerY, map, MoveKey[key.KeyChar]);
- }
- }
- else if (key.KeyChar == ' ')
- {
- if (haveItem)
- {
- PutItem(map, playerX, playerY, ref haveItem);
- }
- else
- {
- TakeItem(map, playerX, playerY, ref haveItem);
- }
- }
- }
- static void Move(ref int playerX, ref int playerY, int[] deltas, char symvol)
- {
- Console.SetCursorPosition(playerX, playerY);
- playerX += deltas[0];
- playerY += deltas[1];
- Console.SetCursorPosition(playerX, playerY);
- Console.Write(symvol);
- }
- static void SymvolOnLastPozition(int playerX,int playerY,char[,] map, int[] deltas)
- {
- Console.SetCursorPosition(playerX-deltas[0], playerY - deltas[1]);
- if (map[playerY - deltas[1], playerX- deltas[0]] != '&')
- {
- Console.Write(" ");
- }
- else
- {
- Console.Write("&");
- }
- Console.SetCursorPosition(playerX, playerY);
- }
- static void TakeItem(char[,] map, int playerX, int playerY, ref bool haveItem)
- {
- if (map[playerY, playerX] == '&')
- {
- map[playerY, playerX] = ' ';
- haveItem = true;
- }
- else
- {
- Console.SetCursorPosition(60, 0);
- Console.WriteLine("Здесь ничего нет");
- Console.SetCursorPosition(playerX, playerY);
- }
- }
- static void PutItem(char[,] map, int playerX, int playerY, ref bool haveItem)
- {
- if (map[playerY, playerX] != '&')
- {
- map[playerY, playerX] = '&';
- haveItem = false;
- }
- else if (map[playerY, playerX] == '&')
- {
- Console.SetCursorPosition(60, 0);
- Console.WriteLine("Здесь уже что-то лежит");
- Console.SetCursorPosition(playerX, playerY);
- }
- }
- static void PaintMap(char[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- static char[,] CreateMap(string fileName, out int playerX, out int playerY)
- {
- playerY = 0;
- playerX = 0;
- string[] file = File.ReadAllLines($"Maps/{fileName}.txt");
- char[,] map = new char[file.Length, file[0].Length];
- for (int i = 0; i < file.Length; i++)
- {
- for (int j = 0; j < file[0].Length; j++)
- {
- map[i, j] = file[i][j];
- if (map[i, j] == '$')
- {
- playerY = i;
- playerX = j;
- }
- }
- }
- return map;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement