Implementation steps of push box game

Simplified game of pushing box

When I was reading my notes recently, I suddenly saw the small game of pushing boxes that I had done before. Here is the content of sorting out

  • (1) Game picture resources:

     Link: https://pan.baidu.com/s/1xqwhwwlh oihkscbf-ikcg extraction code: Rl (personal Baidu online disk link)
    

    Download resources for control easyX:

     www.easyx.cn
    
     Note: the graphics resources should be stored in the. cpp file of the code
    
  • (2) Project requirements:

The effect to be achieved is shown in the following figure:

The rules are as follows:

1. The box can only be pushed forward, not backward, that is to say, once pushed to the dead corner, it fails

2. The person pushing the box cannot pass through the board, the box destination and the box

3. When all the boxes are successfully pushed to the destination, the game is over, and you need to be reminded to pass the customs successfully. Insert the code piece here
  • (3) Personal analysis:

    When making a small software, I feel the most taboo is how much code I have to write at one time. I should think about all the functions of the software to be designed first. Only when we think about the functions, can we design the functions to achieve one by one. It is almost impossible for the novice to write all the functions at one time. Especially for the c + + language, I feel that it is right at the beginning When preparing and writing code to realize the software, the most important thing is to understand the purpose of writing the software is what I want to do. I need to make clear the logic structure of the software. Then I use a function to realize it. I want to divide the difficult modules into simple modules, so as to simplify the complexity

  • (4) Overall module division of the game:

1: Map initialization

Use easyX graphics library to build a background, which is used to initialize the map, coordinate system, length and width

First of all, we should understand that the x,y coordinates of easyxare a little different from what we usually understand: as follows

Code implementation:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>

using namespace std;

int main(void) {
	initgraph(800,650);	//Initialize map

	system("pause");
	return 0;
}    

Program running results:

Stage:

Subsequent code:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>

using namespace std;

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(800,650);	//Initialize map
	loadimage(&bg_img,"blackground.bmp",800,650,true); 
	putimage(0,0,&bg_img); //Put the prepared background picture into the console interface



	system("pause");
	return 0;
}

Program running results:

  • How to represent a map and how to define its elements

1. The map is only flat, so it can be described with two-dimensional array

2. Expression of props:
Wall: 0 floor: 1 box destination: 2 villains: 3 boxes: 4 boxes hit target: 5

Code implementation:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>

using namespace std;

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(800,650);	//Initialize map
	loadimage(&bg_img,"blackground.bmp",800,650,true); 
	putimage(0,0,&bg_img); //Put the prepared background picture into the console interface

	IMAGE images[6]; //There are only six elements 0-5 in the global array

	/*Game map*/ 
	int map[9][12] = { 
		{ 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 }, };

	//Load item icon 
	loadimage(&images[0], _T("wall.bmp"), 50, 50, true); 
	loadimage(&images[1], _T("floor.bmp"), 50, 50, true); 
	loadimage(&images[2], _T("des.bmp"), 50, 50, true); 
	loadimage(&images[3], _T("man.bmp"), 50, 50, true); 
	loadimage(&images[4], _T("box.bmp"), 50, 50, true); 
	loadimage(&images[5], _T("box.bmp"), 50, 50, true);

	for (int i = 0; i < 9; i++) { 
		for (int j = 0; j < 12; j++) { 
			putimage(100 + j * 50, 100 + i * 50, &images[map[i][j]]); 
		}
	}

	system("pause");
	return 0;
}

Program running results:

  • Note: all the numerical constants should be replaced with macro definitions later. In terms of future modification and maintenance, code readability can also be improved

Modified code:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

using namespace std;

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(SCREEN_WIDTH , SCREEN_HEIGHT);	//Initialize map
	loadimage(&bg_img,"blackground.bmp", SCREEN_WIDTH, SCREEN_HEIGHT,true);
	putimage(0,0,&bg_img); //Put the prepared background picture into the console interface

	IMAGE images[6]; //There are only six elements 0-5 in the global array

	/*Game map*/ 
	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 }, };

	//Load item icon 
	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++) {
			putimage(START_X + j * RATIO, START_Y + i * RATIO, &images[map[i][j]]);
		}
	}

	system("pause");
	return 0;
}

Optimize the code again:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

using namespace std;

