当前位置:   article > 正文

C语言经典游戏代码大全(珍藏版)_c语言小游戏代码

c语言小游戏代码

前言

发现很多朋友都想要一些小项目来练手,却找不到从哪里寻找,给大家整理了游戏项目开发源代码汇总。

一、最经典游戏之俄罗斯方块

  1. #include<iostream>
  2. #include<math.h>
  3. #include<Windows.h>
  4. #include<conio.h>
  5. #include<ctime>
  6. using namespace std;
  7. enum DIR
  8. {
  9. UP,
  10. RIGHT,
  11. DOWN,
  12. LEFT
  13. };
  14. time_t start = 0, finish = 0;
  15. int _x = 6, _y = 1;//图形生成位置
  16. int map[30][16] = { 0 };
  17. int sharp[20][8] = {
  18. {0,0,0,0,0,0,0,0},
  19. //I形
  20. {0,0,0,1,0,2,0,3},
  21. {0,0,1,0,2,0,3,0},
  22. //■形
  23. {0,0,1,0,0,1,1,1},
  24. //L形
  25. {0,0,0,1,0,2,1,2},
  26. {0,0,0,1,1,0,2,0},
  27. {0,0,1,0,1,1,1,2},
  28. {0,1,1,1,2,0,2,1},
  29. //J形
  30. {0,2,1,0,1,1,1,2},
  31. {0,0,0,1,1,1,2,1},
  32. {0,0,0,1,0,2,1,0},
  33. {0,0,1,0,2,0,2,1},
  34. //Z形
  35. {0,0,1,0,1,1,2,1},
  36. {0,1,0,2,1,0,1,1},
  37. //S形
  38. {0,1,1,0,1,1,2,0},
  39. {0,0,0,1,1,1,1,2},
  40. //T形
  41. {0,1,1,0,1,1,2,1},
  42. {0,0,0,1,0,2,1,1},
  43. {0,0,1,0,1,1,2,0},
  44. {0,1,1,0,1,1,1,2}
  45. };
  46. class Game
  47. {
  48. public:
  49. int score;//游戏分数
  50. int _id;//图形编号
  51. int top;//最高点高度
  52. int speed;//下落速度
  53. Game();
  54. void showMenu();//显示菜单
  55. void showGround();//显示游戏界面
  56. void gameOver();//游戏结束界面
  57. void Run();//运行游戏
  58. void sharpDraw(int id, bool show = false);//绘制图形
  59. void keyControl();//键盘控制
  60. bool move(int dir, int id);//移动判断
  61. bool downSet(int id);//下落
  62. void Turn(int id);//旋转
  63. void clean();//消行
  64. };
  65. void SetPos(int i, int j)//控制光标位置, 列, 行
  66. {
  67. COORD pos = { i,j };
  68. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  69. }
  70. int main()
  71. {
  72. CONSOLE_CURSOR_INFO cursor;
  73. GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
  74. cursor.bVisible = 0; //这四行用来设置光标不显示
  75. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
  76. srand((unsigned)time(NULL));
  77. Game game;
  78. game.showMenu();
  79. return 0;
  80. }
  81. Game::Game()
  82. {
  83. score = 0;
  84. _id = 0;
  85. top = 58;
  86. speed = 1000;
  87. }
  88. void Game::showMenu()
  89. {
  90. for (int i = 0; i < 30; i++)
  91. {
  92. for (int j = 0; j < 26; j++)
  93. {
  94. if ((i == 0 || i == 29) || (j == 0 || j == 25))
  95. {
  96. cout << "■";
  97. }
  98. else
  99. {
  100. cout << " ";
  101. }
  102. }
  103. cout << endl;
  104. }
  105. SetPos(17, 8);
  106. cout << "俄 罗 斯 方 块" << endl;
  107. SetPos(13, 12);
  108. cout << "↑旋转方块 ↓加速下滑" << endl;
  109. SetPos(12, 14);
  110. cout << "← →左右移动 空格 暂停" << endl;
  111. SetPos(15, 20);
  112. cout << "0 退出 Enter 开始" << endl;
  113. while (1)
  114. {
  115. int select = _getch();
  116. if (select == 13)
  117. {
  118. system("cls");
  119. this->Run();
  120. }
  121. else if (select = 48)
  122. {
  123. system("cls");
  124. exit(0);
  125. }
  126. }
  127. }
  128. void Game::showGround()
  129. {
  130. for (int i = 0; i < 30; i++)
  131. {
  132. for (int j = 0; j < 26; j++)
  133. {
  134. if ((i == 0 || i == 29) || (j == 0 || j == 25 || j == 15))
  135. {
  136. cout << "■";
  137. }
  138. else if (i == 15 && j > 15)
  139. {
  140. cout << "■";
  141. }
  142. else
  143. {
  144. cout << " ";
  145. }
  146. }
  147. cout << endl;
  148. }
  149. SetPos(31, 2);
  150. cout << "下 个图形" << endl;
  151. SetPos(31, 17);
  152. cout << "当 前得分" << endl;
  153. for (int i = 0; i < 30; i++)
  154. {
  155. for (int j = 0; j < 16; j++)
  156. {
  157. if ((i == 0 || i == 29) || (j == 0 || j == 15))
  158. {
  159. map[i][j] = 1;
  160. }
  161. else
  162. {
  163. map[i][j] = 0;
  164. }
  165. }
  166. }
  167. }
  168. void Game::gameOver()
  169. {
  170. for (int i = 5; i < 15; i++)
  171. {
  172. SetPos(1, i);
  173. cout << " " << endl;
  174. }
  175. SetPos(8, 7);
  176. cout << "G a m e O v e r" << endl;
  177. SetPos(3, 10);
  178. cout << "0 退出 Enter 重新开始" << endl;
  179. while (1)
  180. {
  181. int select = _getch();
  182. if (select == 13)
  183. {
  184. system("cls");
  185. this->Run();
  186. }
  187. else if (select == 48)
  188. {
  189. system("cls");
  190. exit(0);
  191. }
  192. }
  193. }
  194. void Game::Run()
  195. {
  196. score = 0;
  197. _id = 0;
  198. top = 58;
  199. _x = 6;
  200. _y = 1;
  201. showGround();
  202. start = clock();
  203. int new_id = rand() % 19 + 1;
  204. while (1)
  205. {
  206. sharpDraw(_id);
  207. keyControl();
  208. if (downSet(_id))
  209. {
  210. sharpDraw(-new_id, 1);
  211. _id = new_id;
  212. new_id = rand() % 19 + 1;
  213. sharpDraw(new_id, 1);
  214. clean();
  215. }
  216. SetPos(34, 20);
  217. cout << score << endl;
  218. }
  219. }
  220. void Game::sharpDraw(int id, bool show)
  221. {
  222. int x, y;
  223. if (show == true)
  224. {
  225. if (id > 0)
  226. {
  227. for (int i = 0; i < 4; i++)
  228. {
  229. x = 19 + sharp[id][2 * i];
  230. y = 6 + sharp[id][2 * i + 1];
  231. SetPos(2 * x, y);
  232. cout << "■";
  233. }
  234. }
  235. else
  236. {
  237. for (int i = 0; i < 4; i++)
  238. {
  239. x = 19 + sharp[-id][2 * i];
  240. y = 6 + sharp[-id][2 * i + 1];
  241. SetPos(2 * x, y);
  242. cout << " ";
  243. }
  244. }
  245. return;
  246. }
  247. if (id > 0)
  248. {
  249. for (int i = 0; i < 4; i++)
  250. {
  251. x = _x + sharp[id][2 * i];
  252. y = _y + sharp[id][2 * i + 1];
  253. SetPos(2 * x, y);
  254. cout << "■";
  255. }
  256. }
  257. else
  258. {
  259. for (int i = 0; i < 4; i++)
  260. {
  261. x = _x + sharp[-id][2 * i];
  262. y = _y + sharp[-id][2 * i + 1];
  263. SetPos(2 * x, y);
  264. cout << " ";
  265. }
  266. }
  267. return;
  268. }
  269. bool Game::downSet(int id)
  270. {
  271. if (id == 0)
  272. return true;
  273. finish = clock();
  274. if (finish - start < speed)
  275. {
  276. return false;
  277. }
  278. start = clock();
  279. if (!move(DOWN, _id))
  280. {
  281. int x, y;
  282. for (int i = 0; i < 4; i++)
  283. {
  284. x = _x + sharp[id][2 * i];
  285. y = _y + sharp[id][2 * i + 1];
  286. map[y][x] = 1;
  287. if (y < top)
  288. {
  289. top = y;
  290. }
  291. if (top <= 1)
  292. {
  293. gameOver();
  294. }
  295. }
  296. _x = 6;
  297. _y = 1;
  298. return true;
  299. }
  300. sharpDraw(-id);
  301. _y++;
  302. sharpDraw(id);
  303. return false;
  304. }
  305. bool Game::move(int dir, int id)
  306. {
  307. int x, y;
  308. switch (dir)
  309. {
  310. case UP:
  311. for (int i = 0; i < 4; i++)
  312. {
  313. x = _x + sharp[id][2 * i];
  314. y = _y + sharp[id][2 * i + 1];
  315. if (map[y][x] == 1)
  316. {
  317. return false;
  318. }
  319. }
  320. break;
  321. case DOWN:
  322. {
  323. for (int i = 0; i < 4; i++)
  324. {
  325. x = _x + sharp[id][2 * i];
  326. y = _y + sharp[id][2 * i + 1];
  327. if (map[y + 1][x] == 1)
  328. {
  329. return false;
  330. }
  331. }
  332. }
  333. break;
  334. case RIGHT:
  335. {
  336. for (int i = 0; i < 4; i++)
  337. {
  338. x = _x + sharp[id][2 * i];
  339. y = _y + sharp[id][2 * i + 1];
  340. if (map[y][x + 1] == 1)
  341. {
  342. return false;
  343. }
  344. }
  345. }
  346. break;
  347. case LEFT:
  348. {
  349. for (int i = 0; i < 4; i++)
  350. {
  351. x = _x + sharp[id][2 * i];
  352. y = _y + sharp[id][2 * i + 1];
  353. if (map[y][x - 1] == 1)
  354. {
  355. return false;
  356. }
  357. }
  358. }
  359. break;
  360. default:
  361. break;
  362. }
  363. return true;
  364. }
  365. void Game::Turn(int id)
  366. {
  367. switch (id)
  368. {
  369. case 1:id++; break;
  370. case 2:id--; break;
  371. case 3: break;
  372. case 4:id++; break;
  373. case 5:id++; break;
  374. case 6:id++; break;
  375. case 7:id -= 3; break;
  376. case 8:id++; break;
  377. case 9:id++; break;
  378. case 10:id++; break;
  379. case 11:id -= 3; break;
  380. case 12:id++; break;
  381. case 13:id--; break;
  382. case 14:id++; break;
  383. case 15:id--; break;
  384. case 16:id++; break;
  385. case 17:id++; break;
  386. case 18:id++; break;
  387. case 19:id -= 3; break;
  388. default:
  389. break;
  390. }
  391. if (!move(UP, id))
  392. {
  393. return;
  394. }
  395. sharpDraw(-_id);
  396. _id = id;
  397. }
  398. void Game::keyControl()
  399. {
  400. if (!_kbhit())
  401. return;
  402. int key = _getch();
  403. switch (key)
  404. {
  405. case 72:
  406. Turn(_id);
  407. break;
  408. case 80:
  409. if (move(DOWN, _id))
  410. {
  411. sharpDraw(-_id);
  412. _y++;
  413. }
  414. break;
  415. case 75:
  416. if (move(LEFT, _id))
  417. {
  418. sharpDraw(-_id);
  419. _x--;
  420. }
  421. break;
  422. case 77:
  423. if (move(RIGHT, _id))
  424. {
  425. sharpDraw(-_id);
  426. _x++;
  427. }
  428. break;
  429. case 32:
  430. {
  431. for (int i = 5; i < 15; i++)
  432. {
  433. SetPos(1, i);
  434. cout << " " << endl;
  435. }
  436. SetPos(10, 7);
  437. cout << "游 戏 暂 停" << endl;
  438. SetPos(3, 10);
  439. cout << "0 返回菜单 回车 继续游戏" << endl;
  440. while (1)
  441. {
  442. int select = _getch();
  443. if (select == 13)
  444. {
  445. for (int i = 5; i < 15; i++)
  446. {
  447. SetPos(1, i);
  448. cout << " " << endl;
  449. }
  450. break;
  451. }
  452. else if (select == 48)
  453. {
  454. system("cls");
  455. showMenu();
  456. }
  457. }
  458. }
  459. default:
  460. break;
  461. }
  462. }
  463. void Game::clean()
  464. {
  465. int n = -1;
  466. int line = -1;
  467. while (1)
  468. {
  469. for (int i = 28; i > 0; i--)
  470. {
  471. for (int j = 1; j < 15; j++)
  472. {
  473. line = i;
  474. if (map[i][j] == 0)
  475. {
  476. line = -1;
  477. break;
  478. }
  479. }
  480. if (line != -1)
  481. break;
  482. }
  483. if (line == -1)
  484. break;
  485. for (int i = line; i > 0; i--)
  486. {
  487. for (int j = 1; j < 15; j++)
  488. {
  489. if (i == 1)
  490. map[i][j] = 0;
  491. else
  492. {
  493. map[i][j] = map[i - 1][j];
  494. SetPos(2 * j, i);
  495. if (map[i][j] == 1)
  496. cout << "■";
  497. else
  498. cout << " ";
  499. }
  500. }
  501. }
  502. top++;
  503. n++;
  504. }
  505. if (n >= 0)
  506. {
  507. score += n * n * 100 + 100;
  508. if (speed > 100)
  509. speed = 1000 - score / 10;
  510. }
  511. }

二、雷霆战机游戏源代码

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<graphics.h>
  5. #include<conio.h>
  6. #include<time.h>
  7. #include<mmsystem.h>
  8. #pragma comment(lib,"winmm.lib")
  9. typedef struct Node
  10. {
  11. int x;
  12. int y;
  13. struct Node *pnext;
  14. }NODE;
  15. #define WINDOW_WIDTH 1024
  16. #define WINDOW_HEIGHT 680
  17. #define WIDTH 480
  18. //我机图片尺寸
  19. #define pw 86
  20. #define ph 82
  21. //敌机图片尺寸
  22. #define aw 70
  23. #define ah 70
  24. #define boss1w 192
  25. #define boss1h 290
  26. //敌机重出现的y坐标
  27. #define APStart -ah-20
  28. NODE *p_bullet = NULL;
  29. //MyPlane
  30. NODE *p_MP = NULL;
  31. //AttackPlane
  32. NODE* p_AP = NULL;
  33. //子弹时间差
  34. NODE* p_AP2 = NULL;
  35. DWORD b1, b2, b3, b4, b5, b6;
  36. IMAGE i_MP,i_MPS,i_AP,i_APS;
  37. IMAGE i_backeve, i_backxing, i_backduicheng, i_backguan,i_backcontrol,i_backgo;
  38. IMAGE i_boss1_1, i_boss1_1S, i_boss1_2, i_boss1_2S;
  39. //backxing的左右移动
  40. int left = (WINDOW_WIDTH / 2 - WIDTH / 2);
  41. //分数
  42. int score = 0;
  43. //击毁敌机的数量
  44. int kill = 0;
  45. //boss是否出现
  46. int boss1show = 0;
  47. //boss1贴图开关
  48. int boss1image = 0;
  49. int boss1hp = 20;
  50. int line1_x = WINDOW_WIDTH / 2 - 20;
  51. int line1_y = boss1h;
  52. int line2_x = WINDOW_WIDTH / 2 + 20;
  53. int line2_y = boss1h;
  54. //Beam只播放一次
  55. int test = 1;
  56. int MP_HP = 1;
  57. void CreateList()
  58. {
  59. p_MP = (NODE*)malloc(sizeof(NODE));
  60. p_MP->x = WINDOW_WIDTH / 2 - pw / 2;
  61. p_MP->y = WINDOW_HEIGHT-100;
  62. p_MP->pnext = NULL;
  63. p_bullet = (NODE*)malloc(sizeof(NODE));
  64. p_bullet->pnext = NULL;
  65. b1 = GetTickCount();
  66. p_AP = (NODE*)malloc(sizeof(NODE));
  67. srand((unsigned)time(NULL));
  68. p_AP->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  69. p_AP->y = APStart;
  70. p_AP->pnext = NULL;
  71. b3 = GetTickCount();
  72. p_AP2 = (NODE*)malloc(sizeof(NODE));
  73. p_AP2->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  74. p_AP2->y = -350;
  75. p_AP2->pnext = NULL;
  76. b5 = GetTickCount();
  77. }
  78. void GameBackInit()
  79. {
  80. loadimage(&i_MP, L"mp.jpg");
  81. loadimage(&i_MPS, L"mpbit.jpg");
  82. loadimage(&i_backeve, L"backeve.jpg");
  83. loadimage(&i_backxing, L"backtaikong.jpg");
  84. loadimage(&i_AP, L"AP.jpg", aw, ah);
  85. loadimage(&i_APS, L"APS.jpg", aw, ah);
  86. loadimage(&i_backduicheng, L"backduicheng.jpg");
  87. loadimage(&i_backguan, L"backguan.jpg", WIDTH, WINDOW_HEIGHT);
  88. loadimage(&i_backcontrol, L"backcontrol.jpg",WINDOW_WIDTH,WINDOW_HEIGHT);
  89. loadimage(&i_boss1_1, L"boss1_1.jpg");
  90. loadimage(&i_boss1_1S, L"boss1_1S.jpg");
  91. loadimage(&i_boss1_2, L"boss1_2.jpg");
  92. loadimage(&i_boss1_2S, L"boss1_1S.jpg");
  93. loadimage(&i_backgo, L"Gameover.jpg", WINDOW_WIDTH, WINDOW_HEIGHT);
  94. putimage(0, 30, &i_backeve);
  95. Sleep(1000);
  96. PlaySound(L"baozou.wav", NULL, SND_FILENAME | SND_ASYNC);
  97. putimage(0, 0, &i_backcontrol);
  98. outtextxy(600, 540, L"【PRESS】 按任意键进入游戏");
  99. system("pause");
  100. mciSendString(L"open bgmusic.mp3 alias bg", NULL, 0, NULL);
  101. mciSendString(L"play bg repeat", NULL, 0, NULL);
  102. putimage((WINDOW_WIDTH / 2 - WIDTH / 2), 0, WIDTH, WINDOW_HEIGHT, &i_backguan, 0, 0, SRCCOPY);
  103. putimage(0, 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 0, 100, SRCCOPY);
  104. putimage((WINDOW_WIDTH / 2 + WIDTH / 2), 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 1200 - (WINDOW_WIDTH / 2 - WIDTH / 2), 100, SRCCOPY);
  105. //字体出现的高度
  106. int text_h = WINDOW_HEIGHT/2;
  107. Sleep(300);
  108. BeginBatchDraw();
  109. for (int i = 0; i < text_h; i++)
  110. {
  111. clearrectangle((WINDOW_WIDTH / 2 - WIDTH / 2), 0, (WINDOW_WIDTH / 2 + WIDTH / 2), WINDOW_HEIGHT);
  112. putimage((WINDOW_WIDTH / 2 - WIDTH / 2), 0-i, WIDTH, text_h , &i_backguan, 0, 0, SRCCOPY);
  113. putimage((WINDOW_WIDTH / 2 - WIDTH / 2), text_h + i, WIDTH, WINDOW_HEIGHT - (text_h + i), &i_backguan, 0, text_h, SRCCOPY);
  114. putimage((WINDOW_WIDTH / 2 - WIDTH / 2), text_h - i, WIDTH, 2*i, &i_backxing, left, text_h-i, SRCCOPY);
  115. FlushBatchDraw();
  116. Sleep(5);
  117. }
  118. EndBatchDraw();
  119. Sleep(100);
  120. }
  121. void Boss1show()
  122. {
  123. p_AP->y = WINDOW_HEIGHT + 100;
  124. p_AP2->y = WINDOW_HEIGHT + 100;
  125. if (boss1hp >14)
  126. {
  127. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1S, NOTSRCERASE);
  128. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1, SRCINVERT);
  129. }
  130. else if(boss1hp >= 9 && boss1hp <=14)
  131. {
  132. if (boss1hp % 2 == 0)
  133. {
  134. setlinecolor(0x996666);
  135. setlinestyle(PS_DOT, 3);
  136. line(line1_x, line1_y, line1_x, WINDOW_HEIGHT);
  137. line(line2_x, line2_y, line2_x, WINDOW_HEIGHT);
  138. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2S, NOTSRCERASE);
  139. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2, SRCINVERT);
  140. }
  141. else
  142. {
  143. setlinecolor(0xCC6666);
  144. setlinestyle(PS_DOT, 3);
  145. line(line1_x, line1_y, line1_x, WINDOW_HEIGHT);
  146. line(line2_x, line2_y, line2_x, WINDOW_HEIGHT);
  147. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1S, NOTSRCERASE);
  148. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1, SRCINVERT);
  149. }
  150. }
  151. else
  152. {
  153. if (test == 1)
  154. {
  155. PlaySound(L"Beam.wav", NULL, SND_FILENAME | SND_ASYNC);
  156. test++;
  157. }
  158. setlinecolor(0xFF6666);
  159. setlinestyle(PS_DASH, 5);
  160. line(line1_x, line1_y, line1_x, WINDOW_HEIGHT);
  161. line(line2_x, line2_y, line2_x, WINDOW_HEIGHT);
  162. line(WINDOW_WIDTH / 2 - boss1w / 2, boss1h -90, 482, boss1h + 50);
  163. line(WINDOW_WIDTH / 2 + boss1w / 2, boss1h - 90, 542, boss1h + 50);
  164. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2S, NOTSRCERASE);
  165. putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2, SRCINVERT);
  166. if ((boss1hp!=8)&&(p_MP->x - line1_x) > -pw && (p_MP->x - line2_x)<0&& (p_MP->y - line1_y)>-ph) MP_HP = 0;
  167. }
  168. if(boss1image<=boss1h ) boss1image+=2;
  169. }
  170. void AddNode(int flag)
  171. {
  172. //后插法,更新第二个位置
  173. if (flag == 0)
  174. {
  175. NODE* p_new = (NODE*)malloc(sizeof(NODE));
  176. p_new->x = p_MP->x + 35;
  177. p_new->y = p_MP->y - 45;
  178. p_new->pnext = p_bullet->pnext;
  179. p_bullet->pnext = p_new;
  180. }
  181. }
  182. int main()
  183. {
  184. //create a window
  185. initgraph(WINDOW_WIDTH, WINDOW_HEIGHT);
  186. setfillcolor(0xFF9999);
  187. GameBackInit();
  188. char key;
  189. CreateList();
  190. //批量绘图
  191. BeginBatchDraw();
  192. while (1)
  193. {
  194. //清画板,要不然就成重叠的残影了
  195. cleardevice();
  196. putimage((WINDOW_WIDTH / 2 - WIDTH / 2), 0, WIDTH, WINDOW_HEIGHT, &i_backxing, left, 0, SRCCOPY);
  197. putimage(0, 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 0, 100, SRCCOPY);
  198. putimage((WINDOW_WIDTH / 2 + WIDTH / 2), 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 1200 - (WINDOW_WIDTH / 2 - WIDTH / 2), 100, SRCCOPY);
  199. putimage(p_MP->x, p_MP->y, &i_MPS, NOTSRCERASE);
  200. putimage(p_MP->x, p_MP->y, &i_MP, SRCINVERT);
  201. putimage(p_AP->x, p_AP->y, &i_APS, NOTSRCERASE);
  202. putimage(p_AP->x, p_AP->y, &i_AP, SRCINVERT);
  203. putimage(p_AP2->x, p_AP2->y, &i_APS, NOTSRCERASE);
  204. putimage(p_AP2->x, p_AP2->y, &i_AP, SRCINVERT);
  205. //MP单位时间发射子弹的数量
  206. b2 = GetTickCount();
  207. //不能等于,有偏差
  208. if (b2 - b1 >= 600)
  209. {
  210. AddNode(0);
  211. b1 = b2;
  212. }
  213. //我方战机子弹递增
  214. NODE* P = p_bullet->pnext;
  215. while (P != NULL)
  216. {
  217. if (boss1show == 0)
  218. {
  219. //确定敌机重生位置不是在原位置
  220. int mid;
  221. //10是子弹宽度,但半个子弹打中也算,要不然太难了,就右边是-3,左边是-7
  222. if ((P->y - p_AP->y) < ah && (P->y - p_AP->y) >= 0 && (P->x - p_AP->x) < aw -3 && (P->x - p_AP->x) >= -7)
  223. {
  224. P->y = APStart -100;
  225. P = P->pnext;
  226. p_AP->y = APStart;
  227. kill++;
  228. PlaySound(L"Bomb.wav", NULL, SND_FILENAME | SND_ASYNC);
  229. while (1)
  230. {
  231. mid = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  232. if (abs(mid - p_AP->x) > aw)
  233. {
  234. p_AP->x = mid;
  235. break;
  236. }
  237. }
  238. }
  239. else if((P->y - p_AP2->y) < ah && (P->y - p_AP2->y) >= 0 && (P->x - p_AP2->x) < aw - 3 && (P->x - p_AP2->x) >= -7)
  240. {
  241. P->y = APStart -100;
  242. P = P->pnext;
  243. p_AP2->y = APStart;
  244. kill++;
  245. while (1)
  246. {
  247. mid = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  248. if (abs(mid - p_AP2->x) > aw)
  249. {
  250. p_AP2->x = mid;
  251. break;
  252. }
  253. }
  254. PlaySound(L"Bomb.wav", NULL, SND_FILENAME | SND_ASYNC);
  255. }
  256. else
  257. {
  258. fillroundrect(P->x, P->y, P->x + 10, P->y + 35, 10, 30);
  259. P->y -= 5;
  260. P = P->pnext;
  261. }
  262. }
  263. else if (boss1show == 1)
  264. {
  265. if (boss1image > boss1h)
  266. {
  267. if ((P->y) < boss1h && (P->y) >= 0 && (P->x - (WINDOW_WIDTH / 2 - boss1w / 2)) < boss1w - 3 && (P->x - (WINDOW_WIDTH / 2 - boss1w / 2)) >= -7)
  268. {
  269. P->y = APStart -100;
  270. P = P->pnext;
  271. boss1hp--;
  272. if (boss1hp>9||boss1hp<7) PlaySound(L"Bomb.wav", NULL, SND_FILENAME | SND_ASYNC);
  273. }
  274. else
  275. {
  276. fillroundrect(P->x, P->y, P->x + 10, P->y - 35, 10, 30);
  277. P->y -= 10;
  278. P = P->pnext;
  279. }
  280. TCHAR s_boss1hp[100];
  281. _stprintf(s_boss1hp, _T("【Boss】HP:%d"), boss1hp);
  282. outtextxy((WINDOW_WIDTH / 2 + WIDTH / 2) + 45, 200, s_boss1hp);
  283. if (boss1hp <= 0)
  284. {
  285. boss1show = 0;
  286. kill += 50;
  287. }
  288. }
  289. else
  290. {
  291. fillroundrect(P->x, P->y, P->x + 10, P->y + 35, 10, 30);
  292. P->y -= 5;
  293. P = P->pnext;
  294. }
  295. }
  296. }
  297. //AP飞行的速度
  298. b4 = GetTickCount();
  299. //不能等于,有偏差
  300. if (b4- b3 >= 50)
  301. {
  302. if (p_AP->y < WINDOW_HEIGHT)
  303. {
  304. p_AP->y += 3;
  305. }
  306. else
  307. {
  308. p_AP->y = 0;
  309. p_AP->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  310. }
  311. b3 = b4;
  312. }
  313. //AP2飞行的速度
  314. b6 = GetTickCount();
  315. //不能等于,有偏差
  316. if (b6 - b5 >= 50)
  317. {
  318. if (p_AP2->y < WINDOW_HEIGHT)
  319. {
  320. p_AP2->y += 3;
  321. }
  322. else
  323. {
  324. p_AP2->y = 0;
  325. p_AP2->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  326. }
  327. b5 = b6;
  328. }
  329. if (kill==10&& boss1hp > 0) boss1show = 1;
  330. if (boss1show==1)
  331. {
  332. Boss1show();
  333. }
  334. if ((p_MP->x - p_AP->x) > -pw && (p_MP->x - p_AP->x)<pw && (p_MP->y - p_AP->y)>-ph && (p_MP->y - p_AP->y)<ph ) MP_HP = 0;
  335. else if ((p_MP->x - p_AP2->x) > -pw && (p_MP->x - p_AP2->x)<pw && (p_MP->y - p_AP2->y)>-ph && (p_MP->y - p_AP2->y)<ph) MP_HP = 0;
  336. else if (boss1show==1&&boss1image>boss1h&&(p_MP->x-(WINDOW_WIDTH / 2 - boss1w / 2)) >-pw && (p_MP->x-(WINDOW_WIDTH / 2 + boss1w / 2))<pw && p_MP->y<boss1h) MP_HP = 0;
  337. if (MP_HP == 0)
  338. {
  339. mciSendString(L"close bg", NULL, 0, NULL);
  340. mciSendString(L"open bggo.mp3 alias bg", NULL, 0, NULL);
  341. mciSendString(L"play bg repeat", NULL, 0, NULL);
  342. putimage(0, 0, &i_backgo, SRCCOPY);
  343. outtextxy(430, 540, L"3秒后自动退出");
  344. EndBatchDraw();
  345. Sleep(3000);
  346. closegraph();
  347. return 0;
  348. }
  349. TCHAR s_score[100];
  350. _stprintf(s_score, _T("你的分数:%d"), kill);
  351. outtextxy((WINDOW_WIDTH / 2 + WIDTH / 2) + 50, WINDOW_HEIGHT/2, s_score);
  352. FlushBatchDraw();
  353. //子弹飞行速度以及按键延迟等
  354. Sleep(15);
  355. if (kbhit())
  356. {
  357. key = getch();
  358. switch (key)
  359. {
  360. case 72://
  361. p_MP->y -= 5;
  362. break;
  363. case 80://
  364. p_MP->y += 5;
  365. break;
  366. case 75://
  367. p_MP->x -= 5;
  368. left -= 5;
  369. break;
  370. case 77://
  371. p_MP->x += 5;
  372. left += 5;
  373. break;
  374. }
  375. }
  376. if (p_MP->x<(WINDOW_WIDTH / 2 - WIDTH / 2))
  377. p_MP->x = (WINDOW_WIDTH / 2 - WIDTH / 2);
  378. if (p_MP->x>(WINDOW_WIDTH / 2 + WIDTH / 2 - pw))
  379. p_MP->x = (WINDOW_WIDTH / 2 + WIDTH / 2 - pw);
  380. if (p_MP->y<0 )
  381. p_MP->y = 0;
  382. if (p_MP->y>WINDOW_HEIGHT - ph)
  383. p_MP->y = WINDOW_HEIGHT - ph;
  384. if (left < 0)
  385. left = 0;
  386. if (left>1280 - WIDTH)
  387. left = 1280 - WIDTH;
  388. }
  389. EndBatchDraw();
  390. closegraph();
  391. return 0;
  392. }

三、五子棋经典游戏源代码

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define MAX_ROW 3
  3. #define MAX_COL 3
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. void init(char chessBoard[MAX_ROW][MAX_COL]) {
  8. for (int row = 0; row < MAX_ROW; row++) {
  9. for (int col = 0; col < MAX_COL; col++) {
  10. chessBoard[row][col] = ' ';
  11. }
  12. }
  13. }
  14. void print_chessBoard(char chessBoard[MAX_ROW][MAX_COL]) {
  15. printf("+---+---+---+\n");
  16. for (int row = 0; row < MAX_ROW; row++) {
  17. printf("| %c | %c | %c |\n", chessBoard[row][0],
  18. chessBoard[row][1], chessBoard[row][2]);
  19. printf("+---+---+---+\n");
  20. }
  21. }
  22. void playerMove(char chessBoard[MAX_ROW][MAX_COL]) {
  23. while (1) {
  24. int row = 0;
  25. int col = 0;
  26. printf("请输入坐标(row col):");
  27. scanf("%d %d", &row, &col);
  28. if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) {
  29. printf("您的坐标不在合法范围内 [0, 2],请重新输入:\n");
  30. continue;
  31. }
  32. if (chessBoard[row][col] != ' ') {
  33. printf("您的坐标位置已经有子了!\n");
  34. continue;
  35. }
  36. chessBoard[row][col] = 'x';
  37. break;
  38. }
  39. }
  40. void computerMove(char chessBoard[MAX_ROW][MAX_COL]) {
  41. while (1) {
  42. int row = rand() % MAX_ROW;
  43. int col = rand() % MAX_COL;
  44. if (chessBoard[row][col] != ' ') {
  45. continue;
  46. }
  47. chessBoard[row][col] = 'o';
  48. break;
  49. }
  50. }
  51. int isFull(char chessBoard[MAX_ROW][MAX_COL]) {
  52. for (int row = 0; row < MAX_ROW; row++) {
  53. for (int col = 0; col < MAX_COL; col++) {
  54. if (chessBoard[row][col] == ' ') {
  55. return 0;
  56. }
  57. }
  58. }
  59. return 1;
  60. }
  61. char isWin(char chessBoard[MAX_ROW][MAX_COL]) {
  62. for (int row = 0; row < MAX_ROW; row++) {
  63. if (chessBoard[row][0] != ' '
  64. && chessBoard[row][0] == chessBoard[row][1]
  65. && chessBoard[row][0] == chessBoard[row][2]) {
  66. return chessBoard[row][0];
  67. }
  68. }
  69. for (int col = 0; col < MAX_COL; col++) {
  70. if (chessBoard[0][col] != ' '
  71. && chessBoard[0][col] == chessBoard[1][col]
  72. && chessBoard[0][col] == chessBoard[2][col]) {
  73. return chessBoard[0][col];
  74. }
  75. }
  76. if (chessBoard[0][0] != ' '
  77. && chessBoard[0][0] == chessBoard[1][1]
  78. && chessBoard[0][0] == chessBoard[2][2]) {
  79. return chessBoard[0][0];
  80. }
  81. if (chessBoard[2][0] != ' '
  82. && chessBoard[2][0] == chessBoard[1][1]
  83. && chessBoard[2][0] == chessBoard[0][2]) {
  84. return chessBoard[2][0];
  85. }
  86. if (isFull(chessBoard)) {
  87. return 'q';
  88. }
  89. return ' ';
  90. }
  91. void game() {
  92. char chessBoard[MAX_ROW][MAX_COL] = { 0 };
  93. init(chessBoard);
  94. char winner = ' ';
  95. while (1) {
  96. system("cls");
  97. print_chessBoard(chessBoard);
  98. playerMove(chessBoard);
  99. winner = isWin(chessBoard);
  100. if (winner != ' ') {
  101. break;
  102. }
  103. computerMove(chessBoard);
  104. winner = isWin(chessBoard);
  105. if (winner != ' ') {
  106. break;
  107. }
  108. }
  109. print_chessBoard(chessBoard);
  110. if (winner == 'x') {
  111. printf("恭喜您, 您赢了!\n");
  112. }
  113. else if (winner == 'o') {
  114. printf("哈哈,您连人工智障都下不过!\n");
  115. }
  116. else {
  117. printf("您只能和人工智障打平手!!\n");
  118. }
  119. }
  120. int menu() {
  121. printf("--------------------------\n");
  122. printf("--------1.开始游戏--------\n");
  123. printf("--------0.退出游戏--------\n");
  124. printf("--------------------------\n");
  125. int choice = 0;
  126. printf("请输入你的选择:");
  127. scanf("%d", &choice);
  128. return choice;
  129. }
  130. int main()
  131. {
  132. srand((unsigned int)time(0));
  133. while (1) {
  134. int choice = menu();
  135. if (choice == 1) {
  136. game();
  137. }
  138. else if (choice == 0) {
  139. printf("退出游戏,GOODBYE!!!!!\n");
  140. break;
  141. }
  142. else {
  143. printf("输入错误!请重新输入!\n");
  144. continue;
  145. }
  146. }
  147. system("pause");
  148. return 0;
  149. }

四、贪吃蛇完整版EN

  1. // 必要的头文件
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <time.h>
  6. #include <windows.h>
  7. #include <string.h>
  8. // 定义标记上下左右的明示常量
  9. #define UP 1
  10. #define DOWN 2
  11. #define LEFT 3
  12. #define RIGHT 4
  13. #define ESC 5
  14. #define FOOD 10
  15. // 定义表示位置的结构体类型
  16. typedef struct snake{
  17. int x;
  18. int y;
  19. struct snake *next;
  20. }snake;
  21. // 定义全局变量
  22. int score = 0; // 当前得分
  23. int speed = 200; // 存储当前速度
  24. int status;
  25. snake *tail, *head; // 存储蛇头蛇尾
  26. snake *food, *q;// q用于遍历链表
  27. HANDLE hOUT;
  28. void gotoxy(int x, int y); // 设置光标位置
  29. int choice(void); // 载入游戏界面
  30. int color(int c); // 设置字体颜色
  31. void printGame(void); // 打印游戏界面
  32. void printSnake(void); // 打印蛇身
  33. void printFood(void); // 打印食物
  34. void printTips(void); // 打印提示
  35. void snakeMove(void); // 主操作函数
  36. int biteSelf(void); // 判断是否咬到了自己
  37. int encounterWall(void); // 判断是否撞墙
  38. void keyboardControl(void); // 获取击键
  39. void speedUp(void); // 加速
  40. void speedDown(void); // 减速
  41. int endGame(void); // 结束函数;
  42. char *s_gets(char *st, int n); // 读取字符
  43. void frees(snake *); // 释放内存
  44. int main(int argc, char *argv[]){
  45. while (1)
  46. {
  47. if (choice() == 1)
  48. keyboardControl();
  49. else
  50. {
  51. gotoxy(5, 15);
  52. printf("按任意键返回");
  53. getchar(); // 去除前一个前导换行
  54. while (1)
  55. {
  56. if (getchar())
  57. {
  58. system("cls");
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. frees(head);
  65. return 0;
  66. }
  67. void gotoxy(int x, int y)
  68. {
  69. COORD c;
  70. c.X = x;
  71. c.Y = y;
  72. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
  73. }
  74. int choice(void)
  75. {
  76. int yourchoice;
  77. // 画出界面
  78. gotoxy(35, 5);
  79. color(11);
  80. printf("\t贪吃蛇大作战\n");
  81. printf("\n\n");
  82. color(13);
  83. printf("\t\t★★★★★★★★ Snake!");
  84. printf("\t\t★★★★★★★★ Snake!");
  85. gotoxy(25, 15);
  86. color(12);
  87. printf("1.进入游戏\t2.查看说明\t3.退出游戏\n");
  88. color(11);
  89. printf("请选择:");
  90. scanf("%d", &yourchoice);
  91. switch (yourchoice)
  92. {
  93. case 1:
  94. system("cls");
  95. // 初始化
  96. printGame();
  97. printSnake();
  98. printFood();
  99. break;
  100. case 2:
  101. system("cls");
  102. printTips();
  103. break;
  104. case 3:
  105. system("cls");
  106. gotoxy(30, 10);
  107. color(11);
  108. printf("Bye!");
  109. exit(0);
  110. default:
  111. system("cls");
  112. printf("没有此序号,请输入1,2或3\n");
  113. Sleep(2000);
  114. system("cls");
  115. }
  116. return yourchoice;
  117. }
  118. int color(int c)
  119. {
  120. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字颜色
  121. return 0;
  122. }
  123. void printGame()
  124. {
  125. int i, j;
  126. gotoxy(5, 5);
  127. printf("游戏载入中...请稍后");
  128. Sleep(2000);
  129. system("cls");
  130. // 打印上下界面
  131. for (i = 0; i <= 50; i += 2)
  132. {
  133. gotoxy(i, 0);
  134. printf("□");
  135. gotoxy(i, 25);
  136. printf("□");
  137. }
  138. // 打印左右界面
  139. for (i = 0; i <= 25; i += 1)
  140. {
  141. gotoxy(0, i);
  142. printf("□");
  143. gotoxy(50, i);
  144. printf("□");
  145. }
  146. // 打印中间网格
  147. for (i = 1; i <= 24; i += 1)
  148. {
  149. for (j = 2; j <= 48; j += 2)
  150. {
  151. gotoxy(j, i);
  152. color(11);
  153. printf("■");
  154. }
  155. }
  156. // 打印右侧的规则和计分栏
  157. gotoxy(60, 13);
  158. printf("当前分数:%d分,当前速度%d", score, speed);
  159. gotoxy(60, 15);
  160. printf("用↑ ↓ ← →分别控制蛇的移动\n");
  161. gotoxy(60, 18);
  162. printf("每次获取食物加10分 按下F1加速,F2减速,空格暂停\n");
  163. gotoxy(60, 20);
  164. printf("不能撞墙和咬到自己!");
  165. gotoxy(60, 22);
  166. printf("速度不低于100,不高于300");
  167. }
  168. void printSnake(void)
  169. {
  170. int i;
  171. // 设定蛇尾(16,13),头插入,初始向右
  172. tail = (snake*)malloc(sizeof(snake));
  173. tail->x = 16;
  174. tail->y = 13;
  175. tail->next = NULL;
  176. // 设定初始蛇长是4
  177. for (i = 1; i <= 4; i++)
  178. {
  179. head = (snake*)malloc(sizeof(snake));
  180. head->next = tail;
  181. head->x = 16 + 2 * i;
  182. head->y = 13;
  183. tail = head; // 头成为尾
  184. }
  185. // 输出蛇身
  186. while (tail->next)
  187. {
  188. gotoxy(tail->x, tail->y);
  189. color(14);
  190. printf("★");
  191. tail = tail->next;
  192. }
  193. }
  194. void printFood(void)
  195. {
  196. srand((unsigned)time(NULL)); // 利用时钟修改种子
  197. food = (snake*)malloc(sizeof(snake));
  198. food->x = 1; // 初始化x坐标
  199. while (food->x % 2 && food->x)
  200. {
  201. food->x = rand() % 46 + 2;// 2-48
  202. }
  203. food->y = rand() % 23 + 1; // 1-24
  204. q = head; // 不改变头遍历链表
  205. while (q->next)
  206. {
  207. if (q->x == food->x && q->y == food->y)
  208. {
  209. free(food);
  210. printFood();
  211. }
  212. else
  213. {
  214. gotoxy(food->x, food->y);
  215. color(12);
  216. printf("●");
  217. break;
  218. }
  219. }
  220. }
  221. void printTips(void)
  222. {
  223. color(11);
  224. printf("***********Tips************\n");
  225. printf("1.采用合理的速度可以获得更高的分数哦!\n");
  226. printf("2.一定不要撞到自己或者两边的墙!\n");
  227. printf("3.游戏过程中按ESC退出游戏!\n");
  228. }
  229. void snakeMove(void)
  230. {
  231. snake *snakenext;
  232. snakenext = (snake*)malloc(sizeof(snake));
  233. if (biteSelf())
  234. {
  235. gotoxy(60, 11);
  236. printf("咬到自己啦!");
  237. free(snakenext);
  238. Sleep(1500);
  239. system("cls");
  240. exit(0);
  241. }
  242. else if (encounterWall())
  243. {
  244. gotoxy(60, 11);
  245. printf("撞到墙啦!");
  246. free(snakenext);
  247. Sleep(1500);
  248. system("cls");
  249. exit(0);
  250. }
  251. else
  252. {
  253. // 前两个条件判断完成才开始移动
  254. Sleep(350 - speed);
  255. if (status == UP)
  256. {
  257. snakenext->x = head->x;
  258. snakenext->y = head->y - 1;
  259. snakenext->next = head;
  260. head = snakenext;
  261. q = head;
  262. if (snakenext->x == food->x && snakenext->y == food->y)
  263. {
  264. while (q)
  265. {
  266. gotoxy(q->x, q->y);
  267. color(14);
  268. printf("★");
  269. q = q->next;
  270. }
  271. score += FOOD;
  272. gotoxy(60, 13);
  273. printf("当前分数:%d分,当前速度%d", score, speed);
  274. printFood();
  275. }
  276. else
  277. {
  278. while (q->next->next)
  279. {
  280. gotoxy(q->x, q->y);
  281. color(14);
  282. printf("★");
  283. q = q->next;
  284. }
  285. gotoxy(q->next->x, q->next->y);
  286. color(11);
  287. printf("■");
  288. free(q->next);
  289. q->next = NULL;
  290. }
  291. }
  292. else if (status == DOWN)
  293. {
  294. snakenext->x = head->x;
  295. snakenext->y = head->y + 1;
  296. snakenext->next = head;
  297. head = snakenext;
  298. q = head;
  299. if (snakenext->x == food->x && snakenext->y == food->y)
  300. {
  301. while (q)
  302. {
  303. gotoxy(q->x, q->y);
  304. color(14);
  305. printf("★");
  306. q = q->next;
  307. }
  308. score += FOOD;
  309. gotoxy(60, 13);
  310. printf("当前分数:%d分,当前速度%d", score, speed);
  311. printFood();
  312. }
  313. else
  314. {
  315. while (q->next->next)
  316. {
  317. gotoxy(q->x, q->y);
  318. color(14);
  319. printf("★");
  320. q = q->next;
  321. }
  322. gotoxy(q->next->x, q->next->y);
  323. color(11);
  324. printf("■");
  325. free(q->next);
  326. q->next = NULL;
  327. }
  328. }
  329. else if (status == LEFT)
  330. {
  331. snakenext->x = head->x - 2;
  332. snakenext->y = head->y;
  333. snakenext->next = head;
  334. head = snakenext;
  335. q = head;
  336. if (snakenext->x == food->x && snakenext->y == food->y)
  337. {
  338. while (q)
  339. {
  340. gotoxy(q->x, q->y);
  341. color(14);
  342. printf("★");
  343. q = q->next;
  344. }
  345. score += FOOD;
  346. gotoxy(60, 13);
  347. printf("当前分数:%d分,当前速度%d", score, speed);
  348. printFood();
  349. }
  350. else
  351. {
  352. while (q->next->next)
  353. {
  354. gotoxy(q->x, q->y);
  355. color(14);
  356. printf("★");
  357. q = q->next;
  358. }
  359. gotoxy(q->next->x, q->next->y);
  360. color(11);
  361. printf("■");
  362. free(q->next);
  363. q->next = NULL;
  364. }
  365. }
  366. else if (status == RIGHT)
  367. {
  368. snakenext->x = head->x + 2;
  369. snakenext->y = head->y;
  370. snakenext->next = head;
  371. head = snakenext;
  372. q = head;
  373. if (snakenext->x == food->x && snakenext->y == food->y)
  374. {
  375. while (q)
  376. {
  377. gotoxy(q->x, q->y);
  378. color(14);
  379. printf("★");
  380. q = q->next;
  381. }
  382. score += FOOD;
  383. gotoxy(60, 13);
  384. printf("当前分数:%d分,当前速度%d", score, speed);
  385. printFood();
  386. }
  387. else
  388. {
  389. while (q->next->next)
  390. {
  391. gotoxy(q->x, q->y);
  392. color(14);
  393. printf("★");
  394. q = q->next;
  395. }
  396. gotoxy(q->next->x, q->next->y);
  397. color(11);
  398. printf("■");
  399. free(q->next);
  400. q->next = NULL;
  401. }
  402. }
  403. }
  404. }
  405. int biteSelf(void)
  406. {
  407. int x = 0; // 默认未咬到自己
  408. q = head->next;
  409. // 遍历蛇身
  410. while (q->next)
  411. {
  412. if (q->x == head->x && q->y == head->y)
  413. {
  414. x = 1;
  415. }
  416. q = q->next;
  417. }
  418. return x;
  419. }
  420. int encounterWall(void)
  421. {
  422. int x = 0; // 默认未撞到墙
  423. if (head->x == 0 || head->x == 50 || head->y == 0 || head->y == 25)
  424. x = 1;
  425. return x;
  426. }
  427. void keyboardControl(void)
  428. {
  429. status = RIGHT; // 初始蛇向右移动
  430. while (1)
  431. {
  432. if (GetAsyncKeyState(VK_UP) && status != DOWN) // GetAsyncKeyState函数用来判断函数调用时指定虚拟键的状态
  433. {
  434. status = UP; //如果蛇不是向下前进的时候,按上键,执行向上前进操作
  435. }
  436. else if (GetAsyncKeyState(VK_DOWN) && status != UP) // 如果蛇不是向上前进的时候,按下键,执行向下前进操作
  437. {
  438. status = DOWN;
  439. }
  440. else if (GetAsyncKeyState(VK_LEFT) && status != RIGHT) // 如果蛇不是向右前进的时候,按左键,执行向左前进
  441. {
  442. status = LEFT;
  443. }
  444. else if (GetAsyncKeyState(VK_RIGHT) && status != LEFT) // 如果蛇不是向左前进的时候,按右键,执行向右前进
  445. {
  446. status = RIGHT;
  447. }
  448. if (GetAsyncKeyState(VK_SPACE))// 空格暂停
  449. {
  450. while (1)
  451. {
  452. Sleep(300);
  453. if (GetAsyncKeyState(VK_SPACE)) // 再次按空格改变状态
  454. {
  455. break;
  456. }
  457. }
  458. }
  459. else if (GetAsyncKeyState(VK_ESCAPE))
  460. {
  461. status = ESC; // 按esc键,直接到结束界面
  462. if (endGame())
  463. {
  464. Sleep(500);
  465. system("cls");
  466. break;
  467. }
  468. }
  469. else if (GetAsyncKeyState(VK_F1)) // 按F1键,加速
  470. {
  471. speedUp();
  472. gotoxy(60, 13);
  473. printf("当前分数:%d分,当前速度%d", score, speed);
  474. }
  475. else if (GetAsyncKeyState(VK_F2)) // 按F2键,减速
  476. {
  477. speedDown();
  478. gotoxy(60, 13);
  479. printf("当前分数:%d分,当前速度%d", score, speed);
  480. }
  481. snakeMove();
  482. }
  483. }
  484. void speedUp(void)
  485. {
  486. if (speed <= 280)
  487. speed += 20;
  488. }
  489. void speedDown(void)
  490. {
  491. if (speed >= 120)
  492. speed -= 20;
  493. }
  494. int endGame(void)
  495. {
  496. char x = 0;
  497. char judge[5];
  498. getchar();
  499. gotoxy(60, 9);
  500. printf("确定退出吗?(Yes/No)");
  501. gotoxy(60, 11);
  502. s_gets(judge, 5);
  503. if (strcmp(judge, "Yes") == 0)
  504. {
  505. Sleep(250);
  506. system("cls");
  507. gotoxy(40, 11);
  508. printf("\tBye!");
  509. x = 1;
  510. }
  511. else
  512. x = 0;
  513. return x;
  514. }
  515. char *s_gets(char *st, int n)
  516. {
  517. char *ret_val;
  518. char *find;
  519. gotoxy(60, 11);
  520. ret_val = fgets(st, n, stdin);
  521. if (ret_val)
  522. {
  523. find = strchr(st, '\n');
  524. if (find)
  525. *find = '\0';
  526. else
  527. while (getchar() != '\n')
  528. continue;
  529. }
  530. return ret_val;
  531. }
  532. void frees(snake *s)
  533. {
  534. snake *current = s;
  535. while (current)
  536. {
  537. current = s;
  538. s = current->next;
  539. free(current);
  540. }
  541. }

END

源代码比较长只能先放上去这么多,还有更多的小项目 加一下我的c/c++编程资料交流Q群:214574728

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/487015
推荐阅读
相关标签
  

闽ICP备14008679号