-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_operate.cpp
871 lines (727 loc) · 24.9 KB
/
main_operate.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
#include <Windows.h>
#include <graphics.h>
#include <stdio.h>
#include <string.h>
#include "CatMouse_Button.h"
#include <math.h>
#include <Mmsystem.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#pragma comment(lib,"winmm.lib")
//#define COMPIL_CAT_TOM
//#define PI 3.14159265359
#define GRAPH_WIDTH 1800
#define GRAPH_HEIGHT 1200
#define TITLE_WIDTH (GRAPH_WIDTH / 2)
#define TITLE_HEIGHT (GRAPH_HEIGHT / 5)
#define BUTTON_GAP 150
#define SPEAKER_SIZE 60
#define JERRY_GRAPH_SIZE 60
#define TOM_GRAPH_SIZE 100
#define PLAYER_SPEED 9
#define RANDOM_MOUSE_SPEED 3
#define EASY_MOUSE_RUN_SPEED 6
#define DETOUR_MOUSE_SPEED 6
#define MOUSE_TOTAL_NUMBER 3
#define MOUSE_ADD_CYCLE 3
#define GAME_TOTAL_TIME 40
#define FONT_SCORE_SIZE 40
#define FONT_GAMETIME_SIZE 40
#define FONT_GMOVER_SIZE 80
#define FONT_NAME_SIZE 30
#define FONT_RANK_SIZE 40
typedef struct mouse {
int x_coordinate;
int y_coordinate;
int width;
int height;
int speed;
IMAGE* frame1;
IMAGE* frame1_logic;
IMAGE* frame2;
IMAGE* frame2_logic;
IMAGE* frame3;
IMAGE* frame3_logic;
struct mouse* nextPtr;
}Mouse;
typedef Mouse* MousePtr;
typedef Mouse** MouseDoublePtr;
typedef struct cat
{
int x_coordinate;
int y_coordinate;
int width;
int height;
int speed;
}Cat;
typedef Cat* CatPtr;
void menu();
int game_running();
int help_running();
int rank_running();
int music_running();
int mute_running();
int quit_running();
void outbreak();
void mouseInitialLink(int number, MousePtr* mouseKing);
void catInitial(CatPtr catPlayer);
void inputInformation(MousePtr * it);
int isEmpty_forMouseLink(MousePtr goodday);
void mouseNodeFree(MouseDoublePtr* mouseKing, MouseDoublePtr* unluckOne);
void outputMouseImage(const Mouse mouse, IMAGE* pic, IMAGE* pic_logic);
void outputScore(int score);
void outputTime(int time);
void mouseRandomRun(MousePtr it);
void mouseLineEscape(MousePtr mouse, const Cat cat);
void mouseDetour(MousePtr mouse);
void catControl(CatPtr it);
void catCatch(MousePtr* headMice, MousePtr* unluckOne, const Cat cat, int* score);
void isAddMouseTime(MousePtr* headMice);
void isEnd(int* signal);
void freeAllMouse(MousePtr* mouseKing);
void inputPlayerName(int score);
int theWorld();
void descendSwap(char name[100][12], int score[100], int length);
int main(void) {
initgraph(GRAPH_WIDTH, GRAPH_HEIGHT);
menu();
closegraph();
return 1;
}
void menu() {
int continueOrNot = 1;
int playMusicOrNot = 1;
struct MOUSEMSG myMouse;
setlinecolor(LIGHTMAGENTA);
setlinestyle(PS_SOLID, 4);
mciSendString("open .\\musics\\test.mp3", NULL, 0, 0);
mciSendString("play .\\musics\\test.mp3 repeat", NULL, 0, 0);
BeginBatchDraw();
IMAGE map(GRAPH_WIDTH, GRAPH_HEIGHT);
loadimage(&map, ".\\pictures\\map_grassland.jpg", GRAPH_WIDTH, GRAPH_HEIGHT);
IMAGE title(TITLE_WIDTH, TITLE_HEIGHT);
loadimage(&title, ".\\pictures\\title.png", TITLE_WIDTH, TITLE_HEIGHT);
IMAGE title_logic(TITLE_WIDTH, TITLE_HEIGHT);
loadimage(&title_logic, ".\\pictures\\title_logic.png", TITLE_WIDTH, TITLE_HEIGHT);
Button b1(game_running, ".\\pictures\\start_button.png", (GRAPH_WIDTH-CLASSIC_BUTTON_WID)/2, (GRAPH_HEIGHT - CLASSIC_BUTTON_HEI) / 2);
Button b2(help_running, ".\\pictures\\helping_button.png", (GRAPH_WIDTH - CLASSIC_BUTTON_WID) / 2, (GRAPH_HEIGHT - CLASSIC_BUTTON_HEI) / 2 + BUTTON_GAP);
Button b3(rank_running, ".\\pictures\\rank_button.png", (GRAPH_WIDTH - CLASSIC_BUTTON_WID) / 2, (GRAPH_HEIGHT - CLASSIC_BUTTON_HEI) / 2 + BUTTON_GAP*2);
Button sound_button(music_running, ".\\pictures\\sound_button.jpg", 30, 30, &playMusicOrNot, SPEAKER_SIZE, SPEAKER_SIZE);
Button mute_button(mute_running, ".\\pictures\\mute_button.png", 30, 30, &playMusicOrNot, SPEAKER_SIZE, SPEAKER_SIZE);
Button b5(quit_running, ".\\pictures\\quit_button.jpg", (GRAPH_WIDTH - CLASSIC_BUTTON_WID) / 2, (GRAPH_HEIGHT - CLASSIC_BUTTON_HEI) / 2 + BUTTON_GAP*3, &continueOrNot);
while (1) {
putimage(0, 0, &map);
putimage((GRAPH_WIDTH - TITLE_WIDTH) / 2, GRAPH_HEIGHT / 6, &title_logic, NOTSRCERASE);
putimage((GRAPH_WIDTH - TITLE_WIDTH) / 2, GRAPH_HEIGHT / 6, &title, SRCINVERT);
b1.outputButton();
b2.outputButton();
b3.outputButton();
if (playMusicOrNot == 1) {
sound_button.outputButton();
}
else {
mute_button.outputButton();
}
b5.outputButton();
myMouse = GetMouseMsg();
b1.mouseTouch(myMouse);
b2.mouseTouch(myMouse);
b3.mouseTouch(myMouse);
if (playMusicOrNot == 1) {
sound_button.mouseTouch(myMouse);
}
else {
mute_button.mouseTouch(myMouse);
}
b5.mouseTouch(myMouse);
if (continueOrNot == 0) break;
FlushBatchDraw();
}
EndBatchDraw();
}
int game_running() {
srand(time(NULL));
int score = 0; //玩家得分
int choice = 0; //控制打印不同帧
time_t startTime = time(NULL);
time_t nodeTime = startTime;
time_t pauseTime = 0;
//int playTime = 0;
int continueSignal = 1; //游戏是否结束的信号
MousePtr parallel;
MousePtr mouseKing = NULL;
mouseInitialLink(MOUSE_TOTAL_NUMBER, &mouseKing); //老鼠初始化完成
Cat player_cat;
catInitial(&player_cat);
#ifdef COMPIL_CAT_TOM
IMAGE cat1(player_cat.width, player_cat.height);
loadimage(&cat1, ".\\pictures\\cat.jpg", player_cat.width, player_cat.height);
IMAGE cat1_logic(player_cat.width, player_cat.height);
loadimage(&cat1_logic, ".\\pictures\\cat_logic.png", player_cat.width, player_cat.height); //猫猫初始化完成
#else
IMAGE cat_1(player_cat.width, player_cat.height);
loadimage(&cat_1, ".\\pictures\\cat_1.jpg", player_cat.width, player_cat.height);
IMAGE cat_1_logic(player_cat.width, player_cat.height);
loadimage(&cat_1_logic, ".\\pictures\\cat_1_logic.png", player_cat.width, player_cat.height);
IMAGE cat_2(player_cat.width, player_cat.height);
loadimage(&cat_2, ".\\pictures\\cat_2.jpg", player_cat.width, player_cat.height);
IMAGE cat_2_logic(player_cat.width, player_cat.height);
loadimage(&cat_2_logic, ".\\pictures\\cat_2_logic.png", player_cat.width, player_cat.height);
IMAGE cat_3(player_cat.width, player_cat.height);
loadimage(&cat_3, ".\\pictures\\cat_3.jpg", player_cat.width, player_cat.height);
IMAGE cat_3_logic(player_cat.width, player_cat.height);
loadimage(&cat_3_logic, ".\\pictures\\cat_3_logic.png", player_cat.width, player_cat.height);
IMAGE cat_4(player_cat.width, player_cat.height);
loadimage(&cat_4, ".\\pictures\\cat_4.jpg", player_cat.width, player_cat.height);
IMAGE cat_4_logic(player_cat.width, player_cat.height);
loadimage(&cat_4_logic, ".\\pictures\\cat_4_logic.png", player_cat.width, player_cat.height); //猫猫初始化完成
//rotateimage(&cat_1, &cat_1, PI*3/2);
//rotateimage(&cat_1_logic, &cat_1_logic, PI*3/2);
//rotateimage(&cat_2, &cat_2, PI*3/2);
//rotateimage(&cat_2_logic, &cat_2_logic, PI*3/2);
//rotateimage(&cat_3, &cat_3, PI*3/2);
//rotateimage(&cat_3_logic, &cat_3_logic, PI*3/2);
//rotateimage(&cat_4, &cat_4, PI*3/2);
//rotateimage(&cat_4_logic, &cat_4_logic, PI*3/2);
#endif
IMAGE map(GRAPH_WIDTH, GRAPH_HEIGHT);
loadimage(&map, ".\\pictures\\map_grassland.jpg", GRAPH_WIDTH, GRAPH_HEIGHT); //地图加载完成
setbkcolor(DARKGRAY);
settextcolor(YELLOW);
/***************初始化完成***************/
/****************进入游戏界面绘图*****************/
cleardevice();
BeginBatchDraw();
while (continueSignal) {
choice++;
if (choice > 999) choice = 0;
parallel = mouseKing;
putimage(0, 0, &map);
#ifdef COMPIL_CAT_TOM
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat1_logic, NOTSRCERASE);
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat1, SRCINVERT);
#else
switch ((choice / 6) % 4 + 1)
{
case 1:
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_1_logic, NOTSRCERASE);
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_1, SRCINVERT);
break;
case 2:
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_2_logic, NOTSRCERASE);
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_2, SRCINVERT);
break;
case 3:
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_3_logic, NOTSRCERASE);
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_3, SRCINVERT);
break;
case 4:
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_4_logic, NOTSRCERASE);
putimage(player_cat.x_coordinate, player_cat.y_coordinate, &cat_4, SRCINVERT);
break;
default:
break;
}
#endif
settextstyle(FONT_SCORE_SIZE, FONT_SCORE_SIZE, "consolas");
outtextxy(GRAPH_WIDTH - 400, 200, "score:");
outtextxy(GRAPH_WIDTH - 400, 260, "time:");
if ((time(NULL) - startTime - pauseTime) >= GAME_TOTAL_TIME) isEnd(&continueSignal);
while (parallel != NULL) {
switch ((choice / 10) % 3 + 1)
{
case 1:
outputMouseImage(*parallel, (parallel->frame1), (parallel->frame1_logic));
break;
case 2:
outputMouseImage(*parallel, (parallel->frame2), (parallel->frame2_logic));
break;
case 3:
outputMouseImage(*parallel, (parallel->frame3), (parallel->frame3_logic));
break;
default:
break;
}
mouseRandomRun(parallel);
mouseDetour(parallel);
mouseLineEscape(parallel, player_cat);
catCatch(&mouseKing, ¶llel, player_cat, &score);
if (parallel == NULL) break; //老鼠被吃光了
else parallel = parallel->nextPtr;
}
outputScore(score);
outputTime( GAME_TOTAL_TIME - (time(NULL) - startTime - pauseTime) );
if ((time(NULL) -pauseTime - nodeTime) > MOUSE_ADD_CYCLE) {
isAddMouseTime(&mouseKing);
nodeTime = time(NULL) - pauseTime;
}
FlushBatchDraw();
catControl(&player_cat);
pauseTime += theWorld();
Sleep(30);
}
freeAllMouse(&mouseKing);
inputPlayerName(score);
cleardevice();
return 0;
}
int help_running() {
IMAGE map(GRAPH_WIDTH, GRAPH_HEIGHT);
loadimage(&map, ".\\pictures\\map_teach.png", GRAPH_WIDTH, GRAPH_HEIGHT);
cleardevice();
putimage(0, 0, &map);
FlushBatchDraw();
outbreak();
return 0;
}
int rank_running() {
FILE* sfPtr; //score file pointer
int score;
char name[12];
int allScore[100];
char allName[100][12];
char str[20];
int count = 0;
IMAGE map(GRAPH_WIDTH, GRAPH_HEIGHT);
loadimage(&map, ".\\pictures\\map_prize.jpg", GRAPH_WIDTH, GRAPH_HEIGHT);
cleardevice();
putimage(0, 0, &map);
if (!fopen_s(&sfPtr, "score.txt", "r")) {
//fscanf_s(sfPtr, "%s", name, 11);
//fscanf_s(sfPtr, "%d", &score);
fscanf_s(sfPtr, "%s %d", name, 12, &score);
while (!feof(sfPtr)) {
strcpy_s(allName[count], 11, name);
allScore[count] = score;
count++;
/*fscanf_s(sfPtr, "%s", name, 11);
fscanf_s(sfPtr, "%d", &score);*/
fscanf_s(sfPtr, "%s %d", name, 11, &score);
}
rewind(sfPtr);
fclose(sfPtr);
}
descendSwap(allName, allScore, count); //降幂整理文件数据
if (!fopen_s(&sfPtr, "score.txt", "w")) {
for (int i = 0; i < ( (count < 10)? count : 10 ); i++) {
fprintf(sfPtr, "%s %d\n", allName[i], allScore[i]);
sprintf_s(str, "%d", allScore[i]);
setbkcolor(BLUE);
settextcolor(GREEN);
settextstyle(FONT_RANK_SIZE, FONT_RANK_SIZE, "consolas");
outtextxy((GRAPH_WIDTH / 3), (GRAPH_HEIGHT / 13) * (i + 1), allName[i]);
settextstyle(FONT_RANK_SIZE, FONT_RANK_SIZE, "consolas");
outtextxy(GRAPH_WIDTH / 3 + FONT_RANK_SIZE * 11, (GRAPH_HEIGHT / 13) * (i + 1), str);
FlushBatchDraw();
}
fclose(sfPtr);
}
outbreak();
return 0;
}
int music_running() {
mciSendString("stop .\\musics\\test.mp3", NULL, 0, 0);
return 0;
}
int mute_running() {
mciSendString("resume .\\musics\\test.mp3", NULL, 0, 0);
return 1;
}
int quit_running() {
return 0;
}
void mouseInitialLink(int number, MousePtr* mouseKing) {
MousePtr parallelPtr = *mouseKing;
for (int i = 0; i < number; i++) {
MousePtr newPtr = (MousePtr)malloc(sizeof(Mouse));
if (newPtr != NULL) {
inputInformation(&newPtr);
newPtr->nextPtr = NULL;
if (isEmpty_forMouseLink(parallelPtr)) {
(*mouseKing) = newPtr;
parallelPtr = *mouseKing;
}
else {
parallelPtr->nextPtr = newPtr;
parallelPtr = parallelPtr->nextPtr;
}
}
}
}
void catInitial(CatPtr catPlayer) {
catPlayer->width = TOM_GRAPH_SIZE;
catPlayer->height = TOM_GRAPH_SIZE;
catPlayer->x_coordinate = (GRAPH_WIDTH - TOM_GRAPH_SIZE) / 2;
catPlayer->y_coordinate = (GRAPH_HEIGHT - TOM_GRAPH_SIZE) / 2;
catPlayer->speed = PLAYER_SPEED;
}
void inputInformation(MousePtr* it) {
srand(time(NULL));
int type = rand() % 3 + 1;
switch (type)
{
case 1:
(*it)->width = JERRY_GRAPH_SIZE;
(*it)->height = JERRY_GRAPH_SIZE;
(*it)->x_coordinate = rand() % GRAPH_WIDTH / 3;
(*it)->y_coordinate = rand() % GRAPH_WIDTH / 3;
(*it)->speed = RANDOM_MOUSE_SPEED;
(*it)->frame1 = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame1), ".\\pictures\\mouse_1.jpg", (*it)->width, (*it)->height);
(*it)->frame1_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame1_logic), ".\\pictures\\mouse_1_logic.png", (*it)->width, (*it)->height);
(*it)->frame2 = new IMAGE((*it)->width, (*it)->height);;
loadimage(((*it)->frame2), ".\\pictures\\mouse_2.jpg", (*it)->width, (*it)->height);
(*it)->frame2_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame2_logic), ".\\pictures\\mouse_2_logic.png", (*it)->width, (*it)->height);
(*it)->frame3 = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame3), ".\\pictures\\mouse_3.jpg", (*it)->width, (*it)->height);
(*it)->frame3_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame3_logic), ".\\pictures\\mouse_3_logic.png", (*it)->width, (*it)->height);
break;
case 2:
(*it)->width = JERRY_GRAPH_SIZE;
(*it)->height = JERRY_GRAPH_SIZE;
(*it)->x_coordinate = rand() % GRAPH_WIDTH / 3 + GRAPH_WIDTH / 2;
(*it)->y_coordinate = rand() % GRAPH_WIDTH / 3;
(*it)->speed = RANDOM_MOUSE_SPEED;
(*it)->frame1 = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame1), ".\\pictures\\mole_1.jpg", (*it)->width, (*it)->height);
(*it)->frame1_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame1_logic), ".\\pictures\\mole_1_logic.png", (*it)->width, (*it)->height);
(*it)->frame2 = new IMAGE((*it)->width, (*it)->height);;
loadimage(((*it)->frame2), ".\\pictures\\mole_2.jpg", (*it)->width, (*it)->height);
(*it)->frame2_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame2_logic), ".\\pictures\\mole_2_logic.png", (*it)->width, (*it)->height);
(*it)->frame3 = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame3), ".\\pictures\\mole_3.jpg", (*it)->width, (*it)->height);
(*it)->frame3_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame3_logic), ".\\pictures\\mole_3_logic.png", (*it)->width, (*it)->height);
break;
case 3:
(*it)->width = JERRY_GRAPH_SIZE;
(*it)->height = JERRY_GRAPH_SIZE;
(*it)->x_coordinate = rand() % GRAPH_WIDTH / 3;
(*it)->y_coordinate = rand() % GRAPH_WIDTH / 3 + GRAPH_WIDTH / 2;
(*it)->speed = RANDOM_MOUSE_SPEED;
(*it)->frame1 = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame1), ".\\pictures\\can.jpg", (*it)->width, (*it)->height);
(*it)->frame1_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame1_logic), ".\\pictures\\can_logic.png", (*it)->width, (*it)->height);
(*it)->frame2 = new IMAGE((*it)->width, (*it)->height);;
loadimage(((*it)->frame2), ".\\pictures\\can.jpg", (*it)->width, (*it)->height);
(*it)->frame2_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame2_logic), ".\\pictures\\can_logic.png", (*it)->width, (*it)->height);
(*it)->frame3 = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame3), ".\\pictures\\can.jpg", (*it)->width, (*it)->height);
(*it)->frame3_logic = new IMAGE((*it)->width, (*it)->height);
loadimage(((*it)->frame3_logic), ".\\pictures\\can_logic.png", (*it)->width, (*it)->height);
break;
default:
break;
}
}
int isEmpty_forMouseLink(MousePtr goodday) {
return goodday == NULL;
}
void outputMouseImage(const Mouse mouse, IMAGE* pic, IMAGE* pic_logic) {
putimage(mouse.x_coordinate, mouse.y_coordinate, pic_logic, NOTSRCERASE);
putimage(mouse.x_coordinate, mouse.y_coordinate, pic, SRCINVERT);
}
void mouseRandomRun(MousePtr it) {
srand( time(NULL) + (int)it);
it->speed = RANDOM_MOUSE_SPEED;
it->x_coordinate += (rand() % 3 - 1) * it->speed;
it->y_coordinate += (rand() % 3 - 1) * it->speed;
if (it->x_coordinate < 0) {
it->x_coordinate += (rand() % 2 + 1) * it->speed;
}
if (it->x_coordinate > GRAPH_WIDTH - it->width) {
it->x_coordinate -= (rand() % 2 + 1) * it->speed;
}
if (it->y_coordinate < 0) {
it->y_coordinate += (rand() % 2 + 1) * it->speed;
}
if (it->y_coordinate > GRAPH_HEIGHT - it->height) {
it->y_coordinate -= (rand() % 2 + 1) * it->speed;
}
}
void mouseLineEscape(MousePtr mouse, const Cat cat) {
int x_distance = mouse->x_coordinate + mouse->width / 2 - cat.x_coordinate - cat.width / 2;
int y_distance = mouse->y_coordinate + mouse->height / 2 - cat.y_coordinate - cat.height / 2;
double distance = pow((pow(x_distance, 2) + pow(y_distance, 2)), 0.5);
//if (distance > 300) {
// mouse->speed = RANDOM_MOUSE_SPEED;
//}
if (distance <= 180) {
mouse->speed = EASY_MOUSE_RUN_SPEED;
if (x_distance == 0) {
if(y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
else {
double absolute_slope = abs(y_distance / x_distance);
if (x_distance > 0) {
if (absolute_slope >= 2.414) {
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope < 2.414 && absolute_slope > 0.414) {
mouse->x_coordinate += mouse->speed;
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope <= 0.414) mouse->x_coordinate += mouse->speed;
}
if (x_distance < 0) {
if (absolute_slope >= 2.414) {
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope < 2.414 && absolute_slope > 0.414) {
mouse->x_coordinate -= mouse->speed;
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope <= 0.414) mouse->x_coordinate -= mouse->speed;
}
}
if (mouse->x_coordinate < 0) {
mouse->x_coordinate += mouse->speed;
}
if (mouse->x_coordinate > GRAPH_WIDTH - mouse->width) {
mouse->x_coordinate -= mouse->speed;
}
if (mouse->y_coordinate < 0) {
mouse->y_coordinate += mouse->speed;
}
if (mouse->y_coordinate > GRAPH_HEIGHT - mouse->height) {
mouse->y_coordinate -= mouse->speed;
}
}
}
void mouseDetour(MousePtr mouse) {
MousePtr parallelPtr = mouse->nextPtr;
int x_distance;
int y_distance;
double distance;
while (parallelPtr != NULL) {
x_distance = mouse->x_coordinate - parallelPtr->x_coordinate;
y_distance = mouse->y_coordinate - parallelPtr->y_coordinate;
distance = pow((pow(x_distance, 2) + pow(y_distance, 2)), 0.5);
if (distance <= 120) {
mouse->speed = DETOUR_MOUSE_SPEED;
if (x_distance == 0) {
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
else {
double absolute_slope = abs(y_distance / x_distance);
if (x_distance > 0) {
if (absolute_slope >= 2.414) {
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope < 2.414 && absolute_slope > 0.414) {
mouse->x_coordinate += mouse->speed;
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope <= 0.414) mouse->x_coordinate += mouse->speed;
}
if (x_distance < 0) {
if (absolute_slope >= 2.414) {
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope < 2.414 && absolute_slope > 0.414) {
mouse->x_coordinate -= mouse->speed;
if (y_distance > 0) mouse->y_coordinate += mouse->speed;
else mouse->y_coordinate -= mouse->speed;
}
if (absolute_slope <= 0.414) mouse->x_coordinate -= mouse->speed;
}
}
if (mouse->x_coordinate < 0) {
mouse->x_coordinate += mouse->speed;
}
if (mouse->x_coordinate > GRAPH_WIDTH - mouse->width) {
mouse->x_coordinate -= mouse->speed;
}
if (mouse->y_coordinate < 0) {
mouse->y_coordinate += mouse->speed;
}
if (mouse->y_coordinate > GRAPH_HEIGHT - mouse->height) {
mouse->y_coordinate -= mouse->speed;
}
}
parallelPtr = parallelPtr->nextPtr;
}
}
void catControl(CatPtr it) {
if (GetAsyncKeyState(0x41)) { //A
it->x_coordinate -= it->speed;
}
if (GetAsyncKeyState(0x44)) { //D
it->x_coordinate += it->speed;
}
if (GetAsyncKeyState(0x53)) { //S
it->y_coordinate += it->speed;
}
if (GetAsyncKeyState(0x57)) { //W
it->y_coordinate -= it->speed;
}
if (it->x_coordinate < 0) {
it->x_coordinate += it->speed;
}
if (it->x_coordinate > GRAPH_WIDTH - it->width) {
it->x_coordinate -= it->speed;
}
if (it->y_coordinate < 0) {
it->y_coordinate += it->speed;
}
if (it->y_coordinate > GRAPH_HEIGHT - it->height) {
it->y_coordinate -= it->speed;
}
}
void catCatch(MousePtr* headMice, MousePtr* unluckOne, const Cat cat, int* score) {
int x_distance = (*unluckOne)->x_coordinate + (*unluckOne)->width / 2 - cat.x_coordinate - cat.width / 2;
int y_distance = (*unluckOne)->y_coordinate + (*unluckOne)->height / 2 - cat.y_coordinate - cat.height / 2;
double distance = pow((pow(x_distance, 2) + pow(y_distance, 2)), 0.5);
if (distance <= 50) {
mouseNodeFree(&headMice, &unluckOne);
*score += 10;
}
}
void mouseNodeFree(MouseDoublePtr* mouseKing, MouseDoublePtr* unluckOne) {
MousePtr parallelPtr = **mouseKing;
MousePtr tempPtr = **unluckOne;
if (**mouseKing == **unluckOne) {
**mouseKing = (**mouseKing)->nextPtr;
**unluckOne = (**unluckOne)->nextPtr;
free(parallelPtr);
return;
}
while (parallelPtr->nextPtr != **unluckOne) {
parallelPtr = parallelPtr->nextPtr;
}
parallelPtr->nextPtr = (**unluckOne)->nextPtr;
//free(tempPtr);
**unluckOne = parallelPtr;
free(tempPtr);
}
void outputScore(int score) {
char str[5];
sprintf_s(str, "%d", score);
outtextxy(GRAPH_WIDTH - 400 + FONT_SCORE_SIZE * 6, 200, str);
}
void outputTime(int time) {
char str[5];
sprintf_s(str, "%d", time);
outtextxy(GRAPH_WIDTH - 400 + FONT_GAMETIME_SIZE * 5, 260, str);
}
void isAddMouseTime(MousePtr* headMice) {
MousePtr newPtr = (MousePtr)malloc(sizeof(Mouse));
inputInformation(&newPtr);
newPtr->nextPtr = *headMice;
*headMice = newPtr;
}
void isEnd(int* signal) {
*signal = 0;
}
void freeAllMouse(MousePtr* mouseKing) {
MousePtr temp;
while ((*mouseKing) != NULL) {
temp = *mouseKing;
*mouseKing = (*mouseKing)->nextPtr;
free(temp);
}
}
void inputPlayerName(int score) {
char name[12] = {'_', '_' , '_' , '_' , '_',
'_' , '_' , '_' , '_' , '_','|', '\0' };
int i = 0;
setbkcolor(YELLOW);
settextcolor(RED);
settextstyle(FONT_GMOVER_SIZE, FONT_GMOVER_SIZE, "consolas");
outtextxy((GRAPH_WIDTH - FONT_GMOVER_SIZE * 10) / 2, (GRAPH_HEIGHT - FONT_GMOVER_SIZE) / 2, "Game over!");
settextcolor(BLACK);
settextstyle(FONT_NAME_SIZE, FONT_NAME_SIZE, "consolas");
outtextxy(GRAPH_WIDTH / 4 + 50, (GRAPH_HEIGHT - FONT_GMOVER_SIZE) / 2 + 100, "Please leave your name: ");
outtextxy(GRAPH_WIDTH / 4 + 50, (GRAPH_HEIGHT - FONT_GMOVER_SIZE) / 2 + 150, name);
FlushBatchDraw();
//fflush(stdin);
while (_kbhit()) {
_getch();
}
Sleep(1500); //防止用户仍按方向键
while (1) {
if (_kbhit()) {
name[i] = _getch(); //8 = delete 13 = enter 27 = esc
if ( name[i] == 27 || (0 == i && name[i] == 13) ) { return; }//当按下ESC时循环,ESC键的键值时27.
if ( name[i] == 13 ) { break; }
if (name[i] == 8 && i > 0) {
i -= 2;
name[i + 1] = '_';
if (10 == i + 2) name[i + 2] = '|';
else name[i + 2] = '_';
}
if ((i == 10 && name[i] != 13 && name[i] != 8)) { name[i] = '|'; }
if (name[i] == 8 && i == 0) { name[i] = '_'; }
else if (i < 10) { i++; }
outtextxy(GRAPH_WIDTH / 4 + 50, (GRAPH_HEIGHT - FONT_GMOVER_SIZE) / 2 + 150, name);
FlushBatchDraw();
}
}
name[i] = '\0';
//if (name[0] == '\0') return;
FILE* sfPtr; //玩家得分记录文件指针: score.dat file pointer
if ( !fopen_s(&sfPtr, "score.txt", "a+") ) {
fprintf(sfPtr, "%s %d\n", name, score);
fclose(sfPtr);
}
//fopen_s(&sfPtr, "score.txt", "a+");
//fprintf(sfPtr, "%s %d\n", name, score);
}
void descendSwap(char name[100][12], int score[100], int length) {
int tempS; //score临时变量
char tempN[12]; //name临时变量
for (int i = 0; i < length - 1; i++) {
for (int j = i + 1; j < length; j++) {
if (score[i] < score[j]) {
tempS = (score[j]);
score[j] = score[i];
score[i] = tempS;
strcpy_s(tempN, 12, name[j]);
strcpy_s(name[j], 12, name[i]);
strcpy_s(name[i], 12, tempN);
}
}
}
}
int theWorld() {
if (GetAsyncKeyState(0x42)) {
time_t start = time(NULL);
Sleep(500);
//outbreak();
while (1) {
if (_kbhit()) {
char temp = _getch();
if (27 == temp || 13 == temp || 32 == temp) break;
}
}
Sleep(500);
return (time(NULL) - start);
}
return 0;
}
void outbreak() {
while (1) {
//GetKeyState();
if (_kbhit()) {
char temp = _getch();
if (27 == temp || 13 == temp || 32 == temp) break;
}
}
}