Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace C_sharp_Light
- {
- class Program
- {
- static void Main(string[] args)
- {
- char[,] map = {
- {'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'},
- {'W', ' ', ' ', ' ', 'W', ' ', ' ', 'W'},
- {'W', ' ', ' ', ' ', 'W', ' ', ' ', 'W'},
- {'W', ' ', ' ', ' ', 'W', ' ', 'W', 'W'},
- {'W', ' ', ' ', ' ', ' ', ' ', ' ', 'W'},
- {'W', ' ', 'W', 'W', 'W', ' ', ' ', 'W'},
- {'W', ' ', ' ', ' ', 'W', ' ', ' ', 'W'},
- {'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'}};
- int playerX = 1, playerY = 1;
- char playerChar = '@';
- bool isPlaying = true;
- Console.CursorVisible = false;
- DrawMap(map);
- Move(ref playerX, ref playerY, 0, 0, playerChar, map);
- while (isPlaying)
- {
- Console.SetCursorPosition(20, 20);
- ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
- switch (consoleKeyInfo.Key)
- {
- case ConsoleKey.UpArrow:
- Move(ref playerX, ref playerY, -1, 0, playerChar, map);
- break;
- case ConsoleKey.DownArrow:
- Move(ref playerX, ref playerY, 1, 0, playerChar, map);
- break;
- case ConsoleKey.RightArrow:
- Move(ref playerX, ref playerY, 0, 1, playerChar, map);
- break;
- case ConsoleKey.LeftArrow:
- Move(ref playerX, ref playerY, 0, -1, playerChar, map);
- break;
- }
- }
- }
- public static void Move(ref int x, ref int y, int dX, int dY, char playerChar, char[,] map)
- {
- if (map[x + dX, y + dY] != 'W')
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.SetCursorPosition(y, x);
- Console.Write(' ');
- x += dX;
- y += dY;
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- Console.SetCursorPosition(y, x);
- Console.Write(playerChar);
- }
- }
- public static void DrawMap(char[,] map)
- {
- Console.SetCursorPosition(0, 0);
- for (int x = 0; x < map.GetLength(0); x++)
- {
- for (int y = 0; y < map.GetLength(1); y++)
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write(map[x, y]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement