程序代码:
#include<iostream>
#include<string>
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#define isValid(pos) pos.y < COLUMN &&pos.y >= 0 &&pos.x < LINE &&pos.x>= 0
#define RATIO 61
#define LINE 9
#define COLUMN 12
#define SCREEN_WIDTH 960
#define SCREEN_HEIGHT 768
#define START_X 100
#define START_Y 150
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'
//typedef enum _PROPS pos;
using namespace std;
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},
};
/*
枚举所用的元素
*/
enum _PROPS {
WALL,
FLOOR,
DES,
MAN,
BOX,
HIT,
ALL,
};
struct _POS {
int x; //小人所在的二维数组行
int y;//小人所在的二维数组列
};
struct _POS man;
enum _DIRECTION {
UP,
DOWN,
LEFT,
RIGHT,
};
IMAGE images[ALL];
/*
改变游戏道具并显示出来
输入:line 行下标 columu 列下标
prop:道具类型
*/
void changeMap(struct _POS *pos , enum _PROPS props){
map[pos->x][pos->y] = props;
putimage(START_Y + RATIO * pos->y, START_X + RATIO * pos->x, &images[props]);
}
/*
实现控制
*/
void gameControl(enum _DIRECTION direct ){
//int x = man.x;
//int y = man.y;
struct _POS nextpos = man;
struct _POS nextnextpos =man ;
switch (direct) {
case UP:
nextpos.x--;
nextnextpos.x -= 2;
break;
case DOWN:
nextpos.x++;
nextnextpos.x += 2;
break;
case LEFT:
nextpos.y--;
nextnextpos.y -= 2;
break;
case RIGHT:
nextpos.y++;
nextnextpos.y += 2;
break;
}
//if (direct == UP) {
if (isValid(nextpos) && map[nextpos.x][nextpos.y] == FLOOR) {
changeMap(&nextpos, MAN);//小人前进一格
changeMap(&man, FLOOR);//人所在的位置换为地板
man= nextpos;//更改人的初始位置坐标
}
else if (isValid(nextnextpos) && map[nextpos.x][nextpos.y] == BOX) {
if (map[nextnextpos.x][nextnextpos.y]==FLOOR) {
changeMap(&nextnextpos, BOX);//箱子下一个位置换为箱子
changeMap(&nextpos,MAN );//箱子所在位置换为人
changeMap(&man, FLOOR);//人所在的位置换为地板
man = nextpos;
}
else if (map[nextnextpos.x][nextnextpos.y]==DES) {
changeMap(&nextnextpos, HIT);
changeMap(&nextpos, MAN);//箱子所在位置换为人
changeMap(&man, FLOOR);//人所在的位置换为地板
man = nextpos;
}
}
/**}
else if(direct==DOWN){
if ((x +1) < LINE && map[x+1][y] == FLOOR) {
changeMap(x + 1, y, MAN);//小人前进一格
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);//小人前进一格
man.y = y - 1;
changeMap(x, y, FLOOR);//人所在的位置换为地板
}
}
else if (direct == RIGHT) {
if ((y + 1) < COLUMN && map[x][y+1] == FLOOR) {
changeMap(x , y+1, MAN);//小人前进一格
man.y = y + 1;
changeMap(x, y, FLOOR);//人所在的位置换为地板
}
}*/
}
bool isGameover() {
for (int i = 0; i < LINE; i++) {
for (int j = 0; j < COLUMN; j++)
if (map[i][j] == DES)return false;
}
return true;
}
void Thankyou(IMAGE *bg) {
putimage(0, 0, bg);
setcolor(WHITE);
RECT rec = { 0,0,SCREEN_WIDTH,SCREEN_HEIGHT };
settextstyle(20, 0, "宋体");
drawtext(("恭喜您,成为了一个合格的搬箱子老司机"), & rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
int main(void) {
IMAGE bg_img;
initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
loadimage(&bg_img,"blackground.bmp", SCREEN_WIDTH, SCREEN_WIDTH, true);
putimage(0, 0, &bg_img);
loadimage(&images[WALL], "wall_right.bmp", RATIO, RATIO, true);
loadimage(&images[FLOOR], "floor.bmp", RATIO, RATIO, true);
loadimage(&images[DES], "des.bmp", RATIO, RATIO, true);
loadimage(&images[MAN], "man.bmp", RATIO, RATIO, true);
loadimage(&images[BOX], "box.bmp", RATIO, RATIO, true);
loadimage(&images[HIT], "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_Y + RATIO *j, START_X + RATIO *i, &images[map[i][j]]);
}
}
bool quit = false;
do {
if (_kbhit()){
char ch = _getch();
if (ch == KEY_UP) {
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()) {
Thankyou(&bg_img);
quit = true;
}
}
Sleep(100);
} while (quit==false);
system("pause");
closegraph();
return 0;
}
推箱子游戏 GG