C++学习路线(二十)

项目

模块划分

        推箱子游戏

                地图初始化        热键控制        推箱子控制        游戏结束

地图初始化

        坐标系(650,650)

地图表示:

        使用二维数组

        游戏道具展示(墙 箱子 箱子目的地 小人 地板)

        判断游戏结果

        控制小人前进

道具表示:

        墙:0 , 地板:1 箱子目的地:2 小人:3 箱子:4  箱子命中目标 5

初始化整张题图 和  道具

#include <iostream>
#include <graphics.h>
#include <windows.h>
using namespace std;

#define SCREEN_WIDTH 960 // 屏幕宽度
#define SCREEN_HEIGHT 768 // 屏幕高度
#define RATIO 61 // 图片缩放比例
#define LINE 9 // 行
#define COL 12 // 列
#define START_X 100 // 起始坐标X
#define START_Y 150 // 起始坐标Y
IMAGE images[6]; // 图片数组
int map[LINE][COL] = { // 地图数组
	{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}
};
void init_graphs() { // 初始化图形
	IMAGE bg_img;
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
	loadimage(&bg_img, _T("E:\\c++file\\推箱子游戏资源\\blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img);
}
void init_images() { // 初始化图片
	loadimage(&images[0], _T("E:\\c++file\\推箱子游戏资源\\wall.bmp"), RATIO, RATIO, true);
	loadimage(&images[1], _T("E:\\c++file\\推箱子游戏资源\\floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[2], _T("E:\\c++file\\推箱子游戏资源\\des.bmp"), RATIO, RATIO, true);
	loadimage(&images[3], _T("E:\\c++file\\推箱子游戏资源\\man.bmp"), RATIO, RATIO, true);
	loadimage(&images[4], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	loadimage(&images[5], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	// 地图绘制
	for (int i = 0; i < LINE; i++)
		for (int j = 0; j < COL; j++)
			putimage(START_X + j * RATIO, START_Y + i * RATIO, &images[map[i][j]]);
	// 这里需要解释下 为什么 START_X + j * RATIO, START_Y + i * RATIO
	// 这两个值是根据地图数组的大小计算出来的,因为地图数组的大小是12行9列,每个格子的大小是61*61,所以
	// 起始坐标X是100,起始坐标Y是150,然后加上i行j列的坐标乘以61,就得到了每个格子的坐标。
}
int main() {
	init_graphs();
	init_images();
	system("pause");
	return 0;
}

热键控制

#include <iostream>
#include <graphics.h>
#include <windows.h>
#include <conio.h>
using namespace std;

#define SCREEN_WIDTH 960 // 屏幕宽度
#define SCREEN_HEIGHT 768 // 屏幕高度
#define RATIO 61 // 图片缩放比例
#define LINE 9 // 行
#define COL 12 // 列
#define START_X 100 // 起始坐标X
#define START_Y 150 // 起始坐标Y
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'


IMAGE images[6]; // 图片数组
int map[LINE][COL] = { // 地图数组
	{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}
};


void init_graphs() { // 初始化图形
	IMAGE bg_img;
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
	loadimage(&bg_img, _T("E:\\c++file\\推箱子游戏资源\\blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img);
}
void init_images() { // 初始化图片
	loadimage(&images[0], _T("E:\\c++file\\推箱子游戏资源\\wall_right.bmp"), RATIO, RATIO, true);
	loadimage(&images[1], _T("E:\\c++file\\推箱子游戏资源\\floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[2], _T("E:\\c++file\\推箱子游戏资源\\des.bmp"), RATIO, RATIO, true);
	loadimage(&images[3], _T("E:\\c++file\\推箱子游戏资源\\man.bmp"), RATIO, RATIO, true);
	loadimage(&images[4], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	loadimage(&images[5], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	// 地图绘制
	for (int i = 0; i < LINE; i++)
		for (int j = 0; j < COL; j++)
			putimage(START_X + j * RATIO, START_Y + i * RATIO, &images[map[i][j]]);
	//竖着进行排列
}
int main() {
	//init_graphs();
	//init_images();
	bool quit = false;
	do {
		if (_kbhit()) {
			char key = _getch();
			if (key == KEY_UP) {
				// 向上移动
				cout << "向上移动" << endl;
			}
			else if (key == KEY_DOWN) {
				// 向下移动
				cout << "向下移动" << endl;
			}
			else if (key == KEY_LEFT) {
				// 向左移动
				cout << "向左移动" << endl;
			}
			else if (key == KEY_RIGHT) {
				// 向右移动
				cout << "向右移动" << endl;
			}
			else if (key == KEY_QUIT) {
				// 退出游戏
				quit = true;
				cout << "退出游戏" << endl;
			}
		}
	} while (quit == false);

	system("pause");
	return 0;
}

推箱子控制

初级版本(可以上下左右动):

#include <iostream>
#include <graphics.h>
#include <windows.h>
#include <conio.h>
using namespace std;

#define SCREEN_WIDTH 960 // 屏幕宽度
#define SCREEN_HEIGHT 768 // 屏幕高度
#define RATIO 61 // 图片缩放比例
#define LINE 9 // 行
#define COL 12 // 列
#define START_X 100 // 起始坐标X
#define START_Y 150 // 起始坐标Y
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'


IMAGE images[6]; // 图片数组
int map[LINE][COL] = { // 地图数组
	{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 _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

enum _PROPS {
	WALLS,
	FLOORS,
	BOX_DES,
	MAN,
	BOX,
	HIT,
	ALL
};

struct _POS {
	int x;
	int y;
};

struct _POS man; // 玩家位置


void init_graphs() { // 初始化图形
	IMAGE bg_img;
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
	loadimage(&bg_img, _T("E:\\c++file\\推箱子游戏资源\\blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img);
}
void init_images() { // 初始化图片
	loadimage(&images[WALLS], _T("E:\\c++file\\推箱子游戏资源\\wall_right.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOORS], _T("E:\\c++file\\推箱子游戏资源\\floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("E:\\c++file\\推箱子游戏资源\\des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("E:\\c++file\\推箱子游戏资源\\man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	// 地图绘制
	for (int i = 0; i < LINE; i++)
		for (int j = 0; j < COL; 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]]);
		}
	//竖着进行排列
}

void changeMap(int line, int columns, enum _PROPS props) {
	map[line][columns] = props;
	putimage(START_X + columns * RATIO, START_Y + line * RATIO, &images[props]);
}


void gameControl(enum _DIRECTION direction) {
	struct _POS nowPos = man;
	struct _POS nextPos = man;
	switch (direction) {
	case UP:
		nowPos.x = nowPos.x - 1;
		nextPos.x = nextPos.x - 2;
		break;
	case DOWN:
		nowPos.x = nowPos.x + 1;
		nextPos.x = nextPos.x + 2;
		break;
	case LEFT:
		nowPos.y = nowPos.y - 1;
		nextPos.y = nextPos.y - 2;
		break;
	case RIGHT:
		nowPos.y = nowPos.y + 1;
		nextPos.y = nextPos.y + 2;
		break;
	}
	
	if (direction == UP) {
		if ((man.x - 1) > 0 && map[man.x - 1][man.y] == FLOORS) {
			changeMap(man.x - 1, man.y, MAN);
			changeMap(man.x, man.y, FLOORS);
			man.x--;
		}
	}
	else if (direction == DOWN) {
		if ((man.x + 1) < LINE && map[man.x + 1][man.y] == FLOORS) {
			changeMap(man.x + 1, man.y, MAN);
			changeMap(man.x, man.y, FLOORS);
			man.x++;
		}
	}
	else if (direction == LEFT) {
		if ((man.y - 1) > 0 && map[man.x][man.y - 1] == FLOORS) {
			changeMap(man.x, man.y - 1, MAN);
			changeMap(man.x, man.y, FLOORS);
			man.y--;
		}
	}
	else {
		if ((man.y + 1) < COL && map[man.x][man.y + 1] == FLOORS) {
			changeMap(man.x, man.y + 1, MAN);
			changeMap(man.x, man.y, FLOORS);
			man.y++;
		}
	}
}
int main() {
	init_graphs();
	init_images();
	bool quit = false;
	do {
		if (_kbhit()) {
			char key = _getch();
			if (key == KEY_UP) {
				gameControl(UP);
			}
			else if (key == KEY_DOWN) {
				gameControl(DOWN);
			}
			else if (key == KEY_LEFT) {
				gameControl(LEFT);
			}
			else if (key == KEY_RIGHT) {
				gameControl(RIGHT);
			}
			else if (key == KEY_QUIT) {
				quit = true;
				cout << "退出游戏" << endl;
			}
		}
	} while (quit == false);

	system("pause");
	return 0;
}

最终版

#include <iostream>
#include <graphics.h>
#include <windows.h>
#include <conio.h>
using namespace std;

#define SCREEN_WIDTH 960 // 屏幕宽度
#define SCREEN_HEIGHT 768 // 屏幕高度
#define RATIO 61 // 图片缩放比例
#define LINE 9 // 行
#define COL 12 // 列
#define START_X 100 // 起始坐标X
#define START_Y 150 // 起始坐标Y
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'


IMAGE images[6]; // 图片数组
int map[LINE][COL] = { // 地图数组
	{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 _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

enum _PROPS {
	WALLS,
	FLOORS,
	BOX_DES,
	MAN,
	BOX,
	HIT,
	ALL
};

struct _POS {
	int x;
	int y;
};

struct _POS man; // 玩家位置


void init_graphs() { // 初始化图形
	IMAGE bg_img;
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
	loadimage(&bg_img, _T("E:\\c++file\\推箱子游戏资源\\blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img);
}
void init_images() { // 初始化图片
	loadimage(&images[WALLS], _T("E:\\c++file\\推箱子游戏资源\\wall_right.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOORS], _T("E:\\c++file\\推箱子游戏资源\\floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("E:\\c++file\\推箱子游戏资源\\des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("E:\\c++file\\推箱子游戏资源\\man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	// 地图绘制
	for (int i = 0; i < LINE; i++)
		for (int j = 0; j < COL; 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]]);
		}
	//竖着进行排列
}

void changeMap(int line, int columns, enum _PROPS props) {
	map[line][columns] = props;
	putimage(START_X + columns * RATIO, START_Y + line * RATIO, &images[props]);
}

void changeMap(struct _POS pos , enum _PROPS props){
	map[pos.x][pos.y] = props;
	putimage(START_X + pos.y * RATIO, START_Y + pos.x * RATIO, &images[props]);
}

bool isValid(struct _POS pos) {
	if (pos.x >= 0 && pos.x < LINE && pos.y >= 0 && pos.y < COL)
		return true;
	return false;
}

bool isOver() {
	for (int i = 0; i < LINE; i++)
		for (int j = 0; j < COL; j++)
			if (map[i][j] == BOX_DES) return false;
	return true;
}

void gameControl(enum _DIRECTION direction) {
	struct _POS nextPos = man;
	struct _POS nextnextPos = man;
	switch (direction) {
	case UP:
		nextPos.x--;
		nextnextPos.x-=2;
		break;
	case DOWN:
		nextPos.x++;
		nextnextPos.x+=2;
		break;
	case LEFT:
		nextPos.y-=1;
		nextnextPos.y-=2;
		break;
	case RIGHT:
		nextPos.y+=1;
		nextnextPos.y+=2;
		break;
	}
	if (isValid(nextPos) && map[nextPos.x][nextPos.y] == FLOORS) {
		changeMap(nextPos, MAN);
		changeMap(man, FLOORS);
		man = nextPos;
	}
	else if (isValid(nextnextPos) && map[nextPos.x][nextPos.y] == BOX) {
		if (map[nextnextPos.x][nextnextPos.y] == FLOORS) {
			changeMap(nextnextPos, BOX);
			changeMap(nextPos, MAN);
			changeMap(man, FLOORS);
			man = nextPos;
		}
		else if (map[nextnextPos.x][nextnextPos.y] == BOX_DES) {
			changeMap(nextnextPos, HIT);
			changeMap(nextPos, MAN);
			changeMap(man, FLOORS);
			man = nextPos;
		}
	}
}

void gameOverScene() {
	IMAGE bg_img;
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
	loadimage(&bg_img, _T("E:\\c++file\\推箱子游戏资源\\blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img);
	settextstyle(20, 0, _T("Arial"));
	RECT rect = { 0,0,SCREEN_WIDTH,SCREEN_HEIGHT };
	drawtext(_T("游戏结束"), &rect, DT_CENTER | DT_VCENTER);
}

int main() {
	init_graphs();
	init_images();
	bool quit = false;
	do {
		if (isOver()) {
			quit = true;
			gameOverScene();
		}
		if (_kbhit()) {
			char key = _getch();
			if (key == KEY_UP) {
				gameControl(UP);
			}
			else if (key == KEY_DOWN) {
				gameControl(DOWN);
			}
			else if (key == KEY_LEFT) {
				gameControl(LEFT);
			}
			else if (key == KEY_RIGHT) {
				gameControl(RIGHT);
			}
			else if (key == KEY_QUIT) {
				quit = true;
				cout << "退出游戏" << endl;
			}
		}
	} while (quit == false);
	system("pause");
	return 0;
}

加了增加地图的功能

#include <iostream>
#include <graphics.h>
#include <windows.h>
#include <conio.h>
#include <fstream>
using namespace std;

#define SCREEN_WIDTH 960 // 屏幕宽度
#define SCREEN_HEIGHT 768 // 屏幕高度
#define RATIO 61 // 图片缩放比例
#define LINE 9 // 行
#define COL 12 // 列
#define START_X 100 // 起始坐标X
#define START_Y 150 // 起始坐标Y
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'


IMAGE images[6]; // 图片数组
int map[LINE][COL]; // 地图数组
string mapPath[] = {"E:\\c++file\\推箱子游戏资源\\推箱子地图1.txt" , "E:\\c++file\\推箱子游戏资源\\推箱子地图2.txt"  , "E:\\c++file\\推箱子游戏资源\\推箱子地图3.txt" };
int pathIndex = 0; // 地图索引
int pathLen = 3; // 地图总数
enum _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

enum _PROPS {
	WALLS,
	FLOORS,
	BOX_DES,
	MAN,
	BOX,
	HIT,
	ALL
};

struct _POS {
	int x;
	int y;
};

struct _POS man; // 玩家位置


void init_graphs() { // 初始化图形
	IMAGE bg_img;
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
	loadimage(&bg_img, _T("E:\\c++file\\推箱子游戏资源\\blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img);
}
void init_images() { // 初始化图片
	loadimage(&images[WALLS], _T("E:\\c++file\\推箱子游戏资源\\wall_right.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOORS], _T("E:\\c++file\\推箱子游戏资源\\floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("E:\\c++file\\推箱子游戏资源\\des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("E:\\c++file\\推箱子游戏资源\\girl.jpg"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _T("E:\\c++file\\推箱子游戏资源\\box.bmp"), RATIO, RATIO, true);
	// 地图绘制
	for (int i = 0; i < LINE; i++)
		for (int j = 0; j < COL; 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]]);
		}
	//竖着进行排列
}

void changeMap(int line, int columns, enum _PROPS props) {
	map[line][columns] = props;
	putimage(START_X + columns * RATIO, START_Y + line * RATIO, &images[props]);
}

void changeMap(struct _POS pos , enum _PROPS props){
	map[pos.x][pos.y] = props;
	putimage(START_X + pos.y * RATIO, START_Y + pos.x * RATIO, &images[props]);
}

bool isValid(struct _POS pos) {
	if (pos.x >= 0 && pos.x < LINE && pos.y >= 0 && pos.y < COL)
		return true;
	return false;
}

bool isOver() {
	for (int i = 0; i < LINE; i++)
		for (int j = 0; j < COL; j++)
			if (map[i][j] == BOX_DES) return false;
	return true;
}

void gameControl(enum _DIRECTION direction) {
	struct _POS nextPos = man;
	struct _POS nextnextPos = man;
	switch (direction) {
	case UP:
		nextPos.x--;
		nextnextPos.x-=2;
		break;
	case DOWN:
		nextPos.x++;
		nextnextPos.x+=2;
		break;
	case LEFT:
		nextPos.y-=1;
		nextnextPos.y-=2;
		break;
	case RIGHT:
		nextPos.y+=1;
		nextnextPos.y+=2;
		break;
	}
	if (isValid(nextPos) && map[nextPos.x][nextPos.y] == FLOORS) {
		changeMap(nextPos, MAN);
		changeMap(man, FLOORS);
		man = nextPos;
	}
	else if (isValid(nextnextPos) && map[nextPos.x][nextPos.y] == BOX) {
		if (map[nextnextPos.x][nextnextPos.y] == FLOORS) {
			changeMap(nextnextPos, BOX);
			changeMap(nextPos, MAN);
			changeMap(man, FLOORS);
			man = nextPos;
		}
		else if (map[nextnextPos.x][nextnextPos.y] == BOX_DES) {
			changeMap(nextnextPos, HIT);
			changeMap(nextPos, MAN);
			changeMap(man, FLOORS);
			man = nextPos;
		}
	}
}

void gameOverScene() {
	IMAGE bg_img;
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
	loadimage(&bg_img, _T("E:\\c++file\\推箱子游戏资源\\blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img);
	settextstyle(20, 0, _T("Arial"));
	RECT rect = { 0,0,SCREEN_WIDTH,SCREEN_HEIGHT };
	drawtext(_T("游戏结束"), &rect, DT_CENTER | DT_VCENTER);
}

void loadMap() {
	ifstream inFile(mapPath[pathIndex]);
	if (!inFile.is_open()) {
		cout << "地图文件打开失败" << endl;
		exit(0);
	}
	for (int i = 0; i < LINE; i++) {
		for (int j = 0; j < COL; j++) {
			inFile >> map[i][j];
		}
	}
	inFile.close();
	pathIndex++;
	init_graphs();
	init_images();
}

int main() {
	loadMap();
	bool quit = false;
	do {
		if (isOver()) {
			if (pathIndex == pathLen) {
				quit = true;
				gameOverScene();
			}
			else {
				loadMap();
			}
		}
		if (_kbhit()) {
			char key = _getch();
			if (key == KEY_UP) {
				gameControl(UP);
			}
			else if (key == KEY_DOWN) {
				gameControl(DOWN);
			}
			else if (key == KEY_LEFT) {
				gameControl(LEFT);
			}
			else if (key == KEY_RIGHT) {
				gameControl(RIGHT);
			}
			else if (key == KEY_QUIT) {
				quit = true;
				cout << "退出游戏" << endl;
			}
		}
	} while (quit == false);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45169583/article/details/143047146