Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.IO;
- using System.Threading;
- public class Program
- {
- public static void Main()
- {
- ConsoleKey[] consoleKeys = {ConsoleKey.UpArrow, ConsoleKey.LeftArrow,
- ConsoleKey.RightArrow, ConsoleKey.DownArrow};
- Random random = new Random();
- char[,] gameMap = null;
- int playerX = 1;
- int playerY = 1;
- int allDots = 0;
- int collectDots = 0;
- int enemyX = 5;
- int enemyY = 1;
- int direction = 0;
- bool isRun = true;
- Console.CursorVisible = false;
- ReadFile(ref gameMap, "map", playerX, playerY, ref allDots);
- DrawMap(gameMap);
- int scoreX = gameMap.GetLength(1) + 4;
- int scoreY = 4;
- DrawScore(scoreX, scoreY, allDots, collectDots);
- while (isRun)
- {
- DrawPlayer(ref playerX, ref playerY, gameMap, ref collectDots);
- DrawEnemy(ref enemyX, ref enemyY, ref direction, gameMap, consoleKeys, random);
- DrawScore(scoreX, scoreY, allDots, collectDots);
- if (collectDots == allDots || (playerX == enemyX && playerY == enemyY))
- {
- Console.Clear();
- isRun = false;
- }
- }
- }
- public static void ReadFile(ref char[,] map, string fileName, int playerX, int playerY, ref int allDots)
- {
- string[] fileArray = File.ReadAllLines($"../../../{fileName}.txt");
- map = new char[fileArray.Length, fileArray[0].Length];
- for (int i = 0; i < fileArray.Length; ++i)
- {
- for (int j = 0; j < fileArray[0].Length; ++j)
- {
- if (fileArray[i][j] == ' ')
- {
- map[i, j] = '*';
- ++allDots;
- }
- else
- {
- map[i, j] = '#';
- }
- }
- }
- --allDots;
- map[playerY, playerX] = '@';
- }
- public 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();
- }
- Console.WriteLine();
- }
- public static void DrawScore(int x, int y, int allDots, int collectDots)
- {
- Console.SetCursorPosition(x, y);
- Console.WriteLine($"All Dots : {allDots}");
- Console.SetCursorPosition(x, y + 1);
- Console.WriteLine($"Collect Dots : {collectDots}");
- }
- public static void DrawEnemy(ref int positionX, ref int positionY, ref int direction,
- char[,] map, ConsoleKey[] keys, Random random)
- {
- int directionX = 0;
- int directionY = 0;
- ChangePosition(keys[direction], ref directionX, ref directionY);
- if (map[positionY + directionY, positionX + directionX] != '#')
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.Write(map[positionY, positionX]);
- positionX += directionX;
- positionY += directionY;
- Console.SetCursorPosition(positionX, positionY);
- Console.Write('$');
- }
- else
- {
- direction = random.Next(0, keys.Length);
- }
- Thread.Sleep(50);
- }
- public static void DrawPlayer(ref int positionX, ref int positionY, char[,] map, ref int collectDots)
- {
- if (Console.KeyAvailable)
- {
- int directionX = 0;
- int directionY = 0;
- ConsoleKey key = Console.ReadKey(true).Key;
- ChangePosition(key, ref directionX, ref directionY);
- if (map[positionY + directionY, positionX + directionX] != '#')
- {
- Console.SetCursorPosition(positionX, positionY);
- map[positionY, positionX] = ' ';
- Console.Write(" ");
- positionX += directionX;
- positionY += directionY;
- if (map[positionY, positionX] == '*')
- {
- ++collectDots;
- map[positionY, positionX] = ' ';
- }
- Console.SetCursorPosition(positionX, positionY);
- map[positionY, positionX] = '@';
- Console.Write('@');
- }
- }
- }
- public static void ChangePosition(ConsoleKey key, ref int directionX, ref int directionY)
- {
- switch (key)
- {
- case ConsoleKey.UpArrow:
- case ConsoleKey.W:
- directionX = 0;
- directionY = -1;
- break;
- case ConsoleKey.RightArrow:
- case ConsoleKey.D:
- directionX = 1;
- directionY = 0;
- break;
- case ConsoleKey.LeftArrow:
- case ConsoleKey.A:
- directionX = -1;
- directionY = 0;
- break;
- case ConsoleKey.DownArrow:
- case ConsoleKey.S:
- directionX = 0;
- directionY = 1;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement