Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Diagnostics.CodeAnalysis;
- using System.Dynamic;
- using System.Globalization;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.IO;
- namespace CSLight
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- Random random = new Random();
- bool isPlaying = true;
- int packmanX, packmanY;
- int packmanDX = 0, packmanDY = 1;
- bool isAlive = true;
- int allDots = 0;
- int collectDots = 0;
- int ghostX, ghostY;
- int ghostDX = 0, ghostDY = -1;
- char[,] map = ReadMap("map", out packmanX, out packmanY,out ghostX, out ghostY, 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 packmanDX, ref packmanDY);
- }
- if (map[packmanX + packmanDX, packmanY + packmanDY] != '*')
- {
- Move(map, '@', ref packmanX, ref packmanY, packmanDX, packmanDY);
- CollectDots(map, packmanX, packmanY, ref collectDots);
- }
- if (map[ghostX + ghostDX, ghostY + ghostDY] != '*')
- {
- Move(map,'$', ref ghostX, ref ghostY, ghostDX, ghostDY);
- }
- else
- {
- ChangeDirection(random, ref ghostDX, ref ghostDY);
- }
- if(ghostX == packmanX && ghostY == packmanY)
- {
- isAlive = false;
- }
- System.Threading.Thread.Sleep(250);
- if(collectDots == allDots || !isAlive)
- {
- isPlaying = false;
- }
- }
- Console.SetCursorPosition(0, 25);
- if(collectDots == allDots)
- {
- Console.WriteLine("Вы победили.");
- }
- else if (!isAlive)
- {
- Console.WriteLine("Вас съели");
- }
- }
- 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 random, ref int DX, ref int DY)
- {
- int ghostDir = random.Next(1, 5);
- switch (ghostDir)
- {
- 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 simbol,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(simbol);
- }
- static void CollectDots(char[,] map, int packmanX, int packmanY, ref int collectDots)
- {
- if (map[packmanX, packmanY] == '.')
- {
- collectDots++;
- map[packmanX, packmanY] = ' ';
- }
- }
- 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 ghostX, out int ghostY, ref int allDots)
- {
- packmanX = 0;
- packmanY = 0;
- ghostX = 0;
- ghostY = 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] == '$')
- {
- ghostX = i;
- ghostY = j;
- map[i, j] = '.';
- }
- else if(map[i, j] == ' ')
- {
- map[i, j] = '.';
- allDots++;
- }
- }
- }
- return map;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment