最近课设闲着没事参加了一个培训
写了个贪吃蛇
感觉还是挺有意思的
把代码留下来以后玩= =
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<windows.h> 5 #include<conio.h> 6 #define TRUE 1 7 #define FALSE 0 8 9 typedef struct snake 10 { 11 int x; 12 int y; 13 struct snake* g_next; 14 struct snake* g_front; 15 } snake; 16 17 snake* snake_head=NULL; 18 snake* snake_end=NULL; 19 char PlayGround[20][20]; 20 21 typedef struct bean 22 { 23 int x; 24 int y; 25 } bean; 26 27 bean g_bean; 28 char fx='s'; 29 30 void create_snake(); 31 void create_bean(); 32 void show(); 33 void snake_move(); 34 void snake_eat(); 35 int check(); 36 37 38 int main() 39 { 40 srand((unsigned int)time(NULL)); 41 create_snake(); 42 create_bean(); 43 while(1) 44 { 45 show(); 46 if(kbhit()) 47 { 48 fx=getch(); 49 } 50 if(check()) 51 { 52 snake_eat(); 53 create_bean(); 54 } 55 snake_move(); 56 Sleep(100); 57 system("cls"); 58 } 59 return 0; 60 } 61 62 void create_bean() 63 { 64 int x; 65 int y; 66 x=rand()%20; 67 y=rand()%20; 68 snake* temp=snake_head; 69 while(temp) 70 { 71 if(temp->x==x&&temp->y==y) 72 { 73 break; 74 } 75 temp=temp->g_next; 76 } 77 if(NULL == temp) 78 { 79 g_bean.x=x; 80 g_bean.y=y; 81 } 82 else create_bean(); 83 } 84 85 void create_snake() 86 { 87 snake* temp; 88 int i; 89 for(i=0; i<3; i++) 90 { 91 temp=(snake*)malloc(sizeof(snake)); 92 temp->x=0; 93 temp->y=i; 94 temp->g_next=NULL; 95 temp->g_front=NULL; 96 if(NULL == snake_head) 97 { 98 snake_head=temp; 99 snake_end=temp; 100 } 101 else 102 { 103 snake_end->g_next=temp; 104 temp->g_front=snake_end; 105 snake_end=temp; 106 } 107 } 108 } 109 110 void show() 111 { 112 snake* temp; 113 temp=snake_head; 114 int i,j; 115 while(temp) 116 { 117 PlayGround[temp->x][temp->y]=1; 118 temp=temp->g_next; 119 } 120 PlayGround[g_bean.x][g_bean.y]=1; 121 for(i=0; i<20; i++) 122 { 123 for(j=0; j<20; j++) 124 { 125 if(PlayGround[i][j]==1) 126 { 127 printf("*"); 128 } 129 else 130 { 131 printf(" "); 132 } 133 } 134 printf(" "); 135 } 136 for(i=0; i<20; i++) 137 { 138 for(j=0; j<20; j++) 139 { 140 PlayGround[i][j]=0; 141 142 } 143 } 144 } 145 146 void snake_move() 147 { 148 snake* temp=snake_head; 149 while(temp->g_next) 150 { 151 temp->x=temp->g_next->x; 152 temp->y=temp->g_next->y; 153 temp=temp->g_next; 154 } 155 switch(fx) 156 { 157 case 'w': 158 if(snake_end->x==0) 159 snake_end->x=19; 160 else snake_end->x-=1; 161 break; 162 case 'a': 163 if(snake_end->y==0) 164 snake_end->y=19; 165 else snake_end->y-=1; 166 break; 167 case 's': 168 if(snake_end->x==19) 169 snake_end->x=0; 170 else snake_end->x+=1; 171 break; 172 case 'd': 173 if(snake_end->y==19) 174 snake_end->y=0; 175 else snake_end->y+=1; 176 break; 177 } 178 } 179 180 void snake_eat() 181 { 182 snake* temp=(snake*)malloc(sizeof(snake)); 183 temp->x=g_bean.x; 184 temp->y=g_bean.y; 185 temp->g_front=NULL; 186 temp->g_next=NULL; 187 188 snake_end->g_next=temp; 189 temp->g_front=snake_end; 190 snake_end=temp; 191 } 192 193 int check() 194 { 195 switch(fx) 196 { 197 case 'w': 198 if(snake_end->x-1 == g_bean.x && snake_end->y == g_bean.y) 199 return TRUE; 200 else 201 return FALSE; 202 break; 203 case 'a': 204 if(snake_end->x == g_bean.x && snake_end->y-1 == g_bean.y) 205 return TRUE; 206 else 207 return FALSE; 208 break; 209 case 's': 210 if(snake_end->x+1 == g_bean.x+1 && snake_end->y == g_bean.y) 211 return TRUE; 212 else 213 return FALSE; 214 break; 215 case 'd': 216 if(snake_end->x == g_bean.x && snake_end->y+1 == g_bean.y) 217 return TRUE; 218 else 219 return FALSE; 220 break; 221 } 222 }