#include<graphics.h>
#include<Windows.h>
#include<stdio.h>
#include
#include<stdlib.h>
#include
#include<conio.h>
#define RATIO 61
#define Distance_UP 125
#define Distance_LETF 110
#define ROW 9
#define COLUMN 12
#define Des1 map[2][5]
#define Des2 map[2][8]
#define Des3 map[4][3]
#define Des4 map[6][2]
#define LONG 960
#define WIDE 768
#define Valid(pos) pos.x<ROW&&pos.x>=0&&pos.y>=0&&pos.y<COLUMN
#define UP ‘w’
#define DOWN ‘s’
#define LEFT ‘a’
#define RIGHT ‘d’
#define goout ‘q’
struct direct{
int x;
int y;
};
int map[ROW][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 }, };
enum POS{
WALL,
FLOOR,
BOX_DES,
MAN,
BOX,
HIT,
ALL
};
struct direct man;
IMAGE img[ALL];
void ChangeMap(struct direct pos,enum POS prop){
map[pos->x][pos->y] = prop;
putimage((pos->y) RATIO+Distance_LETF ,(pos->x)* RATIO+Distance_UP,&img[prop]);
}
void GameControl(char input){
//坐标的转换
struct direct pos = man;
struct direct pos_next = man;
switch(input){
case UP:
pos.x–;
pos_next.x-=2;
break;
case DOWN:
pos.x++;
pos_next.x+=2;
break;
case LEFT:
pos.y–;
pos_next.y-=2;
break;
case RIGHT:
pos.y++;
pos_next.y+=2;
break;
}
if(Valid(pos)&&map[pos.x][pos.y]==FLOOR||map[pos.x][pos.y]==BOX_DES){
if(map[man.x][man.y]==Des1||map[man.x][man.y]==Des2||map[man.x][man.y]==Des3||map[man.x][man.y]==Des4)
{
ChangeMap(&pos,MAN);//人向前走一步
ChangeMap(&man,BOX_DES);
man = pos;
}else{
ChangeMap(&pos,MAN);//人向前走一步
ChangeMap(&man,FLOOR);
man = pos;
}
}else if(Valid(pos)&&map[pos.x][pos.y]==HIT&&map[pos_next.x][pos_next.y]==FLOOR){
ChangeMap(&pos,MAN);//人向前走一步
ChangeMap(&man,FLOOR);
ChangeMap(&pos_next,BOX);
man = pos;
}
else if(Valid(pos_next)&&map[pos.x][pos.y]==BOX)
if(map[pos_next.x][pos_next.y]==FLOOR){
ChangeMap(&pos,MAN);//人向前走一步
ChangeMap(&man,FLOOR);
ChangeMap(&pos_next,BOX);
man = pos;
}else if(map[pos_next.x][pos_next.y]==BOX_DES){//箱子前面是目的地
ChangeMap(&pos,MAN);//人向前走一步
ChangeMap(&man,FLOOR);
ChangeMap(&pos_next,HIT);
man = pos;
}
}
bool GameJude(){
for(int i=0;i<9;i++){
for(int j=0;j<12;j++){
if(map[i][j]==BOX){
return false;
}
}
}
return true;
}
void WinnerDisplay(IMAGE *img){
putimage(0,0,img);
settextcolor(WHITE);
RECT rec={0,0,LONG,WIDE};
settextstyle(20,0,_T(“宋体”));
drawtext(_T(“恭喜您~\n成为了一个合格的推箱子老司机!”),&rec,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
int main(void){
//设置大小及添加背景
IMAGE bg_img;
initgraph(960,768);
loadimage(&bg_img,_T("blackground.bmp"),960,768,true);
putimage(0,0,&bg_img);
//地图初始化准备
loadimage(&img[0],_T("wall.bmp"),RATIO,RATIO,true);
loadimage(&img[1],_T("floor.bmp"),RATIO,RATIO,true);
loadimage(&img[2],_T("des.bmp"),RATIO,RATIO,true);
loadimage(&img[3],_T("man.bmp"),RATIO,RATIO,true);
loadimage(&img[4],_T("box.bmp"),RATIO,RATIO,true);
loadimage(&img[5],_T("box.bmp"),RATIO,RATIO,true);
//地图初始化
for(int i=0;i<9;i++){
for(int j=0;j<12;j++){
if(map[i][j]==MAN){
man.x=i;
man.y=j;
}
putimage(Distance_LETF+j*RATIO,Distance_UP +i*RATIO,&img[map[i][j]]);
}
}
bool qiut = false;
do{
if(_kbhit()){
char ch;
ch = _getch();
if(ch==UP){
GameControl(UP);
}else if(ch==DOWN){
GameControl(DOWN);
}else if(ch==LEFT){
GameControl(LEFT);
}else if(ch==RIGHT){
GameControl(RIGHT);
}else if(ch==goout){
return true;
}
if( GameJude()){
WinnerDisplay(&bg_img);
qiut = true;
}
Sleep(100);
}
}while(qiut==false);
closegraph();
system("pause");
return 0;
}
//