c语言小游戏(贪食蛇)

类别:编程语言 点击:0 评论:0 推荐:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <graphics.h>
int SCR[10][15];
typedef struct QNode
{int a,b;
 struct QNode *next;
}QNode,*QueuePtr;

typedef struct
{QueuePtr front;
 QueuePtr rear;
}LinkQueue;

LinkQueue *Q;
QNode *p;

void InitQueue()
{Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
 if(!Q->front) exit(0);
 Q->front->next=NULL;
}

void EnQueue(int x,int y)
{p=(QueuePtr)malloc(sizeof(QNode));
 if(!p) exit(0);
 p->a=x;
 p->b=y;
 p->next=NULL;
 Q->rear->next=p;
 Q->rear=p;
}

int DeQueue()
{int e;
 while(Q->front!=Q->rear)
 {p=Q->front->next;
  e=p->a;
  Q->front->next=p->next;
  if(Q->rear==p) Q->rear=Q->front;
  free(p);
  return e;
 }
 if(Q->front==Q->rear) printf("The Queue is empty!\n");
}

main()
{int score=0,i,j,RADx,RADy,key,x=100,y=100,X=100,Y=100,gd=DETECT,gm;
 randomize();
 InitQueue();
 initgraph(&gd,&gm,"");
 for(i=0;i<10;i++)
 for(j=0;j<15;j++)
 SCR[i][j]=0;
 rectangle(X-1,Y-1,X+301,Y+201);
 setfillstyle(1,GREEN);
 bar(x,y,x+18,y+18);
 EnQueue(x,y);
 SCR[0][0]=1;
 settextstyle(0,0,2);
 outtextxy(10,10,"WELCOME TO PLAY!");
 outtextxy(10,400,"exit->Esc");
 outtextxy(450,100,"SCORE:");
 do{RADx=random(200)+100;
    RADy=random(100)+100;
   }while((RADx%20)!=0||(RADy%20)!=0);
 bar(RADx,RADy,RADx+18,RADy+18);
 for(;;)
   {key=getch();
    setfillstyle(1,BLACK);
    bar(x,y,x+18,y+18);
    while(key==77&&Q->rear->a<X+280)
    {x=Q->rear->a;
     y=Q->rear->b;
     x+=20;
     setfillstyle(1,GREEN);
     bar(x,y,x+18,y+18);
     EnQueue(x,y);
     if(x!=RADx||y!=RADy)
    {x=Q->front->next->a;
     y=Q->front->next->b;
     setfillstyle(1,BLACK);
     bar(x,y,x+18,y+18);
     DeQueue();
     }
     else
     {do{RADx=random(200)+100;
 RADy=random(100)+100;
       }while((RADx%20)!=0||(RADy%20)!=0||SCR[(RADx-100)/20][(RADy-100)/20]==1);
      bar(RADx,RADy,RADx+18,RADy+18);
      SCR[(RADx-100)/20][(RADy-100)/20]=1;
      score+=100;
     }
     delay(1000000000);
     if(kbhit())
     {key=getch();
      if(key==80||key==72) break;
     }
    }
    while(key==80&&Q->rear->b<Y+180)
    {x=Q->rear->a;
     y=Q->rear->b;
     y+=20;
     setfillstyle(1,GREEN);
     bar(x,y,x+18,y+18);
     EnQueue(x,y);
     if(x!=RADx||y!=RADy)
    {x=Q->front->next->a;
     y=Q->front->next->b;
     setfillstyle(1,BLACK);
     bar(x,y,x+18,y+18);
     DeQueue();
     }
     else
     {do{RADx=random(200)+100;
 RADy=random(100)+100;
       }while((RADx%20)!=0||(RADy%20)!=0||SCR[(RADx-100)/20][(RADy-100)/20]==1);
      bar(RADx,RADy,RADx+18,RADy+18);
      SCR[(RADx-100)/20][(RADy-100)/20]=1;
     }
     delay(1000000000);
     if(kbhit())
     {key=getch();
      if(key==77||key==75) break;
     }
    }
    while(key==75&&Q->rear->a>X)
    {x=Q->rear->a;
     y=Q->rear->b;
     x-=20;
     setfillstyle(1,GREEN);
     bar(x,y,x+18,y+18);
     EnQueue(x,y);
     if(x!=RADx||y!=RADy)
    {x=Q->front->next->a;
     y=Q->front->next->b;
     setfillstyle(1,BLACK);
     bar(x,y,x+18,y+18);
     DeQueue();
     }
     else
     {do{RADx=random(200)+100;
 RADy=random(100)+100;
       }while((RADx%20)!=0||(RADy%20)!=0||SCR[(RADx-100)/20][(RADy-100)/20]==1);
      bar(RADx,RADy,RADx+18,RADy+18);
      SCR[(RADx-100)/20][(RADy-100)/20]=1;
     }
     delay(1000000000);
     if(kbhit())
     {key=getch();
      if(key==80||key==72) break;
     }
   }
   while(key==72&&Q->rear->b>Y)
    {x=Q->rear->a;
     y=Q->rear->b;
     y-=20;
     setfillstyle(1,GREEN);
     bar(x,y,x+18,y+18);
     EnQueue(x,y);
     if(x!=RADx||y!=RADy)
    {x=Q->front->next->a;
     y=Q->front->next->b;
     setfillstyle(1,BLACK);
     bar(x,y,x+18,y+18);
     DeQueue();
     }
     else
     {do{RADx=random(200)+100;
 RADy=random(100)+100;
       }while((RADx%20)!=0||(RADy%20)!=0||SCR[(RADx-100)/20][(RADy-100)/20]==1);
      bar(RADx,RADy,RADx+18,RADy+18);
      SCR[(RADx-100)/20][(RADy-100)/20]=1;
     }
     delay(1000000000);
     if(kbhit())
     {key=getch();
      if(key==77||key==75) break;
     }
    }
    if(key==27) break;
   }
 closegraph();
}


本文地址:http://com.8s8s.com/it/it22431.htm