首页 > 编程语言 > 详细

c语言贪吃蛇

时间:2016-02-17 17:16:28      阅读:194      评论:0      收藏:0      [点我收藏+]

不多说,附上源码

 

  1 #include <stdio.h>
  2 #include <windows.h>
  3 #include <time.h>
  4 #include <dos.h>
  5 int x[10000];
  6 int y[10000];
  7 int snakelong=3;
  8 int applex,appley;
  9 char c;
 10 int h=25;
 11 int w=50;
 12 int life=0;
 13 int speed;
 14 int go=5;    //0:front 1:back 2:left 3:right
 15 int eatapple=1;    //0:yes 1:no
 16 int setapple=0;    //0:yes 1:no
 17 void color(int b)
 18 {
 19     HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
 20     SetConsoleTextAttribute(hConsole,b) ;
 21 }
 22 void gotoxy(int x, int y)
 23 {
 24         COORD pos;
 25         pos.X = x;
 26         pos.Y = y;
 27         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
 28 }
 29 void snakemove(int go)
 30 {
 31         if(eatapple==0)
 32         {
 33             eatapple=1;
 34             setapple=0;
 35             snakelong++;
 36         }
 37         else
 38         {
 39             gotoxy(x[snakelong]+1,y[snakelong]);
 40             printf("\b ");
 41         }
 42         int i;
 43         for(i=snakelong;i>=2;i--)
 44         {
 45             x[i]=x[i-1];
 46             y[i]=y[i-1];
 47         }
 48             if(go==0)
 49         {
 50             y[1]--;
 51             gotoxy(x[1],y[1]);
 52             printf("■");
 53         }
 54         if(go==1)
 55         {
 56             y[1]++;
 57             gotoxy(x[1],y[1]);
 58             printf("■");
 59         }
 60         if(go==2)
 61         {
 62             x[1]=x[1]-2;
 63             gotoxy(x[1],y[1]);
 64             printf("■");
 65         }
 66         if(go==3)
 67         {
 68             x[1]=x[1]+2;
 69             gotoxy(x[1],y[1]);
 70             printf("■");
 71         }
 72 }
 73 void printback(int high,int weight)
 74 {
 75     int i;
 76     for(i=1;i<=weight;i=i+2)
 77     {
 78         gotoxy(i,0);
 79         printf("□");
 80     }
 81     for(i=1;i<=weight;i=i+2)
 82     {
 83         gotoxy(i,high-1);
 84         printf("□");
 85     }
 86     for(i=1;i<=high;i++)
 87     {
 88         gotoxy(0,i-1);
 89         printf("□");
 90     }
 91         for(i=1;i<=high;i++)
 92     {
 93         gotoxy(weight,i-1);
 94         printf("□");
 95     }
 96     gotoxy(w+3,0);
 97     printf("按空格键暂停");
 98     gotoxy(w+3,1);
 99     printf("按wasd控制前后左右与继续");
100 }
101 void drawapple()
102 {
103     color(14);
104     int i;
105     int a=0;
106     srand(time(0));
107     while(1)
108     {
109         int a=0;
110         applex=(rand()%((w/2)-1))*2+2;
111         appley=(rand()%(h-2))+1;
112         for(i=1;i<=snakelong;i++)
113         {
114             if(applex!=x[i]||appley!=y[i]) a++;
115         }
116         if(a==snakelong) break;
117     }
118     gotoxy(applex,appley);
119     printf("★");
120     setapple=1;
121     color(10);
122 }
123 int main()
124 {
125     system("chcp 936>NUL");//设置编码为UNICODE
126     SetConsoleTitle("贪吃蛇 by 软剑攻城狮");
127     printf("请输入速度等级(1-10),等级越高速度越快,按回车确认:");
128     scanf("%d",&speed);
129     if(speed<1||speed>10)
130     {
131         printf("输入错误,按任意键退出!");
132         getch();
133         exit(0);
134     }
135     x[1]=10;
136     y[1]=10;
137     int i;
138     gotoxy(0,0);
139     for(i=1;i<=1000;i++)
140     {
141         printf(" ");
142     }
143     color(12);
144     printback(h,w);
145     color(10);
146     gotoxy(x[1],y[1]);
147     printf("■");
148     while(life==0)
149     {
150             if(kbhit())
151         {
152         c=getch();
153         if(c==‘w‘&&go!=1) go=0;
154         if(c==‘s‘&&go!=0) go=1;
155         if(c==‘a‘&&go!=3) go=2;
156         if(c==‘d‘&&go!=2) go=3;
157         if(c==‘ ‘) go=5;
158         }
159         for(i=2;i<=snakelong;i++)
160         {
161             if(x[1]==x[i]&&y[1]==y[i])
162             {
163                 life=1;
164                 break;
165             }
166         }
167         if(life==1) break;
168         if(x[1]<=0||x[1]>=w||y[1]<=0||y[1]>=h-1) break;
169         if(x[1]==applex&&y[1]==appley)
170         {
171             eatapple=0;
172             Beep(2000,50);
173         }
174         if(go!=5) snakemove(go);
175         if(setapple==0) drawapple();
176         gotoxy(w+3,3);
177         printf("分数:%d",(snakelong-3)*10);
178         Sleep(250-speed*20);
179     }
180     Beep(350,200);
181     Beep(250,200);
182     gotoxy(0,h+1);
183     printf("你的蛇死了,按任意键退出游戏");
184     getch();
185     return 0;
186 }

 

随意转载,没有版权~

c语言贪吃蛇

原文:http://www.cnblogs.com/rjgcs/p/5195854.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!