论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

贪吃蛇,还在完善中

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:46:47

/*  我的想法是,在各个身体部位的运动当中,后一个方块的下一个位置坐标,总是前一个方块的x1,y1,
    也就是说后面的方块总是按前面的方块的轨迹运动 .
    而且第一个方块是不能回头的,所以用到if((a[0].x0!=(a[1].x0-dx))).......这一句。

    我也想编类似贪吃蛇的游戏.....别人写的没有没有注释,看不太懂..所以我先依自已的想法,写了
    一个小实验品,写得差不多了,但是很多细节处理不好!!如:我没有写假如蛇碰到自已会死;还有
    就是食物随机出现的位置有可能出现在身体正在的位置,这样的话就看不见它了。。等等。
*/

#include"stdio.h"
#include"graphics.h"
#include"stdlib.h"

#define RIGHT 0x4d00
#define LEFT 0x4b00
#define UP 0x4800
#define DOWN 0x5000
#define ESC 0x011b

int dx;                             /*移动时X轴身体变化距离*/
int dy;                             /*移动时Y轴身体变化距离*/
int N=4;                            /*身体节数*/


/*存放身体每一部份的信息*/
struct body
{
   int x0,y0;                     /*每个方块的左上角的坐标,当物体移动时它最先改变*/
   int x1,y1;                     /*每个块的左上角坐标,但主要是为了给它后面的方块指定他下一位置的坐标*/
}a[80]={{300,100,300,100},
        {290,100,290,100},        /*定义了四块身体*/
        {280,100,280,100},
        {270,100,270,100}
       };

/*存放每一块食物的信息*/
struct boX
{
   int x;                       /*食物左上角横坐标*/
   int y;                       /*食物左上角纵坐标*/
   int live;                    /*食物是否存在,1:存在,0:不存在*/
}box;                           /*定义一个食物变量*/

/*初始化图形*/
void initgr()
{
  int gd=DETECT,gm;
  initgraph(&gd,&gm,"");
  setcolor(YELLOW);
  setlinestyle(0,0,3);
  rectangle(200,150,440,200);
  setcolor(7);
  settextstyle(0,0,4);
  setcolor(9);
  outtextxy(240,160,"SNAKE");
  settextstyle(0,0,1);
  setcolor(7);
  outtextxy(280,220,"By UFOZH");
  outtextxy(230,240,"press any key to continue...");
  getch();
  cleardevice();
  setfillstyle(1,BLUE);
  bar(150,80,450,380);
}


