W1thr

Функции-4

Mar 5th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Homework4
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int playerX, playerY;
  15.             int directionX = 1, directionY = 0;
  16.             bool isPlaying = true;
  17.             char[,] map = ReadMap("Map1", out playerX, out playerY);
  18.  
  19.             Console.CursorVisible = false;
  20.             DrawMap(map);
  21.  
  22.             while (isPlaying)
  23.             {
  24.                 if (Console.KeyAvailable)
  25.                 {
  26.                     ChangeDirection(ref directionX, ref directionY);
  27.                 }
  28.  
  29.                 if (map[playerX + directionX, playerY + directionY] != '#')
  30.                 {
  31.                     Move(ref playerX, ref playerY, directionX, directionY);
  32.                 }
  33.             }
  34.         }
  35.  
  36.         static void Move(ref int playerX, ref int playerY, int directionX, int directionY)
  37.         {
  38.             Console.SetCursorPosition(playerY, playerX);
  39.             Console.Write(' ');
  40.  
  41.             playerX += directionX;
  42.             playerY += directionY;
  43.  
  44.             Console.SetCursorPosition(playerY, playerX);
  45.             Console.Write('@');
  46.             System.Threading.Thread.Sleep(150);
  47.         }
  48.  
  49.         static void ChangeDirection(ref int directionX, ref int directionY)
  50.         {
  51.             ConsoleKeyInfo key = Console.ReadKey(true);
  52.             switch (key.Key)
  53.             {
  54.                 case ConsoleKey.RightArrow:
  55.                     directionY = 1; directionX = 0;
  56.                     break;
  57.                 case ConsoleKey.LeftArrow:
  58.                     directionY = -1; directionX = 0;
  59.                     break;
  60.                 case ConsoleKey.UpArrow:
  61.                     directionY = 0; directionX = -1;
  62.                     break;
  63.                 case ConsoleKey.DownArrow:
  64.                     directionY = 0; directionX = 1;
  65.                     break;
  66.             }
  67.         }
  68.  
  69.         static char[,] ReadMap(string mapName, out int playerX, out int playerY)
  70.         {
  71.             playerX = 0;
  72.             playerY = 0;
  73.             string[] newFile = File.ReadAllLines($"Maps/{mapName}.txt");
  74.             char[,] map = new char[newFile.Length, newFile[0].Length];
  75.  
  76.             for (int i = 0; i < map.GetLength(0); i++)
  77.             {
  78.                 for (int j = 0; j < map.GetLength(1); j++)
  79.                 {
  80.                     map[i, j] = newFile[i][j];
  81.                     if (map[i, j] == '@')
  82.                     {
  83.                         playerX = i;
  84.                         playerY = j;
  85.                     }
  86.                 }
  87.             }
  88.  
  89.             return map;
  90.         }
  91.  
  92.         static void DrawMap(char[,] map)
  93.         {
  94.             for (int i = 0; i < map.GetLength(0); i++)
  95.             {
  96.                 for (int j = 0; j < map.GetLength(1); j++)
  97.                 {
  98.                     Console.Write(map[i, j]);
  99.                 }
  100.                 Console.WriteLine();
  101.             }
  102.         }
  103.     }
  104. }
  105.  
Add Comment
Please, Sign In to add comment