C语言项目实战:《自动版贪吃蛇》零基础项目!源码 + 注释
这篇文章主要为大家详细介绍了C语言实现——《进化版贪吃蛇项目》,也就是在我们上一个发布的文章项目分享的基础上,实现贪吃蛇的自动举措,你可以按键也可以不按键,让蛇本身找食物!还是挺好玩的,一起来看看吧!文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下!
项目实现要求:
1.学过C语言
2.熟悉C语言数组
3.熟悉结构体
4.按键交互 步伐:1.按键交互 2.鼠标交互
5.创建图形窗口
6.基本绘图
但是这些在我们项目源码里面都会有注释,大家到时间学习的时间千万不要忽略注释,注释可以更好地帮你理解代码,尤其是C语言初学者。
本项目编译情况:VS2019/VS2013;
插件:图形库插件easyX;
效果图演示:
https://p3.toutiaoimg.com/large/pgc-image/17595fa2822648feaf78b21a088e5c0b
源代码示例:
#define _CRT_SECURE_NO_WARNINGS#include #include #include #include //报错:安装图形库插件#include #define MAX 100 //蛇最大节数/* 1.学过C语言 2.熟悉C语言数组 3.熟悉结构体 4.按键交互步伐:1.按键交互 2.鼠标交互 5.创建图形窗口 6.基本绘图*///1.把步伐所要用的数据抽象出来typedef struct pointXY{ int x; int y;}MYPOINT;HWND hwnd = NULL;//蛇struct mySnake{ int num; MYPOINT xy; char postion; //表示方向,标记}snake;//食物struct myFood{ MYPOINT foodxy; int flag; int eatGrade;}food;//把方向枚举出来enum movPostion{right=1,left=2,down=3,up=4};//整个项目分别//1.初始化蛇void initSnake(){ snake.xy.x = 0; snake.xy.y = 0; snake.xy.x = 10; snake.xy.y = 0; snake.xy.x = 20; snake.xy.y = 0; snake.num = 3; snake.postion = right; food.flag = 0;}//2.绘制蛇void drawSnake(){ for (int i = 0; i < snake.num; i++) { setlinecolor(RED); setfillcolor(GREEN); fillrectangle(snake.xy.x, snake.xy.y, snake.xy.x + 10, snake.xy.y + 10); }}//3.移动蛇void moveSnake(){ //除了第一节之外,后面的坐标都是前一节坐标 for (int i = snake.num - 1; i > 0; i--) { snake.xy.x = snake.xy.x; snake.xy.y = snake.xy.y; } //蛇头怎么走,要根据方向标志去做移动 switch (snake.postion) { case right: snake.xy.x += 10; break; case left: snake.xy.x -= 10; break; case down: snake.xy.y += 10; break; case up: snake.xy.y -= 10; break; }}//4.按键用户:人玩蛇void keyDown(){ static char userkey = '\0'; //VCgetch不需要加下划线 //userkey=_getch(); //不可见输入 //本身生成按键 switch (snake.postion) { case right: case left: if (food.foodxy.y >= snake.xy.y) { userkey = 80; } elseif (food.foodxy.y= snake.xy.x) { userkey = 77; } elseif (food.foodxy.x < snake.xy.x) { userkey = 75; } break; } switch (userkey) { case 'w': case 'W': case 72: //上 if (snake.postion != down) { //蛇往上走,你要排除掉蛇原来是朝下 snake.postion = up; } break; case 's': case 'S': case 80: //下 if (snake.postion != up) { snake.postion = down; } break; case 'a': case 'A': case 75: //左 if (snake.postion != right) { snake.postion = left; } break; case 'd': case 'D': case 77: //右 if (snake.postion != left) { snake.postion = right; } }}//初始化食物voidinitFood(){ //0,63 630 640 //0,47 470 480 food.foodxy.x = rand() % 64 * 10;//只能是10的整数倍,蛇头才能对齐食物 food.foodxy.y = rand() % 48 * 10; food.flag = 1; //食物不能出现在蛇身上 for (int i = 0; i < snake.num; i++) { if (food.foodxy.x == snake.xy.x&&food.foodxy.y == snake.xy.y) { food.foodxy.x = rand() % 64 * 10;//只能是10的整数倍,蛇头才能对齐食物 food.foodxy.y = rand() % 48 * 10; } }}//绘制食物void drawFood(){ fillrectangle(food.foodxy.x, food.foodxy.y, food.foodxy.x + 10, food.foodxy.y + 10);}//蛇吃食物void eatFood(){ if (snake.xy.x == food.foodxy.x && snake.xy.y == food.foodxy.y) { snake.num++; food.eatGrade += 10; food.flag = 0; }}//蛇殒命判断int snakeDie(){ //显示分数 char grade = { 0 }; sprintf(grade, "%d", food.eatGrade); setbkmode(TRANSPARENT); settextcolor(RED); outtextxy(580, 20, "分数:"); //loadimage outtextxy(620, 20, grade); // //判断蛇是否殒命 //撞墙 if (snake.xy.x > 640 || snake.xy.x480 || snake.xy.y < 0) { MessageBox(hwnd, "游戏结束!", "撞墙殒命!", 0); return 1; } //撞本身 for (int i = 1; i < snake.num; i++) { if (snake.xy.x == snake.xy.x&&snake.xy.y == snake.xy.y) { MessageBox(hwnd, "游戏结束!", "撞本身!", 0); return 1; } } return 0;}int main(){ srand((unsigned int)time(NULL)); hwnd=initgraph(640, 480); setbkcolor(WHITE); cleardevice(); initSnake(); while (1) { cleardevice(); //刷掉路径 if (food.flag == 0) { initFood(); } drawFood(); drawSnake(); if (snakeDie()) { break; } eatFood(); moveSnake(); Sleep(100); //控制速度 //while (_kbhit()) //kbhit() 存在按键操作,返回非零 //{ // keyDown(); //} keyDown(); } closegraph(); printf("GameOver!!!"); system("pause"); return 0;}写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心本领(内功)不妨从现在开始!
编程学习册本分享:
https://p26.toutiaoimg.com/large/pgc-image/347cc753fa1942a0bc93c13e11eb6866
编程学习视频分享:
https://p5.toutiaoimg.com/large/pgc-image/d4bef9c7ba984d40aacfe1c199a5c3c0
整理分享(多年学习的源码、项目实战视频、项目条记,基础入门教程)
接待转行和学习编程的伙伴,利用更多的资料学习成长比本身琢磨更快哦!
对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!
页:
[1]