推箱子
游戏特色
1、游戏同时支持鼠标和键盘操作,鼠标操作时,可点击任意你想去的地方。
2、提供回复上一步的功能,可以回到任何一步走过的路径,可以给用户足够的思考空间,使用户的智力在游戏的乐趣中得到充分的锻炼。也可以一退到底。
3、屏幕上方显示当前的关数,您可以方便地选关进行游戏。
4、在目标文件夹里,可以用记事本打开Map.dat文件,自行设计游戏关卡,加载界面及元素,添加后再次打开程序才能更新。
#include <stdio.h>
#include <stdlib.h>
#include "get_keyboard.h"
#define ROA 0
#define WAL 1
#define BOX 2
#define TER 3
#define MOU 4
#define MOUINTER (MOU+TER)
#define BOXINTER (BOX+TER)
int g_board[7][8] =
{
{0,1,1,1,1,1,1,0},
{1,0,0,0,0,0,1,1},
{1,3,0,1,1,2,0,1},
{1,0,3,3,2,0,0,1},
{1,2,0,1,0,0,0,1},
{1,0,4,0,0,0,0,1},
{1,1,1,1,1,1,1,1}
};
int mx;
int my;
void init()
{
int i,j;
for(i = 0;i < 7;i++)
{
for(j = 0;j < 8;j++)
{
if(g_board[i][j] == MOU)
{
mx = i;
my = j;
break;
}
}
}
}
void move(int stepx,int stepy)
{
int nextx = mx + stepx;
int nexty = my + stepy;
switch(g_board[nextx][nexty])
{
case ROA:
case TER:
g_board[nextx][nexty] += MOU;
g_board[mx][my] -= MOU;
mx = nextx;
my = nexty;
break;
case BOX:
case BOXINTER:
{
int nnextx = mx + 2*stepx;
int nnexty = my + 2*stepy;
int val = g_board[nnextx][nnexty];
if(val == ROA || val == TER)
{
g_board[nnextx][nnexty] += BOX;
g_board[nextx][nexty] -= BOX;
g_board[nextx][nexty] += MOU;
g_board[mx][my] -= MOU;
mx = nextx;
my = nexty;
}
}
break;
}
}
void show()
{
int i,j;
for(i = 0;i<7;i++)
{
for(j = 0;j<8;j++)
{
switch(g_board[i][j])
{
case ROA:
printf(" ");break;
case WAL:
printf("#");break;
case BOX:
case BOXINTER:
printf("@");break;
case TER:
printf("o");break;
case MOU:
case MOUINTER:
printf("&");break;
}
}
printf("\n");
}
}
void run(void)
{
while(1)
{
init();
system("clear");
show();
int key = get_keyboard();
switch(key)
{
case KEY_UP:
move(-1,0);break;
case KEY_DOWN:
move(1,0);break;
case KEY_LEFT:
move(0,-1);break;
case KEY_RIGHT:
move(0,1);break;
}
}
}
int main()
{
run();
return 0;
}
获取键位
#ifndef GETCH_H
#define GETCH_H
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <unistd.h>
typedef enum KEYBOARD
{
KEY_UP = 183,
KEY_DOWN = 184,
KEY_RIGHT = 185,
KEY_LEFT = 186,
KEY_BACKSPACE = 127,
KEY_ENTER = 10,
KEY_0 = 48,
KEY_1 = 49,
KEY_2 = 50,
KEY_3 = 51,
KEY_4 = 52,
KEY_5 = 53,
KEY_6 = 54,
KEY_7 = 55,
KEY_8 = 56,
KEY_9 = 57,
KEY_A = 65,
KEY_B = 66,
KEY_C = 67,
KEY_D = 68,
KEY_E = 69,
KEY_F = 70,
KEY_G = 71,
KEY_H = 72,
KEY_I = 73,
KEY_J = 74,
KEY_K = 75,
KEY_L = 76,
KEY_M = 77,
KEY_N = 78,
KEY_O = 79,
KEY_P = 80,
KEY_Q = 81,
KEY_R = 82,
KEY_S = 83,
KEY_T = 84,
KEY_U = 85,
KEY_V = 86,
KEY_W = 87,
KEY_X = 88,
KEY_Y = 89,
KEY_Z = 90,
KEY_a = 97,
KEY_b = 98,
KEY_c = 99,
KEY_d = 100,
KEY_e = 101,
KEY_f = 102,
KEY_g = 103,
KEY_h = 104,
KEY_i = 105,
KEY_j = 106,
KEY_k = 107,
KEY_l = 108,
KEY_m = 109,
KEY_n = 110,
KEY_o = 111,
KEY_p = 112,
KEY_q = 113,
KEY_r = 114,
KEY_s = 115,
KEY_t = 116,
KEY_u = 117,
KEY_v = 118,
KEY_w = 119,
KEY_x = 120,
KEY_y = 121,
KEY_z = 122
}KEYBOARD;
static int get_keyboard(void)
{
int ret = 0;
struct termios old;
ret=tcgetattr(STDIN_FILENO,&old);
if(0 > ret)
{
perror("tcgetattr");
return -1;
}
struct termios new = old;
new.c_lflag &= ~(ICANON|ECHO);
ret= tcsetattr(STDIN_FILENO,TCSANOW,&new);
if(0 > ret)
{
perror("tcsetattr");
return -2;
}
int key_value = 0;
do
{
key_value += getchar();
}while(stdin->_IO_read_end - stdin->_IO_read_ptr);
ret = tcsetattr(STDIN_FILENO,TCSANOW,&old);
if(0 > ret)
{
perror("tcsetattr");
return -3;
}
return key_value;
}
#endif
|