c語言貪吃蛇游戲源碼_第1頁
c語言貪吃蛇游戲源碼_第2頁
c語言貪吃蛇游戲源碼_第3頁
c語言貪吃蛇游戲源碼_第4頁
c語言貪吃蛇游戲源碼_第5頁
已閱讀5頁,還剩2頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、/*運行是按任意鍵開始,不過呢,反向也算輸喲*/#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 0

2、x3920#define M 25#define N 25#define blue   1#define green  2#define red    4#define yellow 14#define yes    1#define no     0struct snake int l,t,r,b;  int color;  int direction;       

3、0;      /*0不動 1 上 2下 3左 4右 蛇頭的方向     */qu1M*N;int d4=-25,25,-1,1;     /*蛇頭移動的方向  */int length;              /*蛇身長度        */int sp

4、eed=50000000;          /*移動的速度      */int head;            /*蛇頭的位置      */int food;int appear;int key;int direct;int all_food;int tail,mark;int Q=1;vo

5、id draw_map()         /*地圖            */int i,j,n=17,v=1; int a2525; for(i=0;i<=24;i+)     for(j=0;j<=24;j+)        if(j=0|j=24|i=24|i

6、=0)    aij=0;                 /* 0 stans for  block */         else     aij=1;         for(i=0;i<

7、=24;i+)     for(j=0;j<=24;j+)   if(aij=0)              setfillstyle(1,green);                qu1v.color=green;   

8、;                if(aij=1)                      setfillstyle(1,blue);          &

9、#160;      qu1v.color=blue;                         qu1v.l=20+n*j;  qu1v.t=20+n*i;           qu1v.r=35+n*

10、j;  qu1v.b=35+n*i;           qu1v.direction=0;           bar( qu1v.l, qu1v.t, qu1v.r, qu1v.b);           v+;      &

11、#160;         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( qu1v.l, qu1v.t, qu1v.r, qu1v.b);   

12、60;   qu1v.color=red;       qu1v.direction=4;       length+;     void diamond() float t; int x0=540,y0=165; int n=16,i,j,r=90; int x50,y50; t=6.28318/n;     

13、60;  for(i=0;i<n;i+)     xi=x0+r*cos(i*t);           yi=y0+r*sin(i*t);  setcolor(yellow);        for(i=0;i<=n-2;i+)   setcolor(YELLOW);     

14、;      for(j=1;j<=n-1;j+)              line(xi,yi,xj,yj);   main()int gdrive=VGA;                  

15、60;    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"); 

16、60;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 !")

17、;  outtextxy(450,33,"Class:");  outtextxy(450,51,"All_food:"); randomize(); diamond(); for(class=1;class<=3;class+)    draw_map();       draw_snake();     appear=no;    

18、60; /*沒有食物*/     getch();     while(bioskey(1)!=0) key=0; getch();       while(1)        gotoxy(63,3);          printf("%d",class);  &#

19、160;       gotoxy(66,4);          if(all_food>=10)            printf("%d",all_food);          else  &#

20、160;         printf("0%d",all_food);          if(all_food=0)            setcolor(red+class);         

21、0;    outtextxy(50,450, " Congratulation and Please any key goto the next!  ");              break;                  

22、60;  if(appear=no)           do             food=random(600);             while(qu1food.color=yellow|qu1food.color=red|qu1foo

23、d.color=green|food=0);            qu1food.color=red;            qu1food.direction=0;            setfillstyle(1,red);   &

24、#160;                     bar(qu1food.l, qu1food.t, qu1food.r, qu1food.b);              appear=yes;        

25、60;     star:    do          tail=head;                           mark=length;   

26、;         switch(qu1head.direction)                      case 1:   head=head+d0;            

27、60;                   direct=1;                              

28、; break;                     case 2:   head=head+d1;                       

29、        direct=2;                               break;          &

30、#160;          case 3:   head=head+d2;                               direct=3;   

31、;                            break;                      

32、60;      case 4:   head=head+d3;                               direct=4;       &

33、#160;                       if(qu1head.direction!=0|qu1head.color=green) /*撞墻或撞自己*/                   

34、60;          if(Q=1)                               setcolor(red);       

35、60;        outtextxy(450,200, " Any key to exit !  ");                           getch();closegraph();  exit(0); &#

36、160;                                       else            &

37、#160;     Q=1;                                     qu1head.direction=direct;     

38、0;      if(head=food)              appear=no;                delay(speed);          &

39、#160;     all_food-;                length+;                continue;          &

40、#160;                  qu1head.color=yellow;            setfillstyle(1,yellow);                

41、         bar(qu1head.l, qu1head.t, qu1head.r, qu1head.b);              setfillstyle(1,red);                   

42、      bar( qu1tail.l, qu1tail.t, qu1tail.r, qu1tail.b);            for(;length>1;length-)                      &

43、#160;        tail=tail-dqu1tail.direction-1;            setfillstyle(1,blue);                       

44、  bar( qu1tail.l, qu1tail.t, qu1tail.r, qu1tail.b);                           qu1tail.color=blue;            qu1tai

45、l.direction=0;            length=mark;            delay(speed);       while(!kbhit();            

46、60;        key=bioskey(0);           if(qu1head.direction=1)                  if(key=UP)         

47、60;                 Q=0;              continue;                    

48、60;  if(qu1head.direction=2)          if(key=DOWN)                           Q=0;       &#

49、160;      continue;                              if(qu1head.direction=3)          if(key=LEFT)&

50、#160;                          Q=0;              continue;         

51、60;                     if(qu1head.direction=4)          if(key=RIGHT)                

52、0;          Q=0;              continue;                        tail=head;  

53、                     mark=length;        switch(key)                     ca

54、se   UP :    head=head+d0;                                    direct=1;     

55、0;                             break;                    case DOW

56、N :    head=head+d1;                                   direct=2;        

57、60;                          break;                    case LEFT :  &#

58、160; head=head+d2;                                   direct=3;           &#

59、160;                       break;                           

60、 case RIGHT:    head=head+d3;                                   direct=4;       

61、                            break;                    case ESC  :

62、    exit(0);                    case SPACE:    getch(); goto star;                /* 暫停*/  

63、;                                      if(qu1head.direction!=0|qu1head.color=green) /*撞墻或撞自己*/     &

64、#160;                          if(Q=1)                         &#

65、160;     setcolor(red);                outtextxy(450,200, " Any key to exit !  ");                   

66、;        getch();closegraph();  exit(0);                                      &#

67、160;  else                  Q=1;                                &

68、#160;        qu1head.direction=direct;            if(head=food)              appear=no;                delay(speed);      

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論