yuleol 发表于 2006-7-11 10:24

改进后的贪食蛇

<P>  贪食蛇&lt;2.0&gt;<BR>  游戏中,用四个方向键来控制蛇的运用,空格键暂停,ESC退出!<BR>  吃掉十个食物升一级!~升级同时速度加快!</P>
<P>较前一版本更新:<BR>1.更新了蛇身显示,使之更加美观!<BR>2.食物由原来的一个增加到三个!<BR>3.增加了蛇移动时的加速功能,低等级时,可以使用加速功能,更快的吃到食物!</P>
<P>作者:yuleol E_mail:kuworm@126.com (2006.7.11)<BR><BR>附件里有源码及编译后的程序下载<BR></P>
<P>[attach]9739[/attach]<BR><BR>源码:<BR>#include &lt;graphics.h&gt;<BR>#include &lt;stdlib.h&gt;<BR>#include &lt;stdio.h&gt;<BR>#include &lt;bios.h&gt;<BR>#include &lt;dos.h&gt;<BR>#define SPACE 0x3920<BR>#define UP 0x4800<BR>#define DOWN 0x5000<BR>#define LEFT 0x4b00<BR>#define RIGHT 0x4d00<BR>#define ESC 0x011b<BR>#define LONG 150/*定义最大蛇身长度!*/<BR>#ifndef  CLOCKS_PER_SEC<BR>#define  CLOCKS_PER_SEC 18<BR>#endif<BR>struct  container{<BR>    int left;<BR>    int top;<BR>    int right;<BR>    int bottom;<BR>    int sign; /*记号,0表示空白,1表示墙,2表示食物*/<BR>}lab[22][42];/*定义大容器存储结构*/</P>
<P>struct snakes{<BR>    int i;<BR>    int j;<BR>    int sign;/*0空白,1蛇头,2蛇身*/<BR>}snake[LONG];</P>
<P>int mark,grade,max;<BR>/*初始化图形显示*/<BR>int initialize(void)<BR>{<BR>    int gdriver, gmode,errorcode;<BR>    gdriver=VGA;<BR>    gmode=VGAHI;<BR>    initgraph(&amp;gdriver, &amp;gmode, "");<BR>    errorcode = graphresult();<BR>    if (errorcode != grOk)  /* an error occurred */<BR>   {<BR>      printf("Graphics error: %s\n", grapherrormsg(errorcode));<BR>      printf("Press any key to halt:");<BR>      getch();<BR>      exit(1);             /* return with error code */<BR>   }<BR>   return 0;<BR>}</P>
<P>void my_delay(unsigned long ms)<BR>{ /*延时函数*/<BR>    double start_time = (double)clock() / CLOCKS_PER_SEC * 1000;<BR>    while ( (double)clock() / CLOCKS_PER_SEC * 1000 - start_time &lt; ms ) ;<BR>}</P>
<P>void showcontainer(int i,int j)<BR>{/*显示容器函数*/<BR>    if(lab[i][j].sign!=0 &amp;&amp; lab[i][j].sign!=1 &amp;&amp; lab[i][j].sign!=2)<BR>    {<BR>       setfillstyle(SOLID_FILL,BLUE);<BR>       bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);<BR>    }<BR>    switch(lab[i][j].sign)<BR>    {<BR>        case 0: setfillstyle(SOLID_FILL,WHITE);break;<BR>        case 1: setfillstyle(SOLID_FILL,RED); break;<BR>        case 2: setfillstyle(SOLID_FILL,RED);break;<BR>        case 3: setfillstyle(SOLID_FILL,GREEN);break;<BR>        case 4: setfillstyle(SOLID_FILL,CYAN);break;<BR>        case 5: setfillstyle(SOLID_FILL,MAGENTA);break;<BR>        case 6: setfillstyle(SOLID_FILL,BROWN);break;<BR>        case 7: setfillstyle(SOLID_FILL,LIGHTGRAY);break;<BR>        case 8: setfillstyle(SOLID_FILL,DARKGRAY);break;<BR>        case 9: setfillstyle(SOLID_FILL,LIGHTBLUE);break;<BR>        case 10: setfillstyle(SOLID_FILL,LIGHTGREEN);break;<BR>        case 11: setfillstyle(SOLID_FILL,LIGHTCYAN);break;<BR>        case 12: setfillstyle(SOLID_FILL,LIGHTRED);break;<BR>        case 13: setfillstyle(SOLID_FILL,LIGHTMAGENTA);break;<BR>        case 14: setfillstyle(SOLID_FILL,YELLOW);break;<BR>    }<BR>    if(lab[i][j].sign!=0 &amp;&amp; lab[i][j].sign!=1 &amp;&amp; lab[i][j].sign!=2)<BR>        bar(lab[i][j].left+2,lab[i][j].top+2,lab[i][j].right-2,lab[i][j].bottom-2);<BR>    else<BR>        bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);<BR>}</P>
<P>/*生成容器函数*/<BR>void initialcontainer()<BR>{<BR>    int i,j,leftx=100,topy=50,rightx=110,bottomy=60;<BR>    for(i=0;i&lt;22;i++)<BR>      for(j=0;j&lt;42;j++)<BR>      {<BR>        lab[i][j].left=leftx+j*10;<BR>        lab[i][j].top=topy+i*10;<BR>        lab[i][j].right=rightx+j*10;<BR>        lab[i][j].bottom=bottomy+i*10;<BR>        lab[i][j].sign=0;<BR>       }<BR>    for(i=0;i&lt;22;i++)<BR>    {<BR>        lab[i][0].sign=1;<BR>        lab[i][41].sign=1;<BR>    }<BR>    for(j=0;j&lt;42;j++)<BR>    {<BR>        lab[0][j].sign=1;<BR>        lab[21][j].sign=1;<BR>    }<BR>    for(i=0;i&lt;22;i++)<BR>      for(j=0;j&lt;42;j++)<BR>        showcontainer(i,j);<BR>}</P>
<P>void initialsnake()<BR>{/*初始化蛇身*/<BR>    int i;<BR>    for(i=3;i&lt;LONG;i++)<BR>      snake[i].sign=0;<BR>    snake[0].i=10;<BR>    snake[0].j=3;<BR>    snake[0].sign=2; /*蛇头*/</P>
<P>    snake[1].i=10;  /*蛇身*/<BR>    snake[1].j=2;<BR>    snake[1].sign=3;</P>
<P>    snake[2].i=10;<BR>    snake[2].j=1;<BR>    snake[2].sign=4;<BR>}</P>
<P>void showsnake()<BR>{/*显示蛇*/<BR>    int n,i,j;<BR>    for(n=0;n&lt;LONG &amp;&amp; snake[n].sign!=0;n++)<BR>    {<BR>        i=snake[n].i;<BR>        j=snake[n].j;<BR>        lab[i][j].sign=snake[n].sign;<BR>        showcontainer(i,j);<BR>    }<BR>}</P>
<P>void cleansnake()<BR>{/*擦除蛇*/<BR>    int n,i,j;<BR>    for(n=0;n&lt;LONG &amp;&amp; snake[n].sign!=0;n++)<BR>    {<BR>        i=snake[n].i;<BR>        j=snake[n].j;<BR>        lab[i][j].sign=0;<BR>        showcontainer(i,j);<BR>    }<BR>}</P>
<P>void createfood()<BR>{/*创建食物*/<BR>    int n,i,j,flag;<BR>    srand((int)time(0));<BR>    do{<BR>        flag=1;<BR>        i=rand()%20+1;<BR>        j=rand()%40+1;<BR>        if(lab[i][j].sign!=0) flag=0;<BR>    }while(flag==0);<BR>    lab[i][j].sign=rand()%12+3;<BR>    showcontainer(i,j);<BR>}<BR>void showmark()<BR>{/*显示分数和等及*/<BR>    char b[10],*p=b;<BR>    p=itoa(mark,p,10);<BR>    setfillstyle(SOLID_FILL,BLUE);<BR>    bar(200,20, 400, 40);<BR>    moveto(210,25);<BR>    outtext("score:");<BR>    outtext(p);<BR>    p=itoa(grade,p,10);<BR>    outtext("  grade:");<BR>    outtext(p);<BR>}<BR>int run(int i,int j)<BR>{<BR>    int n;<BR>    if(i&lt;1 || i&gt;20 || j&lt;1 || j&gt;40)<BR>        return 0;/*碰壁*/<BR>    for(n=0;n&lt;LONG &amp;&amp; snake[n].sign!=0;n++)<BR>        if(snake[n].i==i &amp;&amp; snake[n].j==j)<BR>          return 0;/*碰到自己*/<BR>    if(lab[i][j].sign!=0 &amp;&amp; lab[i][j].sign!=1 &amp;&amp; lab[i][j].sign!=2)<BR>    {/*遇到食物*/<BR>        snake[max].sign=lab[i][j].sign;<BR>        max++;<BR>        lab[i][j].sign=0;<BR>        mark=mark+10*grade;<BR>        if((max-3)%10==0 &amp;&amp; grade&lt;10)   grade++;<BR>        showmark();<BR>        createfood();<BR>    }<BR>    cleansnake();<BR>    for(n=max;n&gt;=1;n--)<BR>    {<BR>        snake[n].i=snake[n-1].i;<BR>        snake[n].j=snake[n-1].j;<BR>    }<BR>    snake[0].i=i;<BR>    snake[0].j=j;<BR>    showsnake();<BR>    return 1;<BR>}</P>
<P>void pause()<BR>{/*暂停*/<BR>    setfillstyle(SOLID_FILL,BLUE);<BR>    bar(120,300, 480, 350);<BR>    moveto(130,320);<BR>    outtext("Press any key to start...");<BR>    while(!bioskey(0)) sleep(1);<BR>    bar(120,300, 480, 350);<BR>    moveto(130,320);<BR>    outtext("Press SPACE to pause,ESC to exit...");<BR>}</P>
<P>int main(void)<BR>{<BR>    int i=10,j=3,flag=1,n,m;<BR>    int key=RIGHT,keytemp,keyreve=LEFT;<BR>    mark=0;<BR>    grade=1;<BR>    max=3;<BR>    initialize();/*初始化图形显示*/<BR>    setbkcolor(LIGHTBLUE);<BR>    setcolor(RED);<BR>    initialcontainer();<BR>    initialsnake();<BR>    showsnake();<BR>    createfood();<BR>    createfood();<BR>    createfood();<BR>    showmark();<BR>    pause();<BR>    while(flag==1)<BR>    {<BR>        if(bioskey(1))<BR>            keytemp=bioskey(0);<BR>        if(keytemp==ESC)<BR>          break;<BR>        if(keytemp==SPACE)<BR>        {<BR>            pause();<BR>            keytemp=key;<BR>        }<BR>        if((keytemp==UP || keytemp==DOWN || keytemp==RIGHT || keytemp==LEFT)&amp;&amp;keytemp!=keyreve)<BR>            key=keytemp;<BR>        switch(key)<BR>        {<BR>            case UP: i--;keyreve=DOWN;break;<BR>            case DOWN: i++;keyreve=UP;break;<BR>            case RIGHT: j++;keyreve=LEFT;break;<BR>            case LEFT:  j--;keyreve=RIGHT;break;<BR>        }<BR>        flag=run(i,j);<BR>        keytemp=0;<BR>        for(m=11-grade;m&gt;=0 &amp;&amp; flag==1;m--)/*加速及延时*/<BR>        {<BR>           if(bioskey(1))<BR>             keytemp=bioskey(0);<BR>           if(keytemp==key)<BR>           {<BR>            switch(key)<BR>            {<BR>            case UP: i--;break;<BR>            case DOWN: i++;break;<BR>            case RIGHT: j++;break;<BR>            case LEFT:  j--;break;<BR>            }<BR>            flag=run(i,j);<BR>            my_delay(20);<BR>            keytemp=0;<BR>           }<BR>           else<BR>            my_delay(4);<BR>        }<BR>    }<BR>    setfillstyle(SOLID_FILL,BLUE);<BR>    bar(120,300, 480, 350);<BR>    moveto(130,310);<BR>    outtext("GAME OVER!");<BR>    moveto(130,330);<BR>    outtext("Press any key to exit...");<BR>    sleep(2);<BR>    fflush(stdin);<BR>    getch();<BR>    closegraph();/*关闭图形显示*/<BR>    return 0;<BR>}<BR><BR></P>

jig 发表于 2006-7-11 11:48

不错,UP~~!!!

hyerty 发表于 2006-7-15 06:06

蛇身其实用单一颜色显示就不错了啊

yuleol 发表于 2006-7-15 12:47

其实我想用链表来存储蛇身,蛇身当中有三个或以上的相同颜色的食物就消掉~~~,让蛇身减短~<BR><BR>这个功能正在开发当中~~

aton 发表于 2006-7-15 21:20

<P>第一贴就给楼主了。。第一次接触。<BR><BR></P>

页: [1]

编程论坛