这篇文章主要为大家详细介绍了C++结构体数组实现贪吃蛇,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C++结构体数组实现贪吃蛇的具体代码,供大家参考,具体内容如下
代码:
#include #include #include using namespace std; const int h=50,w=50,MaxLen=400; void gotoxy(short y,short x)//光标移动函数 { COORD pos={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); } struct node { int x,y; }; node s[MaxLen]; node food; int dir,len; int Map[h+5][w+5]; int Time[7]={0},level;//building。。。 void FoodCreate() { srand ((unsigned)time(NULL));//时间作随机数种子,避免伪随机 while(1) { food.x=rand()%h,food.y=rand()%w; if(food.x>0&&food.y>0&&Map[food.x][food.y]==0)break; } gotoxy(food.x,food.y),printf("@"); gotoxy(h+1,0); } void init() { system("cls"); for(int i=0;i<=h;i++)//画图 { for(int j=0;j1;i--)s[i]=s[i-1]; s[1]=next; Sleep(100); //Sleep放在最后比较顺滑//速度在这儿调 return 1; } void GameOver() { for(int i=1;i属性->字体 选择点阵字体并调整字体大小为8×8\n"); printf("再选择布局设置窗口大小,推荐60×60\n\n"); printf("WASD控制方向\n"); printf("\n任意键进入贪吃蛇皮..."); getch(); } int main() { Welcome(); init(); while(1) { if(kbhit()) { char ch=getch(); int temp=dir; switch(ch) { case 'd':temp=0;break; case '<strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>w':temp=1;break; case 'a':temp=2;break; case 's':temp=3;break; } if((temp+dir)%2)dir=temp;//如果方向不冲突 } if(move()==0) { GameOver(); getch(); init(); } } }
运行
关于C++小游戏的更多精彩内容请点击专题: 《C++经典小游戏》 学习了解
以上就是C++结构体数组实现贪吃蛇的详细内容,更多请关注gaodaima搞代码网其它相关文章!