/*****************************************************************************/
main()
{
    int n,i;
    int key;                                                  /*存放按键值*/
    dx=10;                                                    /*每个方块每次移动长度*/
    dy=10;

    initgr();

    setlinestyle(0,0,1);
    setcolor(WHITE);
    randomize();                                              /*游戏开始前画出第一个要吃掉的方块*/
    box.x=300+10*(rand()%14+1);
    box.y=100+10*(rand()%26+1);
    box.live=1;
    rectangle(box.x,box.y,box.x+10,box.y+10);

/*默认刚开始条形是向右移动*/
    while(!kbhit())
    {
       a[0].x0=a[0].x0+dx;
       for(i=1;i<N;i++)
       {
          a[i].x0=a[i-1].x1;
          a[i].y0=a[i-1].y1;
       }
       setcolor(BLUE);
       for(n=0;n<4;n++)
           rectangle(a[n].x1,a[n].y1,a[n].x1+dx,a[n].y1+dy);
       setcolor(WHITE);
       delay(3000);
       for(n=0;n<4;n++)
           rectangle(a[n].x0,a[n].y0,a[n].x0+dx,a[n].y0+dy);
       for(i=0;i<N;i++)
       {
          a[i].x1=a[i].x0;
          a[i].y1=a[i].y0;
       }
       delay(50000);
    }

       key=bioskey(0);
       while(1)
       {
        switch(key)
        {
           case RIGHT:if((a[0].x0!=(a[1].x0-dx)))           /*当第一个方块不是在第二个的正左边时,条形向右移,否则执行case LEFT*/
                      {
                        while(!kbhit())
                        {
                            if(a[0].x0<150||a[0].x0>450||a[0].y0<80||a[0].y0>380)   /*当第一个方块出个任四个界时,退出*/
                                 exit(0);

                            if(box.live==0)                                      /*当食物不存在时,随机位置增加一食物*/
                            {
                                box.x=10*(rand()%30+15);
                                box.y=10*(rand()%30+8);
                                rectangle(box.x,box.y,box.x+10,box.y+10);
                                box.live=1;                                      /*食物变为存在*/
                            }

                            /*当蛇头在食物的正左方且食物存在时,食物被吃掉*/
                            if(((a[0].x0==(box.x-10))&&(a[0].y0==box.y))&&(box.live==1))
                            {
                                for(i=N;i>=1;i--)                               /*把现有的身体统一向后移一个单位*/
                                {
                                    a[i].x0=a[i-1].x0;
                                    a[i].x1=a[i-1].x1;
                                    a[i].y0=a[i-1].y0;
                                    a[i].y1=a[i-1].y1;
                                }
                                a[0].x0=box.x;                                  /*把食物当做身体加入蛇头*/
                                a[0].y0=box.y;
                                a[0].x1=a[0].x0;
                                a[0].y1=a[0].y0;
                                N++;                                     /*身体节数加1*/
                                box.live=0;                              /*食物消失*/
                            }


                            a[0].x0+=dx;
                            for(i=1;i<N;i++)
                            {
                               a[i].x0=a[i-1].x1;
                               a[i].y0=a[i-1].y1;
                            }
                            setcolor(BLUE);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x1,a[n].y1,a[n].x1+dx,a[n].y1+dy);
                            setcolor(WHITE);
                            delay(3000);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x0,a[n].y0,a[n].x0+dx,a[n].y0+dy);
                            for(i=0;i<N;i++)
                            {
                               a[i].x1=a[i].x0;
                               a[i].y1=a[i].y0;
                            }
                            delay(50000);
                        }
                            key=bioskey(0);
                            break;
                      }
           case LEFT:if((a[0].x0!=(a[1].x0+dx)))           /*当第一个方块不是在第二个的正右边时,条形向左移*/
                     {
                        while(!kbhit())
                        {
                            if(a[0].x0<150||a[0].x0>450||a[0].y0<80||a[0].y0>380)
                                 exit(0);

                            if(box.live==0)
                            {
                                box.x=10*(rand()%30+15);
                                box.y=10*(rand()%30+8);
                                rectangle(box.x,box.y,box.x+10,box.y+10);
                                box.live=1;
                            }

                            if(((a[0].x0==(box.x+10))&&(a[0].y0==box.y))&&(box.live==1))
                            {
                                for(i=N;i>=1;i--)
                                {
                                    a[i].x0=a[i-1].x0;
                                    a[i].x1=a[i-1].x1;
                                    a[i].y0=a[i-1].y0;
                                    a[i].y1=a[i-1].y1;
                                }
                                a[0].x0=box.x;
                                a[0].y0=box.y;
                                a[0].x1=a[0].x0;
                                a[0].y1=a[0].y0;
                                N++;
                                box.live=0;
                            }


                            a[0].x0-=dx;
                            for(i=1;i<N;i++)
                            {
                               a[i].x0=a[i-1].x1;
                               a[i].y0=a[i-1].y1;
                            }
                            setcolor(BLUE);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x1,a[n].y1,a[n].x1+dx,a[n].y1+dy);
                            setcolor(WHITE);
                            delay(3000);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x0,a[n].y0,a[n].x0+dx,a[n].y0+dy);
                            for(i=0;i<N;i++)
                            {
                               a[i].x1=a[i].x0;
                               a[i].y1=a[i].y0;
                            }
                            delay(50000);
                        }
                            key=bioskey(0);
                            break;
                     }

                     key=RIGHT;                           /*否则执行case RIGHT*/
                     break;

           case UP:if((a[0].y0!=(a[1].y0+dx)))           /*当第一个方块不是在第二个的正下方时,条形向上移,否则执行case DOWN*/
                   {
                        while(!kbhit())
                        {
                            if(a[0].x0<150||a[0].x0>450||a[0].y0<80||a[0].y0>380)
                                 exit(0);

                            if(box.live==0)
                            {
                                box.x=10*(rand()%30+15);
                                box.y=10*(rand()%30+8);
                                rectangle(box.x,box.y,box.x+10,box.y+10);
                                box.live=1;
                            }

                            if(((a[0].y0==(box.y+10))&&(a[0].x0==box.x))&&(box.live==1))
                            {
                                for(i=N;i>=1;i--)
                                {
                                    a[i].x0=a[i-1].x0;
                                    a[i].x1=a[i-1].x1;
                                    a[i].y0=a[i-1].y0;
                                    a[i].y1=a[i-1].y1;
                                }
                                a[0].x0=box.x;
                                a[0].y0=box.y;
                                a[0].x1=a[0].x0;
                                a[0].y1=a[0].y0;
                                N++;
                                b ox.live=0;
                            }


                            a[0].y0-=dy;
                            for(i=1;i<N;i++)
                            {
                               a[i].x0=a[i-1].x1;
                               a[i].y0=a[i-1].y1;
                            }
                            setcolor(BLUE);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x1,a[n].y1,a[n].x1+dx,a[n].y1+dy);
                            setcolor(WHITE);
                            delay(3000);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x0,a[n].y0,a[n].x0+dx,a[n].y0+dy);
                            for(i=0;i<N;i++)
                            {
                               a[i].x1=a[i].x0;
                               a[i].y1=a[i].y0;
                            }
                            delay(50000);
                        }
                        key=bioskey(0);
                        break;
                   }
           case DOWN:if((a[0].y0!=(a[1].y0-dx)))           /*当第一个方块不是在第二个的正上方时,条形向下移*/
                   {
                        while(!kbhit())
                        {
                            if(a[0].x0<150||a[0].x0>450||a[0].y0<80||a[0].y0>380)
                                 exit(0);

                            if(box.live==0)
                            {
                                box.x=10*(rand()%30+15);
                                box.y=10*(rand()%30+8);
                                rectangle(box.x,box.y,box.x+10,box.y+10);
                                box.live=1;
                            }

                            if(((a[0].y0==(box.y-10))&&(a[0].x0==box.x))&&(box.live==1))
                            {
                                for(i=N;i>=1;i--)
                                {
                                    a[i].x0=a[i-1].x0;
                                    a[i].x1=a[i-1].x1;
                                    a[i].y0=a[i-1].y0;
                                    a[i].y1=a[i-1].y1;
                                }
                                a[0].x0=box.x;
                                a[0].y0=box.y;
                                a[0].x1=a[0].x0;
                                a[0].y1=a[0].y0;
                                N++;
                                box.live=0;
                            }


                            a[0].y0+=dy;
                            for(i=1;i<N;i++)
                            {
                               a[i].x0=a[i-1].x1;
                               a[i].y0=a[i-1].y1;
                            }
                            setcolor(BLUE);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x1,a[n].y1,a[n].x1+dx,a[n].y1+dy);
                            setcolor(WHITE);
                            delay(3000);
                            for(n=0;n<N;n++)
                                rectangle(a[n].x0,a[n].y0,a[n].x0+dx,a[n].y0+dy);
                            for(i=0;i<N;i++)
                            {
                               a[i].x1=a[i].x0;
                               a[i].y1=a[i].y0;
                            }
                            delay(50000);
                        }
                        key=bioskey(0);
                        break;
                   }
                   key=UP;                 /*否则执行case UP*/
                   break;
           case ESC:exit(0);               /*按ESC键退出游戏*/
           default:exit(0);                /*按其它键也退出游戏*/
        }
       }
    closegraph();
}

上一篇:{应用}井字棋游戏.不够完善 人气:6324
下一篇:{应用}安全过河问题 人气:4926
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058