您的位置:首页 > 运维架构 > Linux

基于Linux C语言的字符界面《推箱子》

2015-10-19 22:16 537 查看
博客搬家了,最近同时更新,没准什么时候就完全搬走了-_-

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   : michaelhaozi@hotmail.com
* 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)
break;
else
continue;
}
system("clear");
printf ("\n\n\n\n\n\n\n             You win the game!\n");
getch();
system("clear");
return 0;
}

//getch()   by.YouGui   http://my.oschina.net/yougui/blog/111345 char getch()
{
int c = 0;
struct termios org_opts, new_opts;
int res = 0;
res = tcgetattr(STDIN_FILENO, &org_opts);
assert(res == 0);
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c = getchar();
res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res == 0);
return c;
}

//init this map
void initMap()
{
int i, j;

for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX; j++)
{
map[i][j] = WALL;
}
}
for (i = 2; i < 8; i++)
{
map[i][2] = BLANK;
map[i][3] = BLANK;
map[i][5] = BLANK;
map[i][6] = BLANK;
map[i][7] = BLANK;
}
map[5][4] = BLANK;

initDes();
}

//print map
void printMap()
{
printf ("This is a game !\n");

int i, j;
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX; j++)
{
if (map[i][j] == WALL)
printf (" # ");
else if (map[i][j] == BOX)
printf (" @ ");
else if (map[i][j] == PLAYER)
printf (" X ");
else if (map[i][j] == BLANK)
printf ("   ");
else if (map[i][j] == DES)
printf (" O ");
}
printf ("\n");
}
}

//init the player
void initPlayer()
{
player.x = 2;
player.y = 2;
}

//init the boxs
void initBox()
{
box_1.x = 3;
box_1.y = 6;

box_2.x = 4;
box_2.y = 3;

box_3.x = 6;
box_3.y = 3;
}

//init the des
void initDes()
{
des_1.x = 5;
des_1.y = 7;

des_2.x = 6;
des_2.y = 7;

des_3.x = 7;
des_3.y = 7;
}

//set map
void setMap()
{
int i, j;

//set blank
for (i = 2; i < 8; i++)
{
map[i][2] = BLANK;
map[i][3] = BLANK;
map[i][5] = BLANK;
map[i][6] = BLANK;
map[i][7] = BLANK;
}
map[5][4] = BLANK;

//set des
map[des_1.x][des_1.y] = DES;
map[des_2.x][des_2.y] = DES;
map[des_3.x][des_3.y] = DES;

//set player
map[player.x][player.y] = PLAYER;

//set box
map[box_1.x][box_1.y] = BOX;
map[box_2.x][box_2.y] = BOX;
map[box_3.x][box_3.y] = BOX;
}

//computing the success move the box to the des
int computingSuccess()
{
int num = 0;
if (map[des_1.x][des_1.y] == BOX)
num++;
if (map[des_2.x][des_2.y] == BOX)
num++;
if (map[des_3.x][des_3.y] == BOX)
num++;
return num;
}

/*
* after this is control your boy to move up down left and right
* all of the  functions to control the boy to move
*/

//control the boy go up
void goUP()
{
if (map[player.x - 1][player.y] == BLANK ||
map[player.x - 1][player.y] == DES)
{
player.x--;
return ;
}
if (player.x - 1 == box_1.x && player.y == box_1.y &&
map[box_1.x - 1][box_1.y] == BLANK ||
player.x - 1 == box_1.x && player.y == box_1.y &&
map[box_1.x - 1][box_1.y] == DES)
{
box_1.x--;
player.x--;
return ;
}
else if (player.x - 1 == box_2.x && player.y == box_2.y &&
map[box_2.x - 1][box_2.y] == BLANK ||
player.x - 1 == box_2.x && player.y == box_2.y &&
map[box_2.x - 1][box_2.y] == DES)
{
box_2.x--;
player.x--;
return ;
}
else if (player.x - 1 == box_3.x && player.y == box_3.y &&
map[box_3.x - 1][box_3.y] == BLANK ||
player.x - 1 == box_3.x && player.y == box_3.y &&
map[box_3.x - 1][box_3.y] == DES)
{
box_3.x--;
player.x--;
return ;
}
}

//control the boy go down
void goDown()
{
if (map[player.x + 1][player.y] == BLANK ||
map[player.x + 1][player.y] == DES)
player.x++;
if (player.x + 1 == box_1.x && player.y == box_1.y &&
map[box_1.x + 1][box_1.y] == BLANK ||
player.x + 1 == box_1.x && player.y == box_1.y &&
map[box_1.x + 1][box_1.y] == DES)
{
box_1.x++;
player.x++;
return ;
}
else if (player.x + 1 == box_2.x && player.y == box_2.y &&
map[box_2.x + 1][box_2.y] == BLANK ||
player.x + 1 == box_2.x && player.y == box_2.y &&
map[box_2.x + 1][box_2.y] == DES)
{
box_2.x++;
player.x++;
return ;
}
else if (player.x + 1 == box_3.x && player.y == box_3.y &&
map[box_3.x + 1][box_3.y] == BLANK ||
player.x + 1 == box_3.x && player.y == box_3.y &&
map[box_3.x + 1][box_3.y] == DES)
{
box_3.x++;
player.x++;
return ;
}
}

//control the boy go left
void goLeft()
{
if (map[player.x][player.y - 1] == BLANK ||
map[player.x][player.y - 1] == DES)
player.y--;
if (player.x == box_1.x && player.y - 1 == box_1.y &&
map[box_1.x][box_1.y - 1] == BLANK ||
player.x == box_1.x && player.y - 1 == box_1.y &&
map[box_1.x][box_1.y - 1] == DES)
{
box_1.y--;
player.y--;
return ;
}
else if (player.x == box_2.x && player.y - 1 == box_2.y &&
map[box_2.x][box_2.y - 1] == BLANK ||
player.x == box_2.x && player.y - 1 == box_2.y &&
map[box_2.x][box_2.y - 1] == DES)
{
box_2.y--;
player.y--;
return ;
}
else if (player.x == box_3.x && player.y - 1 == box_3.y &&
map[box_3.x][box_3.y - 1] == BLANK ||
player.x == box_3.x && player.y - 1 == box_3.y &&
map[box_3.x][box_3.y - 1] == DES)
{
box_3.y--;
player.y--;
return ;
}
}

//control the boy go right
void goRight()
{
if (map[player.x][player.y + 1] == BLANK ||
map[player.x][player.y + 1] == DES)
player.y++;
if (player.x == box_1.x && player.y + 1 == box_1.y &&
map[box_1.x][box_1.y + 1] == BLANK ||
player.x == box_1.x && player.y + 1 == box_1.y &&
map[box_1.x][box_1.y + 1] == DES)
{
box_1.y++;
player.y++;
return ;
}
else if (player.x == box_2.x && player.y + 1 == box_2.y &&
map[box_2.x][box_2.y + 1] == BLANK ||
player.x == box_2.x && player.y + 1 == box_2.y &&
map[box_2.x][box_2.y + 1] == DES)
{
box_2.y++;
player.y++;
return ;
}
else if (player.x == box_3.x && player.y + 1 == box_3.y &&
map[box_3.x][box_3.y + 1] == BLANK ||
player.x == box_3.x && player.y + 1 == box_3.y &&
map[box_3.x][box_3.y + 1] == DES)
{
box_3.y++;
player.y++;
return ;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: