原来
if(direct == UP){//先处理前进方向是地板的情况,向上就是X-1
if((x-1)>0 && map[x-1][y] == FLOOR){
changeMap(x-1,y,MAN);//小人前进一格
man.x=x-1;
changeMap(x,y,FLOOR);//原来人的位置变为地板
}
}else if(direct == DOWN){
if((x+1)<LINE && map[x+1][y] == FLOOR){
changeMap(x+1,y,MAN);
man.x=x+1;
changeMap(x,y,FLOOR);
}
}else if(direct == LEFT){
if((y-1)>=0 && map[x][y-1] == FLOOR){
changeMap(x,y-1,MAN);
man.y=y-1;
changeMap(x,y,FLOOR);
}
}else if(direct == RIGHT){
if((y+1)<COLUMN && map[x][y+1] == FLOOR){
changeMap(x,y+1,MAN);
man.y=y+1;
changeMap(x,y,FLOOR);
}
}
}
优化
struct _POS next_pos = man;//下一步的位置
switch(direct){
case UP:
next_pos.x--;
break;
case DOWN:
next_pos.x++;
break;
case LEFT:
next_pos.y--;
break;
case RIGHT:
next_pos.y++;
break;
}
// 1,第二行map[][]改为下一步的位置
// 2,最后两行位置互换,人的位置最后改
if(direct == UP){//先处理前进方向是地板的情况,向上就是X-1
if((x-1)>0 && map[next_pos.x][next_pos.y] == FLOOR){
changeMap(next_pos.x,next_pos.y,MAN);//小人前进一格
changeMap(man.x,man.y,FLOOR);//原来人的位置变为地板
man=next_pos;
}
}else if(direct == DOWN){
if((x+1)<LINE && map[next_pos.x][next_pos.y] == FLOOR){
changeMap(next_pos.x,next_pos.y,MAN);//小人前进一格
changeMap(man.x,man.y,FLOOR);//原来人的位置变为地板
man=next_pos;
}
}else if(direct == LEFT){
if((y-1)>=0 && map[next_pos.x][next_pos.y] == FLOOR){
changeMap(next_pos.x,next_pos.y,MAN);//小人前进一格
changeMap(man.x,man.y,FLOOR);//原来人的位置变为地板
man=next_pos;
}
}else if(direct == RIGHT){
if((y+1)<COLUMN && map[next_pos.x][next_pos.y] == FLOOR){
changeMap(next_pos.x,next_pos.y,MAN);//小人前进一格
changeMap(man.x,man.y,FLOOR);//原来人的位置变为地板
man=next_pos;
}
}
}
进而
struct _POS next_pos = man;//下一步的位置
switch(direct){
case UP:
next_pos.x--;
break;
case DOWN:
next_pos.x++;
break;
case LEFT:
next_pos.y--;
break;
case RIGHT:
next_pos.y++;
break;
}//合法性判断太长了
if(next_pos.x>=0 && next_pos.x<LINE && next_pos.y>=0 && next_pos.y<COLUMN &&map[next_pos.x][next_pos.y] == FLOOR){
changeMap(next_pos.x,next_pos.y,MAN);//小人前进一格
changeMap(man.x,man.y,FLOOR);//原来人的位置变为地板
man=next_pos;
}
}
#define isValid(pos) pos.x>=0 && pos.x<LINE && pos.y>=0 && pos.y<COLUMN
//用宏函数
if(isValid(next_pos) &&map[next_pos.x][next_pos.y] == FLOOR){
void changeMap(struct _POS *pos,enum _PROPS prop){
map[pos->x][pos->y]=prop;
putimage(START_X+pos->y * RATIO,START_Y+pos->x * RATIO,&images[prop]);
}
}//合法性判断太长,用宏函数展开
if(isValid(next_pos) &&map[next_pos.x][next_pos.y] == FLOOR){
changeMap(&next_pos,MAN);//小人前进一格
changeMap(&man,FLOOR);//原来人的位置变为地板
man=next_pos;
}
}
最终版
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<conio.h>//热键头文件
using namespace std;
#define RATIO 61
#define SCREEN_WIDTH 960
#define SCREEN_HEIGHT 768
//控制键 上下左右
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_RIGHT 'd'
#define KEY_LEFT 'a'
#define KEY_QUIT 'q'
#define LINE 9
#define COLUMN 12
#define isValid(pos) pos.x>=0 && pos.x<LINE && pos.y>=0 && pos.y<COLUMN
#define START_X 100
#define START_Y 150
enum _PROPS{
WALL, //墙
FLOOR, //地板
BOX_DES, //箱子目的地
MAN, //小人
BOX, //箱子
HIT, //箱子命中目标
ALL //代表前面所有道具的数量
};
enum _DIRECTION
{
UP,
DOWN,
LEFT,
RIGHT
};
struct _POS{
int x;//小人所在的二维数组的行
int y;//小人所在的二维数组的列
};
IMAGE images[ALL];
struct _POS man;
//游戏地图
int map[LINE][COLUMN] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
{ 0, 1, 4, 1, 0, 2, 1, 0, 2, 1, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0 },
{ 0, 1, 0, 2, 0, 1, 1, 4, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 3, 1, 1, 1, 4, 1, 0 },
{ 0, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
//判断游戏是否结束,如果不存在任何一个箱子目的地,就代表游戏结束
bool isGameOver(){
for (int i=0;i<LINE;i++){
for(int j=0;j<COLUMN;j++){
if(map[i][j] == BOX_DES)
return false;
}
}
return true;
}
void gameOverScene(IMAGE *bg){
putimage(0,0,bg);
settextcolor(WHITE);
RECT rec = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};
settextstyle(20,0,_T("楷体"));
drawtext(_T("恭喜你\n你已经成为了一名合格的搬箱子老司机了"),&rec,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
//改变游戏地图视图中一格对应的道具并重新显示
//输入:line-道具在地图数组的行下标
//column-道具在地图数组的列下标
//prop -道具的类型
void changeMap(struct _POS *pos,enum _PROPS prop){
map[pos->x][pos->y]=prop;
putimage(START_X+pos->y * RATIO,START_Y+pos->x * RATIO,&images[prop]);
}
void gameControl(enum _DIRECTION direct){
struct _POS next_pos = man;//下一步的位置
struct _POS next_next_pos = man;//下两步位置
switch(direct){
case UP:
next_pos.x--;
next_next_pos.x-=2;
break;
case DOWN:
next_pos.x++;
next_next_pos.x+=2;
break;
case LEFT:
next_pos.y--;
next_next_pos.y-=2;
break;
case RIGHT:
next_pos.y++;
next_next_pos.y+=2;
break;
}//合法性判断太长,用宏函数展开
if(isValid(next_pos) &&map[next_pos.x][next_pos.y] == FLOOR){
changeMap(&next_pos,MAN);//小人前进一格
changeMap(&man,FLOOR);//原来人的位置变为地板
man=next_pos;
}else if(isValid(next_next_pos) &&map[next_pos.x][next_pos.y] == BOX){//人的前面是箱子
//两种情况,箱子前面是地板,箱子前面是箱子目的地
if(map[next_next_pos.x][next_next_pos.y] == FLOOR){
changeMap(&next_next_pos,BOX);
changeMap(&next_pos,MAN);//小人前进一格
changeMap(&man,FLOOR);//原来人的位置变为地板
man=next_pos;
}else if(map[next_next_pos.x][next_next_pos.y] == BOX_DES){
changeMap(&next_next_pos,HIT);
changeMap(&next_pos,MAN);//小人前进一格
changeMap(&man,FLOOR);//原来人的位置变为地板
man=next_pos;
}
}
}
int main(void){
IMAGE bg_img;
initgraph(SCREEN_WIDTH,SCREEN_HEIGHT);
loadimage(&bg_img,_T("blackground.bmp"),
SCREEN_WIDTH,SCREEN_HEIGHT);
putimage(0,0,&bg_img);
loadimage(&images[0],_T("wall.bmp"),RATIO,RATIO,true);
loadimage(&images[1],_T("floor.bmp"),RATIO,RATIO,true);
loadimage(&images[2],_T("des.bmp"),RATIO,RATIO,true);
loadimage(&images[3],_T("man.bmp"),RATIO,RATIO,true);
loadimage(&images[4],_T("box.bmp"),RATIO,RATIO,true);
loadimage(&images[5],_T("box.bmp"),RATIO,RATIO,true);
for(int i = 0;i<LINE;i++){
for(int j=0;j<COLUMN;j++){
if(map[i][j] == MAN){
man.x=i;
man.y=j;
}
putimage(START_X+j*RATIO,START_Y+i*RATIO,&images[map[i][j]]);
}
}
//游戏环节
bool quit = false;
do{
if(_kbhit()){ //玩家按键
char ch =_getch();
if(ch==KEY_UP){
gameControl(UP);//搞一个函数实现人的移动
}else if(ch==KEY_DOWN){
gameControl(DOWN);
}else if(ch==KEY_LEFT){
gameControl(LEFT);
}else if(ch==KEY_RIGHT){
gameControl(RIGHT);
}else if(ch==KEY_QUIT){
quit = true;
}
if(isGameOver()){
gameOverScene(&bg_img);
quit = true;
}
}
Sleep(100);
}while(quit ==false);
system("pause");
closegraph();
return 0;
}