我写的推箱子

这几天闲着无聊,想学习下win tc库函数,找了一些资料,就写了个小游戏推箱子。

游戏需要地图文件,我只编写了两个,最多支持20个,当然可以修改源代码来让它支持更多的地图。

map1.dat:

0,0,0,0,0,0,0,0,0,0,0,0,0,0 
0,1,1,1,1,1,1,1,1,1,1,1,1,0
0,1,2,2,2,2,2,2,2,2,2,4,1,0
0,1,5,2,3,3,3,2,2,2,2,4,1,0
0,1,2,2,2,2,2,2,2,2,2,4,1,0
0,1,1,1,1,1,1,1,1,1,1,1,1,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0

map2.dat:

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0
0,0,0,1,1,1,2,2,2,2,1,1,1,0,0,0
0,0,0,1,2,2,2,1,3,2,2,2,1,1,1,0
0,0,0,1,2,2,2,3,2,2,2,3,3,2,1,0
0,0,0,1,2,3,3,2,1,3,2,2,2,2,1,0
0,0,0,1,1,2,2,2,3,2,2,2,3,2,1,0
0,1,1,1,1,1,1,2,1,3,1,1,1,1,1,0
0,1,4,4,5,2,1,3,2,2,1,0,0,0,0,0
0,1,4,1,4,4,2,2,3,1,1,0,0,0,0,0
0,1,4,4,4,4,3,1,2,1,0,0,0,0,0,0
0,1,4,4,4,4,2,2,2,1,0,0,0,0,0,0
0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

有兴趣的朋友可以自己编写相应的地图文件,具体命名格式如下:

四周要用0围起来,而里面的1、2、3、4、5的含义如下:

 
#define WALL 1 //墙壁
#define WAY 2 //道路
#define BOX 3 //箱子
#define GOAL 4 //目的地
#define MAN 5 //
人只能有一个,目的地和箱子数目要相等,当每个箱子都在目标地点上,就赢了。
 
/* 推箱子  小Q泉制作 */
/* 牧马工作室——Challenge Everything (*^-^*) */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <bios.h>

#define IS_NUM(X) (X>='0' && X<='9')

#define N 50 /*箱子最大数量*/
#define UP 0x4800
#define DOWN 0x5000
#define RIGHT 0x4d00
#define LEFT 0x4b00
#define REST 0x3920 /*空格键*/
#define ESC 0x011b
#define WIN_ALL window(1,1,80,25)
#define WIN_MAI window(4,2,55,24)
#define WIN_SCO window(60,8,75,8)
#define WIN_GRA window(60,15,75,15)
#define WIN_HELP window(21,18,75,24)
#define MAN_COL RED
#define BOX_COL 7
#define WALL_COL 3

#define WALL 1
#define WAY 2
#define BOX 3
#define GOAL 4
#define MAN 5
#define ACC 6
char ico[6] = {219, 219, '', 8, '*', 2};
int color[6] = {2,3,5,6,8,10};


struct mapData{
int a[24][50];
int mx, my;/*横纵坐标最大值*/
}map = {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,2,2,2,2,2,2,2,2,2,4,1,0},
{0,1,5,2,3,3,3,2,2,2,2,4,1,0},
{0,1,2,2,2,2,2,2,2,2,2,4,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0}},13, 6}, tmap = {{0},0,0};

struct manboxxy{
int x;
int y;
}mbox[N];
int Bn;
int grade, score;
char far *screen=(char far* )0xb8000000;
void putchxy(int y,int x,char ch,char fc,char bc)
{
screen[(x*160)+(y<<1)+0]=ch;
screen[(x*160)+(y<<1)+1]=(bc*16)+fc;
}

int ReadMap(int g)
{
char ch, fname[20] = "map", Gn[4], sL[]=".dat";
int tnum, x, y;
FILE *fp;

Gn[3]='\0';
for(x=2; g>0&&x>=0; x--)
{
Gn[x] = '0'+(g%10);
g /= 10;
}
for(y=3,++x; Gn[x]!='\0'; x++,y++)
{
fname[y] = Gn[x];
}
for(x=0; sL[x]!='\0';x++,y++)
{
fname[y] = sL[x];
}
fname[y] = '\0';
if((fp = fopen(fname, "r")) == NULL)
{
WIN_ALL;
gotoxy(5,15);
printf("File open error! Can't find map.dat!");
return -1;
}
/*读取地图资源矩阵*/
x=0; y=0;
map.mx = x;
map.my = y;
ch=fgetc(fp);
while(!IS_NUM(ch)) ch=fgetc(fp);
while(ch!=EOF)
{
if(IS_NUM(ch))
{
map.a[y][x] = ch-'0';
}
else
switch(ch)
{
case ',':
{
++x;
if(x>map.mx)
map.mx = x;
break;
}
case '\n':
{
++y;
if(y>map.my)
map.my = y;
x=0;
break;
}
}
ch = fgetc(fp);
}
if(map.mx==0 || map.my==0)
{
WIN_MAI;
gotoxy(5,15);
printf("Read map fail!");
return -2;
}
return 0;
}

