中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

純C語(yǔ)言寫(xiě)的貪吃蛇源碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
    #include  
    #include  
    #include  
    #include  
    #include   
      
    #define N 225  
      
    struct Food  
    {  
        int x;  
        int y;  
        int yes;//1表示需要出現(xiàn)新食物,0表示已有食物。  
    }food;  
      
    struct Snake  
    {  
        struct Food body[N];  
        int life;//1表示蛇死啦,0表示蛇活著。  
        int node;//蛇的節(jié)數(shù)。  
        char direction;//蛇自動(dòng)運(yùn)動(dòng)的方向。  
    }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;//蛇的節(jié)數(shù)。 food.yes=1; srand(time(NULL)); snake.direction='d';//一開(kāi)始蛇自動(dòng)往右跑。 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)//需要隨機(jī)出現(xiàn)新的食物。 { food.x=1+rand()%14; food.y=1+rand()%14; for(i=0;i0;i--)//蛇往前移動(dòng)。  
            {  
                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);//休眠函數(shù)。  
            }  
              
            for(i=3;i=15 || snake.body[0].x<=0 || snake.body[0].y>=15 || snake.body[0].y<=0) { printf("蛇撞墻死了!\n"); printf("共得分:%d\n",score); snake.life=1; break; } } fclose(p); return 0; } 
  1. 下面是file.txt: 
  1. 1111111111111111  
  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. 1111111111111111 

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:經(jīng)典算法11:任意長(zhǎng)度整數(shù)加法

下一篇:Android 打電話發(fā)短信代碼