• C++ 游戏飞机大战, 字符型的


    1. //#define _CRT_SECURE_NO_WARNINGS 1 用于禁止不安全函数的警告
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. char ch;
    11. #define Count 5//敌机数量
    12. #define Col 40//列
    13. #define Row 40//行
    14. //玩家飞机坐标,声明:左上角坐标为(0,0)
    15. int PlayerPlane_y = Row - 2;//39,墙上面最后一行
    16. int PlayerPlane_x = Col / 2;//20,列中央
    17. //子弹坐标
    18. int Bullet_y;
    19. int Bullet_x;
    20. //敌机坐标
    21. int Enemy_y[Count] = { 0 };
    22. int Enemy_x[Count] = { 0 };
    23. //敌机的移动速度
    24. int EnemySleep = 250;
    25. int sleep = 0;//当二者相等时敌机才发生移动,sleep可认为缓冲,该设置用于控制速度与难度梯度
    26. //分数
    27. int score = 0;
    28. //技能充能
    29. int skill1 = 20;
    30. int skill2 = 5;
    31. //容错度
    32. int error = 0;//抵达五时失败
    33. //获取系统时间
    34. char* time()//返回指针
    35. {
    36. time_t rawtime;//原始时间
    37. struct tm* curtime;//指向结构体的变量
    38. time(&rawtime); // 获取系统时间并存储
    39. curtime = localtime(&rawtime); //转换为本地时间
    40. char* now = asctime(curtime);//更改为指针类型
    41. return now;
    42. }
    43. //弄一个结构体保存当前位置的各项数据,但是不保存已使用的子弹,以此来实现简单存档
    44. typedef struct history
    45. {
    46. int PlayerPlane_y;
    47. int PlayerPlane_x;
    48. int Enemy_y[Count];
    49. int Enemy_x[Count];
    50. int EnemySleep;
    51. int sleep;
    52. int score;
    53. int skill1;
    54. int skill2;
    55. int error;
    56. char* curtime;
    57. int flag = 0;//初始化标记
    58. }history, * apple;
    59. void contain(apple& L, int PlayerPlane_y, int PlayerPlane_x, int EnemySleep, int sleep, int score, int skill1, int skill2, int error, int flag, char* curtime)
    60. {
    61. L = new history;//赋予空间
    62. L->PlayerPlane_y = PlayerPlane_y;
    63. L->PlayerPlane_x = PlayerPlane_x;
    64. L->EnemySleep = EnemySleep;
    65. L->sleep = sleep;
    66. L->score = score;
    67. L->skill1 = skill1;
    68. L->skill2 = skill2;
    69. L->error = error;
    70. L->flag = flag;
    71. L->curtime = curtime;
    72. }
    73. void game();//有关进入游戏后的各项函数
    74. void menu()
    75. {
    76. printf(" --------------飞机大作战--------------\n");
    77. printf(" | |\n");
    78. printf(" | 3.查看历史记录 |\n");
    79. printf(" | 2.选择存档开始 |\n");
    80. printf(" | 1.开始游戏 |\n");
    81. printf(" | 0.退出游戏 |\n");
    82. printf(" | W/A/S/D移动 |\n");
    83. printf(" | 空格射击 E/R技能 |\n");
    84. printf(" | |\n");
    85. printf(" |w温馨提示,游戏过程中可以按下\"Esc\"退出游戏 |\n");
    86. printf(" ----------------------------------------------\n");
    87. }
    88. int main()
    89. {
    90. system("color b");
    91. int input = 0;
    92. menu();
    93. printf("请选择:");
    94. scanf("%d", &input);
    95. switch (input)
    96. {
    97. case 1:
    98. game();//大部分函数均包括在内
    99. break;
    100. case 0:
    101. printf("退出游戏\n");
    102. break;
    103. default:
    104. printf("输入有误,请重新输入:\n");
    105. break;
    106. }
    107. return 0;
    108. }
    109. //隐藏光标
    110. void HideCursor()
    111. {
    112. CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; //第二个值为0,表示隐藏光标
    113. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    114. }
    115. // 光标移到(X, Y)位置
    116. void gotoxy(int x, int y)
    117. {
    118. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    119. COORD pos;
    120. pos.X = x;
    121. pos.Y = y;
    122. SetConsoleCursorPosition(handle, pos);
    123. }
    124. void DisPlay(int arr[Col][Row])//绘制画面
    125. {
    126. gotoxy(0, 0);
    127. for (int i = 0; i < Col; i++)
    128. {
    129. for (int j = 0; j < Row; j++)
    130. {
    131. if (arr[i][j] == 0)//路
    132. {
    133. printf(" ");
    134. }
    135. if (arr[i][j] == 1)//基地区域
    136. {
    137. printf("█");
    138. }
    139. if (arr[i][j] == 2)//己方
    140. {
    141. printf("++");
    142. }
    143. if (arr[i][j] == 3)//敌机
    144. {
    145. printf("%%+");
    146. }
    147. if (arr[i][j] == 4)//子弹
    148. {
    149. printf("/\\");
    150. }
    151. }
    152. printf("\n");
    153. }
    154. //各项数值
    155. char* curtime = time();
    156. printf("得分:%d ", score);
    157. printf("EnemySleep=%d ", EnemySleep);
    158. printf("skill1(20)=%d ", skill1);
    159. printf("skill2(5)=%d ", skill2);
    160. printf("时间:%s", curtime);
    161. Sleep(20);//刷新频率
    162. }
    163. void InSet(int arr[Col][Row])
    164. {
    165. srand(time(NULL));//设置随机数种子
    166. //路--0
    167. //墙--1
    168. for (int i = 0; i < Col; i++)//赋值定义
    169. {
    170. arr[i][0] = 1;
    171. arr[i][Row - 1] = 1;
    172. }
    173. for (int i = 0; i < Row; i++)
    174. {
    175. arr[0][i] = 1;
    176. arr[Col - 1][i] = 1;
    177. }
    178. //玩家飞机--2
    179. arr[PlayerPlane_y][PlayerPlane_x] = 2;//一开始在中央
    180. //敌机--3
    181. for (int i = 0; i < Count; i++)//随机出现敌机
    182. {
    183. Enemy_y[i] = rand() % 3 + 1;//确保敌机不出现在墙区域,并且不会太接近下方基地
    184. Enemy_x[i] = rand() % (Row - 2) + 1;//确保不会出现在墙中
    185. arr[Enemy_y[i]][Enemy_x[i]] = 3;//敌机位置
    186. }
    187. //子弹--4
    188. }
    189. void PlayerPlay(int arr[Col][Row])
    190. {
    191. if ((ch == 'w' || ch== 72) && arr[PlayerPlane_y - 1][PlayerPlane_x] == 0)//上飞,且路通
    192. {
    193. arr[PlayerPlane_y][PlayerPlane_x] = 0;//清除
    194. PlayerPlane_y--;//数值小则上
    195. arr[PlayerPlane_y][PlayerPlane_x] = 2;//飞机位置
    196. }
    197. if ((ch == 'a' || ch== 75) && arr[PlayerPlane_y][PlayerPlane_x - 1] == 0)//下述同理
    198. {
    199. arr[PlayerPlane_y][PlayerPlane_x] = 0;
    200. PlayerPlane_x--;
    201. arr[PlayerPlane_y][PlayerPlane_x] = 2;
    202. }
    203. if ((ch == 's' || ch == 80) && arr[PlayerPlane_y + 1][PlayerPlane_x] == 0)
    204. {
    205. arr[PlayerPlane_y][PlayerPlane_x] = 0;
    206. PlayerPlane_y++;
    207. arr[PlayerPlane_y][PlayerPlane_x] = 2;
    208. }
    209. if ((ch == 'd' || ch == 77) && arr[PlayerPlane_y][PlayerPlane_x + 1] == 0)
    210. {
    211. arr[PlayerPlane_y][PlayerPlane_x] = 0;
    212. PlayerPlane_x++;
    213. arr[PlayerPlane_y][PlayerPlane_x] = 2;
    214. }
    215. if (ch == ' ')//空格射击
    216. {
    217. Bullet_y = PlayerPlane_y - 1;
    218. Bullet_x = PlayerPlane_x;
    219. arr[Bullet_y][Bullet_x] = 4;//子弹位置
    220. }
    221. if (ch == 'r')//技能
    222. {
    223. if (skill1 == 20)//充能结束
    224. {
    225. for (int i = 1; i < Row - 1; i++)//火力覆盖
    226. {
    227. skill1 = 0;//归零
    228. Bullet_y = PlayerPlane_y - 1;//上飞,完成后一行子弹上飞到顶
    229. Bullet_x = i;//布满横行
    230. arr[Bullet_y][Bullet_x] = 4;//位置
    231. }
    232. }
    233. }
    234. if (ch == 'e')//技能
    235. {
    236. int left = PlayerPlane_x - 3;//左线
    237. int right = PlayerPlane_x + 3;//右线
    238. if (skill2 == 5)//充能
    239. {
    240. for (int i = left; i < right; i++)//火力覆盖
    241. {
    242. if (i > 0 && i < Row - 1)//可见r技能火力充足
    243. {
    244. skill2 = 0;//归零
    245. Bullet_y = PlayerPlane_y - 1;//上飞,几颗子弹到顶
    246. Bullet_x = i;
    247. arr[Bullet_y][Bullet_x] = 4;
    248. }
    249. }
    250. }
    251. }
    252. }
    253. void BulletEnemy(int arr[Col][Row])//关于子弹与敌机的处理,包括能量与加速的处理
    254. {
    255. for (int i = 0; i < Col; i++)
    256. {
    257. for (int j = 0; j < Row; j++)
    258. {
    259. if (arr[i][j] == 4)//有子弹
    260. {
    261. for (int k = 0; k < Count; k++)//检查各个敌机
    262. {
    263. //子弹击中敌机的处理
    264. if (i == Enemy_y[k] && j == Enemy_x[k])
    265. {
    266. if (skill1 < 20)
    267. {
    268. skill1++;
    269. }
    270. if (skill2 < 5)
    271. {
    272. skill2++;
    273. }
    274. score += 100;//分数
    275. arr[Enemy_y[k]][Enemy_x[k]] = 0;//清除
    276. Enemy_y[k] = rand() % 3 + 1;
    277. Enemy_x[k] = rand() % (Row - 2) + 1;
    278. arr[Enemy_y[k]][Enemy_x[k]] = 3;//重构
    279. //每500分敌机加速
    280. if (score % 500 == 0 && EnemySleep > 4)
    281. {
    282. EnemySleep -= 2;
    283. }
    284. }
    285. }
    286. //子弹的移动
    287. if (arr[i][j] == 4)
    288. {
    289. arr[i][j] = 0;
    290. if (i > 1)
    291. {
    292. arr[i - 1][j] = 4;
    293. }
    294. }
    295. }
    296. }
    297. //敌机的移动,sleep初始0
    298. if (sleep < EnemySleep)
    299. {
    300. sleep++;
    301. }
    302. else if (sleep > EnemySleep)
    303. {
    304. sleep = 0;
    305. }
    306. for (int i = 0; i < Count; i++)//遍历敌机
    307. {
    308. if (PlayerPlane_y == Enemy_y[i] && PlayerPlane_x == Enemy_x[i] || score < 0)
    309. {
    310. printf(" /\\_/\\ \n");//敌机击中玩家飞机的处理
    311. printf(" ( o.o ) \n");
    312. printf(" > ^ < \n");
    313. printf("游戏失败!\n");
    314. printf("\a");//发出失败警告
    315. system("pause");//等待
    316. exit(0);
    317. }
    318. //敌机到达最底面的处理
    319. if (Enemy_y[i] >= Col - 2)//提前处理,不破坏墙面,当然不提前处理也没问题,可以设1
    320. {
    321. score -= 100;
    322. arr[Enemy_y[i]][Enemy_x[i]] = 0;
    323. Enemy_y[i] = rand() % 3 + 1;
    324. Enemy_x[i] = rand() % (Row - 2) + 1;
    325. arr[Enemy_y[i]][Enemy_x[i]] = 3;
    326. }
    327. //敌机下移的处理
    328. if (sleep == EnemySleep)
    329. {
    330. for (int j = 0; j < Count; j++)
    331. {
    332. arr[Enemy_y[j]][Enemy_x[j]] = 0;
    333. sleep = 0;
    334. Enemy_y[j]++;
    335. arr[Enemy_y[j]][Enemy_x[j]] = 3;
    336. }
    337. }
    338. }
    339. }
    340. }
    341. void write(apple& L, FILE* fp)//传引用,避免指针的使用
    342. {
    343. L->curtime = time();
    344. fprintf
    345. (fp, "%d %d %d %d %d %d %d %d %d %s ",
    346. L->PlayerPlane_y,
    347. L->PlayerPlane_x,
    348. L->EnemySleep,
    349. L->sleep,
    350. L->score,
    351. L->skill1,
    352. L->skill2,
    353. L->error,
    354. L->flag,
    355. *(L->curtime));
    356. for (int i = 0; i < Count; i++)
    357. fprintf(fp, "%d %d", L->Enemy_y[i], L->Enemy_x[i]);
    358. }
    359. void creathistory(apple& L)//创建存档
    360. {
    361. FILE* fp;
    362. if ((fp = fopen("history.txt", "a+")) == NULL) { cout << "打开失败,没有存档,请建立新的存档"; system("pause"); exit(0); }
    363. else
    364. {
    365. write(L, fp);
    366. }
    367. }
    368. void read(apple& L)
    369. {
    370. FILE* fp;
    371. if ((fp = fopen("history.txt", "a+")) == NULL) { cout << "打开失败,没有存档,请建立新的存档"; system("pause"); exit(0); }
    372. else
    373. {
    374. int PlayerPlane_y;
    375. int PlayerPlane_x;
    376. int Enemy_y[Count];
    377. int Enemy_x[Count];
    378. int EnemySleep;
    379. int sleep;
    380. int score;
    381. int skill1;
    382. int skill2;
    383. int error;
    384. char* curtime;
    385. int flag;
    386. while (fscanf
    387. (fp, "%d %d %d %d %d %d %d %d %d %s\n",
    388. &L->PlayerPlane_y,
    389. &L->PlayerPlane_x,
    390. &L->EnemySleep,
    391. &L->sleep,
    392. &L->score,
    393. &L->skill1,
    394. &L->skill2,
    395. &L->error,
    396. &L->flag,
    397. L->curtime))
    398. {
    399. for (int i = 0; i < Count; i++)
    400. fscanf(fp, "%d %d", L->Enemy_y[i], L->Enemy_x[i]);
    401. }
    402. rewind(fp);//指针归位
    403. }
    404. }
    405. void again(apple& L)
    406. {
    407. int arr[Col][Row] = { 0 };
    408. for (int i = 0; i < Count; i++)
    409. {
    410. arr[L->Enemy_y[i]][L->Enemy_x[i]] = 3;
    411. }
    412. arr[L->PlayerPlane_y][L->PlayerPlane_x] = 2;
    413. for (int i = 0; i < Col; i++)//赋值定义
    414. {
    415. arr[i][0] = 1;
    416. arr[i][Row - 1] = 1;
    417. }
    418. for (int i = 0; i < Row; i++)
    419. {
    420. arr[0][i] = 1;
    421. arr[Col - 1][i] = 1;
    422. }
    423. //打印游戏界面
    424. DisPlay(arr);
    425. //玩家移动
    426. while (1)
    427. {
    428. //时间
    429. time();
    430. //玩家操作
    431. PlayerPlay(arr);
    432. //打印棋盘
    433. DisPlay(arr);
    434. //子弹与敌机的操作
    435. BulletEnemy(arr);
    436. }
    437. }
    438. void findhistory(apple& L)//查找存档
    439. {
    440. for (int i = 0; i < L->flag; i++)
    441. {
    442. read(L);
    443. printf("请输入你选择的存档编号flag:");
    444. int a = getch();
    445. cout << a << endl;
    446. if (_kbhit() && i == L->flag - 1)//判断是否有键盘输入
    447. {
    448. system("cls");
    449. again(L);
    450. }
    451. }
    452. }
    453. void game()
    454. {
    455. system("cls");
    456. //设置一个存放信息的数组
    457. int arr[Col][Row] = { 0 };
    458. apple L;
    459. //隐藏光标
    460. //HideCursor();
    461. //放置信息
    462. InSet(arr);
    463. //打印游戏界面
    464. DisPlay(arr);
    465. //玩家移动
    466. while (1)
    467. {
    468. if (_kbhit()) {
    469. ch = getch();
    470. if (ch == 27) {
    471. cout << "是否退出并保存存档" << endl;
    472. system("pause");
    473. if (getch() == 27)
    474. {
    475. creathistory(L);
    476. cout << "存档成功,继续按键将退出" << endl;
    477. system("pause");
    478. exit(0);
    479. }
    480. }
    481. else
    482. {
    483. time();
    484. //玩家操作
    485. PlayerPlay(arr);
    486. }
    487. }
    488. //打印棋盘
    489. DisPlay(arr);
    490. //子弹与敌机的操作
    491. BulletEnemy(arr);
    492. }
    493. }

  • 相关阅读:
    R语言拟合ARIMA模型并使用拟合模型进行预测推理、使用autoplot函数可视化ARIMA模型预测结果(返回的对象为ggplot2对象)
    广电运通面试
    案例分享 | 企业数字化转型中如何进行有效数据治理
    ubuntu 20.4安装k8s 1.24.0(使用containerd)
    Qt——多线程
    linux C 简单线程池实现
    Nodejs 应用编译构建提速建议
    基于python的毕业设计电脑硬件配置推荐系统
    Vita-CLIP: Video and text adaptive CLIP via Multimodal Prompting
    程序分区:全局区、常量区、栈区、堆区、代码区
  • 原文地址:https://blog.csdn.net/laocooon/article/details/136286552