Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace pacman
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- Random rnd = new Random();
- bool win = false;
- int maxPoint = 0;
- int point = 0;
- // Карта из файла
- string[] newFile = File.ReadAllLines("map.txt");
- char[,] map = new char[newFile.Length, newFile[1].Length];
- // PACMAM началные свойства
- int pacX = 0, pacY = 0;
- int DX = 0, DY = 0;
- bool die = false;
- // GHOST начальные свойства
- int ghostX = 0, ghostY = 0;
- int GDX = 1, GDY = 0;
- int ghostDir = 0;
- // Читаем из файла и заносим в память карту
- generateMap(ref map, ref newFile);
- // Выводим карту на экран
- renderMap(map, ref pacX, ref pacY, ref maxPoint, ref ghostX, ref ghostY);
- // УПРАВЛЕНИЕ
- mainLoop(ref win, ref die, ref DX, ref DY, ref GDX, ref GDY, map, ref pacX, ref pacY, ref ghostX, ref ghostY, ref ghostDir, ref point, ref maxPoint, ref rnd);
- if(win)
- {
- Console.Clear();
- Console.Write("Вы выиграли\n");
- }
- else
- {
- Console.Clear();
- Console.Write("Вы проиграли\n");
- }
- }
- static void generateMap(ref char[,] map, ref string[] newFile)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- for (int j = 0; j < map.GetLength(1); j++)
- map[i, j] = newFile[i][j];
- }
- static void renderMap(char[,] map, ref int pacX, ref int pacY, ref int maxPoint, ref int ghostX, ref int ghostY)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (map[i, j] == '@')
- {
- pacX = j;
- pacY = i;
- map[i, j] = ' ';
- }
- else if (map[i, j] == ' ')
- {
- map[i, j] = '.';
- maxPoint++;
- }
- else if (map[i, j] == '0')
- {
- map[i, j] = ' ';
- }
- else if (map[i, j] == 'X')
- {
- map[i, j] = 'X';
- }
- else if (map[i, j] == '%')
- {
- ghostX = j;
- ghostY = i;
- map[i, j] = '.';
- }
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void movePacman(ref int DX, ref int DY)
- {
- ConsoleKeyInfo key;
- if (Console.KeyAvailable)
- {
- key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- DX = 0; DY = -1;
- break;
- case ConsoleKey.DownArrow:
- DX = 0; DY = 1;
- break;
- case ConsoleKey.LeftArrow:
- DX = -1; DY = 0;
- break;
- case ConsoleKey.RightArrow:
- DX = 1; DY = 0;
- break;
- }
- }
- }
- static void moveGhost(char[,] map, ref int ghostY, ref int ghostX, ref int GDX, ref int GDY, ref int ghostDir, int pacX, int pacY, ref bool die, ref Random rnd)
- {
- if (map[ghostY + GDY, ghostX + GDX] == '#')
- {
- ghostDir = rnd.Next(1, 5);
- switch (ghostDir)
- {
- case 1:
- GDX = 0; GDY = 1;
- break;
- case 2:
- GDX = 0; GDY = -1;
- break;
- case 3:
- GDX = -1; GDY = 0;
- break;
- case 4:
- GDX = 1; GDY = 0;
- break;
- }
- }
- else
- {
- Console.SetCursorPosition(ghostX, ghostY);
- Console.Write(map[ghostY, ghostX]);
- ghostX += GDX;
- ghostY += GDY;
- Console.SetCursorPosition(ghostX, ghostY);
- Console.Write('%');
- if (ghostX == pacX && ghostY == pacY)
- {
- die = true;
- }
- }
- }
- static void pacmanCollision( char[,] map, ref int pacY, ref int pacX, ref int DX, ref int DY, ref int point, ref int maxPoint) {
- if (map[pacY + DY, pacX + DX] != '#')
- {
- Console.SetCursorPosition(pacX, pacY);
- Console.Write(' ');
- pacX += DX;
- pacY += DY;
- Console.SetCursorPosition(pacX, pacY);
- Console.Write('@');
- Console.SetCursorPosition(0, map.GetLength(0) + 1);
- Console.Write($"Координаты {pacX} / {pacY} ");
- if (map[pacY, pacX] == '.')
- {
- point++;
- map[pacY, pacX] = ' ';
- Console.SetCursorPosition(0, map.GetLength(0) + 2);
- Console.Write($"Вы собрали {point} / {maxPoint}");
- }
- else if (map[pacY, pacX] == 'X')
- {
- if (pacX == 28 && pacY == 9)
- {
- Console.SetCursorPosition(28, 9);
- Console.Write('X');
- Console.SetCursorPosition(0, 9);
- Console.Write('X');
- pacX = 1;
- pacY = 9;
- }
- else
- {
- Console.SetCursorPosition(0, 9);
- Console.Write('X');
- Console.SetCursorPosition(28, 9);
- Console.Write('X');
- pacX = 27;
- pacY = 9;
- }
- }
- }
- }
- static void mainLoop(ref bool win, ref bool die, ref int DX, ref int DY, ref int GDX, ref int GDY, char[,] map, ref int pacX, ref int pacY, ref int ghostX, ref int ghostY, ref int ghostDir, ref int point, ref int maxPoint, ref Random rnd)
- {
- while (!win && !die)
- {
- movePacman(ref DX, ref DY);
- System.Threading.Thread.Sleep(100);
- pacmanCollision(map, ref pacY, ref pacX, ref DX, ref DY, ref point, ref maxPoint);
- moveGhost(map, ref ghostY, ref ghostX, ref GDX, ref GDY, ref ghostDir, pacX, pacY, ref die, ref rnd);
- if (point == maxPoint)
- {
- win = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement