⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 贪吃蛇游戏.txt

📁 爱因斯坦的思考题 二叉树算法集 分解质因数新解 石子归并问题等有趣的C程序
💻 TXT
字号:
 


/*运行是按任意键开始,不过呢,反向也算输哟!QQ 421077892 mickey*/

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


#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x011b
#define SPACE 0x3920
#define M 25
#define N 25
#define blue   1
#define green  2
#define red    4
#define yellow 14
#define yes    1
#define no     0

struct snake
{ int l,t,r,b;
  int color;
  int direction;              /*0不动 1 上 2下 3左 4右 蛇头的方向     */
}qu1[M*N];

int d[4]={-25,25,-1,1};     /*蛇头移动的方向  */
int length;              /*蛇身长度        */
int speed=50000000;          /*移动的速度      */
int head;            /*蛇头的位置      */
int food;
int appear;
int key;
int direct;
int all_food;
int tail,mark;
int Q=1;
void draw_map()         /*地图            */
{int i,j,n=17,v=1;
 int a[25][25];
 for(i=0;i<=24;i++)
     for(j=0;j<=24;j++)
        {if(j==0||j==24||i==24||i==0)
    a[i][j]=0;                 /* 0 stans for  block */
         else
     a[i][j]=1;
        }
 for(i=0;i<=24;i++)
     { for(j=0;j<=24;j++)
   { if(a[i][j]==0)
              { setfillstyle(1,green);
                qu1[v].color=green;
              }
     if(a[i][j]==1)
     {
                 setfillstyle(1,blue);
                 qu1[v].color=blue;
              }
           qu1[v].l=20+n*j;  qu1[v].t=20+n*i;
           qu1[v].r=35+n*j;  qu1[v].b=35+n*i;
           qu1[v].direction=0;
           bar( qu1[v].l, qu1[v].t, qu1[v].r, qu1[v].b);
           v++;
          }
      }

}

void draw_snake()
{ int v,j;
  v=head=303;
  all_food=10;
  length=0;
  for(j=0;j<=1;j++,v--)
     { setfillstyle(1,yellow);
       bar( qu1[v].l, qu1[v].t, qu1[v].r, qu1[v].b);
       qu1[v].color=red;
       qu1[v].direction=4;
       length++;
     }
}

void diamond()
{
 float t;
 int x0=540,y0=165;
 int n=16,i,j,r=90;
 int x[50],y[50];
 t=6.28318/n;
        for(i=0;i<n;i++)
 {
    x[i]=x0+r*cos(i*t);
           y[i]=y0+r*sin(i*t);
 }
 setcolor(yellow);
        for(i=0;i<=n-2;i++)
 {  setcolor(YELLOW);
           for(j=1;j<=n-1;j++)
              line(x[i],y[i],x[j],y[j]);
 }
 
} 


main()
{int gdrive=VGA;                      
 int gmode=VGAHI;
 int class;
 initgraph(&gdrive,&gmode,"c:\\tc");  
 setcolor(LIGHTRED);
 outtextxy(450,280,"SPACE :Pause"); 
 outtextxy(450,300,"ESC   :Exit"); 
 outtextxy(450,320,"Left"); 
 outtextxy(450,340,"Right"); 
 outtextxy(450,360,"Up  "); 
 outtextxy(450,380,"Down "); 
 outtextxy(450,400,"THE FOUR KEYES TO MOVE"); 
 setcolor(YELLOW);
 outtextxy(40,2," Welcome to snake game and Please any key begin !"); 
 outtextxy(450,33,"Class:"); 
 outtextxy(450,51,"All_food:");
 randomize();
 diamond();
 for(class=1;class<=3;class++)
    {draw_map();  
     draw_snake();
     appear=no;      /*没有食物*/
     getch();
     while(bioskey(1)!=0) {key=0; getch();}  
     while(1)
        { gotoxy(63,3);
          printf("%d",class);
          gotoxy(66,4);
          if(all_food>=10)
            printf("%d",all_food);
          else
            printf("0%d",all_food);
          if(all_food==0)
            { setcolor(red+class);
              outtextxy(50,450, " Congratulation and Please any key goto the next!  ");
              break;
            }
         if(appear==no)
           {do
             {food=random(600);
             }while(qu1[food].color==yellow||qu1[food].color==red||qu1[food].color==green||food==0);
            qu1[food].color=red;
            qu1[food].direction=0;
            setfillstyle(1,red);             
            bar(qu1[food].l, qu1[food].t, qu1[food].r, qu1[food].b);  
            appear=yes;   
           }
star:    do
          { tail=head;               
            mark=length;
            switch(qu1[head].direction)   
                   { case 1:   head=head+d[0]; 
                               direct=1;
                               break;
                     case 2:   head=head+d[1];
                               direct=2;
                               break;
                     case 3:   head=head+d[2];
                               direct=3;
                               break;        
                     case 4:   head=head+d[3];
                               direct=4;
                   }
            if(qu1[head].direction!=0||qu1[head].color==green) /*撞墙或撞自己*/
              { 
                if(Q==1)
               {
                setcolor(red);
                outtextxy(450,200, " Any key to exit !  ");           
                getch();closegraph();  exit(0);           
               }
               else
                  Q=1;
              }         
              qu1[head].direction=direct;
            if(head==food)
              { appear=no;
                delay(speed);
                all_food--;
                length++;
                continue;
              }   
            qu1[head].color=yellow;
            setfillstyle(1,yellow);             
            bar(qu1[head].l, qu1[head].t, qu1[head].r, qu1[head].b);  
            setfillstyle(1,red);             
            bar( qu1[tail].l, qu1[tail].t, qu1[tail].r, qu1[tail].b);
            for(;length>1;length--)                
               tail=tail-d[qu1[tail].direction-1];
            setfillstyle(1,blue);             
            bar( qu1[tail].l, qu1[tail].t, qu1[tail].r, qu1[tail].b);               
            qu1[tail].color=blue;
            qu1[tail].direction=0;
            length=mark;
            delay(speed);
       } while(!kbhit());              
       key=bioskey(0);    
       if(qu1[head].direction==1)        
          if(key==UP)
             {
              Q=0;
              continue;
             }  
        if(qu1[head].direction==2)
          if(key==DOWN)
             {
              Q=0;
              continue;
             }         
        if(qu1[head].direction==3)
          if(key==LEFT)
             {
              Q=0;
              continue;
             }          
        if(qu1[head].direction==4)
          if(key==RIGHT)
             {
              Q=0;
              continue;
             }   
        tail=head;               
        mark=length;
        switch(key)   
                  { case   UP :    head=head+d[0]; 
                                   direct=1;
                                   break;
                    case DOWN :    head=head+d[1];
                                   direct=2;
                                   break;
                    case LEFT :    head=head+d[2];
                                   direct=3;
                                   break;        
                    case RIGHT:    head=head+d[3];
                                   direct=4;
                                   break;
                    case ESC  :    exit(0);
                    case SPACE:    getch(); goto star;                /* 暂停*/
                   }
              
       if(qu1[head].direction!=0||qu1[head].color==green) /*撞墙或撞自己*/
              {  
                if(Q==1)
               {
                setcolor(red);
                outtextxy(450,200, " Any key to exit !  ");           
                getch();closegraph();  exit(0);           
               }
               else
                  Q=1;    
              }         
              qu1[head].direction=direct;
            if(head==food)
              { appear=no;
                delay(speed);
                all_food--;
                length++;
                continue;
              }   
            qu1[head].color=yellow;
            setfillstyle(1,yellow);             
            bar(qu1[head].l, qu1[head].t, qu1[head].r, qu1[head].b);  
            setfillstyle(1,red);             
            bar( qu1[tail].l, qu1[tail].t, qu1[tail].r, qu1[tail].b);
            for(;length>1;length--)
               tail=tail-d[qu1[tail].direction-1];
            setfillstyle(1,blue);             
            bar( qu1[tail].l, qu1[tail].t, qu1[tail].r, qu1[tail].b);               
            qu1[tail].color=blue;
            qu1[tail].direction=0;
            length=mark;
            delay(speed);        
         }                    
   speed=speed-15000;
  }
  setcolor(LIGHTRED);
  outtextxy(200,460," You are wining the game ."); 
  getch();
  closegraph();
}

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -