基于Linux C语言的字符界面《推箱子》
来源:互联网 发布:景区规划图软件 编辑:程序博客网 时间:2024/06/02 19:42
博客搬家了,最近同时更新,没准什么时候就完全搬走了-_-
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 mapint map[MAX][MAX];int ok = 0;//playerstruct player{ int x; int y;}player;//boxsstruct box{ int x; int y;}box_1, box_2, box_3;//desstruct des{ int x; int y;}des_1, des_2, des_3;//statement functionvoid initMap(); //init the mapvoid initPlayer(); //init the playervoid initBox(); //init the boxsvoid initDes(); //init the desvoid printMap(); //print the mapvoid setMap(); //set the player, boxs, deschar getch(); //getch()void goUP(); //go upvoid goDown(); //go downvoid goLeft(); //go leftvoid goRight(); //go rightint computingSuccess();//computing how many box seccessdint 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/111345char 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 mapvoid 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 mapvoid 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 playervoid initPlayer(){ player.x = 2; player.y = 2;}//init the boxsvoid 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 desvoid 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 mapvoid 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 desint 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 upvoid 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 downvoid 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 leftvoid 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 rightvoid 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 ; }}
0 0
- 基于Linux C语言的字符界面《推箱子》
- c语言的推箱子
- C语言,推箱子游戏
- C语言_推箱子
- c语言小游戏,推箱子
- c语言实现的推箱子小游戏-1
- C语言实现推箱子游戏
- 012-C语言小游戏之推箱子
- C语言-推箱子游戏代码
- C语言实现推箱子小游戏
- C语言实现推箱子游戏
- c语言控制台推箱子小游戏
- C语言之推箱子游戏代码
- C语言+EasyX编写推箱子游戏
- C语言——推箱子
- 基于Android的推箱子小游戏 源码
- 推箱子 C语言版
- C 推箱子游戏
- SQL语法 (Structured Query Language)
- 顺序栈-C++实现
- [leetcode] Repeated DNA Sequences
- Android 触摸及手势操作GestureDetector
- java同步关键字Synchronized 的实现原理
- 基于Linux C语言的字符界面《推箱子》
- swt嵌套swing
- 在一个字符串中找到第一个只出现一次的字符(毒辣写法)
- Android 学习(一)
- ios开发如何让navigationItem自带的返回按钮设置成图片或者不显示文字
- Hibernate三种状态
- SQLServer查询数据加一列序号
- Python学习笔记(一)
- find函数