C版貪喫蛇

#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<dos.h>
#include <stdlib.h>

#define ESC 0X011b
#define Up 0X4800
#define Down 0X5000
#define Left 0X4b00
#define Right 0X4d00

#define True 1
#define False 0
typedef int bool;

typedef struct _Snake{
    struct _Snake *Next;
    struct _Snake *Old;
    int x;
    int y;
}Snake;

typedef struct _food{
    int x;
    int y;
}food;
enum Direction
{
    DUp=0,
    DDown=1,
    DLeft=2,
    DRight=3
};
/*
    函數功能:畫牆和蛇移動的方格
        參數:  Wall 牆體所用ascii碼
                Cell 方格所用ascii碼
     返回值 :無
*/
void DrawWall(int Wall,int Cell);
/*
    函數功能:初始化蛇的鏈表 及 畫蛇
        參數: *Snake 蛇
     返回值 :成功返回true 不成功返回false
*/
bool InitSnake(const Snake *S);
/*
    函數功能:遊戲過程
        參數:
     返回值 :無
*/
bool Game(Snake *S,int NanDu);
bool AIOne(Snake *S,int Dre,food fd);
bool HitWall(Snake *S);
bool HitSelf(const Snake *S);
food CreateFood(const Snake *S);
Snake *M_Snake;
bool bfood = False;
void main()
{
   int Grade;
   while(True)
   {
       clrscr();
       gotoxy(1,1);
       cprintf("\t\tWelcome Snake\n\n Input Grade (2~10): ");
       scanf("%d",&Grade);
       /*畫牆*/
       DrawWall(177,1);
       /*初始化蛇*/
       if( !InitSnake(M_Snake) )
           return;
       Game(M_Snake,Grade);
       /*背景色*/
       textbackground(0);
       clrscr();
       gotoxy(1,1);
       cprintf("Game Over!!!!\n  Continue?(yes:1 no:2)");
       if( '1' != getch())
        break;
   }

}

bool InitSnake(const Snake *S)
{/*開始時蛇有3個方塊的身體和一個腦袋*/
    Snake *S1;
    int i;

    S->Next = (Snake*)malloc(sizeof(Snake));
    if(S->Next==NULL)
        return False;
    S->Next->x = 29;
    S->Next->y = 9;

    S1 = S->Next;
    for(i=1;i<=3;i++)
    {
       S1->Next = (Snake*)malloc(sizeof(Snake));
       if(S1->Next==NULL)
           return False;
       S1->Next->x = 29-i;
       S1->Next->y = 9;

       S1 = S1->Next;
    }
    S1->Next = NULL;
    /*畫蛇*/
    textcolor(15);

    gotoxy(S->Next->x,S->Next->y);

    cprintf("%c",26);

    S1 = S->Next;

    while(S1->Next!=NULL)
    {
       gotoxy(S1->Next->x,S1->Next->y);
       cprintf("%c",1);
       S1 = S1->Next;
    }

    return True;
}
void DrawWall(int Wall,int Cell)
{
    int i,j;

    textmode(C80);
    textbackground(15);
    clrscr();
    /*畫牆*/
    textcolor(0);
    for(i=1;i<=80;i++)
    {
        gotoxy(i,3);
        cprintf("%c",Wall);
        gotoxy(i,22);
        cprintf("%c",Wall);
    }
    for(j=1;j<=25;j++)
    {
        gotoxy(10,j);
        cprintf("%c",Wall);
        gotoxy(69,j);
        cprintf("%c",Wall);
    }

    /*畫方格*/
    window(11,4,69,21);

    for(i=1;i<=18;i++)
    {
        for(j=1;j<=58;j++)
        {
            gotoxy(j,i);
            textcolor(1);
            cprintf("%c",Cell);
        }
    }
}
bool Game(Snake *S,int NanDu)
{
    int Key,Count=1,Drection = 3;
    food fd;

    fd = CreateFood(S);
    bfood = True;
     while(True)
     { /*如果有鍵按下 則判斷和處理鍵值*/
       if( 0!=bioskey(1) )
       {
           Key = bioskey(0);
           if(ESC == Key)
               exit(0);
           else if(Up == Key)
           {
               if(Drection == DDown)
                   Drection = 1;
               else
                   Drection = 0;
           }
           else if(Down == Key)
           {
               if(Drection == DUp)
                  Drection = 0;
               else
                   Drection = 1;
           }
           else if(Left == Key)
           {
               if(Drection == DRight)
                  Drection = 3;
               else
                   Drection = 2;
           }
           else if(Right == Key)
           {
               if(Drection == DLeft)
                  Drection = 2;
               else
                   Drection = 3;
           }
       }
       else
       {
           Count++;
           if( (Count++) > NanDu)
           { /*走一格*/
                if(!AIOne(S,Drection,fd))
                {
                    return False;
                }
                Count = 1;
                /*判斷有無食物*/
                if(False == bfood)
                {
                    /*生成食物*/
                    fd = CreateFood(S);
                    bfood = True;
                }
           }
       }
       delay(10000);
    }
}
bool AIOne(Snake *S,int Dre,food fd)
{
    enum Direction NowDrection = Dre;

    int Head;
    Snake *S1 = (Snake*)malloc(sizeof(Snake));
    Snake *S2,*S3;
    if(NowDrection == DUp)
    {
        S1->x = S->Next->x;
        S1->y = S->Next->y - 1;
        Head = 24;
    }
    else if(NowDrection == DDown)
    {
        S1->x = S->Next->x;
        S1->y = S->Next->y + 1;
        Head = 25;
    }
    else if(NowDrection == DLeft)
    {
        S1->x = S->Next->x - 1;
        S1->y = S->Next->y ;
        Head = 27;
    }
    else if(NowDrection == DRight)
    {
        S1->x = S->Next->x + 1;
        S1->y = S->Next->y;
        Head = 26;
    }
    /*判斷是否撞牆*/
    if( !HitWall(S1) )
    {
        return False;
    }
    /*替換掉頭 :將頭換成身體 再在前面加個頭*/
    textcolor(15);
    gotoxy(S->Next->x,S->Next->y);
    cprintf("%c",1);

    S1->Next = S->Next;
    S->Next = S1;

    gotoxy(S->Next->x,S->Next->y);
    cprintf("%c",Head);
    /*判斷食物是否被喫掉*/
    if(True == bfood)
    {
        if(S->Next->x == fd.x && S->Next->y == fd.y)
        {  
             bfood = False;
        }
        else
        {
            /*刪除最後一個尾巴 */
            while(S1->Next!=NULL)
            {
               S2 = S1;
               S1 = S1->Next;
            }
            gotoxy(S1->x,S1->y);
            textcolor(1);

            cprintf("%c",1);
            free(S1);
            S2->Next = NULL;
            if( !HitSelf(S) )
            {
                return False;
            }
        }
    }
    return True;
}
bool HitWall(Snake *S)
{
    if(S->x ==0 || S->x ==59 || S->y ==0 || S->y ==19)
        return False;
    return True;
}
bool HitSelf(const Snake *S)
{
    Snake *S1,*S2;

    S1 = S->Next;

    S2 = S1->Next;
    while(S2 != NULL)
    {
        if( (S1->x == S2->x) && (S1->y == S2->y) )
            return False;
        S2 = S2->Next;
    }
    return True;
}
food CreateFood(const Snake *S)
{
    int x=0,y=0,b=0;
   
    food fd;
    Snake *S1;
    S1 = S->Next;
    while(True)
    {
        x = random(58);
        y = random(18);

        if(x!=0 && y!=0)
        {
            while(S1!=NULL)
            {
                if(S1->x==x && S1->y==y)
                {
                    b = 1;
                    break;
                }
                else
                    S1 = S1->Next;
            }
            if(b==0)
                break;
        }
    }

    gotoxy(x,y);
    cprintf("%c",3);

    fd.x = x;
    fd.y = y;

    return fd;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章