Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace CSLight
- {
- class Program
- {
- static void Main()
- {
- Console.CursorVisible = false;
- int userPositionX = 2;
- int userPositionY = 2;
- char[,] map =
- {
- {'#','#','#','#','#','#','#','#','#','#','#','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#','#','#','#','#','#','#','#','#','#','#','#' }
- };
- DrawMap(map);
- while (true)
- {
- ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
- switch (consoleKeyInfo.Key)
- {
- case ConsoleKey.UpArrow:
- if (!CheckWall(map, userPositionX, userPositionY - 1))
- {
- ErasePlayer(userPositionX, userPositionY);
- Move(ref userPositionX, ref userPositionY, 0, -1);
- DrawPlayer(userPositionX, userPositionY, '$');
- }
- break;
- case ConsoleKey.DownArrow:
- if (!CheckWall(map, userPositionX, userPositionY + 1))
- {
- ErasePlayer(userPositionX, userPositionY);
- Move(ref userPositionX, ref userPositionY, 0, 1);
- DrawPlayer(userPositionX, userPositionY, '$');
- }
- break;
- case ConsoleKey.LeftArrow:
- if (!CheckWall(map, userPositionX - 1, userPositionY))
- {
- ErasePlayer(userPositionX, userPositionY);
- Move(ref userPositionX, ref userPositionY, -1, 0);
- DrawPlayer(userPositionX, userPositionY, '$');
- }
- break;
- case ConsoleKey.RightArrow:
- if (!CheckWall(map, userPositionX + 1, userPositionY))
- {
- ErasePlayer(userPositionX, userPositionY);
- Move(ref userPositionX, ref userPositionY, 1, 0);
- DrawPlayer(userPositionX, userPositionY, '$');
- }
- break;
- }
- }
- }
- static bool CheckWall(char[,] map, int coordinateX, int coordinateY)
- {
- if (map[coordinateY, coordinateX] == '#')
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static void ErasePlayer(int positionX, int positionY)
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.Write(" ");
- }
- static void DrawPlayer(int positionX, int positionY, char symbol)
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.Write(symbol);
- }
- static void Move(ref int positionX, ref int positionY, int directionX, int directionY)
- {
- positionX += directionX;
- positionY += directionY;
- }
- static void DrawMap(char[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- static int ReadInt()
- {
- int parse;
- while (true)
- {
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out parse))
- {
- return parse;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement