Advertisement
Ipashilovo

quest17

May 28th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace ConsoleApp2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.CursorVisible = false;
  12.  
  13.             bool haveItem = false;
  14.  
  15.             int playerX, playerY;
  16.             char[,] map = CreateMap("map", out playerX, out playerY);
  17.            
  18.             PaintMap(map);
  19.  
  20.             while (true)
  21.             {
  22.                 Console.SetCursorPosition(playerX, playerY);
  23.  
  24.                 if (Console.KeyAvailable)
  25.                 {
  26.                     ConsoleKeyInfo key = Console.ReadKey(true);
  27.                     SelectoinActoinByKeyPressed(key, ref playerX, ref playerY, ref haveItem, map);
  28.                 }
  29.             }
  30.         }
  31.  
  32.         static void SelectoinActoinByKeyPressed(ConsoleKeyInfo key, ref int playerX, ref int playerY, ref bool haveItem, char[,] map)
  33.         {
  34.             Dictionary<char, int[]> MoveKey = new Dictionary<char, int[]>
  35.             {
  36.                 ['w'] = new int[] { 0, -1 },
  37.                 ['s'] = new int[] { 0, 1 },
  38.                 ['d'] = new int[] { 1, 0 },
  39.                 ['a'] = new int[] { -1, 0 }
  40.             };
  41.  
  42.             if (key.KeyChar != ' ' & MoveKey.TryGetValue(key.KeyChar,out int[] value))
  43.             {
  44.                 if (map[playerY + MoveKey[key.KeyChar][1], playerX + MoveKey[key.KeyChar][0]] != '#')
  45.                 {
  46.                    
  47.                         Move(ref playerX, ref playerY, MoveKey[key.KeyChar], '$');
  48.                         SymvolOnLastPozition(playerX, playerY, map, MoveKey[key.KeyChar]);
  49.                 }
  50.             }
  51.             else if (key.KeyChar == ' ')
  52.             {
  53.                 if (haveItem)
  54.                 {
  55.                     PutItem(map, playerX, playerY, ref haveItem);
  56.                 }
  57.                 else
  58.                 {
  59.                     TakeItem(map, playerX, playerY, ref haveItem);
  60.                 }
  61.             }
  62.         }
  63.  
  64.         static void Move(ref int playerX, ref int playerY, int[] deltas, char symvol)
  65.         {
  66.                 Console.SetCursorPosition(playerX, playerY);
  67.                 playerX += deltas[0];
  68.                 playerY += deltas[1];
  69.                 Console.SetCursorPosition(playerX, playerY);
  70.                 Console.Write(symvol);
  71.         }
  72.  
  73.         static void SymvolOnLastPozition(int playerX,int playerY,char[,] map, int[] deltas)
  74.         {
  75.             Console.SetCursorPosition(playerX-deltas[0], playerY - deltas[1]);
  76.             if (map[playerY - deltas[1], playerX- deltas[0]] != '&')
  77.             {
  78.                 Console.Write(" ");
  79.             }
  80.             else
  81.             {
  82.                 Console.Write("&");
  83.             }
  84.             Console.SetCursorPosition(playerX, playerY);
  85.         }
  86.  
  87.         static void TakeItem(char[,] map, int playerX, int playerY, ref bool haveItem)
  88.         {
  89.             if (map[playerY, playerX] == '&')
  90.             {
  91.                 map[playerY, playerX] = ' ';
  92.                 haveItem = true;
  93.             }
  94.             else
  95.             {
  96.                 Console.SetCursorPosition(60, 0);
  97.                 Console.WriteLine("Здесь ничего нет");
  98.                 Console.SetCursorPosition(playerX, playerY);
  99.             }
  100.         }
  101.  
  102.         static void PutItem(char[,] map, int playerX, int playerY, ref bool haveItem)
  103.         {
  104.             if (map[playerY, playerX] != '&')
  105.             {
  106.                 map[playerY, playerX] = '&';
  107.                 haveItem = false;
  108.             }
  109.             else if (map[playerY, playerX] == '&')
  110.             {
  111.                 Console.SetCursorPosition(60, 0);
  112.                 Console.WriteLine("Здесь уже что-то лежит");
  113.                 Console.SetCursorPosition(playerX, playerY);
  114.             }
  115.         }
  116.  
  117.         static void PaintMap(char[,] map)
  118.         {
  119.             for (int i = 0; i < map.GetLength(0); i++)
  120.             {
  121.                 for (int j = 0; j < map.GetLength(1); j++)
  122.                 {
  123.                     Console.Write(map[i, j]);
  124.                 }
  125.                 Console.WriteLine();
  126.             }
  127.         }
  128.  
  129.         static char[,] CreateMap(string fileName, out int playerX, out int playerY)
  130.         {
  131.             playerY = 0;
  132.             playerX = 0;
  133.  
  134.             string[] file = File.ReadAllLines($"Maps/{fileName}.txt");
  135.             char[,] map = new char[file.Length, file[0].Length];
  136.  
  137.             for (int i = 0; i < file.Length; i++)
  138.             {
  139.                 for (int j = 0; j < file[0].Length; j++)
  140.                 {
  141.                     map[i, j] = file[i][j];
  142.                     if (map[i, j] == '$')
  143.                     {
  144.                         playerY = i;
  145.                         playerX = j;
  146.                     }
  147.                 }
  148.             }
  149.             return map;
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement