Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace CSLight_4._1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- bool isPlaying = true;
- Random random = new Random();
- int pacmanX, pacmanY;
- int pacmanDX = 0, pacmanDY = 1;
- bool isAlive = true;
- int enemyX, enemyY;
- int enemyDX = 0, enemyDY = -1;
- int allDots = 0, collectDots = 0;
- char[,] map = ReadMap("map1", out pacmanX, out pacmanY, out enemyX, out enemyY, ref allDots);
- DrawMap(map);
- while (isPlaying)
- {
- Console.SetCursorPosition(0, 20);
- Console.WriteLine($"Собрано: {collectDots}/{allDots}");
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key, ref pacmanDX, ref pacmanDY);
- }
- if (map[pacmanX + pacmanDX, pacmanY + pacmanDY] != '#')
- {
- CollectDots(map, pacmanX, pacmanY, ref collectDots);
- Move(map, '@', ref pacmanX, ref pacmanY, pacmanDX, pacmanDY);
- }
- if (map[enemyX + enemyDX, enemyY + enemyDY] != '#')
- {
- Move(map, '$', ref enemyX, ref enemyY, enemyDX, enemyDY);
- }
- else
- {
- ChangeDirection(random, ref enemyDX, ref enemyDY);
- }
- if (enemyX == pacmanX && enemyY == pacmanY)
- {
- isAlive = false;
- }
- System.Threading.Thread.Sleep(200);
- if (collectDots == allDots || !isAlive)
- {
- isPlaying = false;
- }
- }
- Console.SetCursorPosition(0, 21);
- if (collectDots == allDots)
- {
- Console.WriteLine("Вы победили!");
- }
- else if (!isAlive)
- {
- Console.WriteLine("Вас съели!");
- }
- }
- static void CollectDots(char[,] map, int X,int Y, ref int collectDots)
- {
- if (map[X, Y] == '.')
- {
- collectDots++;
- map[X, Y] = ' ';
- }
- }
- static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- DX = -1; DY = 0;
- break;
- case ConsoleKey.DownArrow:
- DX = 1; DY = 0;
- break;
- case ConsoleKey.LeftArrow:
- DX = 0; DY = -1;
- break;
- case ConsoleKey.RightArrow:
- DX = 0; DY = 1;
- break;
- }
- }
- static void ChangeDirection(Random rand, ref int DX, ref int DY)
- {
- int enemyDir = rand.Next(1, 5);
- switch (enemyDir)
- {
- case 1:
- DX = -1; DY = 0;
- break;
- case 2:
- DX = 1; DY = 0;
- break;
- case 3:
- DX = 0; DY = -1;
- break;
- case 4:
- DX = 0; DY = 1;
- break;
- }
- }
- static void Move(char[,] map, char symbol, ref int X, ref int Y, int DX, int DY)
- {
- Console.SetCursorPosition(Y, X);
- Console.Write(map[X,Y]);
- X += DX;
- Y += DY;
- Console.SetCursorPosition(Y, X);
- Console.Write(symbol);
- }
- 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 packmanX, out int packmanY, out int enemyX, out int enemyY, ref int allDots)
- {
- packmanX = 0;
- packmanY = 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] == '@')
- {
- packmanX = i;
- packmanY = j;
- map[i, j] = '.';
- }
- else if (map[i,j] == '$')
- {
- enemyX = i;
- enemyY = j;
- map[i, j] = '.';
- }
- else if (map[i,j] == ' ')
- {
- map[i, j] = '.';
- allDots++;
- }
- }
- }
- return map;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement