贪吃蛇
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <conio.h> 5 6 struct Node 7 { 8 int x; 9 int y; 10 struct Node *pre; 11 struct Node *next; 12 }; 13 14 struct Food 15 { 16 int x; 17 int y; 18 char c; 19 }; 20 21 void main() 22 { 23 int a[15][15]={0}; 24 int i,j,t,flag=0; 25 char c='d',c1='d'; 26 struct Food food={5,8,'A'}; 27 int gameover=0; 28 struct Node *head,*p,*rear,*pt; 29 30 head=(struct Node *)malloc(sizeof(struct Node)); 31 head->x=5; 32 head->y=8; 33 head->pre=NULL; 34 head->next=NULL; 35 rear=head; 36 37 srand((unsigned)time(NULL)); 38 39 while(1) 40 { 41 if(food.x==head->x && food.y==head->y) 42 { 43 p=(struct Node *)malloc(sizeof(struct Node)); 44 pt=head; 45 while(pt->next!=NULL) 46 pt=pt->next ; 47 p->pre= pt; 48 pt->next = p; 49 p->next=NULL; 50 rear=p; 51 52 food.x=rand()%15; 53 food.y=rand()%15; 54 food.c=65+rand()%26; 55 56 flag=1; 57 t=0; 58 while(flag==1) 59 { 60 if(t>5) 61 break; 62 63 flag=0; 64 65 pt=head; 66 while(pt!=NULL) 67 { 68 if(food.x==pt->x && food.y==pt->y) 69 { 70 flag=1; 71 food.x=rand()%15; 72 food.y=rand()%15; 73 break; 74 } 75 pt=pt->next; 76 } 77 t++; 78 } 79 if(t>5) 80 { 81 if(c=='d') 82 { 83 food.x=head->x+1; 84 food.y=head->y; 85 if(food.x>=15) 86 food.x-=15; 87 } 88 else if(c=='a') 89 { 90 food.x=head->x-1; 91 food.y=head->y; 92 if(food.x<0) 93 food.x+=15; 94 } 95 else if(c=='w') 96 { 97 food.x=head->x; 98 food.y=head->y+1; 99 if(food.y>=15) 100 food.y-=15; 101 } 102 else if(c=='s') 103 { 104 food.x=head->x; 105 food.y=head->y-1; 106 if(food.y<0) 107 food.y+=15; 108 } 109 } 110 } 111 112 if(kbhit()) 113 { 114 c1=getch(); 115 if(c1==27) 116 break; 117 118 if(c!='d' && c1=='a') 119 c=c1; 120 else if(c!='a' && c1=='d') 121 c=c1; 122 else if(c!='w' && c1=='s') 123 c=c1; 124 else if(c!='s' && c1=='w') 125 c=c1; 126 } 127 128 pt=rear; 129 while(pt!=head ) 130 { 131 pt->x=pt->pre->x; 132 pt->y=pt->pre->y; 133 pt=pt->pre; 134 } 135 136 if(c=='d') 137 { 138 head->y+=1; 139 if(head->y>=15) 140 head->y-=15; 141 } 142 else if(c=='a') 143 { 144 head->y-=1; 145 if(head->y<0) 146 head->y+=15; 147 } 148 else if(c=='w') 149 { 150 head->x-=1; 151 if(head->x<0) 152 head->x+=15; 153 } 154 else if(c=='s') 155 { 156 head->x+=1; 157 if(head->x>=15) 158 head->x-=15; 159 } 160 161 pt=head->next; 162 while(pt!=NULL) 163 { 164 if(head->x==pt->x && head->y==pt->y) 165 { 166 gameover=1; 167 break; 168 } 169 pt=pt->next ; 170 } 171 if(gameover==1) 172 break; 173 174 system("cls"); 175 printf(" ───────────────\n"); 176 for(i=0;i<15;i++) 177 { 178 printf("│"); 179 for(j=0;j<15;j++) 180 { 181 182 flag=0; 183 pt=head; 184 while(pt!=NULL) 185 { 186 if(i==pt->x && j==pt->y) 187 { 188 if(pt==head) 189 printf("■"); 190 else 191 printf("□"); 192 flag=1; 193 break; 194 } 195 pt=pt->next; 196 } 197 198 199 if(flag==0) 200 { 201 if(i==food.x && j==food.y) 202 { 203 putchar(food.c); 204 putchar(food.c); 205 continue; 206 } 207 printf(" "); 208 } 209 } 210 printf("│"); 211 putchar('\n'); 212 } 213 printf(" ───────────────\n"); 214 215 _sleep(200); 216 217 } 218 219 if(gameover==1) 220 puts("game over!\n"); 221 222 getch(); 223 224 }