博客搬家了,最近同时更新,没准什么时候就完全搬走了-_-
http://blog.just666.cn 这个是新博客地址,希望大家多多关照
基于Linux C语言的字符界面《推箱子》
编写环境:Ubuntu14.04 + Vim + GCC
如果想在Windows环境下编译的话,要注释掉16.17.18行的预编译处理,还要注释掉getch()函数的声明以及定义,最后再添加上conio.h的头文件。
代码下载:http://www.oschina.net/code/snippet_2309129_51582
遇到的一些问题:Windows环境下conio.h头文件里有一个getch()函数,能以输入流的方式(按下之后马上就有反映,不需要按回车)输入。但是Linux下没有conio.h头文件,也没有getch()函数。毕竟每次按下一个操作按键还得去按回车,操作真的是有点逆天。在Linux下实现getch()的功能是在http://my.oschina.net/yougui/blog/111345%20http://my.oschina.net/yougui/blog/111345这里找到的(感谢幽鬼)。貌似是通过临时修改终端属性,达到的这个功能。
/*******************************************************************
* Project name : push the boxs
* Create date : 2015.10.17
* Last modify date : 2015.10.19
* Auther name : mouse_ts
* E-mail address : [email protected]
* Description : this is game, you control a boy push the boxs
* to the destination. but you can't push the stone and two boxs.
* if you'r box touch the wall , you can't pull it.
* ****************************************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <termios.h>//using getch()
#include <unistd.h>
#include <assert.h>
//this is constant
#define TRUE 1
#define FALSE 0
#define MAX 10
#define WALL 6
#define PLAYER 7
#define BOX 8
#define BLANK 5
#define DES 9
#define W 'w'
#define A 'a'
#define S 's'
#define D 'd'
//this is game map
int map[MAX][MAX];
int ok = 0;
//player
struct player
{
int x;
int y;
}player;
//boxs
struct box
{
int x;
int y;
}box_1, box_2, box_3;
//des
struct des
{
int x;
int y;
}des_1, des_2, des_3;
//statement function
void initMap(); //init the map
void initPlayer(); //init the player
void initBox(); //init the boxs
void initDes(); //init the des
void printMap(); //print the map
void setMap(); //set the player, boxs, des
char getch(); //getch()
void goUP(); //go up
void goDown(); //go down
void goLeft(); //go left
void goRight(); //go right
int computingSuccess();//computing how many box seccessd
int main()
{
char ch;
system("clear");
//init the game
initMap();
initPlayer();
initBox();
setMap();
printMap();
//control the boy
while (ch = getch())
{
switch(ch)//where is the boy move
{
case W:
goUP();
break;
case A:
goLeft();
break;
case S:
goDown();
break;
case D:
goRight();
break;
defualt:
printf ("You should press w, a, s, d to control the boy to move\n");
}
setMap();
system("clear");
printMap();
if (computingSuccess() == 3)