Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace CSLight
- {
- internal class Program
- {
- static void Main()
- {
- char[,] map = ReadMap();
- ConsoleKeyInfo pressedKey;
- Console.CursorVisible = false;
- int pacmanX = 1;
- int pacmanY = 1;
- int score = 0;
- int maxScore = 13;
- while (score != maxScore)
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Cyan;
- DrawMap(map);
- DrawObjects(pacmanX, pacmanY, score);
- pressedKey = Console.ReadKey();
- WorkPacman(pressedKey, ref pacmanX, ref pacmanY, map, ref score);
- }
- Console.Clear();
- }
- static char[,] ReadMap()
- {
- string fileName = "map.txt";
- string[] file = File.ReadAllLines(fileName);
- char[,] map = new char[file[0].Length, file.Length];
- for (int x = 0; x < map.GetLength(0); x++)
- for (int y = 0; y < map.GetLength(1); y++)
- map[x, y] = file[y][x];
- return map;
- }
- static void DrawMap(char[,] map)
- {
- for (int y = 0; y < map.GetLength(1); y++)
- {
- for (int x = 0; x < map.GetLength(0); x++)
- {
- Console.Write(map[x, y]);
- }
- Console.Write("\n");
- }
- }
- static void DrawObjects(int pacmanX, int pacmanY, int score)
- {
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.SetCursorPosition(pacmanX, pacmanY);
- Console.Write("@");
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- Console.SetCursorPosition(32, 0);
- Console.Write($"Score: {score}");
- }
- static void WorkPacman(ConsoleKeyInfo pressedKey, ref int pacmanX, ref int pacmanY, char[,] map, ref int score)
- {
- int[] direction = GetDirection(pressedKey);
- int nextPacmanPositionX = pacmanX + direction[0];
- int nextPacmanPositionY = pacmanY + direction[1];
- char nextCell = map[nextPacmanPositionX, nextPacmanPositionY];
- MovePacman(nextCell, ref pacmanX, ref pacmanY, nextPacmanPositionX, nextPacmanPositionY);
- PickUpScore(ref score, ref nextCell);
- map[nextPacmanPositionX, nextPacmanPositionY] = nextCell;
- }
- static int[] GetDirection(ConsoleKeyInfo pressedKey)
- {
- int[] direction = { 0, 0 };
- int oneStep = 1;
- ConsoleKey upMove = ConsoleKey.UpArrow;
- ConsoleKey downMove = ConsoleKey.DownArrow;
- ConsoleKey leftMove = ConsoleKey.LeftArrow;
- ConsoleKey rightMove = ConsoleKey.RightArrow;
- if (pressedKey.Key == upMove)
- direction[1] = -oneStep;
- else if (pressedKey.Key == downMove)
- direction[1] = oneStep;
- else if (pressedKey.Key == leftMove)
- direction[0] = -oneStep;
- else if (pressedKey.Key == rightMove)
- direction[0] = oneStep;
- return direction;
- }
- static void MovePacman(char nextCell, ref int pacmanX, ref int pacmanY, int nextPositionX, int nextPositionY)
- {
- if (nextCell == ' ' || nextCell == '.')
- {
- pacmanX = nextPositionX;
- pacmanY = nextPositionY;
- }
- }
- static void PickUpScore(ref int score, ref char nextCell)
- {
- if (nextCell == '.')
- {
- score++;
- nextCell = ' ';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement