当前位置:   article > 正文

C语言零基础项目:吃豆人小游戏!详细思路+源码分享_吃豆人游戏代码

吃豆人游戏代码

每天一个C语言小项目,提升你的编程能力!

《吃豆游戏》是一款休闲小游戏,和贪吃蛇,球球大作战吃食物都是有差不多的游戏逻辑。

效果展示:

这个游戏本身很简单,一共3关,吃掉画面上全部小豆子就过关,被敌人碰到就over。吃大豆子可以让敌人暂时能被你吃,但是后面关卡,敌人恢复时间很短,敌人速度也很快。

当然现在你用代码自己开发的话,设置多少关卡,什么样的难度都看你自己咯!

游戏中使用了图片【https://codebus.cn/f/a/0/0/135/boxman.zip】

本项目编译环境:Visual Studio 2019/2022,EasyX插件

代码展示:

  1. #include <stdio.h.h>
  2. #include <graphics.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #define M 640 //开屏大小
  6. #define N 480
  7. #define rank 1 //游戏等级
  8. int g_sum = 0; //统计吃豆的个数
  9. int g_score = 0; //分数统计
  10. bool g_win = true; //成败的标志
  11. IMAGE image[5]; //装载图片
  12. enum {LEFT=1, RIGHT, UP, DOWN}; //方向的控制
  13. struct figure //人物属性
  14. {
  15. int x; //人物图纸坐标
  16. int y;
  17. int x1; //人物方向移动
  18. int y1;
  19. int x2; //人物物理坐标
  20. int y2;
  21. }you,enemy[5];
  22. /*0豆子,1可以移动地方,2墙壁,3自己,4敌人*/
  23. int a[15][20]= {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  24. 2,1,0,0,0,0,1,0,1,0,0,0,1,1,1,1,1,1,0,2,
  25. 2,1,2,2,2,1,1,2,1,1,0,0,0,1,1,4,1,1,0,2,
  26. 2,1,1,0,2,1,1,2,0,1,1,2,2,2,2,2,0,0,0,2,
  27. 2,4,1,0,2,1,1,2,1,1,1,0,1,1,1,1,0,1,1,2,
  28. 2,1,2,1,2,1,1,2,1,3,2,2,1,1,1,1,2,2,1,2,
  29. 2,1,2,1,2,1,1,1,1,1,1,1,1,0,0,0,1,1,1,2,
  30. 2,1,2,1,0,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2,2,2,2,2,2,2};/*数组就是地图*/
  53. 函数声明/
  54. void Init_begin(); //初始化开始界面
  55. void Initia(); //初始化界面
  56. void Init_score(); //记分板
  57. void Init_loss(); //失败的界面
  58. void Init_win(); //成功的界面
  59. void Init_leave(); //离开的界面
  60. void Init_copy(); //图形复制
  61. void enemymove(); //敌人运动
  62. void randdire(struct figure *sth); //使方向随即改变
  63. bool judgemeet(); //相遇判断
  64. void youmove(int k ,int speed) ; //对自己进行移动
  65. void mobile(); //自己移动
  66. void eatbean(); //判断是否吃小黄点
  67. void play(); //开始游戏
  68. int sjs(int a,int b); //产生a,b之间的数
  69. void enemymeetroad(int i); //敌人与路
  70. void enemymeetbean(int i) ; //敌人遇豆子
  71. void load_image(); //加载图片
  72. void Game_levels(); //游戏等级设定
  73. /
  74. void Init_begin() //初始化开始界面
  75. {
  76. setbkcolor(BLACK);
  77. char init[20] = "吃豆子游戏";
  78. int x = M/2 - 250,y = 100;
  79. setcolor(CYAN);
  80. setfont(100, 0,"楷体",0,0,FW_DEMIBOLD,false,false,false);
  81. outtextxy(x,y,init);
  82. Sleep(400);
  83. RECT r = {x + 50, 250, 540, 460};
  84. setfont(24, 0, "宋体");
  85. setcolor(BROWN);
  86. outtextxy(x + 50, 250,"吃豆子游戏规则:");
  87. setfont(18, 0, "宋体");
  88. drawtext("\n\n1:游戏中,操作者可以通过方向键(上、下、左、右)或字母键(‘w’、‘s’、‘a’、‘d’)来调整方向 \n2:游戏中途可以按‘空格键’来暂停游戏 \n3:按‘ESC’推出游戏",
  89. &r, DT_WORDBREAK);
  90. setfont(24, 0,"楷体");
  91. setcolor(DARKGRAY);
  92. outtextxy(M/2 - 107,y + 300, "按");
  93. setcolor(RED);
  94. outtextxy(M/2 - 80, y + 300, "回车键");
  95. setcolor(DARKGRAY);
  96. outtextxy(M/2 , y + 300, "开始游戏");
  97. char put ;
  98. put = getch();
  99. while(put != 13)
  100. put = getch();
  101. }
  102. /*0豆子,1可以移动地方,2墙壁,3自己,4敌人*/
  103. void Initia() //初始化界面
  104. {
  105. cleardevice();
  106. setbkcolor(RGB(72,12,12));
  107. for( int i = 0;i < 15;i++)
  108. for(int j = 0;j < 20;j++)
  109. putimage(100+j*20,100+i*20,&image[a[i][j]]);
  110. you.x=9; you.y=5; //敌人和自己的初始图纸坐标
  111. enemy[0].x=15; enemy[0].y=2;
  112. enemy[1].x=1; enemy[1].y=4;
  113. enemy[2].x=16; enemy[2].y=8;
  114. enemy[3].x=13; enemy[3].y=12;
  115. enemy[4].x=7; enemy[4].y=13;
  116. you.x2 = you.x * 20 + 100; //图纸坐标转化为屏幕坐标
  117. you.y2 = you.y * 20 + 100;
  118. for(i=0;i<5;i++)
  119. {
  120. enemy[i].x2 = enemy[i].x * 20 + 100;
  121. enemy[i].y2 = enemy[i].y * 20 + 100;
  122. enemy[i].x1 = 0; enemy[i].y1 = 1;
  123. }
  124. Init_score();
  125. }
  126. void load_image() //加载图片
  127. {
  128. loadimage(&image[0],"IMAGE", "0.jpg");
  129. loadimage(&image[1],"IMAGE", "1.jpg");
  130. loadimage(&image[2],"IMAGE", "2.jpg");
  131. loadimage(&image[3],"IMAGE", "3.jpg");
  132. loadimage(&image[4],"IMAGE", "4.jpg");
  133. }
  134. void Init_score() //记分板
  135. {
  136. setbkcolor(RGB(72,12,12));
  137. char infor[10];
  138. itoa(g_score,infor,10);
  139. setcolor(CYAN);
  140. setfont(28,0,"Arial");
  141. outtextxy(M - 140,90,"当前分数为");
  142. setfillstyle(RGB(72,12,12));
  143. bar(M - 40,140,M - 20,170);
  144. outtextxy(M - 80,140,infor);
  145. }
  146. int sjs(int a,int b) //产生a,b之间的数
  147. {
  148. return a+rand()%(b-a+1);
  149. }
  150. void Init_copy() //图形复制
  151. {
  152. for(int i = 0;i < 15;i++)
  153. for(int j = 0;j < 20;j++)
  154. a[i][j] = b[i][j];
  155. }
  156. void enemymeetbean(int i) //敌人遇豆子
  157. {
  158. int x = enemy[i].x2 ;
  159. int y = enemy[i].y2 ;
  160. for(int j = 0;j<20;j++)
  161. {
  162. putimage(x,y,&image[0]);
  163. enemy[i].x2 += enemy[i].x1;
  164. enemy[i].y2 += enemy[i].y1;
  165. putimage(enemy[i].x2,enemy[i].y2,&image[4]);
  166. }
  167. }
  168. void enemymeetroad(int i) //敌人遇路
  169. {
  170. int x = enemy[i].x2 ;
  171. int y = enemy[i].y2 ;
  172. for( int j = 0;j < 20;j++)
  173. {
  174. putimage(x,y,&image[1]);
  175. enemy[i].x2 += enemy[i].x1;
  176. enemy[i].y2 += enemy[i].y1;
  177. putimage(enemy[i].x2,enemy[i].y2,&image[4]);
  178. }
  179. }
  180. void enemymove() //敌人运动
  181. {
  182. Game_levels();
  183. for(int i = 0;i < 5;i++) //判断是否与墙壁相遇,如果相遇,随即改变方向
  184. {
  185. if(a[enemy[i].y+enemy[i].y1][enemy[i].x+enemy[i].x1] == 2 || abs(enemy[i].y1) == abs(enemy[i].x1))
  186. randdire(&enemy[i]);
  187. }
  188. for(i = 0;i < 5;i++)
  189. {
  190. if(a[enemy[i].y ][enemy[i].x] == 0)
  191. enemymeetbean(i);
  192. else
  193. enemymeetroad(i);
  194. enemy[i].x+=enemy[i].x1;
  195. enemy[i].y+=enemy[i].y1;
  196. if(judgemeet()) break;
  197. }
  198. Sleep(180);
  199. }
  200. void Game_levels() //游戏等级设定
  201. {
  202. if( sjs (1,rank) == 1)
  203. {
  204. int t = rand() % 5;
  205. enemy[t].x1 = enemy[t].x - you.x > 0 ? -1 : 1;
  206. enemy[t].y1 = enemy[t].y - you.y > 0 ? -1 : 1;
  207. }
  208. for(int i = 0;i < 5;i++)
  209. {
  210. if(enemy[i].x == you.x && enemy[i].y + 1 == you.y)
  211. {enemy[i].x1 = 0 ; enemy[i].y1 = 1;}
  212. if(enemy[i].x == you.x && enemy[i].y - 1 == you.y)
  213. {enemy[i].x1 = 0 ; enemy[i].y1 = -1;}
  214. if(enemy[i].x + 1 == you.x && enemy[i].y == you.y)
  215. {enemy[i].x1 = 1 ; enemy[i].y1 = 0;}
  216. if(enemy[i].x - 1 == you.x && enemy[i].y == you.y)
  217. {enemy[i].x1 = -1 ; enemy[i].y1 = 1;}
  218. }
  219. }
  220. void randdire( struct figure *sth) //使方向随即改变
  221. {
  222. int b=1;
  223. int x,y;
  224. while(b)
  225. {
  226. x =sjs(-1,1);
  227. y=sjs(-1,1);
  228. if(a[sth->y + y][sth->x + x] != 2 && abs(y) != abs(x))
  229. {
  230. b = 0;
  231. sth->x1 = x;
  232. sth->y1 = y;
  233. }
  234. else b = 1;
  235. }
  236. }
  237. bool judgemeet () //相遇判断
  238. {
  239. for(int i = 0;i < 5;i++)
  240. {
  241. if(enemy[i].x == you.x && enemy[i].y == you.y )
  242. {
  243. g_win = false;
  244. Init_loss();
  245. return true;
  246. break;
  247. }
  248. }
  249. return false;
  250. }
  251. void eatbean() //判断是否吃小黄点
  252. {
  253. if(a[you.y][you.x] == 0)
  254. {
  255. a[you.y][you.x] = 1;
  256. g_sum++;
  257. g_score += 10;
  258. Init_score();
  259. a[you.y][you.x] = 1;
  260. }
  261. }
  262. void youmove(int k) //对自己进行移动
  263. {
  264. switch(k)
  265. {
  266. case LEFT:
  267. you.x1 = -1;you.y1 = 0;
  268. break;
  269. case RIGHT:
  270. you.x1 = 1;you.y1 = 0;
  271. break;
  272. case DOWN:
  273. you.x1 = 0;you.y1 = 1;
  274. break;
  275. case UP:
  276. you.x1 = 0;you.y1 = -1;
  277. break;
  278. }
  279. if(a[you.y + you.y1][you.x + you.x1] != 2)
  280. {
  281. int x = you.x2 ;
  282. int y = you.y2 ;
  283. for(int j = 0 ;j < 10;j++)
  284. {
  285. putimage(x,y,&image[1]);
  286. you.x2 += you.x1 * 2;
  287. you.y2 += you.y1 * 2;
  288. putimage(you.x2,you.y2,&image[3]);
  289. }
  290. you.x += you.x1;
  291. you.y += you.y1;
  292. eatbean();
  293. }
  294. }
  295. void Init_loss() //失败的界面
  296. {
  297. cleardevice();
  298. char str[] = "挑战失败,还需继续努力呀!";
  299. LOGFONT f;
  300. getfont(&f);
  301. f.lfHeight = 48;
  302. strcpy(f.lfFaceName , "楷体");
  303. f.lfQuality = ANTIALIASED_QUALITY;
  304. f.lfWeight = FW_NORMAL;
  305. setcolor(LIGHTGREEN);
  306. setfont(&f);
  307. outtextxy(20,200,str);
  308. char tryagain[] = "是否想继续挑战?(Y/N)";
  309. f.lfHeight = 28;
  310. setfont(&f);
  311. setcolor(BROWN);
  312. outtextxy(90,300,tryagain);
  313. char yesorno ;
  314. int keep = 1 ;
  315. while(keep)
  316. {
  317. yesorno = getch();
  318. if(yesorno == 'Y' || yesorno == 'y' || yesorno == 'N' || yesorno == 'n')
  319. keep = 0;
  320. }
  321. if(yesorno == 'Y'|| yesorno == 'y')
  322. {
  323. g_win = true;
  324. g_score = 0;
  325. g_sum = 0;
  326. cleardevice();
  327. Init_copy();
  328. Initia();
  329. }
  330. else Init_leave();
  331. }
  332. void Init_leave()
  333. {
  334. cleardevice();
  335. char str[] = "下次继续努力吧!";
  336. LOGFONT f;
  337. getfont(&f);
  338. f.lfHeight = 48;
  339. strcpy(f.lfFaceName , "楷体");
  340. f.lfQuality = ANTIALIASED_QUALITY;
  341. f.lfWeight = FW_NORMAL;
  342. setcolor(LIGHTGREEN);
  343. setfont(&f);
  344. outtextxy(150,200,str);
  345. Sleep(2000);
  346. exit(0);
  347. }
  348. void Init_win() //成功的界面
  349. {
  350. cleardevice();
  351. char str[] = "恭喜你通过了游戏!!!";
  352. LOGFONT f;
  353. getfont(&f);
  354. f.lfHeight = 48;
  355. strcpy(f.lfFaceName , "楷体");
  356. f.lfQuality = ANTIALIASED_QUALITY;
  357. f.lfWeight = FW_NORMAL;
  358. setcolor(LIGHTGREEN);
  359. setfont(&f);
  360. outtextxy(50,200,str);
  361. Sleep(4000);
  362. char tryagain[] = "是否想再次挑战?(Y/N)";
  363. f.lfHeight = 28;
  364. setfont(&f);
  365. setcolor(BROWN);
  366. outtextxy(70,300,tryagain);
  367. char yesorno ;
  368. yesorno = getch();
  369. if(yesorno == 'y' || yesorno == 'Y')
  370. {
  371. g_win = true;
  372. g_score = 0;
  373. g_sum = 0;
  374. cleardevice();
  375. Init_copy();
  376. Initia();
  377. }
  378. else exit(0);
  379. }
  380. void stop() //游戏暂停
  381. {
  382. char key = 'a';
  383. char stop[] = "游戏暂停中";
  384. setcolor(RED);
  385. setfont(28,0,"楷体");
  386. outtextxy(230,50,stop);
  387. Sleep(100);
  388. while(key != ' ')
  389. key = getch();
  390. setcolor(RGB(72,12,12));
  391. outtextxy(230,50,stop);
  392. }
  393. void play()
  394. {
  395. setbkcolor(RGB(72,12,12));
  396. char key; //获取字母
  397. int t; //记录方向
  398. while(g_win)
  399. {
  400. enemymove();
  401. judgemeet();
  402. while(kbhit())
  403. {
  404. key = getch();
  405. switch(key)
  406. {
  407. case 'a':
  408. case 75: t = LEFT; youmove(t ); break;
  409. case 'd':
  410. case 77: t = RIGHT; ; youmove(t ); break;
  411. case 's':
  412. case 80: t = DOWN; youmove(t ); break;
  413. case 'w':
  414. case 72: t = UP; youmove(t ); break;
  415. case 27: Init_leave(); break;
  416. case 32: stop(); break;
  417. default : break;
  418. }
  419. }
  420. if(g_sum == 50) Init_win();
  421. }
  422. }
  423. void main()
  424. {
  425. initgraph(M,N);
  426. load_image();
  427. Init_begin();
  428. setbkcolor(RGB(72,12,12));
  429. srand((unsigned)time(NULL));
  430. Initia();
  431. play();
  432. closegraph();
  433. }

大家赶紧去动手试试吧!

此外,我也给大家分享我收集的其他资源,从最零基础开始的教程到C语言C++项目案例,帮助大家在学习C语言的道路上披荆斩棘!

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)最重要的是你可以在群里面交流提问编程问题哦!

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!(↓↓↓↓↓↓)

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

闽ICP备14008679号