当前位置:   article > 正文

c语言小游戏代码(c语言小游戏代码简单)_c语言简单小游戏代码

c语言简单小游戏代码

c语言编写小游戏请提供俄罗斯方块,坦克大战之类的小游戏的程序的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. #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. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/358871
推荐阅读