加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

纯C语言写的贪吃蛇源码

发布时间:2020-12-16 07:48:03 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #include #include #include #include #include #define N 225 struct Food { int x; int y; int yes;//1表示需要出现新食物,0表示已有食物。 }food;

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    #include  
    #include  
    #include  
    #include  
    #include   
      
    #define N 225  
      
    struct Food  
    {  
        int x;  
        int y;  
        int yes;//1表示需要出现新食物,0表示已有食物。  
    }food;  
      
    struct Snake  
    {  
        struct Food body[N];  
        int life;//1表示蛇死啦,0表示蛇活着。  
        int node;//蛇的节数。  
        char direction;//蛇自动运动的方向。  
    }snake;  
      
    int score=0;  
      
    int main()  
    {  
        FILE *p;  
        int i,j,k,b;  
        char map[16][16],c;  
      
        p=fopen("E:file.txt","r");  
        if(p==NULL)  
        {  
            printf("error");  
            exit(1);  
        }  
        for(i=0;i<16;i++) { for(j=0;j<16;j++) { map[i][j]=fgetc(p); } fgetc(p); } snake.body[0].x=8;//蛇头。 snake.body[0].y=8; snake.body[1].x=8; snake.body[1].y=7; snake.body[2].x=8; snake.body[2].y=6; snake.node=3;//蛇的节数。 food.yes=1; srand(time(NULL)); snake.direction='d';//一开始蛇自动往右跑。 while(1) { if(kbhit()) { c=getch(); if(snake.life==1) break; if(c=='w' && snake.direction!='s') snake.direction='w'; else if(c=='a' && snake.direction!='d') snake.direction='a'; else if(c=='s' && snake.direction!='w') snake.direction='s'; else if(c=='d' && snake.direction!='a') snake.direction='d'; } if(food.yes==1)//需要随机出现新的食物。 { food.x=1+rand()%14; food.y=1+rand()%14; for(i=0;i0;i--)//蛇往前移动。  
            {  
                snake.body[i].x=snake.body[i-1].x;  
                snake.body[i].y=snake.body[i-1].y;  
            }  
      
            switch(snake.direction)//蛇头方向。  
            {  
                case'a':  
                    snake.body[0].y-=1;  
                    break;  
                case'w':  
                    snake.body[0].x-=1;  
                    break;  
                case'd':  
                    snake.body[0].y+=1;  
                    break;  
                case's':  
                    snake.body[0].x+=1;  
                    break;  
            }  
      
      
            if(food.yes==0)//显示蛇,食物和围墙。  
            {  
                system("cls");  
                for(i=0;i<16;i++) { for(j=0;j<16;j++) { b = 1; for(k=0;k0 && j<15 && j>0)//食物。  
                                printf("★");  
                            else if(map[i][j]=='1')  
                                printf("■");  
                            else   
                                printf("  ");  
                        }  
                                  
                    }  
                    putchar('n');  
                }  
                Sleep(250);//休眠函数。  
            }  
              
            for(i=3;i=15 || snake.body[0].x<=0 || snake.body[0].y>=15 || snake.body[0].y<=0) { printf("蛇撞墙死了!n"); printf("共得分:%dn",score); snake.life=1; break; } } fclose(p); return 0; } 
  1. 下面是file.txt:?
  1. 2222222222222221??
  2. 1000000000000001??
  3. 1000000000000001??
  4. 1000000000000001??
  5. 1000000000000001??
  6. 1000000000000001??
  7. 1000000000000001??
  8. 1000000000000001??
  9. 1000000000000001??
  10. 1000000000000001??
  11. 1000000000000001??
  12. 1000000000000001??
  13. 1000000000000001??
  14. 1000000000000001??
  15. 1000000000000001??
  16. 2222222222222221?

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读