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;
- namespace Tasks
- {
- class Program
- {
- static void Main(string[] args)
- {
- char[,] map = { { '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*' },
- { '*', ' ', ' ', ' ', '*',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', ' ', ' ', '*',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', ' ', ' ', '*',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', '*', ' ', ' ',' ', ' ', ' ', ' ', ' ', '*' },
- { '*', ' ', '*', ' ', ' ',' ', ' ', ' ', '*', ' ', '*' },
- { '*', ' ', '*', ' ', ' ',' ', ' ', ' ', '*', ' ', '*' },
- { '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*' }};
- Console.CursorVisible = false;
- int userX = 6, userY = 6;
- while (true)
- {
- DrawMap(ref map);
- Console.SetCursorPosition(userY, userX);
- Console.Write("$");
- ConsoleKeyInfo playerKey = Console.ReadKey();
- if (CheckWall(playerKey, ref map, ref userX, ref userY))
- {
- MovePlayer(playerKey,ref userX, ref userY);
- }
- Console.Clear();
- }
- }
- static void DrawMap(ref char[,] array)
- {
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- Console.Write(array[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void MovePlayer(ConsoleKeyInfo playerInput, ref int xposition, ref int yPosition)
- {
- switch (playerInput.Key)
- {
- case ConsoleKey.UpArrow:
- xposition--;
- break;
- case ConsoleKey.DownArrow:
- xposition++;
- break;
- case ConsoleKey.LeftArrow:
- yPosition--;
- break;
- case ConsoleKey.RightArrow:
- yPosition++;
- break;
- }
- }
- static bool CheckWall (ConsoleKeyInfo playerMove, ref char[,] playGround, ref int xposition, ref int yPosition)
- {
- char newPositionUp = playGround[xposition - 1, yPosition];
- char newPositionDown = playGround[xposition + 1, yPosition];
- char newPositionLeft = playGround[xposition, yPosition - 1];
- char newPositionRight = playGround[xposition, yPosition + 1];
- bool notWall;
- if (playerMove.Key == ConsoleKey.UpArrow && newPositionUp != '*')
- {
- notWall = true;
- }
- else if(playerMove.Key == ConsoleKey.DownArrow && newPositionDown != '*')
- {
- notWall = true;
- }
- else if(playerMove.Key == ConsoleKey.LeftArrow && newPositionLeft != '*')
- {
- notWall = true;
- }
- else if (playerMove.Key == ConsoleKey.RightArrow && newPositionRight != '*')
- {
- notWall = true;
- }
- else
- {
- notWall = false;
- }
- return notWall;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement