黄金矿工easyx

《高级语言程序课设》课设作业,基于easyx模拟黄金矿工,是我的第一个编程项目,虽然很简单而且存在很多问题,但还是把代码留存吧。

GoldMiner.h

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
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<easyx.h>
#include<Windows.h>
#include<graphics.h>
#include<string.h>
#include<time.h>
#include<math.h>
#pragma comment(lib,"Winmm.lib")

#define WIDTH 850
#define HEIGHT 700
#define PI 3.1415926

void init(); //初始化图形界面

void drawAlpha(IMAGE* picture, int picture_x, int picture_y);//载入PNG图片,并去除空白部分

void input();//获取用户名和密码

void login(); //登陆

void start(); //开始游戏界面

void gameGUI(); //游戏界面

void hook(); //钩子

void updateWithoutInput(); //与用户输入无关的更新

void updateWithInput(); //与用户输入有关的更新

void playGame(); //游戏部分

void gameOver(); //游戏结束

GoldMiner.cpp

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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#define _CRT_SECURE_NO_WARNINGS	
#include "GoldMiner.h"

/***********************************结构体及变量定义************************************/
typedef struct edge { //矿石边缘
int left;
int top;
int right;
int bottom;
}EDGE;

typedef struct thing { //矿石结构体
IMAGE img;
int x; //左上角横坐标
int y; //左上角纵坐标
int flag = 1; //矿石状态
int score; //分数
int weight; //重量
EDGE e; //边缘(左,上,右,下)
}TH;

typedef struct users { //定义用户结构体
char id[20];
char pwd[20];
}USERS;

struct Node { /*单链表节点结构*/
TH *info; /*存储地址,直接存储值会抛出异常*/
struct Node * link;
};

typedef struct Node* LinkList; /*单链表类型*/

int centerx = 400, centery = 150; //钩子旋转中心坐标
int hookx = 390, hooky = 155; //钩子顶点坐标
int r = 30, angle = 0; //钩子转动半径、角度
int angle_v = 1, dig_v = 5; //钩子转动速度、挖矿速度
int flag_dig = 0; //是否向下挖
int number = 0,money=0, count = 0; //抓到矿石个数、金钱、简单倒计时所用常量
int num_s = 0, num_d = 0, num_g = 0; //存储抓取到的石头、钻石、金子个数
int checkpoint = 1; //关卡
int retime = 0; //倒计时
int target_money = 1000; //目标金币
TH prop1[10]; //加载矿石
IMAGE temp2; //用于钩子抓取到矿石后收回

/********************************链表相关函数***************************************/
LinkList createNullList_link(void) {/*创建空链表*/
LinkList llist = (LinkList)malloc(sizeof(struct Node)); /*申请表头节点空间*/
llist->link = NULL;
return llist;
}

LinkList createNode(TH* data) { /*创建节点*/
LinkList newNode = (LinkList)malloc(sizeof(struct Node));
newNode->info = data;
newNode->link = NULL;
return newNode;
}

void insertNodeByHead(LinkList headNode, TH* data) { //插入节点,头插法
LinkList newNode = createNode(data);
newNode->link = headNode->link;
headNode->link = newNode;
}


/**********************************工具函数*****************************************/

void drawAlpha(IMAGE* picture, int picture_x, int picture_y) //载入PNG图并去透明部分,x为载入图片的X坐标,y为Y坐标
{

// 变量初始化
DWORD* dst = GetImageBuffer(); // GetImageBuffer()函数,用于获取绘图设备的显存指针,EASYX自带
DWORD* draw = GetImageBuffer();
DWORD* src = GetImageBuffer(picture); //获取picture的显存指针
int picture_width = picture->getwidth(); //获取picture的宽度,EASYX自带
int picture_height = picture->getheight(); //获取picture的高度,EASYX自带
int graphWidth = getwidth(); //获取绘图区的宽度,EASYX自带
int graphHeight = getheight(); //获取绘图区的高度,EASYX自带
int dstX = 0; //在显存里像素的角标

// 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
for (int iy = 0; iy < picture_height; iy++)
{
for (int ix = 0; ix < picture_width; ix++)
{
int srcX = ix + iy * picture_width; //在显存里像素的角标
int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
int sg = ((src[srcX] & 0xff00) >> 8); //G
int sb = src[srcX] & 0xff; //B
if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
{
dstX = (ix + picture_x) + (iy + picture_y) * graphWidth; //在显存里像素的角标
int dr = ((dst[dstX] & 0xff0000) >> 16);
int dg = ((dst[dstX] & 0xff00) >> 8);
int db = dst[dstX] & 0xff;
draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16) //公式: Cp=αp*FP+(1-αp)*BP ; αp=sa/255 , FP=sr , BP=dr
| ((sg * sa / 255 + dg * (255 - sa) / 255) << 8) //αp=sa/255 , FP=sg , BP=dg
| (sb * sa / 255 + db * (255 - sa) / 255); //αp=sa/255 , FP=sb , BP=db
}
}
}
}

void playMusic() {
PlaySound("src\\gameGUI\\music.wav", NULL, SND_ASYNC | SND_FILENAME | SND_LOOP);//异步播放背景音乐
}


/******************************程序功能函数*****************************************/
void init() {
initgraph(WIDTH, HEIGHT/*, SHOWCONSOLE*/);
}

void input()//获取用户输入
{
int loginCheck(char id[], char pwd[]);
wchar_t c = 0;
int startx = 105;
USERS a, b;
void start();
while (true) //startx<250
{
if (MouseHit()) {
MOUSEMSG msg = GetMouseMsg();
if (_kbhit() || ((msg.uMsg == WM_LBUTTONDOWN) && msg.x >= 100 && msg.x <= 250 && msg.y >= 500 && msg.y <= 525))
{
settextstyle(15, 0, "consoals"); //设置为等宽字体
for (int i = 0; i < 20; i++) {
c = _getch();
a.id[i] = c;
if (c == 9 || c == 7 || c == 13 || ((msg.uMsg == WM_LBUTTONDOWN) && msg.x >= 100 && msg.x <= 250 && msg.y >= 535 && msg.y <= 560)) //tab或esc或enter或者密码输入框内点击
break;
if (c == 8 && startx >= 100) { //backspace
setfillcolor(BLACK);
solidrectangle(startx - 7, 501, startx, 524); //覆盖上一个字符的位置
startx -= 7;
continue;
}

outtextxy(startx, 503, a.id[i]);
startx += 7;
}

startx = 105;
for (int i = 0; i < 20; i++) {
c = _getch();
if (c == 9 || c == 7) //tab或esc
break;
if (c == 8 && startx >= 100) { //backspace
setfillcolor(BLACK);
solidrectangle(startx - 7, 536, startx, 559);
startx -= 7;
continue;
}
a.pwd[i] = c;
outtextxy(startx, 538, _T("*"));
startx += 7;
if (c == 13 || ((msg.uMsg == WM_LBUTTONDOWN) && msg.x >= 178 && msg.x <= 265 && msg.y >= 139 && msg.y <= 193)||(loginCheck(a.id,a.pwd))) //如果用户输入enter或者点击“开始”,调用start()函数
{
start(); break;
}
}
}
}
}
}

int loginCheck(char id[],char pwd[]) {
FILE* fp;
int flag = 0;
char s[20] = { 0 };
char u[20] = { 0 };
fp = fopen("info.txt", "r");
if (fp != NULL) {
while (!feof(fp)) {
fgets(s, sizeof(s), fp);
fgets(u, sizeof(u), fp);
}
if (strcmp(s, id)==0 && strcmp(u, pwd)==0) {
flag = 1;
}
else {
flag = 0;
}
}
return flag;
}

void login() //登陆界面
{
init();
IMAGE img1, img2, img3;
loadimage(&img1, "src\\login\\01.png", WIDTH, HEIGHT, true);
loadimage(&img2, "src\\login\\02.png", 100, 50, true);
loadimage(&img3, "src\\login\\03.png", 180, 120, true);
putimage(0, 0, &img1); //登陆主界面
putimage(125, 440, &img2); //贴图login
drawAlpha(&img3, 260, 240);
//setfillcolor(RGB(254, 219, 35));
setfillcolor(BLACK);
fillrectangle(100, 500, 250, 525); //用户名输入框
fillrectangle(100, 535, 250, 560); //密码输入框
setfillcolor(BLACK);

RECT account = { 30, 500, 85, 525 };
RECT password = { 30, 535, 85, 560 };
drawtext(_T("用户名:"), &account, DT_SINGLELINE | DT_VCENTER);
drawtext(_T("密 码:"), &password, DT_SINGLELINE | DT_VCENTER);
setcolor(WHITE);
input();
/*system("pause")*/;
}

void start() { //游戏介绍界面
cleardevice();//清空之前绘制窗体
FlushMouseMsgBuffer();//清空鼠标消息缓冲区
IMAGE img, img2, img3, img4;
loadimage(&img, "src\\start\\01.png"/*, WIDTH, HEIGHT, true*/);
loadimage(&img2, "src\\start\\02.png", 200, 100, true);
loadimage(&img3, "src\\start\\03.png");
loadimage(&img4, "src\\start\\04.png");
putimage(0, 0, &img);
drawAlpha(&img2, 335, 92);//边框花纹
setfillcolor(RGB(0, 122, 204));
solidrectangle(85, 175, 780, 620);//游戏介绍背景
drawAlpha(&img3, 60, 172);
drawAlpha(&img4, 350, 585);//开始游戏按钮
setcolor(WHITE);
settextstyle(25, 0, "simsun");
setbkmode(TRANSPARENT);//背景透明
RECT info1 = { 175,265,690,365 };
drawtext(_T("\t黄金矿工是一款益智休闲类游戏,在游戏中,\n玩家通过不断挖矿,获取金子,只有在一定的\n时间内金子数量达到要求才能够进入下一关。\n"), &info1, DT_LEFT);
RECT info2 = { 175,370,690,410 };
settextstyle(30, 0, "simsun");
drawtext(_T("操作介绍:"), &info2, DT_LEFT);
RECT info3{ 175, 420, 690, 460 };
drawtext(_T("按\"S\"键开始捞金子"), &info3, DT_CENTER);
/*RECT info4 = { 175,465,690,540 };
drawtext(_T("按\"↑\"键放炸弹(如果有)"), &info4, DT_CENTER);*/
RECT info5 = { 175,540,690,600 };
settextstyle(25, 0, "TimesNewRoman");
drawtext(_T("Have a nice day!"), &info5, DT_CENTER | DT_VCENTER);
while (true) {
MOUSEMSG msg = GetMouseMsg();
if ((msg.uMsg == WM_LBUTTONDOWN) && msg.x >= 350 && msg.x <= 512 && msg.y >= 585 && msg.y <= 627) {
playGame();
break;
}
if (_kbhit()) {
wchar_t c = _getch();
if (c == 13)
{
playGame();
break;
}
}
}
}

void gameGUI() {
IMAGE img1, img2, img3;
loadimage(&img1, "src\\gameGUI\\01.jpg", WIDTH, HEIGHT - 150, true); //背景
loadimage(&img2, "src\\gameGUI\\02.png", WIDTH, 150, true);
loadimage(&img3, "src\\gameGUI\\hook.png");
drawAlpha(&img3, 0, 0);
putimage(0, 0, &img2);
putimage(0, 150, &img1);
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 2);
line(430, 100, 430, 155); //画滚筒和钩子之间的连线
}

void loadMines() { //加载矿石图片
loadimage(&prop1[0].img, "src\\stones\\stone1.png");
loadimage(&prop1[1].img, "src\\stones\\stone2.png");
loadimage(&prop1[2].img, "src\\stones\\stone3.png");
loadimage(&prop1[3].img, "src\\stones\\stone4.png");
loadimage(&prop1[4].img, "src\\stones\\gold1.png");
loadimage(&prop1[5].img, "src\\stones\\gold2.png");
loadimage(&prop1[6].img, "src\\stones\\gold3.png");
loadimage(&prop1[7].img, "src\\stones\\gold4.png");
loadimage(&prop1[8].img, "src\\stones\\gold5.png");
loadimage(&prop1[9].img, "src\\stones\\diamond.png");
}

void initMines() { //初始化矿石相关参数
loadMines();
srand((unsigned)time(NULL));
for (int i = 0; i < 10; i++) { //设置矿石边缘
prop1[i].e.left = prop1[i].x = rand() % (WIDTH-200)+100;
prop1[i].e.top = prop1[i].y = rand() % (HEIGHT-350) + 200;
}
for (int i = 0; i < 10; i++) {
prop1[i].e.right = prop1[i].e.left + prop1[i].img.getwidth();
prop1[i].e.bottom = prop1[i].e.top + prop1[i].img.getheight();
}
//初始化矿石分数、重量
for (int i = 0; i < 4; i++) {
prop1[i].score = 50 * (i + 1); //石头
prop1[i].weight = i + 1;
}
for (int i = 4; i < 9; i++) { //黄金
prop1[i].score = 100 * (i - 3);
prop1[i].weight = i - 3;
}
prop1[9].score = 500; //钻石
prop1[9].weight = 0;
}

void putMines() {
for (int i = 0; i < 10; i++) { //绘制矿石
if (prop1[i].flag != 0) {
if(prop1[i].y >= 150)
drawAlpha(&prop1[i].img, prop1[i].x, prop1[i].y);
}
}
}

void victory() {
cleardevice();
settextcolor(RGB(181, 136, 47));
settextstyle(60, 0, _T("Consolas"));
IMAGE img;
loadimage(&img, "src\\gameOver\\victory.jpg");
putimage(0, 0, &img);
outtextxy(160, 370, _T("你获得金币:"));
char s[10];
outtextxy(500, 370, _itoa(money, s, 10));
outtextxy(500, 370, _itoa(money, s, 10));
outtextxy(160, 440, _T("挖到金块:"));
outtextxy(450, 440, _itoa(num_g, s, 10));
outtextxy(160, 510, _T("挖到石头:"));
outtextxy(450, 510, _itoa(num_s, s, 10));
outtextxy(160, 580, _T("挖到钻石:"));
outtextxy(450, 580, _itoa(num_d, s, 10));
Sleep(6000);
gameOver();
}

void defeat() {
cleardevice();
settextcolor(RGB(181, 136, 47));
settextstyle(60, 0, _T("Consolas"));
IMAGE img;
loadimage(&img, "src\\gameOver\\defeat.jpg");
putimage(0, 0, &img);
outtextxy(160, 370, _T("你获得金币:"));
char s[10];
outtextxy(500, 370, _itoa(money, s, 10));
outtextxy(160, 440, _T("挖到金块:"));
outtextxy(450, 440, _itoa(num_g, s, 10));
outtextxy(160, 510, _T("挖到石头:"));
outtextxy(450, 510, _itoa(num_s, s, 10));
outtextxy(160, 580, _T("挖到钻石:"));
outtextxy(450, 580, _itoa(num_d, s, 10));
Sleep(6000);
gameOver();
}

int isCatched(int x,int y, EDGE e) { //判断是否抓取到矿石
int flag = 0;
if (x >= e.left && x <= e.right && y >= e.top && y <= e.bottom)
flag = 1;
return flag;
}

void updateWithoutInput() { //与用户输入无关的更新
BeginBatchDraw();
hook();
}

void updateWithInput() { //与用户输入有关的更新

int flag = 1, root = r, m = -1;
char kb,s2[10];
IMAGE tmp2;
loadimage(&tmp2, "src\\temp\\tmp2.jpg");
LinkList list = createNullList_link();
if (_kbhit()) {
kb = _getch();
if (kb == 'S' || kb == 's') {
flag_dig = 1; //下挖,将flag设置为1
while (flag) {
r += dig_v;
hook();
for (int i = 0; i < 10; i++) { //抓取到
//根据hookx和hooky计算钩子的中心位置,据此判断是否抓取到矿石
int judge_x = hookx+30, judge_y = hooky+27;
if (prop1[i].flag != 0 && isCatched(judge_x, judge_y, prop1[i].e)) {
prop1[i].flag = 2;
//dig_v -= prop1[i].weight;
dig_v = -dig_v; //收回钩子
m = i;
//dig_v += (10 - prop1[m].weight);
}
}
if ((hookx+65) >= 850 || hookx <= 0 || (hooky+40) >= 700) dig_v = -dig_v; //碰到边界收回钩子
if (r == root) {
dig_v = -dig_v;
flag = 0; //收到原来的r时停止
flag_dig = 0; //停止下挖dig
if (m != -1) { //m必须有初值 否则遇到没有抓到东西时会出错
prop1[m].flag = 0;
putimage(0, 0, &tmp2);
money += prop1[m].score;
insertNodeByHead(list, &prop1[m]);
outtextxy(100, 8, _itoa(money, s2, 10));
switch (m)
{
case 0:
case 1:
case 2:
case 3: num_s += 1; break;
case 4:
case 5:
case 6:
case 7:
case 8: num_g += 1; break;
case 9: num_d += 1; break;
}
m = -1;
number++;
}
}
count++; //模拟倒计时
if (count % 50 == 0) retime--;
IMAGE img;
char s[10];
loadimage(&img, "src\\gameGUI\\time.jpg");
putimage(700, 0, &img);
outtextxy(780, 8, _itoa(retime, s, 10));
putimage(0, 150, &temp2); //刷新矿石
putMines();
}
EndBatchDraw();
}
}
}

void hook() { //钩子
Sleep(10);
IMAGE img1, temp, img2;
loadimage(&img1, "src\\gameGUI\\hook_01.jpg"); //加载原图
loadimage(&img2, "src\\gameGUI\\hook_01bg.jpg"); //加载掩码图
BeginBatchDraw(); //开始批量绘图
if (angle >= 90 || angle <= -90)
angle_v = -angle_v;
if (!flag_dig) //不向下挖
angle += angle_v;
hookx = centerx + r * sin(angle * PI / 180); //使用圆上的坐标 x= x0 + r*sin# ; y = y + r*cos# 逆时针旋转
hooky = centery + r * cos(angle * PI / 180);
getimage(&temp,hookx,hooky,hookx+75,hookx+75);
//drawAlpha(&img1, hookx, hooky);
putimage(hookx, hooky, &img2, SRCAND); //贴钩子的时候使用drawAlpah会导致异常,因此使用掩码图贴透明图
putimage(hookx, hooky, &img1, SRCPAINT);
FlushBatchDraw();
putimage(hookx,hooky,&temp);
for (int i = 0; i < 10; i++) {
if (prop1[i].flag == 2) { //如果矿石被抓到
prop1[i].x = centerx + r * sin(angle * PI / 180) - prop1[i].img.getwidth() / 2;
prop1[i].y = centery + r * cos(angle * PI / 180) - 20;
}
}
}

void playGame(){
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));//清空键盘缓存
playMusic();
gameGUI();
//saveimage("d:\\desktop\\test.jpg");
initMines();
putMines();
retime = 30;
IMAGE tmp1;
char s[10],c[10];
settextcolor(RGB(181, 136, 47));
settextstyle(60, 0, _T("Consolas"));
loadimage(&temp2, "src\\gameGUI\\01.jpg", WIDTH, HEIGHT - 150, true);
getimage(&tmp1, 700, 0, 850, 75);
outtextxy(750, 75, _itoa(checkpoint, c, 10));
outtextxy(100, 80, _itoa(target_money, s, 10));
while (true) {
settextcolor(RGB(181, 136, 47));
settextstyle(60, 0, _T("Consolas"));
putimage(700, 0, &tmp1);
char s1[10];
outtextxy(780, 8, _itoa(retime, s1, 10)); //将整型转化为字符串后输出
hook();
updateWithInput();
updateWithInput();
count++; //模拟倒计时
if (count % 50 == 0) retime--;
if (retime <= 0) {
EndBatchDraw();
if (money>=target_money) {
victory();
}
else {
defeat();
}
}
}
}

void gameOver() { //游戏结束界面
cleardevice();
FlushMouseMsgBuffer(); //清空鼠标缓冲区
IMAGE img1;
loadimage(&img1, "src\\gameover\\01.png"); //贴游戏结束背景图片
putimage(0, 0, &img1);
while (true) {
if (MouseHit()) {//如果有鼠标点击
MOUSEMSG msg = GetMouseMsg();
if ((msg.uMsg == WM_LBUTTONDOWN) && msg.x >= 100 && msg.x <= 355 && msg.y >= 510 && msg.y <= 590) {//点击继续游戏 msg.MLBUTTONDOWM
target_money += 600;
checkpoint += 1;
playGame();
}
else if ((msg.uMsg == WM_LBUTTONDOWN) && msg.x >= 480 && msg.x <= 735 && msg.y >= 510 && msg.y <= 590) { //点击退出游戏
closegraph();
PlaySound(NULL, NULL, SND_FILENAME); //结束播放背景音乐
}
else { //点击其他地方
Sleep(6000);
closegraph();
PlaySound(NULL, NULL, SND_FILENAME);
}
}
else {
Sleep(6000);
closegraph();
PlaySound(NULL, NULL, SND_FILENAME);
}
}
}

main.cpp

1
2
3
4
5
#include "GoldMiner.h"
int main() {
login();
return 0;
}