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;
- int pacmanX, pacmanY;
- int enemyX, enemyY;
- int enemyDirectionX = 1, enemyDirectionY = 0;
- int pacmanDirectionX = 0, pacmanDirectionY = 0;
- int allCherry = 0, harvestedCherry = 0;
- char[,] map = ReadMap("MapPacMan", out enemyX, out enemyY, out pacmanX, out pacmanY, ref allCherry);
- DrawMap(map);
- while (isPlay)
- {
- harvestedCherry = PickUpCherry(map, pacmanX, pacmanY, harvestedCherry);
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDireсtion(key, ref pacmanDirectionX, ref pacmanDirectionY);
- }
- if (map[pacmanY + pacmanDirectionY, pacmanX + pacmanDirectionX] != '#')
- {
- Move(map, '@', ref pacmanX, ref pacmanY, pacmanDirectionX, pacmanDirectionY);
- }
- if (map[enemyY + enemyDirectionY, enemyX + enemyDirectionX] != '#')
- {
- Move(map, '&', ref enemyX, ref enemyY, enemyDirectionX, enemyDirectionY);
- }
- else
- {
- Random random = new Random();
- int randomDirection = random.Next(0, 4);
- ChangeDireсtion(randomDirection, ref enemyDirectionX, ref enemyDirectionY);
- }
- if (enemyX == pacmanX && enemyY == pacmanY)
- {
- 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 x, ref int y)
- {
- switch (direction)
- {
- case 0:
- x = 0;
- y = -1;
- break;
- case 1:
- x = 0;
- y = 1;
- break;
- case 2:
- x = -1;
- y = 0;
- break;
- case 3:
- x = 1;
- y = 0;
- break;
- }
- }
- static void ChangeDireсtion(ConsoleKeyInfo key, ref int x, ref int y)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- x = 0;
- y = -1;
- break;
- case ConsoleKey.DownArrow:
- x = 0;
- y = 1;
- break;
- case ConsoleKey.LeftArrow:
- x = -1;
- y = 0;
- break;
- case ConsoleKey.RightArrow:
- x = 1;
- y = 0;
- break;
- }
- }
- static void Move(char[,] map, char creature, ref int x, ref int y, int directionX, int directionY)
- {
- Console.SetCursorPosition(x, y);
- Console.Write(map[y, x]);
- Console.SetCursorPosition(x += directionX, y += directionY);
- Console.Write(creature);
- System.Threading.Thread.Sleep(100);
- }
- static int PickUpCherry(char[,] map, int x, int y, int cherry)
- {
- if (map[y, x] == '.')
- {
- cherry++;
- map[y, x] = ' ';
- }
- 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 enemyX, out int enemyY, out int pacmanX, out int pacmanY, ref int allCHerry)
- {
- pacmanX = 1;
- pacmanY = 0;
- enemyX = 0;
- enemyY = 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] == '@')
- {
- pacmanX = j;
- pacmanY = i;
- map[i, j] = ' ';
- }
- else if (map[i, j] == '&')
- {
- enemyX = j;
- enemyY = i;
- map[i, j] = ' ';
- }
- else if (map[i, j] == ' ')
- {
- map[i, j] = '.';
- allCHerry++;
- }
- }
- }
- return map;
- }
- }
- }
- [EndCode]
Advertisement
Add Comment
Please, Sign In to add comment