#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 맵 [2] [5]
#define Des2 맵 [2] [8]
#define Des3 맵 [4] [3]
#define Des4 맵 [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; 이미지 이미지 [전체]; 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; 구조체 직접 pos_next = 사람; switch (input) { case UP : pos.x–; pos_next.x- = 2; 단절; case DOWN : pos.x ++; pos_next.x + = 2; 단절; case LEFT : pos.y–; pos_next.y- = 2; 단절; case RIGHT : pos.y ++; pos_next.y + = 2; 단절;
}
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 ( "Song Ti")); 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;
}
//