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;
- namespace Lesson5._3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Render render = new Render();
- Player player = new Player(1,1);
- PlayerController playerController = new PlayerController();
- Map map = new Map();
- Console.CursorVisible = false;
- while (true)
- {
- render.RenderMap();
- render.RenderPlayer(player);
- ConsoleKeyInfo consoleKey = Console.ReadKey();
- playerController.Controller(consoleKey, map, player);
- }
- }
- }
- class Player
- {
- private int _playerPositionX;
- private int _playerPositionY;
- public char VisualPlayer { get; }
- public int PlayerPositionX
- {
- get
- {
- return _playerPositionX;
- }
- set
- {
- Map map = new Map();
- if (value > 0 && value < map.Level.GetLength(1) - 1 && value != '#')
- _playerPositionX = value;
- }
- }
- public int PlayerPositionY
- {
- get
- {
- return _playerPositionY;
- }
- set
- {
- Map map = new Map();
- if (value > 0 && value < map.Level.GetLength(0) - 1 && value != '#')
- _playerPositionY = value;
- }
- }
- public Player (int playerPositionX = 1, int playerPositionY = 1, char visualPlayer = 'X')
- {
- PlayerPositionX = playerPositionX;
- PlayerPositionY = playerPositionY;
- VisualPlayer = visualPlayer;
- }
- }
- class PlayerController
- {
- public int Controller(ConsoleKeyInfo consoleKey, Map map, Player player)
- {
- switch (consoleKey.Key)
- {
- case ConsoleKey.LeftArrow:
- if (map.Level[ player.PlayerPositionY, player.PlayerPositionX - 1] != '#')
- {
- return player.PlayerPositionX--;
- }
- break;
- case ConsoleKey.RightArrow:
- if (map.Level[player.PlayerPositionY, player.PlayerPositionX + 1] != '#')
- {
- return player.PlayerPositionX++;
- }
- break;
- case ConsoleKey.UpArrow:
- if (map.Level[player.PlayerPositionY - 1, player.PlayerPositionX] != '#')
- {
- return player.PlayerPositionY--;
- }
- break;
- case ConsoleKey.DownArrow:
- if (map.Level[player.PlayerPositionY + 1, player.PlayerPositionX] != '#')
- {
- return player.PlayerPositionY++;
- }
- break;
- default:
- break;
- }
- return player.PlayerPositionX;
- }
- }
- class Map
- {
- private int _levelNumber;
- public char[,] Level { get; }
- public Map(int levelNumber = 1)
- {
- char[,] level1 =
- {
- {'#','#','#','#','#','#','#','#','#','#','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#' },
- {'#','#','#','#','#','#','#','#','#','#','#' }
- };
- Level = level1;
- }
- }
- class Render
- {
- public void RenderMap ()
- {
- Map map = new Map();
- Console.SetCursorPosition(0, 0);
- for (int i = 0; i < map.Level.GetLength(0); i++)
- {
- for (int j = 0; j < map.Level.GetLength(1); j++)
- {
- Console.Write(map.Level[i, j]);
- }
- Console.WriteLine();
- }
- }
- public void RenderPlayer(Player player)
- {
- Console.SetCursorPosition(player.PlayerPositionX, player.PlayerPositionY);
- Console.Write(player.VisualPlayer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment