Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [StartCode]
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.Eventing.Reader;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Threading.Tasks;
- /* Сделать игровую карту с помощью двумерного массива. Сделать функцию рисования карты.
- *
- * Помимо этого, дать пользователю возможность перемещаться по карте и взаимодействовать с элементами
- * (например пользователь не может пройти сквозь стену)
- *
- * Все элементы являются обычными символами
- */
- namespace ConsoleApp1
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- bool isPlay = true;
- char pacmanSumbol = '@';
- char enemySumbol = '&';
- char wallSumbol = '#';
- char cherrySumbol = '.';
- char emptySumbol = ' ';
- int pacmanPositionX;
- int pacmanPositionY;
- int enemyPositionX;
- int enemyPositionY;
- int enemyDirectionX = 1;
- int enemyDirectionY = 0;
- int pacmanDirectionX = 0;
- int pacmanDirectionY = 0;
- int allCherry = 0;
- int harvestedCherry = 0;
- char[,] map = ReadMap("MapPacMan", out enemyPositionX, out enemyPositionY, out pacmanPositionX, out pacmanPositionY, ref allCherry, pacmanSumbol, enemySumbol, cherrySumbol, emptySumbol);
- DrawMap(map);
- while (isPlay)
- {
- harvestedCherry = PickUpCherry(map, pacmanPositionX, pacmanPositionY, harvestedCherry, cherrySumbol, emptySumbol);
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDireсtion(key, ref pacmanDirectionX, ref pacmanDirectionY);
- }
- if (map[pacmanPositionY + pacmanDirectionY, pacmanPositionX + pacmanDirectionX] != wallSumbol)
- {
- Move(map, pacmanSumbol, ref pacmanPositionX, ref pacmanPositionY, pacmanDirectionX, pacmanDirectionY);
- }
- if (map[enemyPositionY + enemyDirectionY, enemyPositionX + enemyDirectionX] != wallSumbol)
- {
- Move(map, enemySumbol, ref enemyPositionX, ref enemyPositionY, enemyDirectionX, enemyDirectionY);
- }
- else
- {
- Random random = new Random();
- int randomDirection = random.Next(0, 4);
- ChangeDireсtion(randomDirection, ref enemyDirectionX, ref enemyDirectionY);
- }
- if (enemyPositionX == pacmanPositionX && enemyPositionY == pacmanPositionY)
- {
- Console.SetCursorPosition(0, 22);
- Console.WriteLine("Вы попались !");
- isPlay = false;
- }
- if (harvestedCherry == allCherry)
- {
- Console.SetCursorPosition(0, 22);
- Console.WriteLine("Вы победили !");
- isPlay = false;
- }
- Console.SetCursorPosition(0, 20);
- Console.WriteLine($"Собрано : {harvestedCherry}/{allCherry}");
- }
- }
- static void ChangeDireсtion(int direction, ref int directionX, ref int directionY)
- {
- switch (direction)
- {
- case 0:
- directionX = 0;
- directionY = -1;
- break;
- case 1:
- directionX = 0;
- directionY = 1;
- break;
- case 2:
- directionX = -1;
- directionY = 0;
- break;
- case 3:
- directionX = 1;
- directionY = 0;
- break;
- }
- }
- static void ChangeDireсtion(ConsoleKeyInfo key, ref int positionX, ref int positionY)
- {
- const int LeftSide = 37;
- const int UpperSide = 38;
- const int RightSide = 39;
- const int DownSide = 40;
- int direction = Convert.ToChar(key.Key);
- switch (direction)
- {
- case UpperSide:
- positionX = 0;
- positionY = -1;
- break;
- case DownSide:
- positionX = 0;
- positionY = 1;
- break;
- case LeftSide:
- positionX = -1;
- positionY = 0;
- break;
- case RightSide:
- positionX = 1;
- positionY = 0;
- break;
- }
- }
- static void Move(char[,] map, char creature, ref int positionX, ref int positionY, int directionX, int directionY)
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.Write(map[positionY, positionX]);
- Console.SetCursorPosition(positionX += directionX, positionY += directionY);
- Console.Write(creature);
- System.Threading.Thread.Sleep(100);
- }
- static int PickUpCherry(char[,] map, int positionX, int positionY, int cherry, char cherrySumbol, char emptySumbol)
- {
- if (map[positionY, positionX] == cherrySumbol)
- {
- cherry++;
- map[positionY, positionX] = emptySumbol;
- }
- return cherry;
- }
- 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 char[,] ReadMap(string mapName, out int enemyPositionX, out int enemyPositionY, out int pacmanPositionX, out int pacmanPositionY, ref int allCHerry, char pacmanSumbol, char enemySumbol, char cherrySumbol, char emptySumbol)
- {
- pacmanPositionX = 1;
- pacmanPositionY = 0;
- enemyPositionX = 0;
- enemyPositionY = 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] == pacmanSumbol)
- {
- pacmanPositionX = j;
- pacmanPositionY = i;
- map[i, j] = emptySumbol;
- }
- else if (map[i, j] == enemySumbol)
- {
- enemyPositionX = j;
- enemyPositionY = i;
- map[i, j] = emptySumbol;
- }
- else if (map[i, j] == emptySumbol)
- {
- map[i, j] = cherrySumbol;
- allCHerry++;
- }
- }
- }
- return map;
- }
- }
- }
- [EndCode]
Advertisement
Add Comment
Please, Sign In to add comment