Advertisement
trmaxa2820

CSharpTask

Feb 14th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CSharpLightTask
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool isPlaying = true;
  14.             int playerX, playerY;
  15.             int playerDX = 0, playerDY = 0;
  16.  
  17.             char[,] map = { { '#','#','#','#','#'},
  18.                             { '#',' ',' ',' ','#'},
  19.                             { '#',' ','@',' ','#'},
  20.                             { '#',' ',' ',' ','#'},
  21.                             { '#','#','#','#','#'} };
  22.  
  23.             DrawMap(map, out playerX, out playerY);
  24.  
  25.             while (isPlaying)
  26.             {
  27.                 Console.CursorVisible = false;
  28.  
  29.                 if (Console.KeyAvailable)
  30.                 {
  31.                     ConsoleKeyInfo key = Console.ReadKey(true);
  32.  
  33.                     switch (key.Key)
  34.                     {
  35.                         case ConsoleKey.UpArrow:
  36.                             playerDX = -1; playerDY = 0;
  37.                             break;
  38.                         case ConsoleKey.DownArrow:
  39.                             playerDX = 1; playerDY = 0;
  40.                             break;
  41.                         case ConsoleKey.LeftArrow:
  42.                             playerDX = 0; playerDY = -1;
  43.                             break;
  44.                         case ConsoleKey.RightArrow:
  45.                             playerDX = 0; playerDY = 1;
  46.                             break;
  47.                     }
  48.                     if (map[playerX + playerDX, playerY + playerDY] != '#')
  49.                     {
  50.                         Console.SetCursorPosition(playerY, playerX);
  51.                         Console.Write(" ");
  52.  
  53.                         playerX += playerDX;
  54.                         playerY += playerDY;
  55.  
  56.                         Console.SetCursorPosition(playerY, playerX);
  57.                         Console.Write('@');
  58.                     }
  59.                 }
  60.             }
  61.             Console.ReadKey();
  62.         }
  63.  
  64.         static void DrawMap(char[,] map, out int playerX, out int playerY)
  65.         {
  66.             playerX = 0;
  67.             playerY = 0;
  68.  
  69.             for (int i = 0; i < map.GetLength(0); i++)
  70.             {
  71.                 for (int j = 0; j < map.GetLength(1); j++)
  72.                 {
  73.                     Console.Write(map[i, j]);
  74.                     if (map[i, j] == '@')
  75.                     {
  76.                         playerX = i;
  77.                         playerY = j;
  78.                     }
  79.                 }
  80.                 Console.WriteLine();
  81.             }
  82.         }
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement