/* /* 头尾平移的方法实现 By black4yl 原理: 在大小为100的数组中,利用head,rear标记的头,尾坐标值,定位当前头和尾巴。 在行走时,擦去尾巴,按方向标记新头,擦去旧头, 实现蛇行走。 当吃到食物时,只变化头,不变化 */ #include"windows.h" #include"conio.h" #include"iostream" #include<time.h> using namespace std; #define up 72 #define down 80 #define left 75 #define right 77 void GotoXY(int x,int y) { COORD c; c.X=x-1; c.Y=y-1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); } void Display(char img[][22],int score,int speed) //0为空 1为身子 2为头 3为食物 { system("cls"); int i,j; GotoXY(1,1); for(i=0;i<24;i++) cout<<"□"; cout<<endl; for(i=0;i<22;i++) { cout<<"□"; for(j=0;j<22;j++) { if(img[i][j]==0) cout<<" "; if(img[i][j]==1) cout<<"○"; if(img[i][j]==2) cout<<"○"; if(img[i][j]==3) cout<<"○"; } cout<<"□"<<endl; } for(i=0;i<24;i++) cout<<"□"; GotoXY(50,10); printf("Score: %d 分",score); GotoXY(50,11); printf("speed= %d ms",speed); } void Welcome() { long time=clock(); GotoXY(3,8); cout<<"欢迎进入贪吃蛇游戏"<<endl; while(clock()-time<2000); system("cls"); GotoXY(3,8); time=clock(); cout<<"操作方法: 按 上 下 左 右 键控制蛇,以便吃到更多的食物,不要饿死哦~ "<<endl; GotoXY(6,13); int i=4; do { time=clock(); while(clock()-time<1000); GotoXY(6,13); cout<<"游戏将在--"<<i<<"--秒"<<"后开始"; }while(i--); system("cls"); } int main() { int x, y; int fx,fy; //食物 srand(time(0)); fx=rand()%20+1; fy=rand()%20+1; int len=0; int i,j; int tcspos[2][150]; char img[22][22]={0}; char arr=77; long time;//时间 int timeover; int speed=500,score=0; int head,rear; head=3; rear=0; for(i=0; i<4; i++){ //初始化蛇身 tcspos[0][i] = 1; tcspos[1][i] = i + 1; } img[fy][fx]=3; Welcome(); Display(img,score,speed); while(1) { timeover=1; time=clock(); while((timeover=clock()-time<=speed)&&!kbhit()); if(timeover) //timeover 非0,说明是按键中断循环 { getch(); arr=getch(); } switch(arr) { //完成操作 case up: y=tcspos[1][head]-1; x=tcspos[0][head]; break; //上 case down: y=tcspos[1][head]+1; x=tcspos[0][head]; break; //下 case left: y=tcspos[1][head]; x=tcspos[0][head]-1; break; //左 case right: y=tcspos[1][head]; x=tcspos[0][head]+1; break; //右 } if(img[y][x]!=0&&!((x==fx)&&(y==fy))) //冲突 { break; //结束 } if(x<0||x>21||y<0||y>21) { break; //结束 } if((x==fx)&&(y==fy)) //吃到 { len++; score+=2; //每吃一个加2分 if(len>9) //蛇长大于10, { len%=9; //重新计算 speed-=20; //每次速度增加20ms score+=10; //没完成一个10,则多加10分 } img[tcspos[1][head]][tcspos[0][head]]=2; //标记行走后的头 head=(head+1)%150; //head 移到第二位; tcspos[0][head]=x; //设置蛇头坐标 tcspos[1][head]=y; do //生产食物 { fx=rand()%21+1; fy=rand()%21+1; }while(img[fy][fx]!=0); img[fy][fx]=3; Display(img,score,speed); } else //没吃到 { img[tcspos[1][head]][tcspos[0][head]]=1; //头变为身子 head=(head+1)%150; //头前移 img[tcspos[1][rear]][tcspos[0][rear]]=0; //消去尾巴 rear=(rear+1)%150; //尾巴前移 tcspos[1][head]=y; tcspos[0][head]=x; img[tcspos[1][head]][tcspos[0][head]]=2; //标记新头 Display(img,score,speed); } } //while() system("cls"); GotoXY(20,12); cout<<"score:"<<score<<endl; GotoXY(20,13); cout<<"---Game Over---"<<endl; GotoXY(20,14); cout<<"请输入您的姓名:"; getchar(); }
贪吃蛇C语言实现
原创
©著作权归作者所有:来自51CTO博客作者不怕小猫咪的原创作品,请联系作者获取转载授权,否则将追究法律责任
贪吃蛇C语言实现
https://blog.51cto.com/black4yl/1579265
贪吃蛇C语言实现
https://blog.51cto.com/black4yl/1579265
上一篇:智能聊天机器人(JAVA实现)
举报文章
请选择举报类型
内容侵权
涉嫌营销
内容抄袭
违法信息
其他
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++编写贪吃蛇
游戏描述如下:贪吃蛇可以自动直线前进,或者玩家可以通过方向键操纵贪吃蛇上下左右前进,
c++ c语言 贪吃蛇 c++代码 方向键 -
牛得一批!10分钟用Python编写一个贪吃蛇小游戏
Python开发贪吃蛇小游戏
贪吃蛇 python sql -
C语言的本质(3)——整数的本质与运算
C语言的本质(3)——整数的本质与运算
补码 无符号数 有符号数 C语言 -
Lua:01---Lua语言介绍、运行Lua程序(lua解释器)
一、Lua语言介绍Lua语言从一开始就被设计为能与C
Lua语言介绍 lua 环境变量 程序段