-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cs
More file actions
86 lines (79 loc) · 2.41 KB
/
Copy pathMap.cs
File metadata and controls
86 lines (79 loc) · 2.41 KB
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
namespace PacMan;
public class Map
{
private const int tileSize = 30;
public char[,] map;
private int rows, cols;
public int DotCount { get; private set; }
public Map()
{
if (!LoadMap("../../../Map.txt"))
{
throw new Exception("Файл карты не найден");
}
}
public bool IsWall(int x, int y)
{
if (x < 0 || y < 0 || x >= map.GetLength(1) * tileSize || y >= map.GetLength(0) * tileSize)
return true;
int i = y / tileSize;
int j = x / tileSize;
return map[i, j] == '#';
}
private bool LoadMap(string filename)
{
List<string> lines = new();
if (!File.Exists(filename))
{
return false;
}
using (StreamReader reader = File.OpenText(filename))
{
string? str;
while ((str = reader.ReadLine()) != null)
{
lines.Add(str);
rows++;
if (str.Length > cols) cols = str.Length;
foreach (char c in str)
{
if (c == '.') DotCount++;
}
}
}
map = new char[rows, cols];
for (int i = 0; i < lines.Count; i++)
{
for (int j = 0; j < lines[i].Length; j++)
{
map[i, j] = lines[i][j];
}
}
return true;
}
public void DrawMap(Graphics g)
{
Pen pen = new Pen(Color.LightBlue);
Brush brWall = new SolidBrush(Color.Blue);
Brush brCoin = new SolidBrush(Color.PeachPuff);
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
if (map[i, j] == '#')
{
g.DrawRectangle(pen, tileSize * j, tileSize * i, tileSize, tileSize);
g.FillRectangle(brWall, tileSize * j + 1, tileSize * i + 1, tileSize - 1, tileSize - 1);
}
else if (map[i, j] == '.')
{
g.FillEllipse(brCoin, j * tileSize + 11, i * tileSize + 11, tileSize / 3, tileSize / 3);
}
else if (map[i, j] == '-')
{
g.FillRectangle(brCoin, j * tileSize, i * tileSize + tileSize / 3, tileSize, tileSize / 3);
}
}
}
}
}