推箱子小游戏

#include<iostream>
#include<Windows.h>
#include<graphics.h>
#include<conio.h>
using namespace std;
#define SCREEN_WIDTH 960
#define SCREEN_HEIGHT 768
#define SQUARE 61
#define LINE 9
#define ROW 12
#define POSITION_X 100
#define POSITION_Y 150
#define KEY_UP  'w'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_DOWN 's'
#define KEY_QUIT 'q'
#define isVist(pos) pos.x >=0 && pos.x < LINE && pos.y >= 0 && pos.y < ROW
enum _RPOPS {
	WALL,
	FLOOR,
	BOX_DES,
	MAN,
	BOX,
	HIT,
	ALL,
};
enum _DIRECTION {
	UP,
	DOWS,
	LEFT,
	RLGHT
};
IMAGE images[ALL];
int map[LINE][ROW] = {
	{ 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 isState;
int num = 0;
struct _POS {
	int x;
	int y;
};
struct _POS man;
void setUp(IMAGE* bg) {
	putimage(0, 0, bg);
	settextcolor(WHITE);
	settextstyle(20, 0, _T("宋体"));
	RECT rec = { 0,0,SCREEN_WIDTH ,SCREEN_HEIGHT };
	drawtext(_T("恭喜你~\n终于成为一名合格的老司机"), &rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	
}
bool end() {
	for (int i = 0; i < LINE; i++) {
		for (int j = 0; j < ROW; j++) {
			if (map[i][j] == BOX_DES) {
				return false;
			}
		}
	}
	return true;
}
void change(struct _POS *next, enum _RPOPS pos) {
	map[next->x][next->y] = pos;
	putimage(POSITION_X + SQUARE * next->y, POSITION_Y + SQUARE * next->x, &images[pos]);
}
void gramCount(enum _DIRECTION direvs) {
	num++;
	struct _POS next_pos;
	struct _POS next_next_pos;
	struct _POS next_tt_pos;
	next_pos = man;
	next_next_pos = man;
	next_tt_pos = man;
	switch (direvs) {
	case UP:
		next_pos.x--;
		next_tt_pos.x -= 2;
		next_next_pos.x -= 2;
		break;
	case DOWS:
		next_pos.x++;
		next_tt_pos.x += 2;
		next_next_pos.x += 2;
		break;
	case LEFT:
		next_pos.y--;
		next_tt_pos.y -= 2;
		next_next_pos.y -= 2;
		break;
	case RLGHT:
		next_pos.y++;
		next_tt_pos.y += 2;
		next_next_pos.y += 2;
		break;
	}
	//next_pos.x >=0 && next_pos.x < LINE && next_pos.y >= 0 && next_pos.y < ROW
		if (isVist(next_pos) && map[next_pos.x][next_pos.y] == FLOOR) {
			change(&next_pos ,MAN);
			if (isState) {
				change(&man, BOX_DES);
				isState = false;
			}
			else {
				change(&man, FLOOR);
			}
			man = next_pos;
		}
		else if (isVist(next_pos) && map[next_pos.x][next_pos.y] == BOX_DES) {
			isState = true;
			change(&next_pos, MAN);
			change(&man, FLOOR);
			/*change(&next_pos, MAN);
			change(&next_tt_pos, BOX_DES);
			change(&man, FLOOR);*/
			man = next_pos;
		}
		else if (isVist(next_next_pos) && map[next_pos.x][next_pos.y] == BOX) {
			if (map[next_next_pos.x][next_next_pos.y] == FLOOR) {
				change(&next_next_pos, BOX);
				change(&next_pos, MAN);
				if (isState) {
					change(&man, BOX_DES);
					isState = false;
				}
				else {
					change(&man, FLOOR);
				}
				change(&man, FLOOR);
				man = next_pos;
			}
			else if (map[next_next_pos.x][next_next_pos.y] == BOX_DES) {
				change(&next_next_pos, HIT);
				change(&next_pos, MAN);
				change(&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[WALL],_T("wall_right.bmp"), SQUARE, SQUARE, true);
	loadimage(&images[FLOOR], _T("floor.bmp"), SQUARE, SQUARE, true);
	loadimage(&images[BOX_DES], _T("des.bmp"), SQUARE, SQUARE, true);
	loadimage(&images[MAN], _T("man.bmp"), SQUARE, SQUARE, true);
	loadimage(&images[BOX], _T("box.bmp"), SQUARE, SQUARE, true);
	loadimage(&images[HIT], _T("box.bmp"), SQUARE, SQUARE, true);
	for (int i = 0; i < LINE; i++) {
		for (int j = 0; j < ROW; j++) {
			if (map[i][j] == MAN) {
				man.x = i;
				man.y = j;
			}
			putimage(POSITION_X + SQUARE * j, POSITION_Y + SQUARE * i, &images[map[i][j]]);
		}
	}
	bool exit = false;
	do {
		if (_kbhit) {
			char ch = _getch();
			if (KEY_UP == ch) {
				gramCount(UP);
			}
			else if (KEY_DOWN == ch) {
				gramCount(DOWS);
			}
			else if (KEY_LEFT == ch) {
				gramCount(LEFT);
			}
			else if (KEY_RIGHT == ch) {
				gramCount(RLGHT);
			}
			else if (KEY_QUIT == ch) {
				exit = true;
			}
			if (num >= 40) {
				setUp(&bg_img);
			}
			if (end()) {
				setUp(&bg_img);
			}
		}
		Sleep(100);
	} while (exit == false);
	system("pause");
	closegraph();
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值