-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
1091 lines (1034 loc) · 34 KB
/
Game.cpp
File metadata and controls
1091 lines (1034 loc) · 34 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
#include"Game.h"
#include<iostream>
using namespace std;
Wall w1; //墙对象
Food f1(w1); //食物对象
Snake_first s1(w1, f1); //蛇对象(代表玩家一)
Snake_second s2(w1, f1); //蛇对象(代表玩家二)
Snake_AI s3; //蛇对象(代表AI)
clock_t t_end_1,t_end_2; //用于表示记录时间差
Game::Game()
{
x = 0, y = 0,selectmode=1;
isDead = false;
isDead_second = false;
}
void Game::star_mainmenu() //打印主界面选项
{
green();
gotoxy(hOut, 25, 10);
cout << "1. 开始游戏 ";
gotoxy(hOut, 65, 10);
cout << "2. 选择难度 ";
gotoxy(hOut, 25, 20);
cout << "3. 高分纪录 ";
gotoxy(hOut, 65, 20);
cout << "4. 选择模式 ";
gotoxy(hOut, 25, 30);
cout << "5. 选择地图 ";
gotoxy(hOut, 65, 30);
cout << "6. 退出游戏 ";
}
void Game::star_selectmenu() //打印难度选择界面选项
{
green();
gotoxy(hOut, 25, 10);
cout << "1. 简单 ";
gotoxy(hOut, 65, 10);
cout << "2. 普通 ";
gotoxy(hOut, 25, 20);
cout << "3. 困难 ";
gotoxy(hOut, 65, 20);
cout << "4. 地狱 ";
}
void Game::star_mapmenu() //打印地图选择界面选项
{
green();
gotoxy(hOut, 25, 10);
cout << "1. 地图1 ";
gotoxy(hOut, 65, 10);
cout << "2. 地图2 ";
gotoxy(hOut, 25, 20);
cout << "3. 地图3 ";
gotoxy(hOut, 65, 20);
cout << "4. 地图4 ";
}
void Game::star_endmenu() //打印游戏结束界面选项
{
green();
gotoxy(hOut, 25, 15);
cout << "1. 是 ";
gotoxy(hOut, 65, 15);
cout << "2. 否 ";
}
void Game::star_modemenu() //打印模式界面选项
{
green();
gotoxy(hOut, 20, 15);
cout << "1. 单人模式 ";
gotoxy(hOut, 60, 15);
cout << "2. 双人模式 ";
}
void Game::star_modemenu_ai() //打印模式(玩家或AI)界面选项
{
green();
gotoxy(hOut, 20, 15);
cout << "1. 玩家模式 ";
gotoxy(hOut, 60, 15);
cout << "2. AI模式 ";
}
void Game::onChoose_mainmenu() //主界面,重新打印被选择字体(即背景色变红)
{
if (x == 25 && y == 10)
{
on_Select();
cout << "1. 开始游戏 ";
}
else if (x == 65 && y == 10)
{
on_Select();
cout << "2. 选择难度 ";
}
else if (x == 25 && y == 20)
{
on_Select();
cout << "3. 高分纪录 ";
}
else if (x == 65 && y == 20)
{
on_Select();
cout << "4. 选择模式 ";
}
else if (x == 25 && y == 30)
{
on_Select();
cout << "5. 选择地图 ";
}
else if (x == 65 && y == 30)
{
on_Select();
cout << "6. 退出游戏 ";
}
}
void Game::onChoose_selectmenu() //难度选择界面,重新打印被选择字体(即背景色变红)
{
if (x == 25 && y == 10)
{
on_Select();
cout << "1. 简单 ";
}
else if (x == 25 && y == 20)
{
on_Select();
cout << "3. 困难 ";
}
else if (x == 65 && y == 10)
{
on_Select();
cout << "2. 普通 ";
}
else if (x == 65 && y == 20)
{
on_Select();
cout << "4. 地狱 ";
}
}
void Game::onChoose_mapmenu() //地图选择界面,重新打印被选择字体(即背景色变红)
{
if (x == 25 && y == 10)
{
on_Select();
cout << "1. 地图1 ";
}
else if (x == 25 && y == 20)
{
on_Select();
cout << "3. 地图3 ";
}
else if (x == 65 && y == 10)
{
on_Select();
cout << "2. 地图2 ";
}
else if (x == 65 && y == 20)
{
on_Select();
cout << "4. 地图4 ";
}
}
void Game::onChoose_endmenu() //游戏结束界面,重新打印被选择字体(即背景色变红)
{
if (x == 25 && y == 15)
{
on_Select();
cout << "1. 是 ";
}
else if (x == 65 && y == 15)
{
on_Select();
cout << "2. 否 ";
}
}
void Game::onChoose_modemenu() //模式界面,重新打印被选择字体(即背景色变红)
{
if (x == 20 && y == 15)
{
on_Select();
cout << "1. 单人模式 ";
}
else if (x == 60 && y == 15)
{
on_Select();
cout << "2. 双人模式 ";
}
}
void Game::onChoose_modemenu_ai() //模式界面(玩家或AI),重新打印被选择字体(即背景色变红)
{
if (x == 20 && y == 15)
{
on_Select();
cout << "1. 玩家模式";
}
else if (x == 60 && y == 15)
{
on_Select();
cout << "2. AI模式";
}
}
void Game::print_mainmenu() //打印主界面
{
green();
system("cls");
cout << "操作说明:w,a,s,d 控制光标选择;Enter 确定;空格加速;控制蛇(玩家一:w,a,s,d ;玩家二:i,j,k,l)" << endl;//操作说明
gotoxy(hOut, 45, 4);
cout << "贪吃蛇大作战";
blue_border();
gotoxy(hOut, 10, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; //打印上边框
gotoxy(hOut, 10, 35);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"; //打印下边框
for (int i = 10, j = 6; j < 35; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 90, j = 6; j < 35; j++) //打印右边框
{
gotoxy(hOut, i, j);
cout << "*";
}
on_Select(); //打印各个选项
gotoxy(hOut, 25, 10);
cout << "1. 开始游戏 ";
green();
gotoxy(hOut, 65, 10);
cout << "2. 选择难度 ";
gotoxy(hOut, 25, 20);
cout << "3. 高分纪录 ";
gotoxy(hOut, 65, 20);
cout << "4. 选择模式 ";
gotoxy(hOut, 25, 30);
cout << "5. 选择地图 ";
gotoxy(hOut, 65, 30);
cout << "6. 退出游戏 ";
x = 25, y = 10; //初始化坐标
char sel;
sel = _getch();
while (sel != '\r') //如果监测到键盘输入Enter键,代表选择该选项,跳出循环
{
star_mainmenu();
switch (sel) //wsad控制光标进行自由选择
{
case 'w':
y = y - 10;
break;
case 's':
y = y + 10;
break;
case 'a':
x = x - 40;
break;
case 'd':
x = x + 40;
break;
}
//防止超出范围
if (x >= 65)
{
x = 65;
}
if (y >= 30)
{
y = 30;
}
if (x <= 25)
{
x = 25;
}
if (y <= 10)
{
y = 10;
}
gotoxy(hOut, x, y); //到达光标位置
onChoose_mainmenu(); //重新打印被选择选项,标红
sel = _getch();
}
if (x == 25 && y == 10) //开始游戏
{
if (selectmode == 1) //单人模式
{
white();
system("cls"); //先清屏
srand((unsigned int)time(0));
char prekey = NULL; //保存前一键盘输入
w1.Initialwall(); //初始化墙模块
w1.Printwall(); //打印墙
s1.InitSnake(); //初始化蛇
white();
gotoxy(hOut, 0, Wall::row + 1); //在左下角显示当前得分以及难度等级
cout << "得分:" << s1.Getscore() << endl;
cout << "难度:";
if (s1.difficulty == 1)
{
cout << "简单" << endl;
}
else if (s1.difficulty == 2)
{
cout << "普通" << endl;
}
else if (s1.difficulty == 3)
{
cout << "困难" << endl;
}
else if (s1.difficulty == 4)
{
cout << "地狱" << endl;
}
f1.Setfood(); //设置食物
while (!isDead) //如果蛇没有死亡,一直循环
{
char key1 = _getch(); //接收键盘输入
if (prekey == NULL && key1 == s1.LEFT) { continue; } //最开始方向向左
do
{
if (key1 == s1.UP || key1 == s1.DOWN || key1 == s1.LEFT || key1 == s1.RIGHT)
{
if ((key1 == s1.LEFT && prekey == s1.RIGHT) || //如果键盘输入与当前方向相反,则无效,保持当前方向,不更新键盘输入
(key1 == s1.RIGHT && prekey == s1.LEFT) ||
(key1 == s1.UP && prekey == s1.DOWN) ||
(key1 == s1.DOWN && prekey == s1.UP)
)
{
key1 = prekey;
}
else //否则更新键盘输入
{
prekey = key1;
}
if (s1.Move(key1) == true) //如果蛇移动成功
{
gotoxy(hOut, 0, Wall::row + 1);
white();
cout << "得分:" << s1.Getscore(); //更新得分
if (GetAsyncKeyState(VK_SPACE)) //监测是否按下空格
Sleep(25); //按下则加速
else
Sleep(s1.Getsleeptime()); //否则进行正常延迟
}
else
{
isDead = true;
updatescore(); //如果死亡,更新高分纪录;
print_endmenu(); //打印游戏结束界面
if (isDead)
break;
}
}
else
{
key1 = prekey; //如果按下其他键,蛇保持当前方向
}
t_end_1 = clock();
if ((f1.Get_attribute() == 1|| f1.Get_attribute() == 2) && (t_end_1 - f1.t_start > 3000))
{
w1.Setwall(f1.Get_FoodY(), f1.Get_FoodX(), (char*)" ");
gotoxy(hOut, f1.Get_FoodX() * 2, f1.Get_FoodY());
std::cout << " ";
f1.Setfood();
}
t_end_2 = clock();
if (s1.Getability() == 1 && (t_end_2 - s1.start > 5000))
s1.Resetability();
} while (!_kbhit()); //如果没有监测到键盘输入,则保持当前方向
}
return;
}
else if (selectmode == 2) //双人模式
{
white();
system("cls");
srand((unsigned int)time(0));
char prekey = NULL; //保存当前键盘输入
char prekey_second = NULL; //保存前一键盘输入
w1.Initialwall();
w1.Printwall(); //打印墙
f1.Setfood(); //设置食物
s1.InitSnake(); //初始化玩家一
s2.InitSnake(); //初始化玩家二
white();
gotoxy(hOut, 0, Wall::row);
cout << "玩家一得分:" << s1.Getscore(); //左下角打印两名玩家的得分
gotoxy(hOut, 0, Wall::row + 1);
cout << "玩家二得分:" << s2.Getscore();
while ((!isDead) && (!isDead_second))
{
char key = _getch();
char key1 = key; //代表玩家一接收键盘输入
char key2 = key; //代表玩家二接收键盘输入
if (prekey == NULL && key1 == s1.LEFT) { continue; }
if (prekey_second == NULL && key2 == s2.LEFT) { continue; }
do
{
if (key1 == s1.UP || key1 == s1.DOWN || key1 == s1.LEFT || key1 == s1.RIGHT)
{
if ((key1 == s1.LEFT && prekey == s1.RIGHT) || //如果当前输入与蛇的方向反向,输入无效
(key1 == s1.RIGHT && prekey == s1.LEFT) ||
(key1 == s1.UP && prekey == s1.DOWN) ||
(key1 == s1.DOWN && prekey == s1.UP)
)
{
key1 = prekey; //保持前一方向
}
else
{
prekey = key1; //否则更新方向
}
if (s1.Move(key1) == true)
{
gotoxy(hOut, 0, Wall::row);
white();
cout << "玩家一得分:" << s1.Getscore(); //移动成功,更新玩家一得分
Sleep(75);
}
else
{
isDead = true;
print_endmenu_2(); //如果玩家一死亡,游戏结束
if (isDead)
break;
}
}
else
{
key1 = prekey; //其他无效输入则让蛇保持当前方向不变
}
if (key2 == s2.UP || key2 == s2.DOWN || key2 == s2.LEFT || key2 == s2.RIGHT)
{
if ((key2 == s2.LEFT && prekey_second == s2.RIGHT) || //如果当前输入与蛇的方向反向,输入无效
(key2 == s2.RIGHT && prekey_second == s2.LEFT) ||
(key2 == s2.UP && prekey_second == s2.DOWN) ||
(key2 == s2.DOWN && prekey_second == s2.UP)
)
{
key2 = prekey_second; //保持前一方向
}
else
{
prekey_second = key2; //否则更新方向
}
if (s2.Move(key2) == true)
{
gotoxy(hOut, 0, Wall::row + 1);
white();
cout << "玩家二得分:" << s2.Getscore(); //移动成功,更新玩家二得分
Sleep(75);
}
else
{
isDead_second = true;
print_endmenu_2(); //如果玩家二死亡,游戏结束
if (isDead_second)
break;
}
}
else
{
key2 = prekey_second; //其他无效按键则保持当前方向
}
t_end_1 = clock();
if ((f1.Get_attribute() == 1||f1.Get_attribute()==2) && (t_end_1 - f1.t_start > 3000))
{
w1.Setwall(f1.Get_FoodY(), f1.Get_FoodX(), (char*)" ");
gotoxy(hOut, f1.Get_FoodX() * 2, f1.Get_FoodY());
std::cout << " ";
f1.Setfood();
}
t_end_2 = clock();
if (s1.Getability() == 1 && (t_end_2 - s1.start > 5000))
s1.Resetability();
if (s2.Getability() == 1 && (t_end_2 - s2.start > 5000))
s2.Resetability();
} while (!_kbhit()); //如果当前没有键盘输入,蛇就保持当前方向前进
}
return;
}
else if (selectmode == 3)
{
white();
system("cls");
srand((unsigned int)time(0));
s3.printInterface(); //打印界面
while (s3.Snake.size() < static_cast<unsigned long long>(Snake_AI::WIDTH) * Snake_AI::HEIGHT) //未占满全屏
{
s3.snakeMove(); //移动一步
s3.printSnake(); //打印新的蛇
Sleep(20); //控制移动速度
if (GetAsyncKeyState(VK_ESCAPE)) //按下ESC返回
break;
}
while (!s3.Snake.empty())
s3.Snake.pop_back();
print_mainmenu();
return;
}
}
else if (x == 65 && y == 10) //选择难度界面
{
print_selectmenu();
}
else if (x == 25 && y == 20) //选择记录高分界面
{
print_scoremenu();
}
else if (x == 65 && y == 20) //选择模式界面
{
print_modemenu();
}
else if (x == 25 && y == 30) //选择地图界面
{
print_mapmenu();
}
else if (x == 65 && y == 30)
{
exit(-1);
}
}
void Game::print_selectmenu()
{
mode_color();
system("cls");
gotoxy(hOut, 42, 4);
cout << "选择难度";
gotoxy(hOut, 5, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; //打印上边框
gotoxy(hOut, 5, 25);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"; //打印下边框
for (int i = 5, j = 6; j < 25; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 85, j = 6; j < 25; j++) //打印右边框
{
gotoxy(hOut, i, j);
cout << "*";
}
green();
gotoxy(hOut, 65, 10); //打印不同模式
cout << "2. 普通 ";
gotoxy(hOut, 25, 20);
cout << "3. 困难 ";
gotoxy(hOut, 65, 20);
cout << "4. 地狱 ";
on_Select();
gotoxy(hOut, 25, 10);
cout << "1. 简单 ";
//wsad控制光标对进行自由选择
x = 25, y = 10;
char sel;
sel = _getch();
while (sel != '\r')
{
star_selectmenu();
switch (sel)
{
case 'w':
y = y - 10;
break;
case 's':
y = y + 10;
break;
case 'a':
x = x - 40;
break;
case 'd':
x = x + 40;
break;
}
//防止超出范围
if (x >= 65)
{
x = 65;
}
if (y >= 20)
{
y = 20;
}
if (x <= 25)
{
x = 25;
}
if (y <= 10)
{
y = 10;
}
gotoxy(hOut, x, y);
onChoose_selectmenu();
sel = _getch();
}
if (x == 25 && y == 10)
s1.difficulty = 1; //简单
else if (x == 65 && y == 10)
s1.difficulty = 2; //普通
else if (x == 25 && y == 20)
s1.difficulty = 3; //困难
else if (x == 65 && y == 20)
s1.difficulty = 4; //地狱
print_mainmenu(); //返回主界面
}
void Game::print_mapmenu()
{
mode_color();
system("cls");
gotoxy(hOut, 42, 4);
cout << "选择地图";
gotoxy(hOut, 5, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; //打印上边框
gotoxy(hOut, 5, 25);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"; //打印下边框
for (int i = 5, j = 6; j < 25; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 85, j = 6; j < 25; j++) //打印右边框
{
gotoxy(hOut, i, j);
cout << "*";
}
green();
gotoxy(hOut, 65, 10); //打印不同地图
cout << "2. 地图2 ";
gotoxy(hOut, 25, 20);
cout << "3. 地图3 ";
gotoxy(hOut, 65, 20);
cout << "4. 地图4 ";
on_Select();
gotoxy(hOut, 25, 10);
cout << "1. 地图1 ";
//wsad控制光标对进行自由选择
x = 25, y = 10;
char sel;
sel = _getch();
while (sel != '\r')
{
star_mapmenu();
switch (sel)
{
case 'w':
y = y - 10;
break;
case 's':
y = y + 10;
break;
case 'a':
x = x - 40;
break;
case 'd':
x = x + 40;
break;
}
//防止超出范围
if (x >= 65)
{
x = 65;
}
if (y >= 20)
{
y = 20;
}
if (x <= 25)
{
x = 25;
}
if (y <= 10)
{
y = 10;
}
gotoxy(hOut, x, y);
onChoose_mapmenu();
sel = _getch();
}
if (x == 25 && y == 10)
w1.map = 0;
else if (x == 65 && y == 10)
w1.map = 1;
else if (x == 25 && y == 20)
w1.map = 2;
else if (x == 65 && y == 20)
w1.map = 3;
print_mainmenu(); //返回主界面
}
void Game::print_endmenu()
{
mode_color();
system("cls");
gotoxy(hOut, 33, 4);
cout << "本次游戏得分:" << s1.Getscore() << " " << "是否再次进行游戏";
s1.Resetscore(); //得分清零
gotoxy(hOut, 5, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//打印上边框
gotoxy(hOut, 5, 25);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//打印下边框
for (int i = 5, j = 6; j < 25; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 85, j = 6; j < 25; j++) //打印右边框
{
gotoxy(hOut, i, j);
cout << "*";
}
green();
gotoxy(hOut, 65, 15);
cout << "2. 否 ";
gotoxy(hOut, 25, 15);
on_Select();
cout << "1. 是 ";
//wsad控制光标对进行自由选择
x = 25, y = 15;
char sel;
sel = _getch();
while (sel != '\r')
{
star_endmenu();
switch (sel)
{
case 'a':
x = x - 40;
break;
case 'd':
x = x + 40;
break;
}
//防止超出范围
if (x >= 65)
{
x = 65;
}
if (x <= 25)
{
x = 25;
}
gotoxy(hOut, x, y);
onChoose_endmenu();
sel = _getch();
}
if (x == 25 && y == 15)
{
isDead = false; //重新游戏,玩家置活
print_mainmenu(); //返回主界面
}
else if (x == 65 && y == 15) //不重新开始游戏,游戏结束
{
exit(-1);
}
}
void Game::print_endmenu_2() //双人模式结束界面
{
mode_color();
system("cls");
gotoxy(hOut, 43, 4);
if (s1.Getscore() > s2.Getscore())
cout << "玩家一获胜";
else if (s1.Getscore() < s2.Getscore())
cout << "玩家二获胜";
else if (s1.Getscore() == s2.Getscore())
cout << "平局";
s1.Resetscore(); //得分清零
s2.Resetscore();
gotoxy(hOut, 38, 10);
cout << "是否再次进行游戏";
gotoxy(hOut, 5, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//打印上边框
gotoxy(hOut, 5, 25);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//打印下边框
for (int i = 5, j = 6; j < 25; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 85, j = 6; j < 25; j++) //打印右边框
{
gotoxy(hOut, i, j);
cout << "*";
}
green();
gotoxy(hOut, 65, 15);
cout << "2. 否 ";
gotoxy(hOut, 25, 15);
on_Select();
cout << "1. 是 ";
//wsad控制光标对进行自由选择
x = 25, y = 15;
char sel;
sel = _getch();
while (sel != '\r')
{
star_endmenu();
switch (sel)
{
case 'a':
x = x - 40;
break;
case 'd':
x = x + 40;
break;
}
//防止超出范围
if (x >= 65)
{
x = 65;
}
if (x <= 25)
{
x = 25;
}
gotoxy(hOut, x, y);
onChoose_endmenu();
sel = _getch();
}
if (x == 25 && y == 15)
{
isDead = false; // 如果重新游戏,玩家一,玩家二置活
isDead_second = false;
print_mainmenu(); //返回主界面
}
else if (x == 65 && y == 15)
{
exit(-1); //不重新开始游戏,结束游戏
}
}
void Game::print_scoremenu()
{
mode_color();
system("cls");
gotoxy(hOut, 42, 4);
cout << "历史高分";
gotoxy(hOut, 5, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//打印上边框
gotoxy(hOut, 5, 25);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//打印下边框
for (int i = 5, j = 6; j < 25; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 85, j = 6; j < 25; j++) //打印右边框
{
gotoxy(hOut, i, j);
cout << "*";
}
fstream outin("score.dat", ios::in | ios::out | ios::binary);//获取得分纪录
outin.seekg(0);
int score[3]; //创建数组保存得分前三名
for (int i = 0; i < 3; ++i)
outin.read(reinterpret_cast<char*>(&score[i]), sizeof(int));
outin.close();
gotoxy(hOut, 40, 10); //打印前三名得分
cout << "第一名:" << score[0];
gotoxy(hOut, 40, 15);
cout << "第二名:" << score[1];
gotoxy(hOut, 40, 20);
cout << "第三名:" << score[2];
char sel;
sel = _getch();
while (sel != 27) //按下ESC键,返回主界面
{
sel = _getch();
}
print_mainmenu();
}
void Game::print_modemenu()
{
mode_color();
system("cls");
gotoxy(hOut, 41, 4);
cout << "选择模式";
gotoxy(hOut, 5, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//打印上边框
gotoxy(hOut, 5, 25);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//打印下边框
for (int i = 5, j = 6; j < 25; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 85, j = 6; j < 25; j++) //打印右边框
{
gotoxy(hOut, i, j);
cout << "*";
}
green();
gotoxy(hOut, 60, 15);
cout << "2. 双人模式 ";
gotoxy(hOut, 20, 15);
on_Select();
cout << "1. 单人模式 ";
//wsad控制光标对进行自由选择
x = 20, y = 15;
char sel;
sel = _getch();
while (sel != '\r')
{
star_modemenu();
switch (sel)
{
case 'a':
x = x - 40;
break;
case 'd':
x = x + 40;
break;
}
//防止超出范围
if (x >= 60)
{
x = 60;
}
if (x <= 20)
{
x = 20;
}
gotoxy(hOut, x, y);
onChoose_modemenu();
sel = _getch();
}
if (x == 20 && y == 15)
{
selectmode = 1; //单人模式
print_modemenu_ai(); //返回主界面
}
else if (x == 60 && y == 15)
{
selectmode = 2; //双人模式
print_mainmenu(); //返回主界面
}
}
void Game::print_modemenu_ai()
{
mode_color();
system("cls");
gotoxy(hOut, 39, 4);
cout << "选择玩家或AI";
gotoxy(hOut, 5, 5);
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//打印上边框
gotoxy(hOut, 5, 25);
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//打印下边框
for (int i = 5, j = 6; j < 25; j++) //打印左边框
{
gotoxy(hOut, i, j);
cout << "*";
}
for (int i = 85, j = 6; j < 25; j++) //打印右边框
{