Advertisement
junniorrkaa

Brave new world

Jan 28th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | Source Code | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             char[,] map = ReadMap();
  11.             ConsoleKeyInfo pressedKey;
  12.  
  13.             Console.CursorVisible = false;
  14.  
  15.             int pacmanX = 1;
  16.             int pacmanY = 1;
  17.             int score = 0;
  18.             int maxScore = 13;
  19.  
  20.             while (score != maxScore)
  21.             {
  22.                 Console.Clear();
  23.  
  24.                 Console.ForegroundColor = ConsoleColor.Cyan;
  25.                 DrawMap(map);
  26.  
  27.                 DrawObjects(pacmanX, pacmanY, score);
  28.  
  29.                 pressedKey = Console.ReadKey();
  30.                 WorkPacman(pressedKey, ref pacmanX, ref pacmanY, map, ref score);
  31.             }
  32.  
  33.             Console.Clear();
  34.         }
  35.  
  36.         static char[,] ReadMap()
  37.         {
  38.             string fileName = "map.txt";
  39.  
  40.             string[] file = File.ReadAllLines(fileName);
  41.  
  42.             char[,] map = new char[file[0].Length, file.Length];
  43.  
  44.             for (int x = 0; x < map.GetLength(0); x++)
  45.                 for (int y = 0; y < map.GetLength(1); y++)
  46.                     map[x, y] = file[y][x];
  47.  
  48.             return map;
  49.         }
  50.  
  51.         static void DrawMap(char[,] map)
  52.         {
  53.             for (int y = 0; y < map.GetLength(1); y++)
  54.             {
  55.                 for (int x = 0; x < map.GetLength(0); x++)
  56.                 {
  57.                     Console.Write(map[x, y]);
  58.                 }
  59.  
  60.                 Console.Write("\n");
  61.             }
  62.         }
  63.  
  64.         static void DrawObjects(int pacmanX, int pacmanY, int score)
  65.         {
  66.             Console.ForegroundColor = ConsoleColor.Magenta;
  67.             Console.SetCursorPosition(pacmanX, pacmanY);
  68.             Console.Write("@");
  69.  
  70.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  71.             Console.SetCursorPosition(32, 0);
  72.             Console.Write($"Score: {score}");
  73.         }
  74.  
  75.         static void WorkPacman(ConsoleKeyInfo pressedKey, ref int pacmanX, ref int pacmanY, char[,] map, ref int score)
  76.         {
  77.             int[] direction = GetDirection(pressedKey);
  78.  
  79.             int nextPacmanPositionX = pacmanX + direction[0];
  80.             int nextPacmanPositionY = pacmanY + direction[1];
  81.  
  82.             char nextCell = map[nextPacmanPositionX, nextPacmanPositionY];
  83.  
  84.             MovePacman(nextCell, ref pacmanX, ref pacmanY, nextPacmanPositionX, nextPacmanPositionY);
  85.             PickUpScore(ref score, ref nextCell);
  86.  
  87.             map[nextPacmanPositionX, nextPacmanPositionY] = nextCell;
  88.         }
  89.  
  90.         static int[] GetDirection(ConsoleKeyInfo pressedKey)
  91.         {
  92.             int[] direction = { 0, 0 };
  93.  
  94.             int oneStep = 1;
  95.  
  96.             ConsoleKey upMove = ConsoleKey.UpArrow;
  97.             ConsoleKey downMove = ConsoleKey.DownArrow;
  98.             ConsoleKey leftMove = ConsoleKey.LeftArrow;
  99.             ConsoleKey rightMove = ConsoleKey.RightArrow;
  100.  
  101.             if (pressedKey.Key == upMove)
  102.                 direction[1] = -oneStep;
  103.             else if (pressedKey.Key == downMove)
  104.                 direction[1] = oneStep;
  105.             else if (pressedKey.Key == leftMove)
  106.                 direction[0] = -oneStep;
  107.             else if (pressedKey.Key == rightMove)
  108.                 direction[0] = oneStep;
  109.  
  110.             return direction;
  111.         }
  112.  
  113.         static void MovePacman(char nextCell, ref int pacmanX, ref int pacmanY, int nextPositionX, int nextPositionY)
  114.         {
  115.             if (nextCell == ' ' || nextCell == '.')
  116.             {
  117.                 pacmanX = nextPositionX;
  118.                 pacmanY = nextPositionY;
  119.             }
  120.         }
  121.  
  122.         static void PickUpScore(ref int score, ref char nextCell)
  123.         {
  124.             if (nextCell == '.')
  125.             {
  126.                 score++;
  127.                 nextCell = ' ';
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement