-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
133 lines (108 loc) · 2.64 KB
/
Program.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
133
using System;
internal class Program
{
static bool shouldRun = true;
static ConsoleKey pressedKey = new();
static char[,] map = ReadMap("map.txt");
static int pacmanX = 1;
static int pacmanY = 1;
static int score = 0;
private static void Main(string[] args)
{
Task.Run(() =>
{
while (shouldRun)
{
pressedKey = Console.ReadKey().Key;
}
});
Console.CursorVisible = false;
while (shouldRun)
{
Console.Clear();
HandleInput();
DrawMap();
DrawPlayer();
DrawScore();
Thread.Sleep(250);
}
Console.CursorVisible = true;
}
static void DrawScore()
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.SetCursorPosition(0, map.GetLength(1));
Console.WriteLine($"Score: {score}");
}
static void HandleInput()
{
int[] direction = new int[2];
switch (pressedKey)
{
case ConsoleKey.UpArrow: direction[1] = -1; break;
case ConsoleKey.DownArrow: direction[1] = 1; break;
case ConsoleKey.LeftArrow: direction[0] = -1; break;
case ConsoleKey.RightArrow: direction[0] = 1; break;
case ConsoleKey.Escape or ConsoleKey.Q:
shouldRun = false; return;
default: break;
}
int pacmanXNext = pacmanX + direction[0];
int pacmanYNext = pacmanY + direction[1];
char mapChar = map[pacmanXNext, pacmanYNext];
if (mapChar == '#' ||
pacmanXNext < 0 || pacmanXNext >= map.GetLength(0) ||
pacmanYNext < 0 || pacmanYNext >= map.GetLength(1))
return;
pacmanX = pacmanXNext;
pacmanY = pacmanYNext;
if (mapChar == '·')
{
score++;
map[pacmanX, pacmanY] = ' ';
}
}
static void DrawPlayer()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(pacmanX, pacmanY);
Console.Write("@");
}
static void DrawMap()
{
Console.ForegroundColor = ConsoleColor.Blue;
for (int y = 0; y < map.GetLength(1); y++)
{
for (int x = 0; x < map.GetLength(0); x++)
{
Console.Write(map[x, y]);
}
Console.WriteLine();
}
}
static char[,] ReadMap(string path)
{
string[] file = File.ReadAllLines(path);
char[,] map = new char[GetMaxLenghtOfLine(file), file.Length];
for (int y = 0; y < map.GetLength(1); y++)
{
for (int x = 0; x < map.GetLength(0); x++)
{
map[x, y] = file[y][x];
}
}
return map;
}
static int GetMaxLenghtOfLine(string[] lines)
{
int maxLenght = 0;
foreach (var line in lines)
{
if (line.Length > maxLenght)
{
maxLenght = line.Length;
}
}
return maxLenght;
}
}