-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1163 lines (959 loc) · 33.8 KB
/
main.cpp
File metadata and controls
1163 lines (959 loc) · 33.8 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
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//2X2S2XSS2: .:::,:,:,,,:,,,,,,.,,,.,.,.:.,,,,,.,.,.,,,.,,,.,.,,,,,,:,,,,,:.:,:,,,,,,,,,,,,,,,,.,,,,,,,,,.,,,,:,:,.i8i:i:.,::iri7Zi,i:iiiiiiiiiiii;i;;ri
//S7XX7X7XXXi ,a. ii;: ,Z. . ......,.,.,,,,::.
//SX2XSXSXXSa2r. .,,,,:,,.,.,,,.,.,.,.,.,.,.,.,.,.,.,.,.,.,,,.,.,.,.,.,,,.,.,.,.,.,.,.,.,.,,,.,,,,,.,.,,,,,.,.,,,. i8, .ZS;;ZX ;Zi.::::i:i:i:iiiiiiii;:
//XXSSXSXXXXXS22;. ..,.,...,.,.,...,.,...,.,...,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,,,.,,, ;Z, .ZX XS i8,.,::::::::i:iiiiiiii:
//27SXSXXXXXXXXX2X; ..,.,...,.,.....,.,...................,.,.,.,.,.,...,.,.,.,.,.,...,...,.,.,.,.,.,.,.,.,.,.,. ;8. :SS,SX ;Z,.,,:,::::::::i:i:ii;,
//SXSSXXXXXXXX7XXS2Si ......,.....,.,.,...,...........,...,...,.....,.,.,...,.,.,.,.,.,...,.....,.,.....,.,.,.,., ;Z. .i i2a7 ;Z,.,:,::::::::::i:iiii:
//i:SSSXXXXXXXX7XXXX2Xi .....,...,...........,.,...,.......,.....,.....,.,.,...............,.....,...,.....,.,.,. ;Z. 8r ar ;Z,.:,:,:::,::::i:i:i:i,
//i,irXSXXXXXX7X7X7X7XSX, ................,.,.,...............,.........,.....,.....,.,.....,...,.,.,...,.,.,.,., ;aSX,:XXXSSXXSa..,:,:,,,::::::i:::ii,
//;:i:irSSXXX7X777X777XX2X. ..,.................................... . ..,.,...,.............,.......,.,.,.,.,. ;Z .: ,i ,:, ra..:,:,,,,:::::::::::i.
//;:iiiii7XSXX7X7X777777XS .,...............,...,.,.....,.....,.. ......,.,...,.,...,...,.....,...,.,.,.. ra .2iXX:7X7 ;Z...,,,,,::::::,::::ii,
//;:;iii:,irSXX7X7777777XX. ,...............,...,...,.,.......... :SZBZ2; ......,.,...........,...,...,.....,.,. 7Z .S;XXiXXX rZ..,,,,:,,:,,::::::::i.
//i:i;i;ii:::rXSXX777777XS ..,.........,...,.,.......,...,....., .aWBW@B002: ....,.....,.....,.,.,.......,.,....., ;Z7.: i: ,;. rZ .,,.,,:,,,,,::::::::,
//;,;iiiiii::,:rXXX77777XX ..........,...,...,.,.,...,......... WMMBS8@WMr .,.....,.....,.,.....,.,.,...,.,...... .XaXSXXXXXXXa2..,,,,,,,,:,:,:,::::i.
//i,i;iiiiii:i:::rXX7X77XS ..,.....,.,...........,.....,.....,.,. 0B8ia80Z ........,.,.,...,...,...,.,.,.....,.,.. .:.,,:,:::,,.,,:,:,,,:,,,:,::::i:.
//;,iiiii:iii:i:,,irXX77XX ....,.....,.....,.,.,.,.,.....,.,.... :,ii:i7 .....,.,.,.,.,.,...,...,.,.,...,...,.,.... . ... ..,,,,,,:,,,,,,,,,::::i.
//i:;;iiiiii:i:i::,,:7XXXS ..,.....,.,.,.,.......,...,.,...,.,.. ;iX;:0M@a;. ..,.........,.,.,.........,.......,.,...,.,.,.,.,.,.:.,,,,:,,,,::,:,::.
//. i;;iiii:iii:i::::,i7SS. ................,...,.............. iMMarX@MMMMMi,; ......,.....,...,.,.,...,...,...,.,.,.,.,.,.,.,.,,,,,.,,,,:,,,:,:,i.
//, .,:;iiii:i:i:i:i::,,;X ......,.......,...,.,.,...,.,.,... :0 0MMMMMMBW@MW MMS ..,.,.,.,.,.......,.,.,.,.,...,.,.,,,,,.,.,,,,,.,,,,,,,,,,,,,,:,::.
//: ,...ii;:i:i:i:i:i:i:::, ..,.,.....,.....,.....,.,.,.,.... ,MM;.M@WMWWW@WMX;MMMM .,.......,.......,.,.,...,.,.,.,.,.,.,.,,,.,.,.,,:,,,:,:,:,,,:,:::.
//, ,,...,:iii:i:::i:i:::iii,. ..........,.....,.......... MMMM WM0BB@@W@M77M@MMa .............,.,.,.,.,.,.,.,.,.,.,.,,,,,,,,,.,.,,,.,.,,,,:,,::,i:.
//: :.,.,...ii;:i:::::::i:i:ii, ..,...,.,...,.......... iBMMMM7 M0@@@W@WM2:MM@MM ....,...,.....,.,.,.,.,...,.,.,.,.,.,.,.,,,,,.,.,,,,,,,,,,:,:,:,i.
//, ,:,,.,.. ,:iii:i:i:::i:i:iii.. ..,.,...,...... ... i2ZMMMMMBM 8M@@W@W@MM WMMMMZ ....,...,.,...,.,.,.,.,.,.,.,.,.,.,.,.,,,.,.,.,.,,,.,,:,:,,::,::.
//: ,.,.,.,....,iii:i:i:::i:i:iiii:...,.,...... .,;;;;;i0MMMMMMWi ir,MMWWW@W@M2:MXMMMZ ..,.,.,.,.,.....,...,...,.,.,.,.,.,.,,,.,.,.,.,,,,:,,,,,:,,:::i.
//, ,,.,.,.,.,...,:iii:::::i:i:i:iii,. ....... .i7777XXS2@MMM0; S WM@W@W@@MMi2: MMMB ..,.,.,.,.,.,.......,.,.,.,.,.,.,.,.,.,.,,,,,.,,,.,,:,,,:,:,::.
//: ,,,.,...,.... .,iii:i:i:i:i:i:ii;i, ...... ,2XXXXXSSZ2 . XM;:MMM@@@MMMBrMi7MMM; .,.,...,.,.....,.,.,.,.,.,.,.,.,.,.,.,.,.,.,,,,:.,,:,:,:,,:::i.
//, ,,.,.,.,.,...... ,i;ii:i:i:i:i:i:iii...... i88ZZ2aa8BX . BMWi8M@@W@W@WW@BS MMM8 ..,...,.,...,.,...,.,.,.,.,.,,,.,.,.,.,,,,,.,,:,,,,,,,,,:,::::.
//, :,,.,.,.,.,.,.,.. .,iii:i:i:i:i:i:ii;:,.... ;aBB000Z7.....,.. i82aZZZZaZ2aZ0r MMMW ..,.....,...,.,.,.,.,.,.,.,.,,,.,.,.,.,,,.,,,,,,,,,,:,:,::::i.
//, ,,.,.,.,.....,.......:ii:i:i:i:i:i:i:iii.. . .:r;ri, ...,.... 72XX2XSX2aa2aa8a rMMM. .,.,.,.,.,.......,.,.,.,.,,,.,.,.,.,,,.,,,.,,,,,,,,:,,,:,:,::.
//, :,,.,.,.,.,...,.,.... .:iii:i:i:i:i:i:iii:, . ..,....., XaXXXa2SX22aaZZZ @0MX ....,.,.,...,.,.,.,.,.,.,.,.,.,.,,,.,.,,,,,,,,,,:,:,,::,:,::i.
//, ,,.,.,.,.,.,.,.,.,.,.....:i;ii:i:i:i:i:iiii:.. ......,...,.,.. 7XXXXa88a2a222aZ ii,XX .,.,.,.,.,.,.,.,.,...,.,.,.,,,.,.,.,,,.,,,,,,,,:.,,,,:,,::,::.
//: ,,,.,.,.,.,.,.....,.,.,....:;ii:i:i:i:i:i:iii:, ..,.,...,..., i2Xr7rSZZZ0aZ2SSZX .;77 ..,.,.,.,.,.,.,.,.,.,.,.,,,.,.,.,.,,,.,,,.,.:,,,:,:,,,:,:::::.
//, ,,,,.,.,.,...,.,...,.,.,.....iiiii:i:i:i:i:iiiii...,.,....... :ZX7rXXZ: 0BaZa2a8.,;7i..,.,,,.,.,.,.,.,.,.,.,.,,,.,,,.,.,,,,,.:,,,,,,,,,:,,,:,:,:,::.
//: ,,:.,,,,,.,.,.,.,.,.,.,.,.... ,:;ii:i:i:::i:iiiii:, ......,.,. i2777XZi 00aa2Z8X ,...,.,.,.,,,,,.,.,.,.,,,.,.,.,.,.,,:.,,:.:,,,:,:,:.,,,,:,:,:,::i.
//, ,:,,.,,,.,.,.,.,.,...,.,...,.. ..:iiii:i:i:i:i:iiiii...,...,.. .XX77XZ. ,BZZZZZZ. ..,.,.,.,,,,,.,.,,,.,.,.,.,.,.,.,,,.,.,,,,,.,,,.,,,,:,:,,,::::::.
//: ,,,,,.,.,,,.,.....,.,...,...,.... ,:;ii:i:iii:iii:iii,. ..,.,.. rX772Z. 00ZaZZ8r .,.,.,.,.,.,.,.,.,.,,,.,.,.,.,.,.,,,.,,,,:,,,,,:,:,:,,,,,::::::i.
//: ,:,,,,.,,,...,.,...,.,.,.,.,.,.,.,...ii;iiiiiiii:i:ii;i: ..,.,. ;S7Sa2 .. :BZZaZZ2 ..,.,.,.,,,.,.,,,.,.,,,.,.,.,.,.,.,.,,,,,,:,:,,.,,:,:,:,,,:,::i:,
//: :,:,,,,.,,,.,.,.,.....,.,.,.,.,.,.,...,i;i;iiiiiiii:i:ii;,. .. :Z2SXZ: .,. rWaZaZZ ,.,.,,,,,,,.,,,.,,,,,,,,,.,.,.,,,.,.,,,,,,,,,,,,,,,,:,:,:::,::i.
//, ,,.,.,.,.....,.......,.,.,.....,.,.,.. .,ii;iiii:iiiii:ii;i:.. rXX7Sa ... X0ZZZB; .,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,,,.,.,.,,,,,.,,:,,,,,:,::::i:,
//: :,,.,............ ... ... . ..... . . . .:i:i:i:i:i:iii:iii. rSXSZr ... SZZZ08, ................................,...,.,.....,.,.,.,.,,,,:,::::i.
//ri;r;r;;;;;;;;i;iiiiiiiiiiiiii:iiiiiiiiiiiii,ii;;;;;;;;;;;;;;;;;,7XX20:.,::, SZaZ00i ::i:i:i:::::i:i:i:i::::::::::,:,:::,:,:,:::,:,:,:::,::::::i:i:ii,
//r:r;;;;;;;;i;;;i;i;i;i;i;i;;;i;i;i;;;;;;r;;;r;r;r;;;;;;i;;;;;i;iiXX7727:;ii.rZZZBZ:,;i;;;;r;r;;;r;r;;i;;;i;;;;;;;;;;;;;;;;;;r;r;;;r;;;r;rrrrrr7r7r7rX;
//;i;;i;i;iiiiiiiiiiiiiiiiiiiiiiiiiiiiiii;i;iii;i;i;iiiii;iiiiiiii:XX77Si::: :SaZW2.,iiiiiiiiii;iiiiiiiiiiiiiiiiii:iiii;iii;i;i;i;i;i;i;;;;;;;;;;;;r;rr;
//;:;i;iiiiiiiiii:i:i:i:::i:i:iiiii:i:ii::i:i:iiiiiii:i:i:i:i:i:i::;X7XS;,,.ZMBX8S ,i:i:i:iiiiiiiiiiiiiiiiiiiiiiiii:iiiiiiiiiiiiiiiiiiii;i;i;i;;;;r;r;7i
//i:iiiiiiiiiiiiiiii:iiiii:iii:i:i:iii:i:iiii;iiiiiiiiiiiiii:::i::,;r;;X:,.WMMMMB .::i:iii:iii:iiiii:iii:i:i:iiiiiiiiiiiiiiii;iiiiiiiiiii;i;i;i;i;;;;rri
//;,;iiii:iiiiiii:i:i:i:i:i:i:i:::i:iii:i:iii:i:i:i:iii:::i:i:i:i:.20ZW@:..7MMMMMr ,i:i:i:i:iiiiiii:i:iiiii:iii:iiiiiiiii:i:i:i:i:i:i:iiii;i;i;;;;;;;;ri
//i:i;iiiiiiii:::i:i:::::::::i:i:i:i:i:::i:i:i:i:::i:i:i:i:i:i:i::.;B@M2 :: ;MMMMX.,i:i:iii:i:iiiiiii:i:i:i:i:i:i:i:i:i:i:iii:i:iiiiiiiiiiiiii;i;;;;;;i
//;,;iiii:iii:i:::i:i:i:::::::i:i:iiiii:iiiii:i:i:i:i:i:iiiiiiiiiir7XSW;.:i:, 7@@@M8,iiiiiiii:i:i:::i:i:i:i:i:i:i:i:::i:i:::i:i:i:iii:iiii;iii;i;i;i;;r:
//i:iiiiiiiiiiiiii:iiiii:i:i:iii:iii:iii:iiiiiiiiiii:iiiiiiii;i;:raaXSZ:,::i;r;;i;;;:iiiiiiiii:iii:i:iiiiiii:i:iiiiiiiiiiiiiiiiiiiiiiii;iii;i;i;;;;;;r;i
//; ,,,.,.,.,.,...,.,............ ............. ..........,.,.,.. aB0Ba ...,iii::. ..,.........,.,.,...,.,.....................,.,.,.,.:,,,:,:::,::::i.
//
//
#include <graphics.h>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#include<mmsystem.h>
#pragma comment(lib,"WINMM.LIB")//
#include<time.h>
#pragma warning(disable:4996)
//
//const int Bullet_number = 1;
//const int Bullet_speed = 30;
//const int Bullet_range = 250;
//bool hit = false;
//
//
const int WINDOW_WIDTH = 1280;//窗口宽
const int WINDOW_HEIGHT = 720;//窗口长
const int BUTTON_WIDTH = 192;
const int BUTTON_HEIGHT = 75;
#pragma comment(lib, "Winmm.lib")
#pragma comment(lib, "MSIMG32.LIB")
bool running = true;
bool is_game_started = false;//是否开始
inline void putimage_alpha(int x, int y, IMAGE* img)//实现透明通道混叠
{
int w = img->getwidth();
int h = img->getheight();
AlphaBlend(GetImageHDC(NULL), x, y, w, h,
GetImageHDC(img), 0, 0, w, h, { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA });
}
class Atlas//用享元模式防止卡顿
{
public:
Atlas(LPCTSTR path, int num)
{
TCHAR path_file[256];
for (size_t i = 0; i < num; i++)
{
_stprintf_s(path_file, path, i);
IMAGE* frame = new IMAGE();
loadimage(frame, path_file);
frame_list.push_back(frame);
}
}
~Atlas()
{
for (size_t i = 0; i < frame_list.size(); i++)
delete frame_list[i];
}
public:
std::vector<IMAGE*> frame_list;
};
Atlas* atlas_player_left;
Atlas* atlas_player_right;
Atlas* atlas_enemy_left;
Atlas* atlas_enemy_right;
class Animation
{
public:
Animation(Atlas* atlas, int interval)
{
anim_atlas = atlas;
interval_ms = interval;
}
~Animation() = default;
void Play(int x, int y, int delta)
{
timer += delta;
if (timer >= interval_ms)
{
idx_frame = (idx_frame + 1) % anim_atlas->frame_list.size();
timer = 0;
}
putimage_alpha(x, y, anim_atlas->frame_list[idx_frame]);
}
private:
int timer = 0;
int idx_frame = 0;
int interval_ms = 0;
private:
Atlas* anim_atlas;//
};
class Player
{
public:
const int FRAME_WIDTH = 67; // 玩家宽度
const int FRAME_HEIGHT = 67; // 玩家高度
public:
Player()
{
loadimage(&img_shadow, _T("img/shadow_player.png"));
anim_left = new Animation(atlas_player_left, 45);
anim_right = new Animation(atlas_player_right, 45);
}
~Player()
{
delete anim_left;
delete anim_right;
}
void ProcessEvent(const ExMessage& msg)//处理操作信息
{
switch (msg.message)
{
case WM_KEYDOWN:
switch (msg.vkcode)
{
case VK_UP:
is_move_up = true;
break;
case VK_DOWN:
is_move_down = true;
break;
case VK_LEFT:
is_move_left = true;
break;
case VK_RIGHT:
is_move_right = true;
break;
}
break;
case WM_KEYUP:
switch (msg.vkcode)
{
case VK_UP:
is_move_up = false;
break;
case VK_DOWN:
is_move_down = false;
break;
case VK_LEFT:
is_move_left = false;
break;
case VK_RIGHT:
is_move_right = false;
break;
}
break;
}
}
void Move()//处理移动
{
int dir_x = is_move_right - is_move_left;
int dir_y = is_move_down - is_move_up;
double len_dir = sqrt(dir_x * dir_x + dir_y * dir_y);//用向量的方式,防止按2个键时速度矢量叠加导致速度不正常
if (len_dir != 0)
{
double normalized_x = dir_x / len_dir;
double normalized_y = dir_y / len_dir;
position.x += (int)(SPEED * normalized_x);
position.y += (int)(SPEED * normalized_y);
}
//对玩家位置进行校准防止跑出窗口
if (position.x < 0) position.x = 0;
if (position.y < 0) position.y = 0;
if (position.x + FRAME_WIDTH > WINDOW_WIDTH) position.x = WINDOW_WIDTH - FRAME_WIDTH;
if (position.y + FRAME_HEIGHT > WINDOW_HEIGHT) position.y = WINDOW_HEIGHT - FRAME_HEIGHT;
}
void Draw(int delta)//绘制玩家
{
int pos_shadow_x = position.x + (FRAME_WIDTH / 2 - SHADOW_WIDTH / 2);
int pos_shadow_y = position.y + FRAME_HEIGHT - 8;
putimage_alpha(pos_shadow_x, pos_shadow_y, &img_shadow);
static bool facing_left = false;
int dir_x = is_move_right - is_move_left;
if (dir_x < 0)
facing_left = true;
else if (dir_x > 0)
facing_left = false;
if (facing_left)
anim_left->Play(position.x, position.y, delta);
else
anim_right->Play(position.x, position.y, delta);
}
const POINT& GetPosition() const//访问玩家的实时位置
{
return position;
}
//
double GetX() {
return position.x;
}
double GetY() {
return position.y;
}
//
//
private:
const int SPEED = 3;
const int SHADOW_WIDTH = 32; // 阴影宽度
private:
IMAGE img_shadow;
Animation* anim_left;
Animation* anim_right;
POINT position = { 500, 500 };
bool is_move_up = false;
bool is_move_down = false;
bool is_move_left = false;
bool is_move_right = false;
};
class CircleBullet
{
public:
POINT position = { 0, 0 };
public:
CircleBullet() = default;
~CircleBullet() = default;
void Draw() const
{
setlinecolor(RGB(0, 0, 0));//
setfillcolor(RGB(200, 75, 10));//
fillcircle(position.x, position.y, RADIUS);
line(position.x- RADIUS, position.y, position.x+ RADIUS, position.y);
line(position.x - RADIUS/2, position.y+ RADIUS*0.9, position.x - RADIUS/2, position.y- RADIUS*0.9);
line(position.x + RADIUS / 2, position.y + RADIUS * 0.9, position.x + RADIUS / 2, position.y - RADIUS * 0.9);
}
private:
const int RADIUS = 10;//半径
};
//class Shoot_Bullet {
//public:
// Shoot_Bullet(float x, float y, float speed, int direction)
// : x_(x), y_(y), speed_(speed), direction_(direction) {}
//
// void Fire(float player_x, float player_y) {
// x_ = player_x;
// y_ = player_y;
// is_fired_ = true;
// }
//
// void Update() {
// if (!is_fired_) return;
//
// x_ += cos(direction_ * 3.1415926 / 180.0f) * speed_;
// y_ -= sin(direction_ * 3.1415926 / 180.0f) * speed_;
//
// // 检查子弹是否超出屏幕边界,或者达到目标
// if (x_ < 0 || x_ > getmaxx() || y_ < 0 || y_ > getmaxy()) {
// is_fired_ = false;
// }
// }
//
// void Draw() const {
// /*if (is_fired_) {
// setfillstyle(SOLID_FILL, RED);
// floodfill(x_, y_, WHITE);
// }*/
//
// setlinecolor(RGB(0, 0, 0));//
// setfillcolor(RGB(200, 75, 10));//
// fillcircle(x_, y_, RADIUS);
//
// }
//
// bool IsFired() const { return is_fired_; }
//
// void SetDirection(double direction) {
// direction_ = direction;
// NormalizeAngle(); // 确保角度在0到360度之间
// }
// // 辅助方法:确保角度在0到360度之间
// void NormalizeAngle() {
// while (direction_ < 0) {
// direction_ += 360;
// }
// while (direction_ >= 360) {
// direction_ -= 360;
// }
// }
//
// void ProcessEvent(const ExMessage& msg)//获取鼠标位置
// {
// switch (msg.message)
// {
// case WM_LBUTTONUP:
// mx = msg.x;
// my = msg.y;
//
// break;
// default:
// break;
// }
// }
// float Getmx()
// {
// return mx;
//
// }
//
// float Getmy()
// {
// return my;
//
// }
//
//private:
// float x_, y_;
// float speed_;
// int direction_;
// bool is_fired_ = false;
// float mx;
// float my;
// const int RADIUS = 10;
//
//};
class Enemy
{
public:
Enemy()
{
loadimage(&img_shadow, _T("img/shadow_enemy.png"));
anim_left = new Animation(atlas_enemy_left, 45);
anim_right = new Animation(atlas_enemy_right, 45);
// 敌人生成边界
enum class SpawnEdge
{
Up = 0,
Down,
Left,
Right
};
// 将敌人放置在地图外边界处的随机位置
SpawnEdge edge = (SpawnEdge)(rand() % 4);
switch (edge)
{
case SpawnEdge::Up:
position.x = rand() % WINDOW_WIDTH;
position.y = -FRAME_HEIGHT;
break;
case SpawnEdge::Down:
position.x = rand() % WINDOW_WIDTH;
position.y = WINDOW_HEIGHT;
break;
case SpawnEdge::Left:
position.x = -FRAME_WIDTH;
position.y = rand() % WINDOW_HEIGHT;
break;
case SpawnEdge::Right:
position.x = WINDOW_WIDTH;
position.y = rand() % WINDOW_HEIGHT;
break;
default:
break;
}
}
~Enemy()
{
delete anim_left;
delete anim_right;
}
bool CheckBulletCollision(const CircleBullet& bullet)//&避免拷贝构造,const避免函数内部对参数进行了修改
{
// 将子弹等效为点,判断点是否在敌人矩形内
bool is_overlap_x = bullet.position.x >= position.x && bullet.position.x <= position.x + FRAME_WIDTH;
bool is_overlap_y = bullet.position.y >= position.y && bullet.position.y <= position.y + FRAME_HEIGHT;
return is_overlap_x && is_overlap_y;
}
bool CheckPlayerCollision(const Player& player)
{
// 将敌人中心位置等效为点,判断点是否在玩家矩形内
POINT check_position = { position.x + FRAME_WIDTH / 2, position.y + FRAME_HEIGHT / 2 };
const POINT& player_position = player.GetPosition();
bool is_overlap_x = check_position.x >= player_position.x && check_position.x <= player_position.x + player.FRAME_WIDTH;
bool is_overlap_y = check_position.y >= player_position.y && check_position.y <= player_position.y + player.FRAME_HEIGHT;
return is_overlap_x && is_overlap_y;
}
void Move(const Player& player)//需要向玩家移动,所以要Player参数
{
const POINT& player_position = player.GetPosition();
int dir_x = player_position.x - position.x;//获取敌人需要移动的向量
int dir_y = player_position.y - position.y;
double len_dir = sqrt(dir_x * dir_x + dir_y * dir_y);
if (len_dir != 0)
{
double normalized_x = dir_x / len_dir;
double normalized_y = dir_y / len_dir;
position.x += (int)(SPEED * normalized_x);
position.y += (int)(SPEED * normalized_y);
}
if (dir_x < 0)
facing_left = true;
else if (dir_x > 0)
facing_left = false;
}
void Draw(int delta)//不需要用static保存动画翻转状态
{
int pos_shadow_x = position.x + (FRAME_WIDTH / 2 - SHADOW_WIDTH / 2);
int pos_shadow_y = position.y + FRAME_HEIGHT - 35;
putimage_alpha(pos_shadow_x, pos_shadow_y, &img_shadow);
if (facing_left)
anim_left->Play(position.x, position.y, delta);
else
anim_right->Play(position.x, position.y, delta);
}
void Hurt()
{
alive = false;
}
bool CheckAlive()
{
return alive;
}
private:
const int SPEED = 4;
const int FRAME_WIDTH = 71; // 敌人宽度
const int FRAME_HEIGHT = 71; // 敌人高度
const int SHADOW_WIDTH = 48; // 阴影宽度
private:
IMAGE img_shadow;
Animation* anim_left;
Animation* anim_right;
POINT position = { 0, 0 };
bool facing_left = false;
bool alive = true;
};
class Button//idle(默认),hovered(悬停),pushed(按下)
{
public:
Button(RECT rect, LPCTSTR path_img_idle, LPCTSTR path_img_hovered, LPCTSTR path_img_pushed)
{
region = rect;
loadimage(&img_idle, path_img_idle);
loadimage(&img_hovered, path_img_hovered);
loadimage(&img_pushed, path_img_pushed);
}
~Button() = default;
void ProcessEvent(const ExMessage& msg)
{
switch (msg.message)
{
case WM_MOUSEMOVE:
if (status == Status::Idle && CheckCursorHit(msg.x, msg.y))
status = Status::Hovered;
else if (status == Status::Hovered && !CheckCursorHit(msg.x, msg.y))
status = Status::Idle;
break;
case WM_LBUTTONDOWN:
if (CheckCursorHit(msg.x, msg.y))
status = Status::Pushed;
break;
case WM_LBUTTONUP:
if (status == Status::Pushed)
OnClick();
break;
default:
break;
}
}
void Draw()
{
switch (status)
{
case Status::Idle:
putimage(region.left, region.top, &img_idle);
break;
case Status::Hovered:
putimage(region.left, region.top, &img_hovered);
break;
case Status::Pushed:
putimage(region.left, region.top, &img_pushed);
break;
}
}
protected:
virtual void OnClick() = 0;//纯虚函数,button类的函数必须实现onclick函数逻辑才能被实例化为对象
private:
enum class Status
{
Idle = 0,
Hovered,
Pushed
};
private:
RECT region;//描述位置和大小
IMAGE img_idle;
IMAGE img_hovered;
IMAGE img_pushed;
Status status = Status::Idle;//定义按钮当前状态枚举变量
private:
// 检测鼠标点击
bool CheckCursorHit(int x, int y)
{
return x >= region.left && x <= region.right && y >= region.top && y <= region.bottom;
}
};//
// 开始游戏按钮
class StartGameButton : public Button
{
public:
StartGameButton(RECT rect, LPCTSTR path_img_idle, LPCTSTR path_img_hovered, LPCTSTR path_img_pushed)
: Button(rect, path_img_idle, path_img_hovered, path_img_pushed) {}
~StartGameButton() = default;
protected:
void OnClick()
{
is_game_started = true;//开始
mciSendString(_T("play bgm repeat from 0"), NULL, 0, NULL);
}
};
// 退出游戏按钮
class QuitGameButton : public Button
{
public:
QuitGameButton(RECT rect, LPCTSTR path_img_idle, LPCTSTR path_img_hovered, LPCTSTR path_img_pushed)
: Button(rect, path_img_idle, path_img_hovered, path_img_pushed) {}
~QuitGameButton() = default;
protected:
void OnClick()
{
static TCHAR text[128];
_stprintf_s(text, _T("你真的要拒绝kunkun的邀请吗"));
MessageBox(GetHWnd(), text, _T("坤坤晚上你床"), MB_OK);
running = false;
//running = false;
}
};
// 生成新的敌人
void TryGenerateEnemy(std::vector<Enemy*>& enemy_list)
{
const int INTERVAL = 100;//到达指定计数间隔时添加新的敌人
static int counter = 0;
if ((++counter) % INTERVAL == 0)
enemy_list.push_back(new Enemy());
}
// 更新子弹位置
void UpdateCircleBullets(std::vector<CircleBullet>& bullet_list, const Player& player)
{
const double RADIAL_SPEED = 0.0145; // 径向波动速度
const double TANGENT_SPEED = 0.0045; // 切向波动速度
double radian_interval = 2 * 3.14159 / bullet_list.size(); // 子弹之间的弧度间隔
POINT player_position = player.GetPosition();
double radius = 100 + 25 * sin(GetTickCount() * RADIAL_SPEED);//GetTickCount()引入时间
for (size_t i = 0; i < bullet_list.size(); i++)
{
double radian = GetTickCount() * TANGENT_SPEED + radian_interval * i; // 当前子弹所在弧度值
bullet_list[i].position.x = player_position.x + player.FRAME_WIDTH / 2 + (int)(radius * sin(radian));
bullet_list[i].position.y = player_position.y + player.FRAME_HEIGHT / 2 + (int)(radius * cos(radian));
}
}
//
////
////
//void drawAlpha(IMAGE* picture, int picture_x, int picture_y) //xΪ����ͼƬ��X���꣬yΪY����
//{
//
// // ������ʼ��
// DWORD* dst = GetImageBuffer(); // GetImageBuffer()���������ڻ�ȡ��ͼ�豸���Դ�ָ��
// DWORD* draw = GetImageBuffer();
// DWORD* src = GetImageBuffer(picture); //��ȡpicture���Դ�ָ��
// int picture_width = picture->getwidth(); //��ȡpicture�Ŀ���
// int picture_height = picture->getheight(); //��ȡpicture�ĸ߶�
// int graphWidth = getwidth(); //��ȡ��ͼ���Ŀ���
// int graphHeight = getheight(); //��ȡ��ͼ���ĸ߶�
// int dstX = 0; //���Դ������صĽDZ�
//
// // ʵ������ͼ ��ʽ�� 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; //���Դ������صĽDZ�
// 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; //���Դ������صĽDZ�
// 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 drawbullet(int real_bullet_x, int real_bullet_y, IMAGE* bullet)
//{
// int bullet_x = real_bullet_x - 15;
// int bullet_y = real_bullet_y - 15;
// if (bullet_x >= 0 && bullet_x <= WINDOW_WIDTH - 30 && bullet_y >= 0 && bullet_y <= WINDOW_HEIGHT - 30)//????30??????????????С??
// {
// drawAlpha(bullet, bullet_x, bullet_y);
// }
//}
////
// 绘制玩家得分
void DrawPlayerScore(int score)
{
static TCHAR text[64];
_stprintf_s(text, _T("当前玩家得分:%d "), score);
setbkmode(TRANSPARENT);
settextcolor(RGB(255, 85, 185));
outtextxy(10, 10, text);
}
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
static void SetPos(int x, int y)
{
COORD point = { x,y };
HANDLE HOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(HOutput, point);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
int main()
{
//
// 加载子弹图片及信息
IMAGE bulletImage;
loadimage(&bulletImage, L"resource\\篮球.png");//30*30
int bullet_width = bulletImage.getwidth();//获取子弹宽度
int bullet_height = bulletImage.getheight();//获取子弹高度
float bullet_dir_x, bullet_dir_y;//方向坐标
int bullet_x = 0, bullet_y = 0;//实际坐标
int t = 0;//子弹飞行时间
//
initgraph(1280, 720);
atlas_player_left = new Atlas(_T("img/playerleft%d.png"),9 );
atlas_player_right = new Atlas(_T("img/playerright%d.png"), 9);
atlas_enemy_left = new Atlas(_T("img/enemyleft%d.png"), 7);
atlas_enemy_right = new Atlas(_T("img/enemyright%d.png"), 7);
mciSendString(_T("open mus/hit.wav alias hit"), NULL, 0, NULL);
mciSendString(_T("open mus/bgm2.mp3 alias bgm"), NULL, 0, NULL);
mciSendString(_T("open mus/deadhit.mp3 alias deadhit"), NULL, 0, NULL);
mciSendString(_T("open mus/hita.mp3 alias hita"), NULL, 0, NULL);
mciSendString(_T("open mus/hitb.mp3 alias hitb"), NULL, 0, NULL);
mciSendString(_T("open mus/hitc.mp3 alias hitc"), NULL, 0, NULL);
mciSendString(_T("open mus/hitd.mp3 alias hitd"), NULL, 0, NULL);
mciSendString(_T("open mus/hite.mp3 alias hite"), NULL, 0, NULL);
int score = 0;
Player player;
//
/*Shoot_Bullet shootbullet(0, 0, 5, 0);*/
//
ExMessage msg;
IMAGE img_menu;
IMAGE img_background;
std::vector<Enemy*> enemy_list;//敌人数量是动态的用vector存Enemy的对象指针
std::vector<CircleBullet> circlebullet_list(4);//等价于Bullet bullet_list[4];
RECT region_btn_start_game, region_btn_quit_game;
const int leftnum = 330;
const int rightnum = 420;
region_btn_start_game.left = (WINDOW_WIDTH - BUTTON_WIDTH) / 2-leftnum;
region_btn_start_game.right = region_btn_start_game.left + BUTTON_WIDTH;
region_btn_start_game.top = 430;
region_btn_start_game.bottom = region_btn_start_game.top + BUTTON_HEIGHT;
region_btn_quit_game.left = (WINDOW_WIDTH - BUTTON_WIDTH) / 2+rightnum;
region_btn_quit_game.right = region_btn_quit_game.left + BUTTON_WIDTH;
region_btn_quit_game.top = 430;
region_btn_quit_game.bottom = region_btn_quit_game.top + BUTTON_HEIGHT;
StartGameButton btn_start_game = StartGameButton(region_btn_start_game,
_T("img/ui_start_idle.png"), _T("img/ui_start_hovered.png"), _T("img/ui_start_pushed.png"));
QuitGameButton btn_quit_game = QuitGameButton(region_btn_quit_game,
_T("img/ui_quit_idle.png"), _T("img/ui_quit_hovered.png"), _T("img/ui_quit_pushed.png"));
loadimage(&img_menu, _T("img/menu2.png"));
loadimage(&img_background, _T("img/background2.png"));
BeginBatchDraw();
while (running)
{
DWORD start_time = GetTickCount();
while (peekmessage(&msg))
{
if (is_game_started)//处理玩家操作事件,没看懂
player.ProcessEvent(msg);
else
{
btn_start_game.ProcessEvent(msg);
btn_quit_game.ProcessEvent(msg);
}
}
//
FlushMouseMsgBuffer();
flushmessage();
//
//int bullet_speed = Bullet_speed;//子弹速度
//int bullet_number = Bullet_number;//拥有子弹数量,目前不可变
//int bullet_range = Bullet_range;//子弹攻击范围
//hit = false;//判断是否命中
if (is_game_started)
{
// 尝试生成新的敌人
TryGenerateEnemy(enemy_list);
// 移动玩家
player.Move();
// 更新子弹位置
UpdateCircleBullets(circlebullet_list, player);
//while (peekmessage(&msg)) {
// shootbullet.ProcessEvent(msg);
// shootbullet.Fire(player.GetX(), player.GetY());
// float directionX = player.GetX() - shootbullet.Getmx();
// float directionY = player.GetY() - shootbullet.Getmy();
// shootbullet.SetDirection(atan2(directionY, directionX));
//int bullet_speed = Bullet_speed;//子弹速度
//int bullet_number = Bullet_number;//拥有子弹数量,目前不可变
//int bullet_range = Bullet_range;//子弹攻击范围
//hit = false;//判断是否命中
//
//
//peekmessage(&msg, EM_MOUSE);
//if (msg.message == WM_LBUTTONDOWN && bullet_number > 0)
//{
// bullet_number--;
// float length = sqrt((msg.x - WINDOW_WIDTH / 2) * (msg.x - WINDOW_WIDTH / 2) + (msg.y - WINDOW_HEIGHT / 2) * (msg.y - WINDOW_HEIGHT / 2));
// bullet_dir_x = (msg.x - WINDOW_WIDTH / 2) / length;
// bullet_dir_y = (msg.y - WINDOW_HEIGHT / 2) / length;
// bullet_x = player.GetX();
// bullet_y = player.GetY();
// t = bullet_range / bullet_speed;//飞行时间
// mciSendString(_T("close atkmusic"), NULL, 0, NULL);
// mciSendString(_T("open resource\\atk.mp3 alias atkmusic"), NULL, 0, NULL);
// mciSendString(_T("play atkmusic"), NULL, 0, NULL);
//}
//}
////更新射击子弹的位置
//shootbullet.Update();
// 更新敌人位置
for (Enemy* enemy : enemy_list)
enemy->Move(player);
// 检测子弹和敌人的碰撞
for (Enemy* enemy : enemy_list)
{
for (const CircleBullet& circlebullet : circlebullet_list)
{
if (enemy->CheckBulletCollision(circlebullet))
{
srand((unsigned int)time(NULL));
int num = rand() % 6 + 1;
switch (num) {
case 1:
mciSendString(_T("play hita from 0"), NULL, 0, NULL);
break;
case 2:
mciSendString(_T("play hitb from 0"), NULL, 0, NULL);
break;
case 3:
mciSendString(_T("play hitc from 0"), NULL, 0, NULL);
break;
case 4:
mciSendString(_T("play hitd from 0"), NULL, 0, NULL);
break;
case 5:
mciSendString(_T("play hite from 0"), NULL, 0, NULL);
break;
default:
mciSendString(_T("play hit from 0"), NULL, 0, NULL);
}
//mciSendString(_T("play hit from 0"), NULL, 0, NULL);
enemy->Hurt();
score++;
}
}
}
// 移除生命值归零的敌人
for (size_t i = 0; i < enemy_list.size(); i++)