1+ // https://zxbcw.cn/post/218247/
2+
3+ #include <pthread.h>
4+ #include <unistd.h>
5+
6+ #include <stdio.h>
7+ #include <stdlib.h>
8+ #include <time.h>
9+
10+ #define X 40
11+ #define Y 20
12+
13+ // char HEAD = '@'; // The shape of snake head
14+ // char BODY = 'O'; // The shape of snake body
15+ #define HEAD '@' // The shape of snake head
16+ #define BODY 'O' // The shape of snake body
17+ char a [Y ][X ] = {{BODY , BODY , BODY , HEAD }}; // The initial char is 0
18+ char * p [Y * X ] = {& a [0 ][3 ], & a [0 ][2 ], & a [0 ][1 ],
19+ & a [0 ][0 ]}; // p[0] stand for snake head
20+
21+ int n = 3 ; // The length of snake body (without head)
22+ int i , j ;
23+ int direction = 1 ; // 1.right;2.up;3.left;4.down;-1.exit
24+ int delay = 200 * 1000 ; // delay 0.2s(200ms)
25+ void moveBody () {
26+ * p [n ] = 0 ;
27+ for (i = n ; i > 0 ; i -- ) {
28+ p [i ] = p [i - 1 ]; // per part goes to the address of the next part ofbody
29+ }
30+ * p [0 ] = BODY ; // The First part of snake body come to snake head
31+ }
32+ void moveRight () {
33+ moveBody ();
34+ p [0 ] = p [0 ] + 1 ; // Move snake head
35+ * p [0 ] = HEAD ; // change the char of new head(new address)'s shape to HEAD
36+ }
37+ void moveLeft () {
38+ moveBody ();
39+ p [0 ] = p [0 ] - 1 ;
40+ * p [0 ] = HEAD ;
41+ }
42+ void moveDown () {
43+ moveBody ();
44+ p [0 ] = p [0 ] + X ;
45+ * p [0 ] = HEAD ;
46+ }
47+ void moveUp () {
48+ moveBody ();
49+ p [0 ] = p [0 ] - X ;
50+ * p [0 ] = HEAD ;
51+ }
52+
53+ void show () {
54+ system ("clear" );
55+ for (i = 0 ; i < X ; i ++ )
56+ printf ("_" );
57+ printf ("\n" );
58+ for (i = 0 ; i < Y ; i ++ ) {
59+ // printf("|");
60+ for (j = 0 ; j < X ; j ++ ) {
61+ printf ("%c" , (a [i ][j ] == 0 ) ? ' ' : a [i ][j ]);
62+ }
63+ // printf("|");
64+ printf ("\n" );
65+ }
66+ for (i = 0 ; i < X ; i ++ )
67+ printf ("-" );
68+ printf ("\nw,s,a,d->Up Down Left Right;j,k->Speed Up/Down;ESC: Exit\n" );
69+ }
70+
71+ void randomApple () // Random
72+ {
73+ srand (time (NULL ));
74+ do {
75+ i = rand () % Y ;
76+ j = rand () % X ;
77+ // if random location is 0 ->*;else find again and again
78+ } while (a [i ][j ] != 0 );
79+ a [i ][j ] = '*' ;
80+ }
81+
82+ void canEat () {
83+ if (direction == 1 ) // Right
84+ {
85+ if (* (p [0 ] + 1 ) == '*' ) {
86+ n ++ ; // length++
87+ p [n ] = p [n - 1 ];
88+ randomApple ();
89+ }
90+ }
91+ if (direction == 2 ) // Up
92+ {
93+ if (* (p [0 ] - X ) == '*' ) {
94+ n ++ ; // length++
95+ p [n ] = p [n - 1 ];
96+ randomApple ();
97+ }
98+ }
99+ if (direction == 3 ) // Left
100+ {
101+ if (* (p [0 ] - 1 ) == '*' ) {
102+ n ++ ; // length++
103+ p [n ] = p [n - 1 ];
104+ randomApple ();
105+ }
106+ }
107+ if (direction == 4 ) // Down
108+ {
109+ if (* (p [0 ] + X ) == '*' ) {
110+ n ++ ; // length++
111+ p [n ] = p [n - 1 ];
112+ randomApple ();
113+ }
114+ }
115+ }
116+
117+ void isFail () {
118+ if (p [0 ] < & a [0 ][0 ] || p [0 ] > & a [Y - 1 ][X - 1 ]) // snake is not in the matrix
119+ {
120+ printf ("fail!\n" );
121+ direction = -1 ;
122+ }
123+ if (direction == 1 ) // Right
124+ {
125+ for (i = n ; i > 0 ; i -- ) {
126+ if ((p [0 ] + 1 ) == p [i ]) // Right of the head is body
127+ {
128+ printf ("fail!\n" );
129+ direction = -1 ;
130+ }
131+ }
132+ }
133+ if (direction == 2 ) // Up
134+ {
135+ for (i = n ; i > 0 ; i -- ) {
136+ if ((p [0 ] - X ) == p [i ]) // Up of the head is body
137+ {
138+ printf ("fail!\n" );
139+ direction = -1 ;
140+ }
141+ }
142+ }
143+ if (direction == 3 ) // Left
144+ {
145+ for (i = n ; i > 0 ; i -- ) {
146+ if ((p [0 ] - 1 ) == p [i ]) // Left of the head is body
147+ {
148+ printf ("fail!\n" );
149+ direction = -1 ;
150+ }
151+ }
152+ }
153+ if (direction == 4 ) // Down
154+ {
155+ for (i = n ; i > 0 ; i -- ) {
156+ if ((p [0 ] + X ) == p [i ]) // Down of the head is body
157+ {
158+ printf ("fail!\n" );
159+ direction = -1 ;
160+ }
161+ }
162+ }
163+ }
164+
165+ void * key (void * arg ) // Direction Control:w,s,a,d-->Up Down Left Right
166+ {
167+ char k ;
168+ while (1 ) {
169+
170+ k = getchar ();
171+ switch (k ) {
172+ case 'w' : // Up
173+ {
174+ if (direction != 4 )
175+ direction = 2 ;
176+ break ;
177+ }
178+ case 's' : // Down
179+ {
180+ if (direction != 2 )
181+ direction = 4 ;
182+ break ;
183+ }
184+ case 'a' : // Left
185+ {
186+ if (direction != 1 )
187+ direction = 3 ;
188+ break ;
189+ }
190+ case 'd' : // Right
191+ {
192+ if (direction != 3 )
193+ direction = 1 ;
194+ break ;
195+ }
196+ case 'j' : // SpeedUp
197+ {
198+ delay = delay * 4 / 5 ;
199+ break ;
200+ }
201+ case 'k' : // SpeedDown
202+ {
203+ delay = delay * 5 / 4 ;
204+ break ;
205+ }
206+ case 27 : // ESC
207+ {
208+ printf ("Exit!\n" );
209+ direction = -1 ;
210+ break ;
211+ }
212+ }
213+ }
214+ }
215+
216+ int main () {
217+ system ("stty -icanon" );
218+ pthread_t pid ;
219+ pthread_create (& pid , NULL , key , NULL ); // Create pthread to capture input
220+ randomApple ();
221+ while (1 ) {
222+ show ();
223+
224+ usleep (delay );
225+
226+ isFail (); // Judge if will eat self
227+ canEat (); // Judge if will eat *
228+ switch (direction ) // choose which direction to move
229+ {
230+ case 1 : // Right
231+ {
232+ moveRight ();
233+ break ;
234+ }
235+ case 2 : // Up
236+ {
237+ moveUp ();
238+ break ;
239+ }
240+ case 3 : // Left
241+ {
242+ moveLeft ();
243+ break ;
244+ }
245+ case 4 : // Down
246+ {
247+ moveDown ();
248+ break ;
249+ }
250+ case -1 : // Exit
251+ {
252+ pthread_cancel (pid ); // Close pthread
253+ return -1 ;
254+ break ;
255+ }
256+ }
257+ }
258+
259+ return 0 ;
260+ }
0 commit comments