前言

想学一点大项目的编程,原因是在数学学院只学会了算法,对于一些大项目的处理略显得吃力。

还是很好玩的,突然发现自己设置的游戏好难,连自己的AI都赢不了。(我那个算什么辣鸡AI呢?)

下面是B站视频教程

需要额外安装easyx函数库

自己对游戏稍做了修改,增强了AI的追踪能力,虽然追踪看上去很弱鸡,但是被几个傻子壁咚肯定不好受(\汗)

下面是游戏地图,当然可以自己下载其他地图,代码鲁棒性还行。

map

代码

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdio.h>
#include <easyx.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include <conio.h>
#include <time.h>
#include<Windows.h>

//定义数据
#define WIDTH 960
#define HEIGHT 630
#define SIZE 14
#define speed 2
#define ENEMY_NUM 15 //敌人数量
IMAGE mapImg; //定义地图图片
unsigned long tt1, tt2;
#define dtt 400 //敌人换方向速度
int success = 0; //判断游戏是否胜利

//定义玩家
typedef struct Player
{
int x, y; //玩家坐标
IMAGE img; //玩家图片
int dirx, diry; //方向
unsigned long t1, t2, dt;//dt = t2 - t1 速度
}Player;

Player player;
Player enemy[ENEMY_NUM];

//定义豆子 后面想定义就定义吧,游戏已经很难了
typedef struct Food
{
int x, y; //豆子坐标
bool exist; //豆子是否存在
}Food;

Food food;

//次符号函数
int sign(int x)
{
if (x >= 0)
{
return 1;
}
else
{
return -1;
}
}

//初始化数据
void GameInit()
{
//初始化游戏窗口
initgraph(WIDTH, HEIGHT);

//初始化背景图片
loadimage(&mapImg, "../map.png");
putimage(0, 0, &mapImg);

//加载播放音乐
//mciSendString("open ../321.mp3", 0, 0, 0);
//mciSendString("play ../321.mp3", 0, 0, 0);

//初始化玩家
loadimage(&player.img, "../laugh.png", SIZE, SIZE);
player.x = 70;
player.y = 30;
player.dirx = 0;
player.diry = 0; //右方
player.t1 = GetTickCount();
player.dt = 5; //ms

//初始化敌人
srand(time(0));
for (int i = 0; i < ENEMY_NUM; i++)
{
loadimage(&enemy[i].img, "../killer.jpg", SIZE, SIZE);
enemy[i].t1 = GetTickCount();
enemy[i].dt = 5; //ms

//初始化方向
int n = rand() % 4;
if (n == 0)
{
enemy[i].dirx = 1;
enemy[i].diry = 0; //右方
}
else if (n == 1)
{
enemy[i].dirx = -1;
enemy[i].diry = 0; //左方
}
else if (n == 2)
{
enemy[i].dirx = 0;
enemy[i].diry = 1; //上方
}
else
{
enemy[i].dirx = 0;
enemy[i].diry = -1; //下方
}

//初始化位置
while (1)
{
enemy[i].x = rand() % (WIDTH);
enemy[i].y = rand() % (HEIGHT);
if (getpixel(enemy[i].x + SIZE / 2, enemy[i].y + SIZE / 2) != WHITE)
{
continue;
}
else
{
break;
}

}
}

tt1 = GetTickCount();
}

//显示程序
void GameDraw()
{
//开始绘制
BeginBatchDraw();

//绘制背景图片
putimage(0, 0, &mapImg);

//绘制玩家
putimage(player.x, player.y, &player.img);

//绘制敌人
for (int i = 0; i < ENEMY_NUM; i++)
{
//putimage(enemy[i].x, enemy[i].y, &enemy[i].img);
setfillcolor(RED);
fillroundrect(enemy[i].x, enemy[i].y, enemy[i].x + SIZE, enemy[i].y + SIZE, 5, 5);
}

//结束绘制
EndBatchDraw();
}

//数据变化
void GameUpdate()
{
//手动更新
char key;
if (_kbhit())
{
key = _getch();
switch (key)
{
case 72: //上
player.dirx = 0;
player.diry = -1;
break;
case 80: //下
player.dirx = 0;
player.diry = 1;
break;
case 75: //左
player.dirx = -1;
player.diry = 0;
break;
case 77: //右
player.dirx = 1;
player.diry = 0;
break;
/*
case 32: //空格
break;
case 56:
break;
case 'F':
break;
*/
default:
break;
}
}

//玩家数据变化
//普通移动
player.t2 = GetTickCount();
if (player.t2 - player.t1 >= player.dt)
{
player.x = speed * player.dirx + player.x;
player.y = speed * player.diry + player.y;
player.t1 = player.t2;
}

//玩家碰壁后,方向相反
int cen_x = player.x + SIZE / 2;
int cen_y = player.y + SIZE / 2;

if (player.dirx == 1 || player.dirx == -1)
{
for (int yy = cen_y - SIZE / 2; yy <= cen_y + SIZE / 2; yy++)
{
if (getpixel(cen_x + (SIZE / 2 + speed) * player.dirx, yy) != WHITE)
{
if (getpixel(cen_x + (SIZE / 2 + speed) * player.dirx, yy) == RGB(0, 0, 170))
{
success = 1;
}
else if (getpixel(cen_x + (SIZE / 2 + speed) * player.dirx, yy) == RED)
{
success = -1;
}
else
{
player.dirx = -player.dirx;
}
break;
}
}
}
else if (player.diry == 1 || player.diry == -1)
{
for (int xx = cen_x - SIZE / 2; xx <= cen_x + SIZE / 2; xx++)
{
if (getpixel(xx, cen_y + (SIZE / 2 + speed) * player.diry) != WHITE)
{
if (getpixel(xx, cen_y + (SIZE / 2 + speed) * player.diry) == RGB(0, 0, 170))
{
success = 1;
}
else if (getpixel(xx, cen_y + (SIZE / 2 + speed) * player.diry) == RED)
{
success = -1;
}
else
{
player.diry = -player.diry;
}
break;
}
}
}

//敌人数据变化
for (int i = 0; i < ENEMY_NUM; i++)
{
enemy[i].t2 = GetTickCount();
if (enemy[i].t2 - enemy[i].t1 >= enemy[i].dt)
{
enemy[i].x = speed * enemy[i].dirx + enemy[i].x;
enemy[i].y = speed * enemy[i].diry + enemy[i].y;
enemy[i].t1 = enemy[i].t2;
}

//玩家碰壁后,方向相反
int cen_x = enemy[i].x + SIZE / 2;
int cen_y = enemy[i].y + SIZE / 2;

if (enemy[i].dirx == 1 || enemy[i].dirx == -1)
{
for (int yy = cen_y - SIZE / 2; yy <= cen_y + SIZE / 2; yy++)
{
if (getpixel(cen_x + (SIZE / 2 + speed) * enemy[i].dirx, yy) != WHITE)
{
enemy[i].dirx = -enemy[i].dirx;
break;
}
}
}
else if (enemy[i].diry == 1 || enemy[i].diry == -1)
{
for (int xx = cen_x - SIZE / 2; xx <= cen_x + SIZE / 2; xx++)
{
if (getpixel(xx, cen_y + (SIZE / 2 + speed) * enemy[i].diry) != WHITE)
{
enemy[i].diry = -enemy[i].diry;
break;
}
}
}
}

//改变敌人运动方向 失北
tt2 = GetTickCount();
if (tt2 - tt1 > dtt)
{
for (int i = 0; i < ENEMY_NUM; i++)
{
int n = rand() % 2;
if (n == 0)
{
enemy[i].dirx = 1 * sign(player.x - enemy[i].x);
enemy[i].diry = 0; //左方或右方
}
else
{
enemy[i].dirx = 0;
enemy[i].diry = 1 * sign(player.y - enemy[i].y); //上方或下方
}
}
tt1 = tt2;
}
}

//结束游戏
void GameOver()
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (success == -1)
{
MessageBox(0, "滚蛋吧你", "游戏失败", 0);
}
}
if (success == 1)
{
MessageBox(0, "门在那边", "游戏成功", 0);
}
}

int main()
{
GameInit(); //初始化

while (1)
{
GameDraw(); //绘制
GameUpdate(); //更新数据
GameOver(); //结束游戏
}

return 0;
}

有点难度,红点追着你打

游戏实况