Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Homework4
- {
- class Program
- {
- static void Main(string[] args)
- {
- int playerX, playerY;
- int directionX = 1, directionY = 0;
- bool isPlaying = true;
- char[,] map = ReadMap("Map1", out playerX, out playerY);
- Console.CursorVisible = false;
- DrawMap(map);
- while (isPlaying)
- {
- if (Console.KeyAvailable)
- {
- ChangeDirection(ref directionX, ref directionY);
- }
- if (map[playerX + directionX, playerY + directionY] != '#')
- {
- Move(ref playerX, ref playerY, directionX, directionY);
- }
- }
- }
- static void Move(ref int playerX, ref int playerY, int directionX, int directionY)
- {
- Console.SetCursorPosition(playerY, playerX);
- Console.Write(' ');
- playerX += directionX;
- playerY += directionY;
- Console.SetCursorPosition(playerY, playerX);
- Console.Write('@');
- System.Threading.Thread.Sleep(150);
- }
- static void ChangeDirection(ref int directionX, ref int directionY)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.RightArrow:
- directionY = 1; directionX = 0;
- break;
- case ConsoleKey.LeftArrow:
- directionY = -1; directionX = 0;
- break;
- case ConsoleKey.UpArrow:
- directionY = 0; directionX = -1;
- break;
- case ConsoleKey.DownArrow:
- directionY = 0; directionX = 1;
- break;
- }
- }
- static char[,] ReadMap(string mapName, out int playerX, out int playerY)
- {
- playerX = 0;
- playerY = 0;
- string[] newFile = File.ReadAllLines($"Maps/{mapName}.txt");
- char[,] map = new char[newFile.Length, newFile[0].Length];
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- map[i, j] = newFile[i][j];
- if (map[i, j] == '@')
- {
- playerX = i;
- playerY = j;
- }
- }
- }
- return map;
- }
- 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();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment