设为首页 加入收藏

TOP

c语言贪吃蛇详解4.食物的投放与蛇的变长(三)
2018-10-21 18:10:34 】 浏览:194
Tags:语言 详解 食物 投放
循环遍历数组
{ if(a[i][j]==0) //为0输出空格 printf(" "); else //为1输出# printf("#"); } printf("\n"); //别忘了换行 } } void move() { int i; gotoxy(s[sLength-1][0],s[sLength-1][1]); printf(" "); //在尾巴上面画空格以擦除尾巴 if(eated) //如果吃到了食物 { sLength++; eated=false; //设置为false,不然无限变长 } for(i=sLength-1; i>0; i--) //从尾巴开始,每一个点的位置等于它前面一个点的位置 { s[i][0]=s[i-1][0]; s[i][1]=s[i-1][1]; } switch(direction) { case UP: s[0][0]--; break; case DOWN: s[0][0]++; break; case LEFT: s[0][1]--; break; case RIGHT: s[0][1]++; break; } } void drawSnake() //画蛇 { int i; for(i=0; i<sLength; i++) { gotoxy(s[i][0],s[i][1]); //移动关标到蛇的坐标 printf("@"); //在这个位置画蛇 } } void key() { if(kbhit()!=0) //如果有键盘输入 { char in; while(!kbhit()==0) //如果玩家输入了多个按键,以最后一个按键为准 in=getch(); switch(in) { case 'w': case 'W': if(direction!=DOWN) //不能缩头吧。。。。 direction=UP; break; case 's': case 'S': if(direction!=UP) direction=DOWN; break; case 'a': case 'A': if(direction!=RIGHT) direction=LEFT; break; case 'd': case 'D': if(direction!=LEFT) direction=RIGHT; break; case 'p': case 'P': gotoxy(H,0); //将光标移动到下面 system("pause"); gotoxy(H,0); printf(" "); //消去下面的按任意键继续 break; } } } int main() { init(); //程序开始时的初始化操作 drawMap(); //画地图 food(); while(1) { drawSnake(); //画蛇 Sleep(WAIT_TIME); //等待一段时间 key(); move(); //移动蛇(主要是修改蛇身数组的数据) if(a[s[0][0]][s[0][1]]==-1) //如果蛇头碰到食物,就重新投放食物,并且把食物点重置为0 { eated=true; //标记已经吃到食物 food(); a[s[0][0]][s[0][1]]=0; //去掉食物 } } getchar(); return 0; }

 

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【转】C语言中DEFINE简介及多行宏.. 下一篇C语言程序设计:现代方法(第2版..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目