Shawn's Blog
目录 · 3 节

C语言 推箱子 gcc编译通过

#C

0X00 编译环境

Ubuntu 14.04 + GNU/gcc 如果要在Windows下编译的话,* 可能 * 需要注释掉16-18的编译预处理,还 * 可能 * 要注释掉getch()的函数声明及定义,最后在加上conio.h的头文件。

0X01 遇到的问题

Windows中能使用getch()函数,这个函数是以输入流的方式输入。(简单地说就是按下去一个按键就能有反应,而不用点击回车)。但是在Linux环境下没有这个函数也没有connio.h的头文件。但是每次按一下还要按回车还是挺逆天的。不过我在网上找到了替代品(感谢 幽鬼

http://my.oschina.net/yougui/blog/111345

0X02 代码实现

c
1/*******************************************************************
2 * Project name     : push the boxs
3 * Create date      : 2015.10.17
4 * Last modify date : 2015.10.19
5 * Auther name      : mouse_ts
6 * E-mail address   : michaelhaozi@hotmail.com
7 * Description      : this is game, you control a boy push the boxs
8 * to the destination. but you can't push the stone and two boxs.
9 * if you'r box touch the wall , you can't pull it.
10 * ****************************************************************/
11
12#include <stdio.h>
13#include <string.h>
14#include <math.h>
15#include <stdlib.h>
16#include <termios.h>//using getch()
17#include <unistd.h>
18#include <assert.h>
19
20//this is constant
21#define TRUE   1
22#define FALSE  0
23#define MAX    10
24#define WALL   6
25#define PLAYER 7
26#define BOX    8
27#define BLANK  5
28#define DES    9
29#define W      'w'
30#define A      'a'
31#define S      's'
32#define D      'd'
33
34//this is game map
35int map[MAX][MAX];
36int ok = 0;
37
38//player
39struct player
40{
41    int x;
42    int y;
43}player;
44
45//boxs
46struct box
47{
48    int x;
49    int y;
50}box_1, box_2, box_3;
51
52//des
53struct des
54{
55    int x;
56    int y;
57}des_1, des_2, des_3;
58
59//statement function
60void initMap();         //init the map
61void initPlayer();      //init the player
62void initBox();         //init the boxs
63void initDes();         //init the des
64void printMap();        //print the map
65void setMap();          //set the player, boxs, des
66char getch();           //getch()
67void goUP();            //go up
68void goDown();          //go down
69void goLeft();          //go left
70void goRight();         //go right
71int  computingSuccess();//computing how many box seccessd
72
73int main()
74{
75    char ch;
76
77    system("clear");
78
79    //init the game
80    initMap();
81    initPlayer();
82    initBox();
83    setMap();
84    printMap();
85
86    //control the boy
87    while (ch = getch())
88    {
89        switch(ch)//where is the boy move
90        {
91            case W:
92                goUP();
93                break;
94            case A:
95                goLeft();
96                break;
97            case S:
98                goDown();
99                break;
100            case D:
101                goRight();
102                break;
103            defualt:
104                printf ("You should press w, a, s, d to control the boy to move\n");
105        }
106        setMap();
107        system("clear");
108        printMap();
109
110        if (computingSuccess() == 3)
111            break;
112        else
113            continue;
114    }
115    system("clear");
116    printf ("\n\n\n\n\n\n\n             You win the game!\n");
117    getch();
118    system("clear");
119    return 0;
120}
121
122//getch()   by.YouGui   http://my.oschina.net/yougui/blog/111345
123char getch()
124{
125    int c = 0;
126    struct termios org_opts, new_opts;
127    int res = 0;
128    res = tcgetattr(STDIN_FILENO, &org_opts);
129    assert(res == 0);
130    memcpy(&new_opts, &org_opts, sizeof(new_opts));
131    new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
132    tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
133    c = getchar();
134    res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
135    assert(res == 0);
136    return c;
137}
138
139//init this map
140void initMap()
141{
142    int i, j;
143
144    for (i = 0; i < MAX; i++)
145    {
146        for (j = 0; j < MAX; j++)
147        {
148            map[i][j] = WALL;
149        }
150    }
151    for (i = 2; i < 8; i++)
152    {
153        map[i][2] = BLANK;
154        map[i][3] = BLANK;
155        map[i][5] = BLANK;
156        map[i][6] = BLANK;
157        map[i][7] = BLANK;
158    }
159    map[5][4] = BLANK;
160
161    initDes();
162}
163
164//print map
165void printMap()
166{
167    printf ("This is a game !\n");
168
169    int i, j;
170    for (i = 0; i < MAX; i++)
171    {
172        for (j = 0; j < MAX; j++)
173        {
174            if (map[i][j] == WALL)
175                printf (" # ");
176            else if (map[i][j] == BOX)
177                printf (" @ ");
178            else if (map[i][j] == PLAYER)
179                printf (" X ");
180            else if (map[i][j] == BLANK)
181                printf ("   ");
182            else if (map[i][j] == DES)
183                printf (" O ");
184        }
185        printf ("\n");
186    }
187}
188
189//init the player
190void initPlayer()
191{
192    player.x = 2;
193    player.y = 2;
194}
195
196//init the boxs
197void initBox()
198{
199    box_1.x = 3;
200    box_1.y = 6;
201
202    box_2.x = 4;
203    box_2.y = 3;
204
205    box_3.x = 6;
206    box_3.y = 3;
207}
208
209//init the des
210void initDes()
211{
212    des_1.x = 5;
213    des_1.y = 7;
214
215    des_2.x = 6;
216    des_2.y = 7;
217
218    des_3.x = 7;
219    des_3.y = 7;
220}
221
222//set map
223void setMap()
224{
225    int i, j;
226
227    //set blank
228    for (i = 2; i < 8; i++)
229    {
230        map[i][2] = BLANK;
231        map[i][3] = BLANK;
232        map[i][5] = BLANK;
233        map[i][6] = BLANK;
234        map[i][7] = BLANK;
235    }
236    map[5][4] = BLANK;
237
238
239    //set des
240    map[des_1.x][des_1.y] = DES;
241    map[des_2.x][des_2.y] = DES;
242    map[des_3.x][des_3.y] = DES;
243
244    //set player
245    map[player.x][player.y] = PLAYER;
246
247    //set box
248    map[box_1.x][box_1.y] = BOX;
249    map[box_2.x][box_2.y] = BOX;
250    map[box_3.x][box_3.y] = BOX;
251}
252
253//computing the success move the box to the des
254int computingSuccess()
255{
256    int num = 0;
257    if (map[des_1.x][des_1.y] == BOX)
258        num++;
259    if (map[des_2.x][des_2.y] == BOX)
260        num++;
261    if (map[des_3.x][des_3.y] == BOX)
262        num++;
263    return num;
264}
265
266/*
267 * after this is control your boy to move up down left and right
268 * all of the  functions to control the boy to move
269 */
270
271//control the boy go up
272void goUP()
273{
274    if (map[player.x - 1][player.y] == BLANK ||
275        map[player.x - 1][player.y] == DES)
276    {
277        player.x--;
278        return ;
279    }
280    if (player.x - 1 == box_1.x && player.y == box_1.y &&
281        map[box_1.x - 1][box_1.y] == BLANK ||
282        player.x - 1 == box_1.x && player.y == box_1.y &&
283        map[box_1.x - 1][box_1.y] == DES)
284    {
285        box_1.x--;
286        player.x--;
287        return ;
288    }
289    else if (player.x - 1 == box_2.x && player.y == box_2.y &&
290             map[box_2.x - 1][box_2.y] == BLANK ||
291             player.x - 1 == box_2.x && player.y == box_2.y &&
292             map[box_2.x - 1][box_2.y] == DES)
293    {
294        box_2.x--;
295        player.x--;
296        return ;
297    }
298    else if (player.x - 1 == box_3.x && player.y == box_3.y &&
299             map[box_3.x - 1][box_3.y] == BLANK ||
300             player.x - 1 == box_3.x && player.y == box_3.y &&
301             map[box_3.x - 1][box_3.y] == DES)
302    {
303        box_3.x--;
304        player.x--;
305        return ;
306    }
307}
308
309//control the boy go down
310void goDown()
311{
312    if (map[player.x + 1][player.y] == BLANK ||
313        map[player.x + 1][player.y] == DES)
314        player.x++;
315    if (player.x + 1 == box_1.x && player.y == box_1.y &&
316        map[box_1.x + 1][box_1.y] == BLANK ||
317        player.x + 1 == box_1.x && player.y == box_1.y &&
318        map[box_1.x + 1][box_1.y] == DES)
319    {
320        box_1.x++;
321        player.x++;
322        return ;
323    }
324    else if (player.x + 1 == box_2.x && player.y == box_2.y &&
325             map[box_2.x + 1][box_2.y] == BLANK ||
326             player.x + 1 == box_2.x && player.y == box_2.y &&
327             map[box_2.x + 1][box_2.y] == DES)
328    {
329        box_2.x++;
330        player.x++;
331        return ;
332    }
333    else if (player.x + 1 == box_3.x && player.y == box_3.y &&
334             map[box_3.x + 1][box_3.y] == BLANK ||
335             player.x + 1 == box_3.x && player.y == box_3.y &&
336             map[box_3.x + 1][box_3.y] == DES)
337    {
338        box_3.x++;
339        player.x++;
340        return ;
341    }
342}
343
344//control the boy go left
345void goLeft()
346{
347    if (map[player.x][player.y - 1] == BLANK ||
348        map[player.x][player.y - 1] == DES)
349        player.y--;
350    if (player.x == box_1.x && player.y - 1 == box_1.y &&
351        map[box_1.x][box_1.y - 1] == BLANK ||
352        player.x == box_1.x && player.y - 1 == box_1.y &&
353        map[box_1.x][box_1.y - 1] == DES)
354    {
355        box_1.y--;
356        player.y--;
357        return ;
358    }
359    else if (player.x == box_2.x && player.y - 1 == box_2.y &&
360             map[box_2.x][box_2.y - 1] == BLANK ||
361             player.x == box_2.x && player.y - 1 == box_2.y &&
362             map[box_2.x][box_2.y - 1] == DES)
363    {
364        box_2.y--;
365        player.y--;
366        return ;
367    }
368    else if (player.x == box_3.x && player.y - 1 == box_3.y &&
369             map[box_3.x][box_3.y - 1] == BLANK ||
370             player.x == box_3.x && player.y - 1 == box_3.y &&
371             map[box_3.x][box_3.y - 1] == DES)
372    {
373        box_3.y--;
374        player.y--;
375        return ;
376    }
377}
378
379//control the boy go right
380void goRight()
381{
382    if (map[player.x][player.y + 1] == BLANK ||
383        map[player.x][player.y + 1] == DES)
384        player.y++;
385    if (player.x == box_1.x && player.y + 1 == box_1.y &&
386        map[box_1.x][box_1.y + 1] == BLANK ||
387        player.x == box_1.x && player.y + 1 == box_1.y &&
388        map[box_1.x][box_1.y + 1] == DES)
389    {
390        box_1.y++;
391        player.y++;
392        return ;
393    }
394    else if (player.x == box_2.x && player.y + 1 == box_2.y &&
395             map[box_2.x][box_2.y + 1] == BLANK ||
396             player.x == box_2.x && player.y + 1 == box_2.y &&
397             map[box_2.x][box_2.y + 1] == DES)
398    {
399        box_2.y++;
400        player.y++;
401        return ;
402    }
403    else if (player.x == box_3.x && player.y + 1 == box_3.y &&
404             map[box_3.x][box_3.y + 1] == BLANK ||
405             player.x == box_3.x && player.y + 1 == box_3.y &&
406             map[box_3.x][box_3.y + 1] == DES)
407    {
408        box_3.y++;
409        player.y++;
410        return ;
411    }
412}
本文标题
C语言 推箱子 gcc编译通过
文章作者
Shawn
版权声明
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

如果这篇文章对你有帮助,可以请我喝杯咖啡 ☕

评论