C语言贪吃蛇

  1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<conio.h>
4 #include<time.h>
5 struct point
6 {
7 int row;
8 int col;
9 };
10 struct snake
11 {
12 struct point body[100];
13 int length;
14 };
15 struct food
16 {
17 struct point f;
18 };
19 void main()
20 {
21 int i,j,k,b;
22 int b1;
23 FILE *p;
24 struct food f1;
25 char a[20][20];
26 char c='d',c1='d';
27 p=fopen("123.txt","r");
28 if(p==NULL)
29 exit(1);
30 struct snake sna;
31 sna.length =3;
32 sna.body[0].col=5;
33 sna.body [0].row =10;
34 sna.body[1].col=6;
35 sna.body [1].row =10;
36 sna.body[2].col=7;
37 sna.body [2].row =10;
38 srand(time(NULL));
39 f1.f.col=1+rand()%18;
40 f1.f.row=1+rand()%18;
41 while(1)
42 {
43 for(k=3;k<sna.length ;k++)
44 if(sna.body[0].row==sna.body[k].row&&sna.body[0].col==sna.body[k].col)
45 {
46 printf("游戏结束。。。\n");
47 exit(1);
48 }
49 system("cls");
50 rewind(p);
51 for(i=0;i<20;i++)
52 {
53 for(j=0;j<20;j++)
54 a[i][j]=fgetc(p);
55 fgetc(p);
56 }
57 for(i=0;i<20;i++)
58 {
59 for(j=0;j<20;j++)
60 {
61 for(k=1;k<sna.length ;k++)
62 if(j==sna.body[k].col &&i==sna.body[k].row)
63 a[i][j]='2';
64 if(i==f1.f.row&&j==f1.f.col)
65 a[i][j]='3';
66 if(j==sna.body[0].col &&i==sna.body[0].row)
67 a[i][j]='5';
68 }
69 }
70 for(i=0;i<20;i++)
71 {
72 for(j=0;j<20;j++)
73 {
74 if(a[i][j]=='1')
75 printf("");
76 else
77 if(a[i][j]=='2')
78 {printf("");a[i][j]=0;}
79 else
80 if(a[i][j]=='3')
81 {printf("");a[i][j]=0;}
82 else
83 if(a[i][j]=='5')
84 printf("");
85 else
86 printf(" ");
87
88
89
90 }
91 putchar('\n');
92 }
93 printf("目前得分%d\n",sna.length-2);
94 if(kbhit())
95 c=getch();
96 if(sna.body[0].col==f1.f.col&&sna.body[0].row==f1.f.row)
97 {
98 sna.length++;
99 f1.f.col=1+rand()%18;
100 f1.f.row=1+rand()%18;
101 for(k=0;k<sna.length ;k++)
102 {
103 if(sna.body[k].col==f1.f.col&&sna.body[k].row==f1.f.row)
104 k=0;
105 f1.f.col=1+rand()%18;
106 f1.f.row=1+rand()%18;
107 }
108 }
109 for(k=sna.length-1;k>0;k--)
110 {
111 sna.body[k].col =sna.body[k-1].col ;
112 sna.body[k].row =sna.body[k-1].row ;
113 }
114
115 if(c1=='a'&&c!='d')
116 c1=c;
117 else
118 if(c1=='d'&&c!='a')
119 c1=c;
120 else
121 if(c1=='w'&&c!='s')
122 c1=c;
123 else
124 if(c1=='s'&&c!='w')
125 c1=c;
126 if(c1=='a')
127 sna.body[0].col--;
128 else
129 if(c1=='d')
130 sna.body[0].col++;
131 else
132 if(c1=='w')
133 sna.body[0].row--;
134 else
135 if(c1=='s')
136 sna.body[0].row++;
137 if(sna.body[0].col==0)
138 sna.body[0].col=18;
139 else
140 if(sna.body[0].row==19)
141 sna.body[0].row=1;
142 else
143 if(sna.body[0].row==0)
144 sna.body[0].row=18;
145 else
146 if(sna.body[0].col==19)
147 sna.body[0].col=1;
148
149 _sleep(300-sna.length *sna.length );
150
151 }
152 }

txt中内容:

123.txt

11111111111111111111
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
11111111111111111111

原文地址:https://www.cnblogs.com/gaorui/p/2219330.html