Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace world
- {
- class Program
- {
- const int DimentionX = 0;
- const int DimentionY = 1;
- const char CellWall = '#';
- const char CellBonus = '*';
- const char CellPlayer = '@';
- const char CellEmpty = ' ';
- static void Main(string[] args)
- {
- char[,] map = new char[6, 6] {
- {CellWall, CellWall, CellWall, CellWall, CellWall, CellWall},
- {CellWall, CellBonus, CellEmpty, CellWall, CellBonus, CellWall},
- {CellWall, CellEmpty, CellEmpty, CellWall, CellEmpty, CellWall},
- {CellWall, CellEmpty, CellEmpty, CellWall, CellEmpty, CellWall},
- {CellWall, CellBonus, CellEmpty, CellEmpty, CellEmpty, CellWall},
- {CellWall, CellWall, CellWall, CellWall, CellWall, CellWall}};
- int playerX = 1;
- int playerY = 2;
- int score = 0;
- bool isRunning = true;
- Console.CursorVisible = false;
- while (isRunning)
- {
- Console.SetCursorPosition(0, 0);
- DrawMap(map, playerX, playerY);
- Console.SetCursorPosition(0, map.GetLength(DimentionX) + 1);
- Console.WriteLine($"Score: {score}");
- ConsoleKeyInfo userInput = Console.ReadKey();
- switch (userInput.Key)
- {
- case ConsoleKey.UpArrow:
- if (IsPassable(playerY - 1, playerX))
- playerY--;
- break;
- case ConsoleKey.DownArrow:
- if (IsPassable(playerY + 1, playerX))
- playerY++;
- break;
- case ConsoleKey.LeftArrow:
- if (IsPassable(playerY, playerX - 1))
- playerX--;
- break;
- case ConsoleKey.RightArrow:
- if (IsPassable(playerY, playerX + 1))
- playerX++;
- break;
- case ConsoleKey.Escape:
- isRunning = false;
- break;
- }
- if (map[playerY, playerX] == CellBonus)
- {
- map[playerY, playerX] = CellEmpty;
- score++;
- }
- }
- bool IsPassable(int coordinateY, int coordinateX)
- {
- return map[coordinateY, coordinateX] != CellWall;
- }
- }
- static void DrawMap(char[,] map, int playerX, int playerY)
- {
- for (int y = 0; y < map.GetLength(DimentionY); y++)
- {
- for (int x = 0; x < map.GetLength(DimentionX); x++)
- {
- Console.Write(map[y, x]);
- }
- Console.WriteLine();
- }
- Console.SetCursorPosition(playerX, playerY);
- Console.Write(CellPlayer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement