#include <iostream>
#include <graphics.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
#define SCRENEN_WIDTH 800
#define SCREENN_HEIGHT 650
#define LINE 9
#define LIE 12
#define FIFTY 50
#define START_L 100
#define START_C 100
#define PANDUAN(next_pos) (next_pos.line >= 0 && next_pos.line < LINE && next_pos.coll >= 0 && next_pos.coll < LIE)
typedef struct _POS {
int line;
int coll;
}POS;
enum _PROPS {
WALL,
FLOOR,
DES,
MAN,
BOX,
ALL,
};
IMAGE images[ALL];
POS man;
int maps[LINE][LIE] = {
{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 changemap(POS* pos, enum _PROPS prop)
{
maps[pos->line][pos->coll] = prop;
putimage(START_C + pos->coll * FIFTY, START_L + pos->line * FIFTY, &images[prop]);
}
void Gc(char dr)
{
POS next_pos;
POS next_next_pos;
switch (dr)
{
case 'w':
next_pos.line = man.line - 1;
next_next_pos.line = next_pos.line - 1;
break;
case's':
next_pos.line = man.line + 1;
next_next_pos.line = next_pos.line + 1;
break;
case'a':
next_pos.coll = man.coll - 1;
next_next_pos.coll = next_pos.coll - 1;
break;
case'd':
next_pos.coll = man.coll + 1;
next_next_pos.coll = next_pos.coll + 1;
break;
default:
return;
}
if (PANDUAN(next_pos) && maps[next_pos.line][next_pos.coll] == FLOOR)
{
changemap(&next_pos, MAN);
changemap(&man, FLOOR);
man = next_pos;
}
}
int main(void)
{
IMAGE ISACI;
initgraph(SCRENEN_WIDTH, SCREENN_HEIGHT);
loadimage(&ISACI, "blackground.bmp", SCRENEN_WIDTH, SCRENEN_WIDTH, true);
putimage(0, 0, &ISACI);
loadimage(&images[WALL], "wall_right.bmp", FIFTY, FIFTY, true);
loadimage(&images[FLOOR], "floor.bmp", FIFTY, FIFTY, true);
loadimage(&images[DES], "des.bmp", FIFTY, FIFTY, true);
loadimage(&images[MAN], "man.bmp", FIFTY, FIFTY, true);
loadimage(&images[BOX], "box.bmp", FIFTY, FIFTY, true);
for (int i = 0; i < LINE; i++)
{
for (int j = 0; j < LIE; j++)
{
if (maps[i][j] == MAN)
{
man.line = i;
man.coll = j;
}
putimage(START_C + j * FIFTY, START_L + i * FIFTY, &images[maps[i][j]]);
}
}
bool quit = false;
do {
if (_kbhit())
{
char key = _getch();
if (key == 'w')
Gc('w');
else if (key == 's')
Gc('s');
else if (key == 'a')
Gc('a');
else if (key == 'd')
Gc('d');
else if (key == 'q')
quit = true;
}
} while (!quit);
system("pause");
return 0;
}
Run-Time Check Failure #3 - The variable 'next_pos' is being used without being initialized.