最基础的东西来实现“推箱子控制”
经典游戏"推箱子"的核心控制部份"M"是被推的~箱子
"W"是推不动的~墙
"笑脸"是会推东西的"小人"
用wintc,写得有点散,因为边想边写的,后面再整理一下。
其实这里只用到判断和赋值,数组,外加循环控制,很基础的东西,不需要高深的指针,类或者说结构体来做,让大家看得明白。
觉得学以致用才是最重要的,程序也只是个工具。
tuixiangzi.rar
(1.43 KB)
重在想法的实现,高手赐教
程序代码:
# include <stdio.h> # include <stdlib.h> # include <bios.h> char showstate[25][80]; int x = 14; int y = 15; void initstate();/*初始化屏幕*/ void move();/*移动小人*/ void drawhuman_YH(int x,int y);/*向上移动*/ void drawhuman_YD(int x,int y);/*向下移动*/ void drawhuman_XL(int x,int y);/*向左移动*/ void drawhuman_XR(int x,int y);/*向右移动*/ void initwall();/*定义箱子的位置*/ void showall();/*显示屏幕所有的元素*/ void initwow();/*定义墙壁的位置*/ int winning();/*游戏胜利*/ void setgoal();/*设置目标点*/ /*-----------程序入口-----------------*/ int main() { initstate(); initwall(); initwow(); move(); getch(); return 0; } /*---------初始化状态-------------------*/ void initstate() { int i; int j; for(i=0;i<80;++i) { for(j=0;j<25;++j) { showstate[i][j] ='0'; } } return; } /*-----------向上-----------------*/ void drawhuman_YH(int x,int y) { clrscr(); showall(); gotoxy(x,y); printf("%c\b",2); if(showstate[x][y]=='M') { showstate[x][y-1]='M'; gotoxy(x,y-1); printf("%c",showstate[x][y-1]); showstate[x][y]='0'; gotoxy(x,y); } return; } /*------------向下----------------*/ void drawhuman_YD(int x,int y) { clrscr(); showall(); gotoxy(x,y); printf("%c\b",2); if(showstate[x][y]=='M') { showstate[x][y+1]='M'; gotoxy(x,y+1); printf("%c",showstate[x][y+1]); showstate[x][y]='0'; gotoxy(x,y); } return; } /*------------向左----------------*/ void drawhuman_XL(int x,int y) { clrscr(); showall(); gotoxy(x,y); printf("%c\b",2); if(showstate[x][y]=='M') { showstate[x-1][y]='M'; gotoxy(x-1,y); printf("%c",showstate[x-1][y]); showstate[x][y]='0'; gotoxy(x,y); } return; } /*------------向右----------------*/ void drawhuman_XR(int x,int y) { clrscr(); showall(); gotoxy(x,y); printf("%c\b",2); if(showstate[x][y]=='M') { showstate[x+1][y]='M'; gotoxy(x+1,y); printf("%c",showstate[x+1][y]); showstate[x][y]='0'; gotoxy(x,y); } return; } /*------------控制总部----------------*/ void move() { while(1) { switch(bioskey(0)) { case 18432: if(showstate[x][y-2]=='W'&& showstate[x][y-1]=='M') { break; } if(showstate[x][y-2]=='M'&& showstate[x][y-1]=='M') { break; } else if(showstate[x][y-1]=='W') { break; } else { y = y-1;drawhuman_YH(x,y); break; } case 20480: if(showstate[x][y+2]=='W'&& showstate[x][y+1]=='M') { break; } if(showstate[x][y+2]=='M'&& showstate[x][y+1]=='M') { break; } else if(showstate[x][y+1]=='W') { break; } else { y = y+1;drawhuman_YD(x,y);break; } case 19200: if(showstate[x-2][y]=='W'&& showstate[x-1][y]=='M') { break; } if(showstate[x-2][y]=='M'&& showstate[x-1][y]=='M') { break; } else if(showstate[x-1][y]=='W') { break; } else { x = x-1;drawhuman_XL(x,y);break; } case 19712: if(showstate[x+2][y]=='W'&& showstate[x+1][y]=='M') { break; } if(showstate[x+2][y]=='M'&& showstate[x+1][y]=='M') { break; } else if(showstate[x+1][y]=='W') { break; } else { x = x+1;drawhuman_XR(x,y);break; } } } return; } /*----------定义箱子的位置------------------*/ void initwall() { showstate[11][23] = 'M'; showstate[8][7] = 'M'; showstate[6][18] = 'M'; showstate[3][12] = 'M'; return; } /*----------- 定义墙壁的位置------------*/ void initwow() { showstate[5][21] = 'W'; showstate[5][5] = 'W'; showstate[4][50] = 'W'; showstate[12][8] = 'W'; /*--------------目标点--------- showstate[10][21] = 'G'; showstate[11][5] = 'G'; showstate[15][50] = 'G'; showstate[7][8] = 'G'; ---------------*/ return; } /*---------显示屏幕所有的元素-------------------*/ void showall() { int i; int j; for(i=0;i<80;++i) { for(j=0;j<25;++j) { if(showstate[i][j]=='0') { gotoxy(i,j); printf("\0",showstate[i][j]); } else { gotoxy(i,j); printf("%c",showstate[i][j]); } } } return; } int winning() { int i,j; for(i=0;i<80;++i) { for(j=0;j<25;++j) { if(showstate[i][j]!='G') { return 1; } else { return 0; } } } }。
[ 本帖最后由 cosam 于 2011-10-7 11:32 编辑 ]