当前位置: 首页 > news >正文

【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;}


 

http://www.lryc.cn/news/469540.html

相关文章:

  • qt QLineEdit详解
  • DevEco Studio的使用 习题答案<HarmonyOS第一课>
  • 鸿蒙网络编程系列36-固定包头可变包体解决TCP粘包问题
  • 【华为路由】OSPF多区域配置
  • 【C++初阶】一文讲通C++内存管理
  • Vue学习笔记(九、简易计算器)
  • Maven 不同环境灵活构建
  • 第三十篇:TCP连接断开过程,从底层说明白,TCP系列五
  • 代码随想录算法训练营第七天| 哈希表理论基础 454.四数相加II 383.赎金信 15.三数之和 18.四数之和
  • 搜维尔科技:Manus新品发布Metagloves Pro专业版,专为高精度需求的客户打造,尤其是人形机器人产业与人机工效研究使用
  • Spring Boot实现的动态化酒店住宿管理系统
  • 数字IC后端实现Innovus |给各种IP子模块添加port buffer和antenna diode万能脚本
  • 反向代理服务器---NGINX
  • unity3d————场景管理类SceneManager
  • 鹅厂面试官:Transformer 为何需要位置编码?
  • MySQL数据库学习指南
  • 算法刷题-小猫爬山
  • Maven项目管理工具-初始+环境配置
  • 【JavaEE初阶】网络编程TCP协议实现回显服务器以及如何处理多个客户端的响应
  • Android 中的串口开发
  • TensorRt OP
  • 构建负责任的人工智能:数据伦理与隐私保护
  • 微信小程序live-pusher和video同时使用,video播放声音时时大时小
  • MySQL 分库分表实战
  • MySQL—CRUD—进阶—(二) (ಥ_ಥ)
  • 时序分解 | TTNRBO-VMD改进牛顿-拉夫逊算法优化变分模态分解
  • 2024“源鲁杯“高校网络安全技能大赛-Misc-WP
  • CSS行块标签的显示方式
  • Go 语言中的 for range 循环教程
  • 青训营 X 豆包MarsCode 技术训练营--小M的比赛胜场计算