enum _PROPS{
    WALL,   //Wall: 0
    FLOOR,  //Floor: 1
    BOX_DES,    //Indicates box destination: 2
    MAN,    //Mean villain: 3
    BOX,    //Box: 4
    HIT,     //Indicates that the box hit the target: 5
    ALL     //Represents the number of defined global arrays: 6
};

struct _POS{
    int x;  //The row of villain's position in the two-dimensional array
    int y;  //The column of villain's position in two-dimensional array
};

struct _POS man;

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(SCREEN_WIDTH , SCREEN_HEIGHT);	//Initialize map
	loadimage(&bg_img,"blackground.bmp", SCREEN_WIDTH, SCREEN_HEIGHT,true);
	putimage(0,0,&bg_img); //Put the prepared background picture into the console interface

	IMAGE images[ALL]; //There are only six elements 0-5 in the global array

	/*Game map*/ 
	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 }, };

	//Load item icon 
	loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _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]]);
		}
	}

	system("pause");
	return 0;
}
  • 2: Hotkey control

    1) Define hotkeys:

     Left = > a down = > s up = > W right = > d exit = > Q
    

Code implementation:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>
#include<conio.h>

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

//Control key up, down, left and right control direction, 'q' exit 
#define KEY_UP	'w' 
#define KEY_LEFT   'a' 
#define KEY_RIGHT  'd' 
#define KEY_DOWN   's' 
#define KEY_QUIT   'q'

using namespace std;

enum _PROPS {
	WALL,   //Wall: 0
	FLOOR,  //Floor: 1
	BOX_DES,    //Indicates box destination: 2
	MAN,    //Mean villain: 3
	BOX,    //Box: 4
	HIT,     //Indicates that the box hit the target: 5
	ALL     //Represents the number of defined global arrays: 6
};

struct _POS {
	int x;  //The row of villain's position in the two-dimensional array
	int y;  //The column of villain's position in two-dimensional array
};

struct _POS man; The position of villain in two-dimensional array

IMAGE images[ALL]; //There are only six elements 0-5 in the global array

/*Game map*/
	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 }, };

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);	//Initialize map
	loadimage(&bg_img, "blackground.bmp", SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img); //Put the prepared background picture into the console interface

	//Load item icon 
	loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _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]]);
		}
	}

	//Game setup
	bool quit = false;

	do {
		if (_kbhit()) {//Player button
			char ch = _getch();

			if (ch == KEY_UP) { //Up arrow

			}
			else if (ch == KEY_DOWN) {
	
			}
			else if (ch == KEY_LEFT) {

			}
			else if (ch == KEY_RIGHT) {

			}
			else if (ch == KEY_QUIT) {
				quit = true;
			}
		}
		Sleep(100);	//Dormancy processing
	}while (quit == false);

	system("pause");
	return 0;
} 
  • 3 push box control:
    Code implementation:
#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>
#include<conio.h>

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

//Control key up, down, left and right control direction, 'q' exit 
#define KEY_UP	'w' 
#define KEY_LEFT   'a' 
#define KEY_RIGHT  'd' 
#define KEY_DOWN   's' 
#define KEY_QUIT   'q'

using namespace std;

enum _PROPS {
	WALL,   //Wall: 0
	FLOOR,  //Floor: 1
	BOX_DES,    //Indicates box destination: 2
	MAN,    //Mean villain: 3
	BOX,    //Box: 4
	HIT,     //Indicates that the box hit the target: 5
	ALL     //Represents the number of defined global arrays: 6
};

//Game control direction
enum _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

struct _POS {
	int x;  //The row of villain's position in the two-dimensional array
	int y;  //The column of villain's position in two-dimensional array
};

struct _POS man; //The position of villain in two-dimensional array

/*Game map*/
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 }, };

IMAGE images[ALL]; //There are only six elements 0-5 in the global array

//Change the corresponding props in the game map view and display them again
//line: the item is marked in the row of the map array column: the item is marked in the column of the map array prop: the type of the item (such as people or other things)
void changeMap(int line, int column, enum _PROPS prop) {
	map[line][column] = prop;
	putimage(START_X + column * RATIO, START_Y + line * RATIO, &images[prop]);
}

/*
*Control the four directions of the game (up, down, left and right) 
* Input: * direct - person's direction 
* Output: None 
*/
void gameControl(enum _DIRECTION direct) {
	int x = man.x;
	int y = man.y;

	if (direct == UP) {	//Deal with the floor situation in the forward direction first x-1
		if ((x - 1) > 0 && map[x - 1][y] == FLOOR) {
			changeMap(x - 1, y, MAN);	//Villain advance one grid
			man.x = x - 1;
			changeMap(x,y,FLOOR);
		}
	}else if (direct == DOWN) {
		if ((x + 1) > 0 && map[x + 1][y] == FLOOR) {
			changeMap(x + 1, y, MAN);	//Villain step back
			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);	//Villain one space to the left
			man.y = y - 1;
			changeMap(x, y, FLOOR);
		}
	}else if (direct == RIGHT) {
		if ((y + 1) > 0 && map[x][y + 1] == FLOOR) {
			changeMap(x , y+1, MAN);	//Villain one space to the right
			man.y = y + 1;
			changeMap(x, y, FLOOR);
		}
	}
}

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);	//Initialize map
	loadimage(&bg_img, "blackground.bmp", SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img); //Put the prepared background picture into the console interface


	//Load item icon 
	loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _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]]);
		}
	}

	//Game setup
	bool quit = false;

	do {
		if (_kbhit()) {//Player button
			char ch = _getch();

			if (ch == KEY_UP) { //Up arrow
				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;
			}
		}
		Sleep(100);	//Dormancy processing
	}while (quit == false);

	system("pause");
	return 0;
}
  • You can control the villain to walk in the map here! (actually, it runs down and finds that the knowledge points in the code are really few and easy to master.)

Code optimization: (void gamecontrol (enum ﹣ direction) {})

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>
#include<conio.h>

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

//Control key up, down, left and right control direction, 'q' exit 
#define KEY_UP	'w' 
#define KEY_LEFT   'a' 
#define KEY_RIGHT  'd' 
#define KEY_DOWN   's' 
#define KEY_QUIT   'q'

#define isValid(pos)  pos.x >= 0 && pos.x < LINE && pos.y >= 0 && pos.y < COLUMN

using namespace std;

enum _PROPS {
	WALL,   //Wall: 0
	FLOOR,  //Floor: 1
	BOX_DES,    //Indicates box destination: 2
	MAN,    //Mean villain: 3
	BOX,    //Box: 4
	HIT,     //Indicates that the box hit the target: 5
	ALL     //Represents the number of defined global arrays: 6
};

//Game control direction
enum _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

struct _POS {
	int x;  //The row of villain's position in the two-dimensional array
	int y;  //The column of villain's position in two-dimensional array
};

struct _POS man; //The position of villain in two-dimensional array

/*Game map*/
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 }, };

IMAGE images[ALL]; //There are only six elements 0-5 in the global array

//Change the corresponding props in the game map view and display them again
//line: the item is marked in the row of the map array column: the item is marked in the column of the map array prop: the type of the item (such as people or other things)
void changeMap(int line, int column, enum _PROPS prop) {
	map[line][column] = prop;
	putimage(START_X + column * RATIO, START_Y + line * RATIO, &images[prop]);
}

/*
*Control the four directions of the game (up, down, left and right) 
* Input: * direct - person's direction 
* Output: None 
*/
void gameControl(enum _DIRECTION direct) {
	//int x = man.x;
	//int y = man.y;

	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 (direct == UP) {	//Deal with the situation of the floor in the forward direction first x-1
			if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain advance one grid
				changeMap(man.x,man.y,FLOOR);
				man = next_pos;
			}
		}else if (direct == DOWN) {
			if ((x + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain is one step behind
				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, y, MAN);	//Villain one space to the left
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}else if (direct == RIGHT) {
			if ((y + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain one space to the right
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}
	}*/   //These codes are repeated and can be replaced with the following codes
	//if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
	//If (next pos.x > = 0 & & next pos.x < line & & next pos.y > = 0 & & next pos.y < column & & map [next pos.x] [next pos.y]) {/ / check the validity of judgment
	//Macro expand next & pos.x > = 0 & & next & pos.x < line & & next & pos.y > = 0 & & next & pos.y < column
	if (isValid(next_pos) && map[next_pos.x][next_pos.y]) { //Replace macros
		changeMap(next_pos.x, next_pos.y, MAN);	
		changeMap(man.x, man.y, FLOOR);
		man = next_pos;
	}

}

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);	//Initialize map
	loadimage(&bg_img, "blackground.bmp", SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img); //Put the prepared background picture into the console interface


	//Load item icon 
	loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _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]]);
		}
	}

	//Game setup
	bool quit = false;

	do {
		if (_kbhit()) {//Player button
			char ch = _getch();

			if (ch == KEY_UP) { //Up arrow
				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;
			}
		}
		Sleep(100);	//Dormancy processing
	}while (quit == false);

	system("pause");
	return 0;
}

Add the missing conditions and optimization code again:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>
#include<conio.h>

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

//Control key up, down, left and right control direction, 'q' exit 
#define KEY_UP	'w' 
#define KEY_LEFT   'a' 
#define KEY_RIGHT  'd' 
#define KEY_DOWN   's' 
#define KEY_QUIT   'q'

#define isValid(pos)  pos.x >= 0 && pos.x < LINE && pos.y >= 0 && pos.y < COLUMN

using namespace std;

enum _PROPS {
	WALL,   //Wall: 0
	FLOOR,  //Floor: 1
	BOX_DES,    //Indicates box destination: 2
	MAN,    //Mean villain: 3
	BOX,    //Box: 4
	HIT,     //Indicates that the box hit the target: 5
	ALL     //Represents the number of defined global arrays: 6
};

//Game control direction
enum _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

struct _POS {
	int x;  //The row of villain's position in the two-dimensional array
	int y;  //The column of villain's position in two-dimensional array
};

struct _POS man; //The position of villain in two-dimensional array

/*Game map*/
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 }, };

IMAGE images[ALL]; //There are only six elements 0-5 in the global array

//Change the corresponding props in the game map view and display them again
//line: the item is marked in the row of the map array column: the item is marked in the column of the map array prop: the type of the item (such as people or other things)
void changeMap(int line, int column, enum _PROPS prop) {
	map[line][column] = prop;
	putimage(START_X + column * RATIO, START_Y + line * RATIO, &images[prop]);
}

/*
*Control the four directions of the game (up, down, left and right) 
* Input: * direct - person's direction 
* Output: None 
*/
void gameControl(enum _DIRECTION direct) {
	//int x = man.x;
	//int y = man.y;

	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 (direct == UP) {	//Deal with the floor situation in the forward direction first x-1
			if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain advance one grid
				changeMap(man.x,man.y,FLOOR);
				man = next_pos;
			}
		}else if (direct == DOWN) {
			if ((x + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain is one step behind
				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, y, MAN);	//Villain one space to the left
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}else if (direct == RIGHT) {
			if ((y + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain one space to the right
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}
	}*/   //These codes are repeated and can be replaced with the following codes
	//if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
	//If (next pos.x > = 0 & & next pos.x < line & & next pos.y > = 0 & & next pos.y < column & & map [next pos.x] [next pos.y]) {/ / check the validity of judgment
	//Macro expand next & pos.x > = 0 & & next & pos.x < line & & next & pos.y > = 0 & & next & pos.y < column
	if (isValid(next_pos) && map[next_pos.x][next_pos.y] == FLOOR ) { //Use macro instead / / if the floor is in front of you
		changeMap(next_pos.x, next_pos.y, MAN);
		changeMap(man.x, man.y, FLOOR);
		man = next_pos;
	}
	else if (isValid(next_next_pos) && map[next_pos.x][next_pos.y] == BOX) { //Box in front of people
		//In two cases, the front of the box is the floor or the box destination
		if (map[next_next_pos.x][next_next_pos.y] == FLOOR) {
			changeMap(next_next_pos.x, next_next_pos.y, BOX);
			changeMap(next_pos.x, next_pos.y, MAN);
			changeMap(man.x, man.y, FLOOR);
			man = next_pos;
		}
		else if (map[next_next_pos.x][next_next_pos.y] == BOX_DES) {
			changeMap(next_next_pos.x, next_next_pos.y, HIT);
			changeMap(next_pos.x, next_pos.y, MAN);
			changeMap(man.x, man.y, FLOOR);
			man = next_pos;
		}
	}
}

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);	//Initialize map
	loadimage(&bg_img, "blackground.bmp", SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img); //Put the prepared background picture into the console interface


	//Load item icon 
	loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _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]]);
		}
	}

	//Game setup
	bool quit = false;

	do {
		if (_kbhit()) {//Player button
			char ch = _getch();

			if (ch == KEY_UP) { //Up arrow
				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;
			}
		}
		Sleep(100);	//Dormancy processing
	}while (quit == false);
	
	//End of game, release resources
	closegraph();

	system("pause");
	return 0;
}
  • In fact, it's almost finished here. The only difference is an end condition

  • 4 end of game

#include   //Header files to be added to use easyX
#include  
#include  
#include 
#include

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

//Control key up, down, left and right control direction, 'q' exit 
#define KEY_UP	'w' 
#define KEY_LEFT   'a' 
#define KEY_RIGHT  'd' 
#define KEY_DOWN   's' 
#define KEY_QUIT   'q'

#define isValid(pos)  pos.x >= 0 && pos.x < LINE && pos.y >= 0 && pos.y < COLUMN

using namespace std;

enum _PROPS {
	WALL,   //Wall: 0
	FLOOR,  //Floor: 1
	BOX_DES,    //Indicates box destination: 2
	MAN,    //Mean villain: 3
	BOX,    //Box: 4
	HIT,     //Indicates that the box hit the target: 5
	ALL     //Represents the number of defined global arrays: 6
};

//Game control direction
enum _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

struct _POS {
	int x;  //The row of villain's position in the two-dimensional array
	int y;  //The column of villain's position in two-dimensional array
};

struct _POS man; //The position of villain in two-dimensional array

/*Game map*/
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 }, };

/*
*Judge whether the game is over. If there is no box destination, it means the game is over 
*Input: None 
*Return value: 
* true - Game over 
false - Game continues 
*/ 
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;
}

/*
*The end scene of the game will be displayed after the player passes the customs 
* Input: * bg - pointer to the background picture variable 
* Return value: None 
*/ 
void gameOverScene(IMAGE *bg) { 
	putimage(0, 0, bg); 
	settextcolor(WHITE); 
	RECT rec = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; 
	settextstyle(20, 0, _T("Song style")); 
	drawtext(_T("Congratulations~ \n It's a success!"), &rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//Center alignment, see easyX's document for details
}

IMAGE images[ALL]; //There are only six elements 0-5 in the global array

//Change the corresponding props in the game map view and display them again
//line: the item is marked in the row of the map array column: the item is marked in the column of the map array prop: the type of the item (such as people or other things)
void changeMap(int line, int column, enum _PROPS prop) {
	map[line][column] = prop;
	putimage(START_X + column * RATIO, START_Y + line * RATIO, &images[prop]);
}

/*
*Control the four directions of the game (up, down, left and right) 
 * Input: * direct - person's direction 
 * Output: None 
*/
void gameControl(enum _DIRECTION direct) {
	//int x = man.x;
	//int y = man.y;

	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 (direct == UP) {	//Deal with the floor situation in the forward direction first x-1
			if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain advance one grid
				changeMap(man.x,man.y,FLOOR);
				man = next_pos;
			}
		}else if (direct == DOWN) {
			if ((x + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain is one step behind
				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, y, MAN);	//Villain one space to the left
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}else if (direct == RIGHT) {
			if ((y + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain one space to the right
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}
	}*/   //These codes are repeated and can be replaced with the following codes
	//if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
	//if (next_pos.x >= 0 && next_pos.x=0 && next_pos.y=0 && next_pos.x=0 && next_pos.y 

The above is the supplementary end condition code, as well as the complete code of the whole game

  • After the game is over, you can encapsulate the definition, header file, macro definition and other files in the game into a header file,

  • The code in the header file is as follows:

#pragma once

#define RATIO 50 
#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 650
#define LINE 9 
#define COLUMN 12 
#define START_X 100 
#define START_Y 100

//Control key up, down, left and right control direction, 'q' exit 
#define KEY_UP	'w' 
#define KEY_LEFT   'a' 
#define KEY_RIGHT  'd' 
#define KEY_DOWN   's' 
#define KEY_QUIT   'q'

#define isValid(pos)  pos.x >= 0 && pos.x < LINE && pos.y >= 0 && pos.y < COLUMN


enum _PROPS {
	WALL,   //Wall: 0
	FLOOR,  //Floor: 1
	BOX_DES,    //Indicates box destination: 2
	MAN,    //Mean villain: 3
	BOX,    //Box: 4
	HIT,     //Indicates that the box hit the target: 5
	ALL     //Represents the number of defined global arrays: 6
};

//Game control direction
enum _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

struct _POS {
	int x;  //The row of villain's position in the two-dimensional array
	int y;  //The column of villain's position in two-dimensional array
};


The code in the source file is as follows:

#include <graphics.h>  //Header files to be added to use easyX
#include <iostream> 
#include <stdlib.h> 
#include <string>
#include<conio.h>

#include"box_man1.h"

using namespace std;

struct _POS man; //The position of villain in two-dimensional array

/*Game map*/
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 }, };

/*
*Judge whether the game is over. If there is no box destination, it means the game is over 
*Input: None 
*Return value: 
* true - Game over 
false - Game continues 
*/ 
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;
}

