有视频
项目中的整个demo
代码展现:
1 #include<curses.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<pthread.h>
5
6 #define UP 1
7 #define DOWN -1
8 #define LEFT 2
9 #define RIGHT -2
10
11 struct NodeofSnake
12 {
13 int hang;
14 int lie;
15 struct NodeofSnake *next;
16 };
17
18 struct NodeofSnake *head=NULL;
19 struct NodeofSnake *tail=NULL;
20 int key;
21 int dir;
22 struct NodeofSnake food;
23
24 void addNode()
25 {
26 struct NodeofSnake *new;
27 new=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
28 new->next=NULL;
29
30 switch(dir){
31 case UP:
32 new->hang=tail->hang-1;
33 new->lie =tail->lie;
34 break;
35 case DOWN:
36 new->hang=tail->hang+1;
37 new->lie =tail->lie;
38 break;
39 case LEFT:
40 new->hang=tail->hang;
41 new->lie =tail->lie-1;
42 break;
43 case RIGHT:
44 new->hang=tail->hang;
45 new->lie =tail->lie+1;
46 break;
47 }
48
49 tail->next=new;
50 tail=new;
51 }
52
53 void initFood()
54 {
55 // static int x=3; //static function is?
56 // static int y=3;
57 int x=rand()%20;
58 int y=rand()%20;
59
60 food.hang=x;
61 food.lie =y;
62 food.next=NULL;
63 }
64
65 void initSnake()
66 {
67 dir=RIGHT;
68 struct NodeofSnake *point =head;
69 if(point!=NULL){
70 head=head->next;
71 free(point);
72 }
73 initFood();
74 head=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
75 head->hang=2;
76 head->lie =2;
77 head->next=NULL;
78
79 tail=head;
80
81 addNode();
82 addNode();
83 addNode();
84 }
85
86 int wheatherOfSnakeNode(int hang,int lie)
87 {
88
89 struct NodeofSnake *point = head;
90 while(point!=NULL){
91 if(hang==point->hang && lie==point->lie ){
92 printw("[]");
93 return 1;
94 }
95 point=point->next;
96 }
97 return 0;
98 }
99
100 int wheatherOfFoodNode(int hang,int lie)
101 {
102 if(hang==food.hang && lie==food.lie){
103 printw("##");
104 return 1;
105 }
106 return 0;
107 }
108
109 void initMap()
110 {
111 int hang=0;
112 int lie =0;
113 move(0,0);
114
115 for(hang=0;hang<=20;hang++){
116 if(hang==0||hang==20){
117 for(lie=0;lie<20;lie++){
118 printw("--");
119 }
120 printw("\n");
121 }
122 if(hang>0&&hang<=19){
123 for(lie=0;lie<=20;lie++){
124 if(lie==0||lie==20){
125 printw("|");
126 }else if(wheatherOfSnakeNode(hang,lie)){
127 }
128 else if(wheatherOfFoodNode(hang,lie)){
129 }
130 else{
131 printw(" ");
132 }
133 }
134 printw("\n");
135 }
136 }
137 printw("By:GuoMing,key=%d\n",key);
138 printw("food.hang=%d,food.lie=%d\n",food.hang,food.lie);
139 // printw("key=%d\n",key);
140 }
141 void deleteNode()
142 {
143 struct NodeofSnake *point = head;
144 head=head->next;
145 free(point);
146 }
147
148 int deathOfSnake()
149 {
150 if(tail->lie==20 ||tail->lie==0 ||tail->hang==0 ||tail->hang==20 ){
151 return 1;
152 }
153
154 struct NodeofSnake *point =head;
155 while(point->next!=NULL){
156 if(point->hang==tail->hang&&point->lie==tail->lie){
157 return 1;
158 }
159 point=point->next;
160 }
161 return 0;
162 }
163
164 void snakeMove()
165 {
166 addNode();
167 if(tail->hang==food.hang && tail->lie==food.lie){
168 initFood();
169 }else{
170 deleteNode();
171 }
172 if(deathOfSnake()){
173 initSnake();
174 }
175 }
176
177 void* refreshJieMian()
178 {
179 while(1){
180 snakeMove();
181 initMap();
182
183 refresh();
184 usleep(100000);
185 }
186
187 }
188
189 //use abs() to address direction question.
190
191 void dirWheatherTurn(int direction)
192 { // origion key now dir
193 if(abs(dir) != abs(direction)){
194 dir=direction;
195 }
196
197 }
198
199 void* changeDir()
200 {
201 // int key;
202 while(1){ //alway input keyboard.
203 key=getch();
204 // printw("key=%d\n",key);
205
206 switch(key){
207 case KEY_DOWN:
208 dirWheatherTurn(DOWN);
209 break;
210 case KEY_UP:
211 dirWheatherTurn(UP);
212 break;
213 case KEY_LEFT:
214 dirWheatherTurn(LEFT);
215 break;
216 case KEY_RIGHT:
217 dirWheatherTurn(RIGHT);
218 break;
219 }
220 }
221 }
222
223 int main()
224 {
225 pthread_t t1,t2 ;
226 initscr();
227 keypad(stdscr,1);
228
229 // struct NodeofFood food = {3,3}; if write that,food sites will not change.
230
231 // dir=RIGHT; forbid initdir ,cause:only one use.
232 initSnake();
233 initMap();
234 pthread_create(&t1,NULL,refreshJieMian,NULL);
235 pthread_create(&t2,NULL,changeDir,NULL);
236
237 while(1);
238 getch();
239 endwin();
240
241 return 0;
242 }