前因:
main()中有两个while(1)循环问题
一是不断获取用户输入键盘所需要的键盘响应;
二是蛇不断的进行移动;
解决方法------Linux线程中有专有的函数
解决双while(1)的问题:
1 #include<curses.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<pthread.h>
5
6 struct NodeofSnake
7 {
8 int hang;
9 int lie;
10 struct NodeofSnake *next;
11 };
12
13 struct NodeofSnake *head=NULL;
14 struct NodeofSnake *tail=NULL;
15 int key;
16
17 void addNode()
18 {
19 struct NodeofSnake *new;
20 new=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
21 new->hang=tail->hang;
22 new->lie =tail->lie+1;
23 new->next=NULL;
24
25 tail->next=new;
26 tail=new;
27 }
28 void initSnake()
29 {
30 struct NodeofSnake *point =head;
31 if(point!=NULL){
32 head=head->next;
33 free(point);
34 }
35 head=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
36 head->hang=2;
37 head->lie =2;
38 head->next=NULL;
39
40 tail=head;
41
42 addNode();
43 addNode();
44 addNode();
45 }
46 int wheatherOfSnakeNode(int hang,int lie)
47 {
48
49 struct NodeofSnake *point = head;
50 while(point!=NULL){
51 if(hang==point->hang && lie==point->lie ){
52 printw("[]");
53 return 1;
54 }
55 point=point->next;
56 }
57 return 0;
58 }
59
60 void initMap()
61 {
62 int hang=0;
63 int lie =0;
64 move(0,0);
65
66 for(hang=0;hang<=20;hang++){
67 if(hang==0||hang==20){
68 for(lie=0;lie<20;lie++){
69 printw("--");
70 }
71 printw("\n");
72 }
73 if(hang>0&&hang<=19){
74 for(lie=0;lie<=20;lie++){
75 if(lie==0||lie==20){
76 printw("|");
77 }else if(wheatherOfSnakeNode(hang,lie)){
78 }
79 else{
80 printw(" ");
81 }
82 }
83 printw("\n");
84 }
85 }
86 printw("By:GuoMing,key=%d\n",key);
87 // printw("key=%d\n",key);
88 }
89 void deleteNode()
90 {
91 struct NodeofSnake *point = head;
92 head=head->next;
93 free(point);
94 }
95 void snakeMove()
96 {
97 addNode();
98 deleteNode();
99 if(tail->lie==20 ||tail->lie==0 ||tail->hang==0 ||tail->hang==20 ){
100 initSnake();
101 }
102 }
103
104 void* refreshJieMian()
105 {
106 while(1){
107 snakeMove();
108 initMap();
109
110 refresh();
111 usleep(100000);
112 }
113
114 }
115
116 void* changeDir()
117 {
118 // int key;
119 while(1){ //alway input keyboard.
120 key=getch();
121 // printw("key=%d\n",key);
122
123 switch(key){
124 case KEY_DOWN:
125 printw("DOWN\n");
126 break;
127 case KEY_UP:
128 printw("UP\n");
129 break;
130 case KEY_LEFT:
131 printw("LEFT\n");
132 break;
133 case KEY_RIGHT:
134 printw("RIGHT\n");
135 break;
136 }
137 }
138 }
139
140 int main()
141 {
142 pthread_t t1,t2 ;
143 initscr();
144 keypad(stdscr,1);
145
146 initSnake();
147 initMap();
148 pthread_create(&t1,NULL,refreshJieMian,NULL);
149 pthread_create(&t2,NULL,changeDir,NULL);
150 151 while(1);
152 getch();
153 endwin();
154
155 return 0;
156 }
key值可以把你按下的东西打印出来
蛇根据方向键运动(但发现有bug)
1 #include<curses.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<pthread.h>
5
6 #define UP 1
7 #define DOWN 2
8 #define LEFT 3
9 #define RIGHT 4
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
23 void addNode()
24 {
25 struct NodeofSnake *new;
26 new=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
27 new->next=NULL;
28
29 switch(dir){
30 case UP:
31 new->hang=tail->hang-1;
32 new->lie =tail->lie;
33 break;
34 case DOWN:
35 new->hang=tail->hang+1;
36 new->lie =tail->lie;
37 break;
38 case LEFT:
39 new->hang=tail->hang;
40 new->lie =tail->lie-1;
41 break;
42 case RIGHT:
43 new->hang=tail->hang;
44 new->lie =tail->lie+1;
45 break;
46 }
47
48 tail->next=new;
49 tail=new;
50 }
51 void initSnake()
52 {
53 dir=RIGHT;
54 struct NodeofSnake *point =head;
55 if(point!=NULL){
56 head=head->next;
57 free(point);
58 }
59 head=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
60 head->hang=2;
61 head->lie =2;
62 head->next=NULL;
63
64 tail=head;
65
66 addNode();
67 addNode();
68 addNode();
69 }
70 int wheatherOfSnakeNode(int hang,int lie)
71 {
72
73 struct NodeofSnake *point = head;
74 while(point!=NULL){
75 76 printw("[]");
77 return 1;
78 }
79 point=point->next;
80 }
81 return 0;
82 }
83
84 void initMap()
85 {
86 int hang=0;
87 int lie =0;
88 move(0,0);
89
90 for(hang=0;hang<=20;hang++){
91 if(hang==0||hang==20){
92 for(lie=0;lie<20;lie++){
93 printw("--");
94 }
95 printw("\n");
96 }
97 if(hang>0&&hang<=19){
98 for(lie=0;lie<=20;lie++){
99 if(lie==0||lie==20){
100 printw("|");
101 }else if(wheatherOfSnakeNode(hang,lie)){
102 }
103 else{
104 printw(" ");
105 }
106 }
107 printw("\n");
108 }
109 }
110 printw("By:GuoMing,key=%d\n",key);
111 // printw("key=%d\n",key);
112 }
113 void deleteNode()
114 {
115 struct NodeofSnake *point = head;
116 head=head->next;
117 free(point);
118 }
119 void snakeMove()
120 {
121 addNode();
122 deleteNode();
123 if(tail->lie==20 ||tail->lie==0 ||tail->hang==0 ||tail->hang==20 ){
124 initSnake();
125 }
126 }
127
128 void* refreshJieMian()
129 {
130 while(1){
131 snakeMove();
132 initMap();
133
134 refresh();
135 usleep(100000);
136 }
137
138 }
139
140 void* changeDir()
141 {
142 // int key;
143 while(1){ //alway input keyboard.
144 key=getch();
145 // printw("key=%d\n",key);
146
147 switch(key){
148 case KEY_DOWN:
149 dir=DOWN;
150 break;
151 case KEY_UP:
152 dir=UP;
153 break;
154 case KEY_LEFT:
155 dir=LEFT;
156 break;
157 case KEY_RIGHT:
158 dir=RIGHT;
159 break;
160 }
161 }
162 }
163
164 int main()
165 {
166 pthread_t t1,t2 ;
167 initscr();
168 keypad(stdscr,1);
169
170 // dir=RIGHT; forbid initdir ,cause:only one use.
171 initSnake();
172 initMap();
173 pthread_create(&t1,NULL,refreshJieMian,NULL);
174 pthread_create(&t2,NULL,changeDir,NULL);
175
176 while(1);
177 getch();
178 endwin();
179
180 return 0;
蛇运动方向根据方向键运动(无bug)
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
23 void addNode()
24 {
25 struct NodeofSnake *new;
26 new=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
27 new->next=NULL;
28
29 switch(dir){
30 case UP:
31 new->hang=tail->hang-1;
32 new->lie =tail->lie;
33 break;
34 case DOWN:
35 new->hang=tail->hang+1;
36 new->lie =tail->lie;
37 break;
38 case LEFT:
39 new->hang=tail->hang;
40 new->lie =tail->lie-1;
41 break;
42 case RIGHT:
43 new->hang=tail->hang;
44 new->lie =tail->lie+1;
45 break;
46 }
47
48 tail->next=new;
49 tail=new;
50 }
51 void initSnake()
52 {
53 dir=RIGHT;
54 struct NodeofSnake *point =head;
55 if(point!=NULL){
56 head=head->next;
57 free(point);
58 }
59 head=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
60 head->hang=2;
61 head->lie =2;
62 head->next=NULL;
63
64 tail=head;
65
66 addNode();
67 addNode();
68 addNode();
69 }
70 int wheatherOfSnakeNode(int hang,int lie)
71 {
72
73 struct NodeofSnake *point = head;
74 while(point!=NULL){
75 if(hang==point->hang && lie==point->lie ){
76 printw("[]");
77 return 1;
78 }
79 point=point->next;
80 }
81 return 0;
82 }
83
84 void initMap()
85 {
86 int hang=0;
87 int lie =0;
88 move(0,0);
89
90 for(hang=0;hang<=20;hang++){
91 if(hang==0||hang==20){
92 for(lie=0;lie<20;lie++){
93 printw("--");
94 }
95 printw("\n");
96 }
97 if(hang>0&&hang<=19){
98 for(lie=0;lie<=20;lie++){
99 if(lie==0||lie==20){
100 printw("|");
101 }else if(wheatherOfSnakeNode(hang,lie)){
102 }
103 else{
104 printw(" ");
105 }
106 }
107 printw("\n");
108 }
109 }
110 printw("By:GuoMing,key=%d\n",key);
111 // printw("key=%d\n",key);
112 }
113 void deleteNode()
114 {
115 struct NodeofSnake *point = head;
116 head=head->next;
117 free(point);
118 }
119 void snakeMove()
120 {
121 addNode();
122 deleteNode();
123 if(tail->lie==20 ||tail->lie==0 ||tail->hang==0 ||tail->hang==20 ){
124 initSnake();
125 }
126 }
127
128 void* refreshJieMian()
129 {
130 while(1){
131 snakeMove();
132 initMap();
133
134 refresh();
135 usleep(100000);
136 }
137
138 }
139
140 //use abs() to address direction question.
141
142 void dirWheatherTurn(int direction)
143 { // origion key now dir
144 if(abs(dir) != abs(direction)){
145 dir=direction;
146 }
147
148 }
149
150 void* changeDir()
151 {
152 // int key;
153 while(1){ //alway input keyboard.
154 key=getch();
155 // printw("key=%d\n",key);
156
157 switch(key){
158 case KEY_DOWN:
159 dirWheatherTurn(DOWN);
160 break;
161 case KEY_UP:
162 dirWheatherTurn(UP);
163 break;
164 case KEY_LEFT:
165 dirWheatherTurn(LEFT);
166 break;
167 case KEY_RIGHT:
168 dirWheatherTurn(RIGHT);
169 break;
170 }
171 }
172 }
173
174 int main()
175 {
176 pthread_t t1,t2 ;
177 initscr();
178 keypad(stdscr,1);
179
180 // dir=RIGHT; forbid initdir ,cause:only one use.
181 initSnake();
182 initMap();
183 pthread_create(&t1,NULL,refreshJieMian,NULL);
184 pthread_create(&t2,NULL,changeDir,NULL);
185
186 while(1);
187 getch();
188 endwin();
189
190 return 0;
191 }