void DrawMap()
{
int i, j;
WIN_MAI;
Bn = 1;
for(i=1; i<map.my; i++)
for(j=1; j<map.mx; j++)
{
if(map.a[i][j]==MAN)
{mbox[0].x=j; mbox[0].y =i; tmap.a[i][j]=WAY;}
else if(map.a[i][j]==BOX)
{mbox[Bn].x=j; mbox[Bn].y =i; tmap.a[i][j]=WAY;Bn++;}
else
{
tmap.a[i][j] = map.a[i][j];
putchxy(j+3,i+1,ico[map.a[i][j]], color[tmap.a[i][j]], RED);
}
}
tmap.mx = map.mx;
tmap.my = map.my;
}

void DrawMbox()
{
int i, tx, ty;
tx = mbox[0].x;
ty = mbox[0].y;
putchxy(tx+3, ty+1, ico[MAN], color[MAN], RED);
for(i=1; i<Bn; i++)
{
tx = mbox[i].x;
ty = mbox[i].y;
putchxy(tx+3, ty+1, ico[BOX], color[BOX], RED);
}
}

void DisPlayHelp()
{
WIN_HELP;
gotoxy(1,1);
cprintf(" Welcome to Push Box Game!!!");
gotoxy(1,3);
cprintf("1:Up Down Left Right to play.\n");
gotoxy(1,5);
cprintf("2:Space to restart.\n");
gotoxy(1,7);
cprintf("3:Esc to quit.");
}

/*屏幕初始化*/
void InitScreen()
{
WIN_ALL; textbackground(LIGHTGREEN); textcolor(YELLOW);
clrscr();
WIN_MAI; textbackground(BLACK); textcolor(YELLOW);
clrscr();
WIN_SCO; textbackground(BLACK); textcolor(YELLOW);
clrscr();
WIN_GRA; textbackground(BLACK); textcolor(YELLOW);
clrscr();
}

int Leave(int x, int y)
{
if(map.a[y][x]==WAY)
return 1;
else if(map.a[y][x]==GOAL || map.a[y][x]==GOAL+BOX)
return 2;
}

int Judge(int x1,int y1,int x2,int y2)
{
int i, b1, b2, w1, w2;

b1=b2=0;
for(i=1; i<Bn; i++)
{
if(x1==mbox[i].x && y1==mbox[i].y)
b1=i;
if(x2==mbox[i].x && y2==mbox[i].y)
b2=i;
}
w1 = tmap.a[y1][x1];
w2 = tmap.a[y2][x2];

if(w1==WALL || (b1&&b2) || (b1&&w2==WALL))
return 0;
if(b1)
return b1+1;
return 1;
}

int Win()
{
int i, j, tx, ty;
for(i=1; i<Bn; i++)
{
tx = mbox[i].x;
ty = mbox[i].y;
if(tmap.a[ty][tx] != GOAL)
return 0;
}
return 1;
}

void Mov(int x1,int y1,int x2,int y2)
{
int x0, y0, bn0, mf, boxc;
x0 = mbox[0].x;
y0 = mbox[0].y;
if(mf=Judge(x1,y1,x2,y2))
{
score++;
putchxy(x0+3, y0+1, ico[tmap.a[y0][x0]], color[tmap.a[y0][x0]], RED);
mbox[0].x=x1;
mbox[0].y=y1;
putchxy(x1+3, y1+1, ico[MAN], color[MAN], 1);
if(mf>1)
{
bn0 = mf-1;
mbox[bn0].x = x2;
mbox[bn0].y = y2;
if(tmap.a[y2][x2] == GOAL)
boxc=GREEN;
else boxc=RED;
putchxy(x2+3, y2+1, ico[BOX], color[BOX], boxc);
}
}
}

void Begain()
{

int key, g=1;
char but;
begain:
ReadMap(g);
key = -1;
score = 0;
grade = g;
DrawMap();/*绘制地图*/
DrawMbox();/*绘制人和箱子*/
DisPlayHelp();
while(1)
{
WIN_GRA;
cprintf("Grade: %5d", grade);
WIN_SCO;
cprintf("Step: %5d", score);

WIN_MAI;
key = bioskey(0);
switch(key)
{
case UP: Mov(mbox[0].x, mbox[0].y-1, mbox[0].x, mbox[0].y-2);break;
case DOWN: Mov(mbox[0].x, mbox[0].y+1, mbox[0].x, mbox[0].y+2);break;
case LEFT: Mov(mbox[0].x-1, mbox[0].y, mbox[0].x-2, mbox[0].y);break;
case RIGHT: Mov(mbox[0].x+1, mbox[0].y, mbox[0].x+2, mbox[0].y);break;
case REST: goto begain;
case ESC: break;
}
if(key==ESC)
break;
if(Win())
{
gotoxy(3,15);
printf("Congratulations! You win!");
do{
gotoxy(3,16);
printf("Chose the grade:(1~20)");
but = getch();
}while(!IS_NUM(but) && but!='q' && but!='Q');
gotoxy(3,15);
printf("");
gotoxy(3,16);
printf("");
if(IS_NUM(but))
{
g = but-'0';
goto begain;
}
else break;
}
}
}

int main(void)
{
InitScreen();
Begain();
WIN_MAI;
gotoxy(5,15);
printf("Thank you, Made of Muse.");
getch();
return 0;
}
原文地址:https://www.cnblogs.com/AkQuan/p/2245464.html