Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace pac_man
- {
- class Program
- {
- static int pacX = 0, pacY = 0;
- static int DX = 0, DY = 0;
- static int ghostX = 0, ghostY = 0;
- static int GDX = 1, GDY = 0;
- static char[,] map;
- static string[] NewFile;
- static int points = 0;
- static int allDots = 0;
- static ConsoleKeyInfo key;
- static bool kill = false;
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- MapWrite();
- while (!kill)
- {
- System.Threading.Thread.Sleep(120);
- SetMoving();
- if (map[pacY + DY, pacX + DX] != '#')
- {
- PacGo();
- if (allDots == points) break;
- }
- if (map[ghostY + GDY, ghostX + GDX] == '#')
- GhostToPac();
- else
- {
- GhostToPac();
- GhostGo();
- }
- if (ghostX == pacX && ghostY == pacY) kill = true;
- }
- Console.SetCursorPosition(0, map.GetLength(0) + 7);
- if (kill)
- Console.Write("Ты умер.");
- else
- {
- Console.Write("ВЫ ПОБЕДИЛИ!");
- Console.ReadKey();
- }
- }
- private static void MapWrite()
- {
- NewFile = File.ReadAllLines("D:\\map.txt");
- 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];
- 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] = '.';
- allDots++;
- }
- else if (map[i, j] == 'M')
- {
- ghostX = j;
- ghostY = i;
- map[i, j] = ' ';
- }
- Console.Write(map[i, j]);
- }
- Console.Write('\n');
- }
- }
- private static void SetMoving()
- {
- 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;
- }
- }
- }
- private static void PacGo()
- {
- TelePort();
- Console.SetCursorPosition(pacX, pacY);
- Console.Write(' ');
- pacX += DX;
- pacY += DY;
- Console.SetCursorPosition(pacX, pacY);
- Console.Write('@');
- if (map[pacY, pacX] == '.')
- {
- points++;
- map[pacY, pacX] = ' ';
- Console.SetCursorPosition(0, map.GetLength(0) + 5);
- Console.Write("У вас " + points + " очков");
- }
- }
- private static void GhostToPac()
- {
- if (pacX > ghostX && map[ghostY, ghostX + 1] == ' ' ||
- pacX > ghostX && map[ghostY, ghostX + 1] == '.')
- {
- GDX = 1;
- GDY = 0;
- }
- if (pacX < ghostX && map[ghostY, ghostX - 1] == ' ' ||
- pacX < ghostX && map[ghostY, ghostX - 1] == '.')
- {
- GDX = -1;
- GDY = 0;
- }
- if (pacY > ghostY && map[ghostY + 1, ghostX] == ' ' ||
- pacY > ghostY && map[ghostY + 1, ghostX] == '.')
- {
- GDX = 0;
- GDY = 1;
- }
- if (pacY < ghostY && map[ghostY - 1, ghostX] == ' ' ||
- pacY < ghostY && map[ghostY - 1, ghostX] == '.')
- {
- GDX = 0;
- GDY = -1;
- }
- }
- private static void GhostGo()
- {
- Console.SetCursorPosition(ghostX, ghostY);
- Console.Write(map[ghostY, ghostX]);
- ghostX += GDX;
- ghostY += GDY;
- Console.SetCursorPosition(ghostX, ghostY);
- Console.Write('M');
- }
- private static void TelePort()
- {
- if (map[pacY, pacX] == '%')
- {
- Console.SetCursorPosition(pacX, pacY);
- Console.Write('%');
- pacX = 1;
- pacY = 1;
- Console.SetCursorPosition(pacX, pacY);
- Console.Write('@');
- }
- else if (map[pacY, pacX] == '$')
- {
- Console.SetCursorPosition(pacX, pacY);
- Console.Write('$');
- pacX = 25;
- pacY = 1;
- Console.SetCursorPosition(pacX, pacY);
- Console.Write('@');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement