Advertisement
trmaxa2820

CSharpTask

Feb 14th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 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.  
  49.                     if (map[playerX + playerDX, playerY + playerDY] != '#')
  50.                     {
  51.                         Console.SetCursorPosition(playerY, playerX);
  52.                         Console.Write(" ");
  53.  
  54.                         playerX += playerDX;
  55.                         playerY += playerDY;
  56.  
  57.                         Console.SetCursorPosition(playerY, playerX);
  58.                         Console.Write('@');
  59.                     }
  60.  
  61.                 }
  62.             }
  63.  
  64.             Console.ReadKey();
  65.         }
  66.  
  67.         static void DrawMap(char[,] map, out int playerX, out int playerY)
  68.         {
  69.             playerX = 0;
  70.             playerY = 0;
  71.  
  72.             for (int i = 0; i < map.GetLength(0); i++)
  73.             {
  74.                 for (int j = 0; j < map.GetLength(1); j++)
  75.                 {
  76.                     Console.Write(map[i, j]);
  77.                     if (map[i, j] == '@')
  78.                     {
  79.                         playerX = i;
  80.                         playerY = j;
  81.                     }
  82.                 }
  83.                 Console.WriteLine();
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement