欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 【C】用c写贪吃蛇

【C】用c写贪吃蛇

2024/10/27 0:44:31 来源:https://blog.csdn.net/weixin_74154742/article/details/139389874  浏览:    关键词:【C】用c写贪吃蛇

1.输入正确的账号密码及其用户名,登录成功进入贪吃蛇游戏界面,

2.随机生成蛇头★、食物▲的位置(x,y),并使用□打印地图

3.使用w s a d按键,完成蛇头的上下左右移动

4.蛇头碰撞到食物后,吃下食物变成蛇身的一部分●,重新生成食物位置,显示在地图上

5.蛇撞墙后或蛇咬到自己的身体,程序结束,统计吃到的食物数量

#include<stdio.h>
#include <windows.h>//gotoxy()函数头文件
#include<conio.h>//getch()函数头文件
#include<time.h>#define COL 40
#define ROW 20void gotoxy(int x, int y)//形参
{HANDLE hOut;COORD pos = {x, y};// 光标的起始位(第1列,第3行) 0是第1列 2是第3行hOut = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOut, pos);//printf("定位光标位置搜索(%d,%d)\n",pos.X,pos.Y);
}
void paintWindow(int startX,int startY,int width,int height)
{int i=0;int j=0;//起始位置gotoxy(startX,startY);printf("╔");for(i=0;i<width-2;i++){printf("═");}printf("╗");for(j=0;j<height-2;j++){gotoxy(startX,startY+1+j);printf("║");for(i=0;i<width-2;i++){printf(" ");}printf("║");}gotoxy(startX,startY + height-1);printf("╚");for(i=0;i<width-2;i++){printf("═");}printf("╝");gotoxy(20,7);printf("贪吃蛇游戏登录界面");
}int login()//登录界面
{int flag=0;int i=0;char ch;int count=0;char userName[20]={0};char passwd[20]={0};paintWindow(5,5,50,20);gotoxy(15,10);printf("用户名:");gotoxy(15,12);printf("密码:");gotoxy(22,10);while(1){while(1){ch=getch();if(count>=8)//只能输入8位{break;}if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){userName[i]=ch;putch(ch);i++;count++;}else if(ch==13) break;//13为回车的ascll码值else if(ch=='\b')//删除的转义字符{if(count>0){printf("\b \b");count--;userName[i]='\0';}}}gotoxy(22,12);count=0;i=0;while(1){ch=getch();if(count>=12)//只能输入12位{break;}if(ch>='0'&&ch<='9'){passwd[i]=ch;putch('*');i++;count++;}else if(ch==13) break;else if(ch=='\b')//删除的转义字符{if(count>0){printf("\b \b");count--;passwd[i]='\0';}}}gotoxy(22,18);if(strcmp(userName,"chen")==0&&strcmp(passwd,"1234")==0){printf("登录成功!\n");flag=1;break;}else{printf("用户名或密码错误!请重新输入!");i=0;count=0;memset(userName,0,sizeof(userName));//将数组里的值初始化memset(passwd,0,sizeof(passwd));system("cls");//刷新屏幕paintWindow(5,5,50,20);gotoxy(15,10);printf("用户名:");gotoxy(15,12);printf("密码:");gotoxy(24,10);}}return flag;
}
int snake[100][2];//蛇身
int main()
{int i,j;int flag=0;//用来判断贪吃蛇界面是绘制蛇头蛇身还是屏幕int foodx,foody;int s=0;//保存蛇身长度int snakeLen=1;//开始一个蛇头int score=0;//得分int tt;char ch;char ch2;srand(time(NULL));foodx=rand()%COL;//列xfoody=rand()%ROW;//行ysnake[0][0]=rand()%COL;//xsnake[0][1]=rand()%ROW;//ytt=login();//密码正确进入游戏if(tt==1){system("cls");while(1){for(i=0;i<ROW;i++)//行{for(j=0;j<COL;j++)//列{if(foodx==j&&foody==i){printf("▲");flag=1;}for(s=0;s<snakeLen;s++){if(snake[s][0]==j&&snake[s][1]==i&&s==0){printf("★");flag=1;}else if(snake[s][0]==j&&snake[s][1]==i){printf("●");flag=1;}}if(flag==0){printf("□");}flag=0;}//printf("\n");}//跟进蛇身长度for(i=snakeLen;i>0;i--){snake[i][0]=snake[i-1][0];snake[i][1]=snake[i-1][1];}//上下左右ch=getch();switch(ch){case 'w':system("cls");snake[0][1]-=1;break;case 's':system("cls");snake[0][1]+=1;break;case 'a':system("cls");snake[0][0]-=1;break;case 'd':system("cls");snake[0][0]+=1;break;default:break;}//判断游戏结束,碰墙if(snake[0][0]<0||snake[0][0]>=COL||snake[0][1]<0||snake[0][1]>=ROW){system("cls");//按'y'继续,按esc结束printf("是否继续游戏!按'a'可复活继续!\n按'y'重新开始游戏!\n按'esc'结束游戏!");ch2=getch();if(ch2=='a'){system("cls");continue;}else if(ch2=='y'){system("cls");snake[0][0]=rand()%COL;//xsnake[0][1]=rand()%ROW;//ysnakeLen=1;printf("你的总得分:%d分!\n",score);score=0;continue;}else if(ch2==27){printf("\n\n*************************游戏结束!*******************\n");printf("你的总分为:%d分!\n",score);return 0;}}//蛇头碰到蛇身游戏结束for(s=1;s<snakeLen;s++){if(snake[0][0]==snake[s][0]&&snake[0][1]==snake[s][1]){system("cls");//按'y'继续,按esc结束printf("是否继续游戏!\n按'a'可复活继续!\n按'y'重新开始游戏!\n按'esc'结束游戏!");ch2=getch();if(ch2=='a'){system("cls");continue;}else if(ch2=='y'){system("cls");snake[0][0]=rand()%COL;//xsnake[0][1]=rand()%ROW;//ysnakeLen=1;printf("你的总得分:%d分!\n",score);score=0;continue;}else if(ch2==27){printf("\n\n***************************游戏结束!**********************\n");printf("你的总分为:%d分!\n",score);return 0;}}}//吃到食物if(snake[0][0]==foodx&&snake[0][1]==foody){system("cls");foodx=rand()%COL;foody=rand()%ROW;snakeLen++;score++;//每次吃到食物分数累加}}}return 0;}


 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com