/*
*The end scene of the game will be displayed after the player passes the customs 
* Input: * bg - pointer to the background picture variable 
* Return value: None 
*/ 
void gameOverScene(IMAGE *bg) { 
	putimage(0, 0, bg); 
	settextcolor(WHITE); 
	RECT rec = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; 
	settextstyle(20, 0, _T("Song style")); 
	drawtext(_T("Congratulations~ \n It's a success!"), &rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//Center alignment, see easyX's document for details
}

IMAGE images[ALL]; //There are only six elements 0-5 in the global array

//Change the corresponding props in the game map view and display them again
//line: the item is marked in the row of the map array column: the item is marked in the column of the map array prop: the type of the item (such as people or other things)
void changeMap(int line, int column, enum _PROPS prop) {
	map[line][column] = prop;
	putimage(START_X + column * RATIO, START_Y + line * RATIO, &images[prop]);
}

/*
*Control the four directions of the game (up, down, left and right) 
* Input: * direct - person's direction 
* Output: None 
*/
void gameControl(enum _DIRECTION direct) {
	//int x = man.x;
	//int y = man.y;

	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 (direct == UP) {	//Deal with the floor situation in the forward direction first x-1
			if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain advance one grid
				changeMap(man.x,man.y,FLOOR);
				man = next_pos;
			}
		}else if (direct == DOWN) {
			if ((x + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain is one step behind
				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, y, MAN);	//Villain one space to the left
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}else if (direct == RIGHT) {
			if ((y + 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
				changeMap(next_pos.x, y, MAN);	//Villain one space to the right
				changeMap(man.x, man.y, FLOOR);
				man = next_pos;
			}
		}
	}*/   //These codes are repeated and can be replaced with the following codes
	//if ((x - 1) > 0 && map[next_pos.x][next_pos.y] == FLOOR) {
	//If (next pos.x > = 0 & & next pos.x < line & & next pos.y > = 0 & & next pos.y < column & & map [next pos.x] [next pos.y]) {/ / check the validity of judgment
	//Macro expand next & pos.x > = 0 & & next & pos.x < line & & next & pos.y > = 0 & & next & pos.y < column
	if (isValid(next_pos) && map[next_pos.x][next_pos.y] == FLOOR ) { //Use macro instead / / if the floor is in front of you
		changeMap(next_pos.x, next_pos.y, MAN);
		changeMap(man.x, man.y, FLOOR);
		man = next_pos;
	}
	else if (isValid(next_next_pos) && map[next_pos.x][next_pos.y] == BOX) { //Box in front of people
		//In two cases, the front of the box is the floor or the box destination
		if (map[next_next_pos.x][next_next_pos.y] == FLOOR) {
			changeMap(next_next_pos.x, next_next_pos.y, BOX);
			changeMap(next_pos.x, next_pos.y, MAN);
			changeMap(man.x, man.y, FLOOR);
			man = next_pos;
		}
		else if (map[next_next_pos.x][next_next_pos.y] == BOX_DES) {
			changeMap(next_next_pos.x, next_next_pos.y, HIT);
			changeMap(next_pos.x, next_pos.y, MAN);
			changeMap(man.x, man.y, FLOOR);
			man = next_pos;
		}
	}
}

int main(void) {
	IMAGE bg_img;

	//Map mounting
	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);	//Initialize map
	loadimage(&bg_img, "blackground.bmp", SCREEN_WIDTH, SCREEN_HEIGHT, true);
	putimage(0, 0, &bg_img); //Put the prepared background picture into the console interface


	//Load item icon 
	loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true);
	loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
	loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
	loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _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]]);
		}
	}

	//Game setup
	bool quit = false;

	do {
		if (_kbhit()) {//Player button
			char ch = _getch();

			if (ch == KEY_UP) { //Up arrow
				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);	//Dormancy processing
	}while (quit == false);

	system("pause");

	//End of game, release resources
	closegraph();

	return 0;
}
Published 3 original articles, praised 0, visited 103
Private letter follow

Posted on Tue, 11 Feb 2020 07:25:44 -0800 by Tory