Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp37
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- char[,] map =
- {
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
- {'#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ',' ',' ','@',' ',' ',' ',' ',' ','#','#',' ',' ',' ',' ','#'},
- {'#',' ','#','#',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#'},
- {'#',' ',' ','#','#','#',' ','#','#','#','#','#','#',' ','#',' ','#',' ',' ','#'},
- {'#',' ',' ','#','#','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ','#','#','#','#',' ',' ',' ','#',' ','#',' ',' ','#'},
- {'#',' ',' ',' ','$',' ',' ','#','#','#','#',' ',' ',' ',' ',' ','#',' ',' ','#'},
- {'#','#','#','#',' ',' ',' ','#','#','#','#',' ',' ',' ','#','#','#',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ','#','#','#','#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#','#','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#',' ','#',' ',' ',' ','#','#','#','#','#','#','#',' ',' ',' ','#'},
- {'#',' ',' ','#',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#',' ',' ','#','#','#',' ','#',' ','#',' ','#','#','#','#','#','#','#',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#','#','#','#',' ','#'},
- {'#','#','#','#','#','#','#','#',' ','#',' ',' ',' ',' ',' ',' ','#','#',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
- };
- int currentCoin = 0;
- int maxCoin = 0;
- int pacmanX;
- int pacmanY;
- bool isPlaying = true;
- int directionX = 0;
- int directionY = 1;
- bool isAlive = true;
- int ghostX;
- int ghostY;
- int dirGhostX = 0;
- int dirGhostY = -1;
- Random rand = new Random();
- Drawmap(map, ref maxCoin, out pacmanX, out pacmanY, out ghostX, out ghostY);
- while (isPlaying)
- {
- Console.SetCursorPosition(0, 23);
- Console.WriteLine($"собрано:{currentCoin}/{ maxCoin}");
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(ref key, ref directionX, ref directionY);
- }
- if (map[pacmanX + directionX, pacmanY + directionY] != '#')
- {
- CollectCoin(map, ref currentCoin, pacmanX, pacmanY);
- Move(map, ref pacmanX, ref pacmanY, directionX, directionY, '@');
- }
- if (map[ghostX + dirGhostX, ghostY + dirGhostY] != '#')
- {
- Move(map, ref ghostX, ref ghostY, dirGhostX, dirGhostY, '$');
- }
- else
- {
- ChangeDirection(rand, ref dirGhostX, ref dirGhostY);
- }
- System.Threading.Thread.Sleep(150);
- if (ghostX == pacmanX && ghostY == pacmanY)
- {
- isAlive = false;
- }
- if (currentCoin == maxCoin && isAlive)
- {
- isPlaying = false;
- Console.SetCursorPosition(0, 24);
- Console.WriteLine("ВЫ ПОБЕДИЛИ!");
- }
- else if (!isAlive)
- {
- isPlaying = false;
- Console.SetCursorPosition(0, 24);
- Console.WriteLine("ВЫ ПРОИГРАЛИ");
- }
- }
- Console.ReadKey();
- }
- static void CollectCoin(char[,] map, ref int currentCoin, int pacmanX, int pacmanY)
- {
- if (map[pacmanX, pacmanY] == '.')
- {
- map[pacmanX, pacmanY] = ' ';
- currentCoin++;
- }
- }
- static void ChangeDirection(ref 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 ghostDirection = rand.Next(1, 5);
- switch (ghostDirection)
- {
- 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, ref int X, ref int Y, int dX, int dY, char symbol)
- {
- Console.SetCursorPosition(Y, X);
- Console.Write(map[X, Y]);
- X += dX;
- Y += dY;
- Console.SetCursorPosition(Y, X);
- Console.WriteLine(symbol);
- }
- static void Drawmap(char[,] map, ref int maxCoin, out int pacmanX, out int pacmanY, out int ghostX, out int ghostY)
- {
- pacmanX = 0;
- pacmanY = 0;
- ghostX = 0;
- ghostY = 0;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (map[i, j] == '@')
- {
- pacmanX = i;
- pacmanY = j;
- map[i, j] = '.';
- }
- else if (map[i, j] == '$')
- {
- ghostX = i;
- ghostY = j;
- map[i, j] = '.';
- }
- else if (map[i, j] == ' ')
- {
- map[i, j] = '.';
- maxCoin++;
- }
- Console.Write(map[i, j]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement