当前位置:   article > 正文

C语言实现基于EasyX的简单五子棋游戏_c 语言easy 写的游戏

c 语言easy 写的游戏

目录

游戏效果

主要函数分析

完整代码

下载地址


游戏效果

主要函数分析

1.判断胜负函数(judge())

从当前位置向四周遍历,如果有五子相连游戏结束

  1. //判断游戏是否结束
  2. void judge() {
  3. bool l = true, r = true;
  4. int count = 0;
  5. for (int i = 1; i <= 4; ++i) {
  6. if (l && current_y - i >= 0) {
  7. if (map[current_x][current_y - i] == current_play) { //判断左右直线
  8. count++;
  9. }
  10. else {
  11. l = false;
  12. }
  13. }
  14. if (r && current_y + i <= n) {
  15. if (map[current_x][current_y + i] == current_play) {
  16. count++;
  17. }
  18. else {
  19. r = false;
  20. }
  21. }
  22. }
  23. if (count >= 4) {
  24. isEnd = true;
  25. return;
  26. }
  27. l = r = true;
  28. count = 0;
  29. for (int i = 1; i <= 4; ++i) {
  30. if (l && current_x - i >= 0) {
  31. if (map[current_x - i][current_y] == current_play) { //判断上下直线
  32. count++;
  33. }
  34. else {
  35. l = false;
  36. }
  37. }
  38. if (r && current_x + i <= n) {
  39. if (map[current_x + i][current_y] == current_play) {
  40. count++;
  41. }
  42. else {
  43. r = false;
  44. }
  45. }
  46. }
  47. if (count >= 4) {
  48. isEnd = true;
  49. return;
  50. }
  51. l = r = true;
  52. count = 0;
  53. for (int i = 1; i <= 4; ++i) { //左上到右下的直线
  54. if (l && current_x - i >= 0 && current_y - i >= 0) {
  55. if (map[current_x - i][current_y - i] == current_play) {
  56. count++;
  57. }
  58. else {
  59. l = false;
  60. }
  61. }
  62. if (r && current_x + i <= n && current_y + i <= n) {
  63. if (map[current_x + i][current_y + i] == current_play) {
  64. count++;
  65. }
  66. else {
  67. r = false;
  68. }
  69. }
  70. }
  71. if (count >= 4) {
  72. isEnd = true;
  73. return;
  74. }
  75. l = r = true;
  76. count = 0;
  77. for (int i = 1; i <= 4; ++i) { //右上到左下的直线
  78. if (l && current_x - i >= 0 && current_y + i <= n) {
  79. if (map[current_x - i][current_y + i] == current_play) {
  80. count++;
  81. }
  82. else {
  83. l = false;
  84. }
  85. }
  86. if (r && current_x + i <= n && current_y - i >= 0) {
  87. if (map[current_x + i][current_y - i] == current_play) {
  88. count++;
  89. }
  90. else {
  91. r = false;
  92. }
  93. }
  94. }
  95. if (count >= 4) {
  96. isEnd = true;
  97. return;
  98. }
  99. }

2.更新棋盘函数(updata())

根据键盘输入改变棋盘状态,同时如果按下空格下棋时则进行胜负判断。

  1. //更新棋盘
  2. void updata(BYTE vkcode) {
  3. if (vkcode == 0x41) { //A键
  4. if (current_y - 1 >= 0) {
  5. current_y -= 1;
  6. }
  7. }
  8. else if (vkcode == 0x44) {//D
  9. if (current_y + 1 <= n) {
  10. current_y += 1;
  11. }
  12. }
  13. else if (vkcode == 0x57) {//W
  14. if (current_x - 1 >= 0) {
  15. current_x -= 1;
  16. }
  17. }
  18. else if (vkcode == 0x53) {//S
  19. if (current_x + 1 <= n) {
  20. current_x += 1;
  21. }
  22. }
  23. else if (vkcode == VK_SPACE) {//空格
  24. if (!map[current_x][current_y]) {
  25. map[current_x][current_y] = current_play;
  26. judge();
  27. current_play = 3 - current_play;
  28. }
  29. }
  30. }

3.绘制函数(draw())

绘制棋盘

  1. //绘制
  2. void draw() {
  3. //背景
  4. putimage(0, 0, &bk);
  5. //绘制棋盘
  6. setlinecolor(BLACK);
  7. for (int i = 0; i <= n; ++i) {
  8. line(x, y + i * chess_len, x + n * chess_len, y + i * chess_len);
  9. line(x + i * chess_len, y, x + i * chess_len, y + n * chess_len);
  10. }
  11. //在当前位置绘制红色框框
  12. setlinecolor(RED);
  13. rectangle(x + current_y * chess_len - chess_len / 2, y + current_x * chess_len - chess_len / 2,
  14. x + current_y * chess_len + chess_len / 2, y + current_x * chess_len + chess_len / 2);
  15. //绘制棋子
  16. for (int i = 0; i <= n; ++i) {
  17. for (int j = 0; j <= n; ++j) {
  18. if (map[i][j] == 1) {
  19. putTranspareImage(NULL, x + j * chess_len - 14, y + i * chess_len - 14, &white);
  20. }
  21. else if (map[i][j] == 2) {
  22. putTranspareImage(NULL, x + j * chess_len - 14, y + i * chess_len - 14, &black);
  23. }
  24. }
  25. }
  26. setlinecolor(BLACK);
  27. rectangle(550, 50, 750, 350);
  28. setbkmode(TRANSPARENT);
  29. settextcolor(BLACK);
  30. settextstyle(50, 0, L"宋体");
  31. outtextxy(575, 50, L"出棋方");
  32. if (current_play == 1) {
  33. setfillcolor(WHITE);
  34. }
  35. else if (current_play == 2) {
  36. setfillcolor(BLACK);
  37. }
  38. solidcircle(650, 200, 80);
  39. }

完整代码

  1. #include <graphics.h>
  2. #include <vector>
  3. #include <conio.h>
  4. #include <ctime>
  5. #include <cmath>
  6. #include<Windows.h>
  7. #pragma comment( lib, "MSIMG32.LIB")
  8. const int n = 15;
  9. const int x = 50;
  10. const int y = 50;
  11. int chess_len = 30;//棋子长度
  12. int current_x = 0;//当前坐标
  13. int current_y = 0;
  14. int current_play = 1;//当前下棋角色,1表示白棋,2表示黑棋
  15. IMAGE bk;//背景图片
  16. IMAGE white;//白棋
  17. IMAGE black;//黑棋
  18. int map[n + 1][n + 1] = { 0 };//棋盘数组
  19. bool isEnd = false;//游戏结束标志
  20. void putTranspareImage(IMAGE* target, int x, int y, IMAGE* source)
  21. {
  22. /*在屏幕上显示透明贴图函数*/
  23. HDC targetDc = GetImageHDC(target);
  24. HDC sourceDc = GetImageHDC(source);
  25. int w = (*source).getwidth();
  26. int h = (*source).getheight();
  27. // 结构体的第三个成员表示额外的透明度,0 表示全透明,255 表示不透明。
  28. BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
  29. //使用 Windows GDI 函数实现半透明位图
  30. AlphaBlend(targetDc, x, y, w, h, sourceDc, 0, 0, w, h, bf);
  31. }
  32. //判断游戏是否结束
  33. void judge() {
  34. bool l = true, r = true;
  35. int count = 0;
  36. for (int i = 1; i <= 4; ++i) {
  37. if (l && current_y - i >= 0) {
  38. if (map[current_x][current_y - i] == current_play) { //判断左右直线
  39. count++;
  40. }
  41. else {
  42. l = false;
  43. }
  44. }
  45. if (r && current_y + i <= n) {
  46. if (map[current_x][current_y + i] == current_play) {
  47. count++;
  48. }
  49. else {
  50. r = false;
  51. }
  52. }
  53. }
  54. if (count >= 4) {
  55. isEnd = true;
  56. return;
  57. }
  58. l = r = true;
  59. count = 0;
  60. for (int i = 1; i <= 4; ++i) {
  61. if (l && current_x - i >= 0) {
  62. if (map[current_x - i][current_y] == current_play) { //判断上下直线
  63. count++;
  64. }
  65. else {
  66. l = false;
  67. }
  68. }
  69. if (r && current_x + i <= n) {
  70. if (map[current_x + i][current_y] == current_play) {
  71. count++;
  72. }
  73. else {
  74. r = false;
  75. }
  76. }
  77. }
  78. if (count >= 4) {
  79. isEnd = true;
  80. return;
  81. }
  82. l = r = true;
  83. count = 0;
  84. for (int i = 1; i <= 4; ++i) { //左上到右下的直线
  85. if (l && current_x - i >= 0 && current_y - i >= 0) {
  86. if (map[current_x - i][current_y - i] == current_play) {
  87. count++;
  88. }
  89. else {
  90. l = false;
  91. }
  92. }
  93. if (r && current_x + i <= n && current_y + i <= n) {
  94. if (map[current_x + i][current_y + i] == current_play) {
  95. count++;
  96. }
  97. else {
  98. r = false;
  99. }
  100. }
  101. }
  102. if (count >= 4) {
  103. isEnd = true;
  104. return;
  105. }
  106. l = r = true;
  107. count = 0;
  108. for (int i = 1; i <= 4; ++i) { //右上到左下的直线
  109. if (l && current_x - i >= 0 && current_y + i <= n) {
  110. if (map[current_x - i][current_y + i] == current_play) {
  111. count++;
  112. }
  113. else {
  114. l = false;
  115. }
  116. }
  117. if (r && current_x + i <= n && current_y - i >= 0) {
  118. if (map[current_x + i][current_y - i] == current_play) {
  119. count++;
  120. }
  121. else {
  122. r = false;
  123. }
  124. }
  125. }
  126. if (count >= 4) {
  127. isEnd = true;
  128. return;
  129. }
  130. }
  131. //更新棋盘
  132. void updata(BYTE vkcode) {
  133. if (vkcode == 0x41) { //A键
  134. if (current_y - 1 >= 0) {
  135. current_y -= 1;
  136. }
  137. }
  138. else if (vkcode == 0x44) {//D
  139. if (current_y + 1 <= n) {
  140. current_y += 1;
  141. }
  142. }
  143. else if (vkcode == 0x57) {//W
  144. if (current_x - 1 >= 0) {
  145. current_x -= 1;
  146. }
  147. }
  148. else if (vkcode == 0x53) {//S
  149. if (current_x + 1 <= n) {
  150. current_x += 1;
  151. }
  152. }
  153. else if (vkcode == VK_SPACE) {//空格
  154. if (!map[current_x][current_y]) {
  155. map[current_x][current_y] = current_play;
  156. judge();
  157. current_play = 3 - current_play;
  158. }
  159. }
  160. }
  161. //绘制
  162. void draw() {
  163. //背景
  164. putimage(0, 0, &bk);
  165. //绘制棋盘
  166. setlinecolor(BLACK);
  167. for (int i = 0; i <= n; ++i) {
  168. line(x, y + i * chess_len, x + n * chess_len, y + i * chess_len);
  169. line(x + i * chess_len, y, x + i * chess_len, y + n * chess_len);
  170. }
  171. //在当前位置绘制红色框框
  172. setlinecolor(RED);
  173. rectangle(x + current_y * chess_len - chess_len / 2, y + current_x * chess_len - chess_len / 2,
  174. x + current_y * chess_len + chess_len / 2, y + current_x * chess_len + chess_len / 2);
  175. //绘制棋子
  176. for (int i = 0; i <= n; ++i) {
  177. for (int j = 0; j <= n; ++j) {
  178. if (map[i][j] == 1) {
  179. putTranspareImage(NULL, x + j * chess_len - 14, y + i * chess_len - 14, &white);
  180. }
  181. else if (map[i][j] == 2) {
  182. putTranspareImage(NULL, x + j * chess_len - 14, y + i * chess_len - 14, &black);
  183. }
  184. }
  185. }
  186. setlinecolor(BLACK);
  187. rectangle(550, 50, 750, 350);
  188. setbkmode(TRANSPARENT);
  189. settextcolor(BLACK);
  190. settextstyle(50, 0, L"宋体");
  191. outtextxy(575, 50, L"出棋方");
  192. if (current_play == 1) {
  193. setfillcolor(WHITE);
  194. }
  195. else if (current_play == 2) {
  196. setfillcolor(BLACK);
  197. }
  198. solidcircle(650, 200, 80);
  199. }
  200. int main() {
  201. loadimage(&bk, L"back.jpg");
  202. loadimage(&white, L"bai.png", 28, 28);
  203. loadimage(&black, L"hei.png", 28, 28);
  204. initgraph(800, 600);
  205. ExMessage msg;
  206. BeginBatchDraw();
  207. while (!isEnd) {
  208. if (peekmessage(&msg, EM_KEY)) {
  209. if (msg.message == WM_KEYUP) {
  210. updata(msg.vkcode);
  211. }
  212. }
  213. draw();
  214. FlushBatchDraw();
  215. Sleep(10);
  216. cleardevice();
  217. }
  218. MessageBox(GetHWnd(), current_play == 1 ? L"游戏结束,黑棋获胜!" : L"游戏结束,白棋获胜!", L"Game Over", MB_OK);
  219. EndBatchDraw();
  220. }

下载地址

图片资源和完整项目下载链接

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/358864
推荐阅读
相关标签
  

闽ICP备14008679号