-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormMain.cs
More file actions
176 lines (159 loc) · 4.99 KB
/
Copy pathFormMain.cs
File metadata and controls
176 lines (159 loc) · 4.99 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.CodeDom;
using System.Drawing;
using System.Net.Http.Headers;
using System.Windows.Forms.Automation;
namespace PacMan
{
public partial class FormMain : Form
{
const int tileSize = 30;
int startX = 10; int startY = 9;
Map map;
PacMan pacman;
List<Enemy> enemyList;
private System.Windows.Forms.Timer timer;
int dots = 0;
int score = 0;
int lives = 3;
bool gameOver = false;
public FormMain()
{
InitializeComponent();
StartGame();
}
private void StartGame()
{
score = 0;
dots = 0;
gameOver = false;
map = new();
labelGameOver.Visible = false;
map.map[startY, startX] = ' ';
dots++;
Reset();
if (timer == null)
{
timer = new System.Windows.Forms.Timer();
timer.Interval = 32;
timer.Tick += Loop;
}
timer.Start();
}
private void Loop(object sender, EventArgs e)
{
pacman.MoveTo(pacman.Direction, map);
foreach (Enemy enemy in enemyList)
{
float dx = enemy.GetX - pacman.GetX;
float dy = enemy.GetY - pacman.GetY;
if (dx == 0)
{
if (dy < 0) enemy.MoveTo(DirectionType.Down, map);
else enemy.MoveTo(DirectionType.Up, map);
}
else if (dy == 0)
{
if (dx < 0) enemy.MoveTo(DirectionType.Right, map);
else enemy.MoveTo(DirectionType.Left, map);
}
else enemy.MoveTo(enemy.Direction, map);
}
UpdateMap();
}
private void Reset()
{
enemyList = new List<Enemy>();
enemyList.Add(new Enemy(8 * tileSize, 3 * tileSize, 5, Color.Red, tileSize));
enemyList.Add(new Enemy(9 * tileSize, 3 * tileSize, 3, Color.Blue, tileSize));
enemyList.Add(new Enemy(10 * tileSize, 3 * tileSize, 3, Color.Green, tileSize));
pacman = new(tileSize * 10, tileSize * 9, tileSize);
toolStripStatusLabelLives.Text = "Lives: " + lives;
Draw();
}
private void UpdateMap()
{
int i = (pacman.GetY + pacman.GetSide / 2) / tileSize;
int j = (pacman.GetX + pacman.GetSide / 2) / tileSize;
if (map.map[i, j] == '.')
{
map.map[i, j] = ' ';
dots++;
score += 10;
}
toolStripStatusLabelScore.Text = "Score: " + score;
if (IsCollision(enemyList, pacman))
{
lives--;
Reset();
}
if (dots >= map.DotCount || lives < 0)
{
gameOver = true;
}
Draw();
}
private void Draw()
{
if (gameOver)
{
timer.Stop();
labelGameOver.Visible = true;
}
Bitmap bitmap = new Bitmap(pictureBoxBoard.Width, pictureBoxBoard.Height);
Graphics g = Graphics.FromImage(bitmap);
map.DrawMap(g);
pacman.Draw(g);
DrawEnemies(enemyList, g);
pictureBoxBoard.Image = bitmap;
}
private void DrawEnemies(List<Enemy> enemies, Graphics g)
{
foreach(Enemy enemy in enemies)
{
if (enemy != null)
{
enemy.Draw(g);
}
}
}
private bool IsCollision(List<Enemy> enemies, PacMan player)
{
foreach(Enemy enemy in enemies)
{
if (enemy == null)
return false;
float dx = Math.Abs(enemy.GetX - player.GetX);
float dy = Math.Abs(enemy.GetY - player.GetY);
if (dx < player.GetSide && dy < player.GetSide)
return true;
}
return false;
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (pacman == null)
{
return;
}
switch (e.KeyCode)
{
case Keys.Escape:
StartGame();
break;
case Keys.Down:
pacman.MoveTo(DirectionType.Down, map);
break;
case Keys.Up:
pacman.MoveTo(DirectionType.Up, map);
break;
case Keys.Left:
pacman.MoveTo(DirectionType.Left, map);
break;
case Keys.Right:
pacman.MoveTo(DirectionType.Right, map);
break;
}
Draw();
}
}
}