C语言编写推箱子程序.doc
C语言编写推箱子程序
摘要
本文以Turbo C作为编程工具,编写推箱子游戏,并给出最后成功运行的推箱子游戏C语言源程序。
关键词: C语言 编程 游戏 推箱子
一、程序设计
1.1程序的目标
字符模式,四个简单任务的推箱子游戏。
1.2程序任务的流程
二、程序的具体设计
2.1程序的开始
#include #include #include #include #include #include 2.2定义二维数组ghouse来记录屏幕上各点的状态,其中:0表示什么都没有,'b'表示箱子,'w'表示墙壁,'m'表示目的地,'i'表示箱子在目的地。 char ghouse[20][20];直接写屏函数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;}定义判断是否胜利的数据结构typedef struct winer {int x,y;struct winer *p;}winer; 箱子位置的数据结构typedef struct boxs {int x,y;struct boxs *next;}boxs;2.6在特定的坐标上画墙壁并用数组记录状态的函数 void printwall(int x,int y){putchxy(y-1,x-1,219,GREEN,BLACK);ghouse[x][y]='w';}2.7在特定的坐标上画箱子并用数组记录状态的函数 void printbox(int x,int y){putchxy(y-1,x-1,10,WHITE,BLACK);ghouse[x][y]='b';} 在特定的坐标上画目的地并用数组记录状态的函数 void printwhither1(int x,int y,winer **win,winer **pw){winer *qw;putchxy(y-1,x-1,'*',YELLOW,BLACK);ghouse[x][y]='m';if(*win==NULL){*win=*pw=qw=(winer* )malloc(sizeof(winer));(*pw)->x=x;(*pw)->y=y;(*pw)->p=NULL;}else{qw=(winer* )malloc(sizeof(winer));qw->x=x;qw->y=y;(*pw)->p=qw;(*pw)=qw;qw->p=NULL;}}
2.9 在特定的坐标上画目的地并用数组记录状态的函数 void printwhither(int x,int y){putchxy(y-1,x-1,'*',YELLOW,BLACK);ghouse[x][y]='m';}在特定的坐标上画人的函数 void printman(int x,int y){gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}在特定的坐标上画箱子在目的地上并用数组记录状态的函数 void printboxin(int x,int y){putchxy(y-1,x-1,10,YELLOW,BLACK);ghouse[x][y]='i';}初始化函数,初始化数组和屏幕 void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)ghouse[i][j]=0;_AL=3;_AH=0;geninterrupt(0x10);gotoxy(40,4);printf("Welcome to come box world!");gotoxy(40,6);printf("Press up,down,left,right to play.");gotoxy(40,8);printf("Press Esc to quit it.");gotoxy(40,10);printf("Press space to reset the game.");gotoxy(40,12);printf("Producer : wangdehao.");gotoxy(40,14);printf("Mar. 30th 2003.");}2.13第一关的图象初始化