-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.cs
132 lines (105 loc) · 3.64 KB
/
pacman.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace CSharp
{
internal class Program
{
static void Main(string[] args)
{
Console.CursorVisible = false;
char[,] map = ReadMap("map.txt");
ConsoleKeyInfo pressedKey = new ConsoleKeyInfo('w', ConsoleKey.W, false, false, false);
Task.Run(() =>
{
while (true)
pressedKey = Console.ReadKey();
});
int pacmanX = 1;
int pacmanY = 1;
int score = 0;
while (true)
{
Console.Clear();
HandleInput(pressedKey, ref pacmanX, ref pacmanY, map, ref score);
Console.ForegroundColor = ConsoleColor.Magenta;
DrawMap(map);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(pacmanX, pacmanY);
Console.Write("@");
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(40, 0);
Console.Write($"Score: {score}");
Thread.Sleep(500);
}
}
private static char[,] ReadMap(string path)
{
string[] file = File.ReadAllLines("map.txt");
char[,] map = new char[GetMaxLengthOfLine(file), 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;
}
private 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");
}
}
private static void HandleInput(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];
if (nextCell == ' ' || nextCell == '.' )
{
pacmanX = nextPacmanPositionX;
pacmanY = nextPacmanPositionY;
if (nextCell == '.')
{
score++;
map[nextPacmanPositionX, nextPacmanPositionY] = ' ';
}
}
}
private static int[] GetDirection(ConsoleKeyInfo pressedKey)
{
int[] direction = { 0, 0 };
if (pressedKey.Key == ConsoleKey.UpArrow)
direction[1] = -1;
else if (pressedKey.Key == ConsoleKey.DownArrow)
direction[1] = 1;
else if (pressedKey.Key == ConsoleKey.LeftArrow)
direction[0] = -1;
else if (pressedKey.Key == ConsoleKey.RightArrow)
direction[0] = 1;
return direction;
}
private static int GetMaxLengthOfLine(string[] lines)
{
int maxLength = lines[0].Length;
foreach (var line in lines)
{
if (line.Length > maxLength)
{
maxLength = line.Length;
}
}
return maxLength;
}
}
}