C语言推箱子小游戏程序

合集下载

推箱子C语言程序报告

推箱子C语言程序报告

推箱子C语言程序报告推箱子是一种极富智慧和策略性的游戏,是著名的益智游戏之一、它不仅可以锻炼人们的逻辑思维能力和创造力,而且还能提高人们的解决问题的能力和耐心。

本篇报告将介绍一个基于C语言开发的推箱子游戏,包括游戏规则、程序设计思路和实现效果等方面的内容。

一、游戏规则推箱子游戏的规则十分简单,玩家需要操作一个小人将该场景中的木箱全部推到指定位置。

在游戏开始时,场景中会放置一定数量的木箱和一个小人。

玩家可以通过键盘输入上下左右四个方向键来移动小人,小人可以推动场景中的木箱,但不能直接拉动或推动多个箱子。

当所有的木箱都被推到指定位置时,游戏胜利,反之则失败。

二、程序设计思路推箱子游戏的实现涉及到场景的渲染、小人和木箱的移动、游戏的逻辑判断等多个方面。

下面将分别介绍这些方面的程序设计思路。

1.场景渲染2.小人和木箱的移动玩家操作小人的移动,可以通过监听键盘输入的方向键来实现。

根据输入的方向,判断小人与目标位置的关系,如果目标位置是空地或者指定位置,则小人可以移动到目标位置,否则不能移动。

当小人移动后,如果目标位置是木箱,则需要判断木箱与目标位置的关系,如果目标位置是空地或者指定位置,则木箱可以被推动到目标位置,否则不能推动。

3.游戏逻辑判断在每一次小人移动后,需要判断游戏是否胜利或失败。

胜利的条件是所有的木箱都被推到指定位置,判断的方法是在遍历整个场景时,检查是否存在未推到指定位置的木箱。

失败的条件是小人无法再移动,判断的方法是在判断小人是否能够移动时,如果没有可移动的方向,则游戏失败。

三、实现效果通过以上的程序设计思路,可以开发出一个基于C语言的推箱子游戏。

该游戏可以提供丰富的关卡和难度选择,让玩家能够不断挑战和提高自己的智力。

游戏的界面简洁明了,操作方便快捷,给玩家带来舒适的游戏体验。

总结:推箱子是一种极富智慧的益智游戏,本篇报告介绍了一个基于C语言开发的推箱子游戏。

通过对游戏规则、程序设计思路和实现效果的阐述,可以看出该游戏具有多样化的场景和难度选择,能够锻炼玩家的智力和思维能力。

推箱子的c语言代码

推箱子的c语言代码

推箱子的c语言代码推箱子(Sokoban)是一种经典的益智游戏,也是计算机程序设计中常用的案例之一。

在这个游戏中,玩家需要移动箱子,将它们推到指定位置上,以完成关卡的目标。

在C语言中,我们可以使用二维数组来表示游戏的地图,其中不同的字符代表不同的元素。

例如,可以使用'#'表示墙壁,'@'表示玩家,'$'表示箱子,'.'表示目标位置。

玩家可以通过控制输入来移动,将箱子推到目标位置上。

为了实现这个游戏,我们可以先定义一个二维数组来表示地图,然后通过循环读取用户的输入,根据输入来移动玩家和箱子,最后判断是否完成了关卡的目标。

我们需要定义一个二维数组来表示地图。

假设地图的大小是10x10,我们可以这样定义:```c#define MAP_SIZE 10char map[MAP_SIZE][MAP_SIZE] = {"##########","# #","# $ #","# @ #","# #","# #","# #","# #","# #","##########"};```其中,'#'表示墙壁,'@'表示玩家,'$'表示箱子,'.'表示目标位置。

空格表示可通行的空地。

接下来,我们可以定义一个函数来打印地图,以便在每一次移动后显示最新的状态:```cvoid printMap() {for (int i = 0; i < MAP_SIZE; i++) {printf("%s\n", map[i]);}}```然后,我们可以定义一个循环,接收用户的输入,并根据输入来移动玩家和箱子。

实验四【程序源码 8-3】推箱子(C版)学生实验讲解

实验四【程序源码 8-3】推箱子(C版)学生实验讲解

/*实验要求:1.有些方向的移动不能工作,需要根据能工作的进行对比研究后修改,确保四个方向都能正常移动2.计数器工作有故障,需要根据实际效果找到问题解决之3.添加第四关的基础数据并使之在软件开始玩和重新玩中全部承认4.添加栈的机制使得可以在推到墙体时可以后退一步或者数步*///推箱子小游戏C版#include<stdio.h>#include<windows.h>#include<stdlib.h>#include<conio.h>const roomsize=9; //设计房间内部为正方形,边长为9int map[roomsize+2][roomsize+2]; //推箱子房间布局的数据结构:二维数组int data; //记录最短步骤数目int times=0;int array[2]={100,100}; //记录最好成绩char string[30]="正在装入..................";//以下为前几轮游戏房间中细节布局的数据结构:二维数组的实际内容int map1[roomsize+2][roomsize+2]={ //0,1,2,3,4,5,6,7,8,9,10{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, //0{-1,0,0,0,0,1,1,1,1,1,-1}, //1{-1,0,0,0,0,1,0,0,0,1,-1}, //2{-1,1,1,1,0,1,0,0,0,1,-1}, //3{-1,1,2,1,0,1,0,0,0,1,-1}, //4{-1,1,2,1,0,1,0,3,0,1,-1}, //5{-1,1,2,1,1,1,0,3,0,1,-1}, //6{-1,1,0,0,0,0,3,4,0,1,-1}, //7{-1,1,0,0,1,0,0,0,0,1,-1}, //8{-1,1,1,1,1,1,1,1,1,1,-1}, //9{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10};int map2[roomsize+2][roomsize+2]={ //0,1,2,3,4,5,6,7,8,9,10{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//0{-1,0,1,1,1,1,0,0,0,-1,-1}, //1{-1,0,1,4,0,1,1,1,0,-1,-1}, //2{-1,0,1,0,3,0,0,1,0,-1,-1}, //3{-1,1,1,1,0,1,0,1,1,-1,-1}, //4{-1,1,2,1,0,1,0,0,1,-1,-1}, //5{-1,1,2,3,0,0,1,0,1,-1,-1}, //6{-1,1,2,0,0,0,3,0,1,-1,-1}, //7{-1,1,1,1,1,1,1,1,1,-1,-1}, //8{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//9{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10 };int map3[roomsize+2][roomsize+2]={ //0,1,2,3,4,5,6,7,8,9,10{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//0{-1,1,1,1,1,1,1,1,1,-1,-1}, //1{-1,1,4,0,0,0,1,1,1,-1,-1}, //2{-1,1,0,3,3,0,0,0,1,-1,-1}, //36{-1,1,0,2,1,2,0,0,1,-1,-1}, //4{-1,1,0,0,1,1,3,0,1,-1,-1}, //5{-1,1,0,0,1,1,2,1,1,-1,-1}, //6{-1,1,0,0,0,0,0,1,1,-1,-1}, //7{-1,1,1,1,1,1,1,1,1,-1,-1}, //8{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//9{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10 };//其他关的房间布局模板int map4[roomsize+2][roomsize+2]={ //0,1,2,3,4,5,6,7,8,9,10{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//0{-1,1,1,1,1,1,1,1,1,-1,-1}, //1{-1,1,0,0,1,0,0,0,1,-1,-1}, //2{-1,1,0,1,0,0,1,0,1,-1,-1}, //3{-1,1,0,0,0,3,1,0,1,-1,-1}, //4{-1,1,1,1,3,0,3,4,1,-1,-1}, //5{-1,1,0,0,0,0,1,0,1,-1,-1}, //6{-1,1,0,2,2,2,0,0,1,-1,-1}, //7{-1,1,1,1,1,1,1,1,1,-1,-1}, //8{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//9{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} //10};int positionh;//人的位置纵坐标int positionl;//人的位置横坐标int flag;//标志位,记录人在目标位置上int gate;//记录关数int step;//记录步数void initbox();//初始化函数void begin();//开始界面void choose_gate();//选关提示void choose();//游戏时c选项的提示void replay();//重玩void playing();//玩游戏时界面void display();//显示地图void moveleft();//移向左方向void moveright();//移向右方向void movedown();//移向下方向void moveup();//移向上方向void test_flag();//过关提示void record();//成绩排行榜//=====void playing()//Ascii码键盘键位:左为75 右为77 上为72 下为80 {int choice;step=0;printf("开始游戏!");while(1){display();switch(getch()){case 72:moveup();step++;break;case 80:movedown();step++;break;case 75:moveleft();step++;break;case 77:moveright();step++;break;case 'c':case 'C':choose();break;case 'q':case 'Q':printf(" ╭─────────────╮\n");printf(" │是退出游戏还是返回到主界面? │\n");printf(" │ 1. 返回主界面│\n");printf(" │ 2. 退出游戏│\n");printf(" ╰──────────────╯\n");scanf("%d",&choice);switch(choice){case 1:step=0;Sleep(500);system("cls");begin();break;case 2:exit(0);}default:break;}system("cls");}}void display(){printf("\n\n\n\n\n");for(int i=1;i<=roomsize;i++){printf(" ");for(int j=1;j<=roomsize;j++){if(map[i][j]==0) printf(" ");if(map[i][j]==1) printf("■");//墙if(map[i][j]==2) printf("○");//目标位置if(map[i][j]==3) printf("★");//箱子if(map[i][j]==4) printf("☇");//人if(map[i][j]==5) printf("㊣");//箱子在目标位置上}printf("\n");}printf("\n\n");printf("选项(c) 步数:%d\n",step);}void moveright(){if(map[positionh][positionl+1]==0)//向空白位置移动{map[positionh][positionl+1]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionl++;}else if(map[positionh][positionl+1]==2)//人要到目标位置上{map[positionh][positionl+1]=4;if(flag==1)//人在目标位置上map[positionh][positionl]=2;//恢复目标位置else{map[positionh][positionl]=0;//恢复原来的状态flag=1;//标志位,记录人在目标位置上}positionl++;}else if(map[positionh][positionl+1]==3&&map[positionh][positionl+2]==0)//将箱子推到空白位置上{map[positionh][positionl+2]=3;map[positionh][positionl+1]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionl++;}else if(map[positionh][positionl+1]==5&&map[positionh][positionl+2]!=1)//要将箱子从目标位置上推出{if(map[positionh][positionl+2]==2)//下一个位置还是目标位置{map[positionh][positionl+2]=5;map[positionh][positionl+1]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0; flag=1; }}else if(map[positionh][positionl+2]==0)//下一个位置是空白{map[positionh][positionl+2]=3;map[positionh][positionl+1]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0; flag=1; }}positionl++;}else if(map[positionh][positionl+1]==3&&map[positionh][positionl+2]==2)//要将箱子推到目标位置上{map[positionh][positionl+2]=5;//箱子在目标位置上map[positionh][positionl+1]=4;if(flag==1)//人在目标位置上{ map[positionh][positionl]=2; flag=0; }else //人不在目标位置上map[positionh][positionl]=0;positionl++;}else step--;//抵消人不动的情况test_flag();}void moveleft(){if(map[positionh][positionl-1]==0)//向空白位置移动{map[positionh][positionl-1]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionl--;}else if(map[positionh][positionl-1]==2)//人要到目标位置上{map[positionh][positionl-1]=4;if(flag==1)//人在目标位置上map[positionh][positionl]=2;//恢复目标位置else{map[positionh][positionl]=0;//恢复原来的状态flag=1;//标志位,记录人在目标位置上}positionl--;}else if(map[positionh][positionl-1]==3&&map[positionh][positionl-2]==0)//将箱子推到空白位置上{map[positionh][positionl-2]=3;map[positionh][positionl-1]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionl--;}else if(map[positionh][positionl-1]==5&&map[positionh][positionl-2]!=1)//要将箱子从目标位置上推出{if(map[positionh][positionl-2]==2)//下一个位置还是目标位置{map[positionh][positionl-2]=5;map[positionh][positionl-1]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0; flag=1; }}else if(map[positionh][positionl-2]==0)//下一个位置是空白{map[positionh][positionl-2]=3;map[positionh][positionl-1]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0; flag=1; }}positionl--;}else if(map[positionh][positionl-1]==3&&map[positionh][positionl-2]==2)//要将箱子推到目标位置上{map[positionh][positionl-2]=5;//箱子在目标位置上map[positionh][positionl-1]=4;if(flag==1)//人在目标位置上{ map[positionh][positionl]=2; flag=0; }else //人不在目标位置上map[positionh][positionl]=0;positionl--;}else step--;//抵消人不动的情况test_flag();}void moveup(){if(map[positionh-1][positionl]==0){map[positionh-1][positionl]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionh--;}else if(map[positionh-1][positionl]==2)//人要到目标位置上{map[positionh-1][positionl]=4;if(flag==1)map[positionh][positionl]=2;//恢复目标位置else{map[positionh][positionl]=0;//恢复原来的状态flag=1;//标志位,记录人在目标位置上}positionh--;}else if(map[positionh-1][positionl]==3&&map[positionh-2][positionl]==0)//将箱子推到空白位置上{map[positionh-2][positionl]=3;map[positionh-1][positionl]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionh--;}else if(map[positionh-1][positionl]==5&&map[positionh-2][positionl]!=1)//要将箱子从目标位置上推出{if(map[positionh-2][positionl]==2)//下一个位置还是目标位置{map[positionh-2][positionl]=5;map[positionh-1][positionl]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0;flag=1; }}else if(map[positionh-2][positionl]==0)//下一个位置是空白{map[positionh-2][positionl]=3;map[positionh-1][positionl]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0;flag=1; }}positionh--;}else if(map[positionh-1][positionl]==3&&map[positionh-2][positionl]==2)//要将箱子推到目标位置上{map[positionh-2][positionl]=5;//箱子在目标位置上map[positionh-1][positionl]=4;if(flag==1)//人在目标位置上{ map[positionh][positionl]=2;flag=0; }else //人不在目标位置上map[positionh][positionl]=0;positionh--;}else step--;//抵消人不动的情况test_flag();}void movedown(){if(map[positionh+1][positionl]==0){map[positionh+1][positionl]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionh++;}else if(map[positionh+1][positionl]==2)//人要到目标位置上{map[positionh+1][positionl]=4;if(flag==1)map[positionh][positionl]=2;//恢复目标位置else{map[positionh][positionl]=0;//恢复原来的状态flag=1;//标志位,记录人在目标位置上}positionh++;}else if(map[positionh+1][positionl]==3&&map[positionh+2][positionl]==0)//将箱子推到空白位置上{map[positionh+2][positionl]=3;map[positionh+1][positionl]=4;if(flag==1){ map[positionh][positionl]=2; flag=0; }elsemap[positionh][positionl]=0;positionh++;}else if(map[positionh+1][positionl]==5&&map[positionh+2][positionl]!=1)//要将箱子从目标位置上推出{if(map[positionh+2][positionl]==2)//下一个位置还是目标位置{map[positionh+2][positionl]=5;map[positionh+1][positionl]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0;flag=1; }}else if(map[positionh+2][positionl]==0)//下一个位置是空白{map[positionh+2][positionl]=3;map[positionh+1][positionl]=4;if(flag==1)map[positionh][positionl]=2;else{ map[positionh][positionl]=0;flag=1; }}positionh++;else if(map[positionh+1][positionl]==3&&map[positionh+2][positionl]==2)//要将箱子推到目标位置上{map[positionh+2][positionl]=5;//箱子在目标位置上map[positionh+1][positionl]=4;if(flag==1)//人在目标位置上{ map[positionh][positionl]=2;flag=0; }else //人不在目标位置上map[positionh][positionl]=0;positionh++;}else step--;//抵消人不动的情况test_flag();}void initbox()//初始化函数{positionh=0;positionl=0;flag=0;step=0;gate=0;}void begin(){printf(" ╭────────────────────────────────╮\n");printf(" ││\n");printf(" │★☆★推箱子★☆★│\n");printf(" │╭─────╮│\n");printf(" ││游戏简介││\n");printf(" │╰──────╯│\n");printf(" │推箱子游戏:最终目的是操作一个人(☇)将箱子(★)推到目标位置(○)上。

C语言编写的推箱子游戏

C语言编写的推箱子游戏

/* 一个C语言编写的推箱子游戏源代码*//* 本游戏是字符模式的,请不要在中文dos下运行。

本游戏在TURBO C下调试通过*/#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>/* 定义二维数组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;/* 在特定的坐标上画墙壁并用数组记录状态的函数*/void printwall(int x,int y){putchxy(y-1,x-1,219,GREEN,BLACK);ghouse[x][y]='w';}/* 在特定的坐标上画箱子并用数组记录状态的函数*/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;}}/* 在特定的坐标上画目的地并用数组记录状态的函数*/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."); }/* 第一关的图象初始化*/winer *inithouse1(){int x,y;winer *win=NULL,*pw;for(x=1,y=5;y<=9;y++)printwall(x+4,y+10);for(y=5,x=2;x<=5;x++)printwall(x+4,y+10);for(y=9,x=2;x<=5;x++)printwall(x+4,y+10);for(y=1,x=3;x<=8;x++)printwall(x+4,y+10);for(x=3,y=3;x<=5;x++)printwall(x+4,y+10);for(x=5,y=8;x<=9;x++)printwall(x+4,y+10);for(x=7,y=4;x<=9;x++)printwall(x+4,y+10);for(x=9,y=5;y<=7;y++)printwall(x+4,y+10);for(x=8,y=2;y<=3;y++)printwall(x+4,y+10); printwall(5+4,4+10);printwall(5+4,7+10);printwall(3+4,2+10);printbox(3+4,6+10);printbox(3+4,7+10);printbox(4+4,7+10);printwhither1(4+4,2+10,&win,&pw); printwhither1(5+4,2+10,&win,&pw); printwhither1(6+4,2+10,&win,&pw); printman(2+4,8+10);return win;}/* 第三关的图象初始化*/winer *inithouse3(){int x,y;winer *win=NULL,*pw;for(x=1,y=2;y<=8;y++)printwall(x+4,y+10);for(x=2,y=2;x<=4;x++)printwall(x+4,y+10);for(x=4,y=1;y<=3;y++)printwall(x+4,y+10);for(x=5,y=1;x<=8;x++)printwall(x+4,y+10);for(x=8,y=2;y<=5;y++)printwall(x+4,y+10);for(x=5,y=5;x<=7;x++)printwall(x+4,y+10);for(x=7,y=6;y<=9;y++)printwall(x+4,y+10);for(x=3,y=9;x<=6;x++)printwall(x+4,y+10);for(x=3,y=6;y<=8;y++)printwall(x+4,y+10);printwall(2+4,8+10);printwall(5+4,7+10);printbox(6+4,3+10);printbox(4+4,4+10);printbox(5+4,6+10);printwhither1(2+4,5+10,&win,&pw); printwhither1(2+4,6+10,&win,&pw); printwhither1(2+4,7+10,&win,&pw); printman(2+4,4+10);return win;}/* 第二关的图象初始化*/winer *inithouse2(){int x,y;winer *win=NULL,*pw;for(x=1,y=4;y<=7;y++)printwall(x+4,y+10);for(x=2,y=2;y<=4;y++)printwall(x+4,y+10);for(x=2,y=7;x<=4;x++)printwall(x+4,y+10);for(x=4,y=1;x<=8;x++)printwall(x+4,y+10);for(x=8,y=2;y<=8;y++)printwall(x+4,y+10);for(x=4,y=8;x<=8;x++)printwall(x+4,y+10);for(x=4,y=6;x<=5;x++)printwall(x+4,y+10);for(x=3,y=2;x<=4;x++)printwall(x+4,y+10);for(x=4,y=4;x<=5;x++)printwall(x+4,y+10);printwall(6+4,3+10);printbox(3+4,5+10);printbox(6+4,6+10);printbox(7+4,3+10);printwhither1(5+4,7+10,&win,&pw); printwhither1(6+4,7+10,&win,&pw); printwhither1(7+4,7+10,&win,&pw); printman(2+4,6+10);return win;}/* 第四关的图象初始化*/winer *inithouse4(){int x,y;winer *win=NULL,*pw;for(x=1,y=1;y<=6;y++)printwall(x+4,y+10);for(x=2,y=7;y<=8;y++)printwall(x+4,y+10);for(x=2,y=1;x<=7;x++)printwall(x+4,y+10);for(x=7,y=2;y<=4;y++)printwall(x+4,y+10);for(x=6,y=4;y<=9;y++)printwall(x+4,y+10);for(x=3,y=9;x<=5;x++)printwall(x+4,y+10);for(x=3,y=3;y<=4;y++)printwall(x+4,y+10);printwall(3+4,8+10);printbox(3+4,5+10);printbox(4+4,4+10);printbox(4+4,6+10);printbox(5+4,5+10);printbox(5+4,3+10);printwhither1(3+4,7+10,&win,&pw); printwhither1(4+4,7+10,&win,&pw); printwhither1(5+4,7+10,&win,&pw); printwhither1(4+4,8+10,&win,&pw); printwhither1(5+4,8+10,&win,&pw); printman(2+4,2+10);return win;}/* 移动在空地上的箱子到空地上*/ movebox(int x,int y,char a){switch(a){case 'u':ghouse[x-1][y]=0;printf(" ");printbox(x-2,y);printman(x-1,y);ghouse[x-2][y]='b';break;case 'd':ghouse[x+1][y]=0;printf(" ");printbox(x+2,y);printman(x+1,y);ghouse[x+2][y]='b';break;case 'l':ghouse[x][y-1]=0;printf(" ");printbox(x,y-2);printman(x,y-1);ghouse[x][y-2]='b';break;case 'r':ghouse[x][y+1]=0;printf(" ");printbox(x,y+2);printman(x,y+1);ghouse[x][y+2]='b';break;default: break;}}/* 移动在目的地上的箱子到空地上*/ moveinbox(int x,int y,char a){switch(a){case 'u':ghouse[x-1][y]='m';printf(" ");printbox(x-2,y);printman(x-1,y);ghouse[x-2][y]='b';break;case 'd':ghouse[x+1][y]='m';printf(" ");printbox(x+2,y);printman(x+1,y);ghouse[x+2][y]='b';break;case 'l':ghouse[x][y-1]='m';printf(" ");printbox(x,y-2);printman(x,y-1);ghouse[x][y-2]='b';break;case 'r':ghouse[x][y+1]='m';printf(" ");printbox(x,y+2);printman(x,y+1);ghouse[x][y+2]='b';break;default: break;}}/* 移动在空地上的箱子到目的地上*/moveboxin(int x,int y,char a){switch(a){case 'u':ghouse[x-1][y]=0;printf(" ");printboxin(x-2,y);printman(x-1,y);ghouse[x-2][y]='i';break;case 'd':ghouse[x+1][y]=0;printf(" ");printboxin(x+2,y);printman(x+1,y);ghouse[x+2][y]='i';break;case 'l':ghouse[x][y-1]=0;printf(" ");printboxin(x,y-2);printman(x,y-1);ghouse[x][y-2]='i';break;case 'r':ghouse[x][y+1]=0;printf(" ");printboxin(x,y+2);printman(x,y+1);ghouse[x][y+2]='i';break;default: break;}}/* 移动在目的地上的箱子到目的地*/ moveinboxin(int x,int y,char a){switch(a){case 'u':ghouse[x-1][y]='m';printf(" ");printboxin(x-2,y);printman(x-1,y);ghouse[x-2][y]='i';break;case 'd':ghouse[x+1][y]='m';printf(" ");printboxin(x+2,y);printman(x+1,y);ghouse[x+2][y]='i';break;case 'l':ghouse[x][y-1]='m';printf(" ");printboxin(x,y-2);printman(x,y-1);ghouse[x][y-2]='i';break;case 'r':ghouse[x][y+1]='m';printf(" ");printboxin(x,y+2);printman(x,y+1);ghouse[x][y+2]='i';break;default: break;}}/* 判断特定的坐标上的状态*/int judge(int x,int y){int i;switch(ghouse[x][y]){case 0: i=1;break;case 'w': i=0;break;case 'b': i=2;break;case 'i': i=4;break;case 'm': i=3;break;default: break;}return i;}/* 处理按下键盘后,人物移动的主函数*/move(int x,int y,char a){switch(a){case 'u':if(!judge(x-1,y)) {gotoxy(y,x);break;}else if(judge(x-1,y)==1||judge(x-1,y)==3){if(judge(x,y)==3){ printwhither(x,y);printman(x-1,y);break;}else{printf(" ");printman(x-1,y);break;}}else if(judge(x-1,y)==2){ if(judge(x-2,y)==1){movebox(x,y,'u');if(judge(x,y)==3) printwhither(x,y); gotoxy(y,x-1);}else if(judge(x-2,y)==3){ moveboxin(x,y,'u');if(judge(x,y)==3) printwhither(x,y); gotoxy(y,x-1);}else gotoxy(y,x);break;}else if(judge(x-1,y)==4){ if(judge(x-2,y)==1){moveinbox(x,y,'u');if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){ moveinboxin(x,y,'u');if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x-1);}else gotoxy(y,x);break;}case 'd':if(!judge(x+1,y)) {gotoxy(y,x);break;}else if(judge(x+1,y)==1||judge(x+1,y)==3){if(judge(x,y)==3){ printwhither(x,y);printman(x+1,y);break;}else{printf(" ");printman(x+1,y);break;}}else if(judge(x+1,y)==2){ if(judge(x+2,y)==1){movebox(x,y,'d');if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveboxin(x,y,'d');if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);}else gotoxy(y,x);break;}else if(judge(x+1,y)==4){ if(judge(x+2,y)==1){moveinbox(x,y,'d');if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3) {moveinboxin(x,y,'d');if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);}else gotoxy(y,x);break;}case 'l':if(!judge(x,y-1)) {gotoxy(y,x);break;}else if(judge(x,y-1)==1||judge(x,y-1)==3){if(judge(x,y)==3){ printwhither(x,y);printman(x,y-1);break;} else{printf(" ");printman(x,y-1);break;}}else if(judge(x,y-1)==2){ if(judge(x,y-2)==1){movebox(x,y,'l');if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveboxin(x,y,'l');if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);}else gotoxy(y,x);break;}else if(judge(x,y-1)==4){ if(judge(x,y-2)==1){moveinbox(x,y,'l');if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveinboxin(x,y,'l');if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);}else gotoxy(y,x);break;}case 'r':if(!judge(x,y+1)) {gotoxy(y,x);break;}else if(judge(x,y+1)==1||judge(x,y+1)==3){if(judge(x,y)==3){printwhither(x,y);printman(x,y+1);break;}else{printf(" ");printman(x,y+1);break;}}else if(judge(x,y+1)==2){ if(judge(x,y+2)==1){movebox(x,y,'r');if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveboxin(x,y,'r');if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);}else gotoxy(y,x);break;}else if(judge(x,y+1)==4){ if(judge(x,y+2)==1){moveinbox(x,y,'r');if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3) {moveinboxin(x,y,'r');if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);}else gotoxy(y,x);break;}default: break;}}/* 按下空格键后,回到本关开头的函数*/ void reset(int i){switch(i){case 0: init();inithouse1();break;case 1: init();inithouse2();break;case 2: init();inithouse3();break;case 3: init();inithouse4();break;default:break;}}/* 主函数main */void main(){int key,x,y,s,i=0;winer *win,*pw;_AL=3;_AH=0;geninterrupt(0x10);init();win=inithouse1();do{_AH=3;geninterrupt(0x10);x=_DH+1;y=_DL+1;while(bioskey(1)==0);key=bioskey(0);switch(key){case 0x4800:move(x,y,'u');break; /* 按下向上键后*/case 0x5000:move(x,y,'d');break; /* 按下向下键后*/case 0x4b00:move(x,y,'l');break; /* 按下向左键后*/case 0x4d00:move(x,y,'r');break; /* 按下向右键后*/case 0x3920:reset(i);break; /* 按下空格键后*/default:break;}s=0;pw=win;while(pw){if(ghouse[pw->x][pw->y]=='m') s++;pw=pw->p;}if(s==0){free(win);gotoxy(25,2);printf("congratulate! you did a good job!");getch();i++;switch(i){case 1: init();win=inithouse2();break;case 2: init();win=inithouse3();break;case 3: init();win=inithouse4();break;case 4: gotoxy(15,21);printf("My dear Friend, How smart you are! Welcome to play again!"); key=0x011b;getch();break;default: break;}}}while(key!=0x011b);_AL=3;_AH=0;geninterrupt(0x10);}。

C语言推箱子程序设计

C语言推箱子程序设计
{case DOWN:
//printf("\n下\n");
manf_p.x=man_p.x;manff_p.x=man_p.x;
manf_p.y=man_p.y+1,manff_p.y=man_p.y+2;
X=0,Y=1;
break;
case UP:
//printf("\n上\n");
manf_p.x=man_p.x;manff_p.x=man_p.x;
void findman(int &a,int &b,int map[14][16]);/*找到小人坐标位置*/
unsigned char getKey();/*获得有效按键返回按键值*/
int getmap(Point p,int map[14][16]);/*获得点p的地图值*/
void moveman(Point man_p,Point manf_p,int X,int Y,int map[14][16]);/*人移动*/
#define N_AISLE 0
#define WALL "■" //墙壁
#define N_WALL 1
#define AIM "※" //未放箱子的目标
#define N_AIM 2
#define BOX "□" //未在目标位置的箱子
#define N_BOX 3
#define MAN "♀" //人
(注:VC运行时,分别新建两个文件BotIt.cpp和Maps.h,再在同一目录下运行其中cpp)
代码部分
文件“BoxIt.cpp”如下:
#include <stdio.h>

推箱子游戏C语言编码

推箱子游戏C语言编码

#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>typedef struct winer{int x,y;struct winer *p;}winer;char status [20][20];char far *printScreen=(char far* )0xB8000000;void putoutChar(int y,int x,char ch,char fc,char bc);void printWall(int x, int y);void printBox(int x, int y);void printBoxDes(int x, int y);void printDestination(int x, int y);void printDestination1(int x,int y,winer **win,winer **pw); void printMan(int x, int y);void init();winer *initStep1();winer *initStep2();winer *initStep3();winer *initStep4();void moveBoxSpacetoSpace(int x ,int y, char a);void moveBoxDestoSpace(int x ,int y, char a) ;void moveBoxSpacetoDes(int x, int y, char a);void moveBoxDestoDes(int x, int y, char a);int judge(int x, int y);void move(int x, int y, char a);void reset(int i);void putoutChar(int y,int x,char ch,char fc,char bc){printScreen[(x*160)+(y<<1)+0]=ch;printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;}void printWall(int x,int y){putoutChar(y-1,x-1,219,GREEN,BLACK);status[x][y]='w';}void printBox(int x,int y){putoutChar(y-1,x-1,10,WHITE,BLACK);status[x][y]='b';}void printDestination1(int x,int y,winer **win,winer **pw) {winer *qw;putoutChar(y-1,x-1,003,YELLOW,BLACK);status[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;}}void printDestination(int x,int y){putoutChar(y-1,x-1,003,YELLOW,BLACK);status[x][y]='m';}void printMan(int x,int y){gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}void printBoxDes(int x,int y){putoutChar(y-1,x-1,10,YELLOW,BLACK);status[x][y]='i';}void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)status[i][j]=0;_AL=3;_AH=0;geninterrupt(0x10);gotoxy(41,4);printf(" --------- up");gotoxy(41,6);printf(" --------- down");gotoxy(41,8);printf(" --------- left");gotoxy(41,10);printf(" --------- right");gotoxy(40,12);printf("Space ----- reset");gotoxy(40,14);printf("Esc ------- quit");gotoxy(18,24);printf("CopyRight: 2008 LuoFuxing");gotoxy(40,4);_CX=01;_AH=0xa;_AL=24;geninterrupt(0x10);gotoxy(40,6);_CX=01;_AH=0xa;_AL=25;geninterrupt(0x10);gotoxy(40,8);_CX=01;_AH=0xa;_AL=27;geninterrupt(0x10);gotoxy(40,10);_CX=01;_AH=0xa;_AL=26;geninterrupt(0x10);}winer *initStep1(){int x;int y;winer *win=NULL;winer *pw;gotoxy(25,2);printf("Level 1");for(x=1,y=5;y<=9;y++)printWall(x+4,y+10);for(y=5,x=2;x<=5;x++)printWall(x+4,y+10);for(y=9,x=2;x<=5;x++)printWall(x+4,y+10);for(y=1,x=3;x<=8;x++)printWall(x+4,y+10);for(x=3,y=3;x<=5;x++)printWall(x+4,y+10);for(x=5,y=8;x<=9;x++)printWall(x+4,y+10);for(x=7,y=4;x<=9;x++)printWall(x+4,y+10);for(x=9,y=5;y<=7;y++)printWall(x+4,y+10);for(x=8,y=2;y<=3;y++)printWall(x+4,y+10);printWall(5+4,4+10);printWall(5+4,7+10);printWall(3+4,2+10);printBox(3+4,6+10);printBox(3+4,7+10);printBox(4+4,7+10);printDestination1(4+4,2+10,&win,&pw);printDestination1(5+4,2+10,&win,&pw);printDestination1(6+4,2+10,&win,&pw);printMan(2+4,8+10);return win;}winer *initStep2(){int x;int y;winer *win=NULL;winer *pw;gotoxy(25,2);printf("Level 2");for(x=1,y=4;y<=7;y++)printWall(x+4,y+10);for(x=2,y=2;y<=4;y++)printWall(x+4,y+10);for(x=2,y=7;x<=4;x++)printWall(x+4,y+10);for(x=4,y=1;x<=8;x++)printWall(x+4,y+10);for(x=8,y=2;y<=8;y++)printWall(x+4,y+10);for(x=4,y=8;x<=8;x++)printWall(x+4,y+10);for(x=4,y=6;x<=5;x++)printWall(x+4,y+10);for(x=3,y=2;x<=4;x++)printWall(x+4,y+10);for(x=4,y=4;x<=5;x++)printWall(x+4,y+10);printWall(6+4,3+10);printBox(3+4,5+10);printBox(6+4,6+10);printBox(7+4,3+10);printDestination1(5+4,7+10,&win,&pw);printDestination1(6+4,7+10,&win,&pw);printDestination1(7+4,7+10,&win,&pw);printMan(2+4,6+10);return win;}winer *initStep3(){int x;int y;winer *win=NULL;winer *pw;gotoxy(25,2);printf("Level 3");for(x=1,y=2;y<=8;y++)printWall(x+4,y+10);for(x=2,y=2;x<=4;x++)printWall(x+4,y+10);for(x=4,y=1;y<=3;y++)printWall(x+4,y+10);for(x=5,y=1;x<=8;x++)printWall(x+4,y+10);for(x=8,y=2;y<=5;y++)printWall(x+4,y+10);for(x=5,y=5;x<=7;x++)printWall(x+4,y+10);for(x=7,y=6;y<=9;y++)printWall(x+4,y+10);for(x=3,y=9;x<=6;x++)printWall(x+4,y+10);for(x=3,y=6;y<=8;y++)printWall(x+4,y+10);printWall(2+4,8+10);printWall(5+4,7+10);printBox(6+4,3+10);printBox(4+4,4+10);printBox(5+4,6+10);printDestination1(2+4,5+10,&win,&pw);printDestination1(2+4,6+10,&win,&pw);printDestination1(2+4,7+10,&win,&pw);printMan(2+4,4+10);return win;}winer *initStep4(){int x;int y;winer *win=NULL;winer *pw;gotoxy(23,2);printf("The Last Level");for(x=1,y=1;y<=6;y++)printWall(x+4,y+10);for(x=2,y=7;y<=8;y++)printWall(x+4,y+10);for(x=2,y=1;x<=7;x++)printWall(x+4,y+10);for(x=7,y=2;y<=4;y++)printWall(x+4,y+10);for(x=6,y=4;y<=9;y++)printWall(x+4,y+10);for(x=3,y=9;x<=5;x++)printWall(x+4,y+10);for(x=3,y=3;y<=4;y++)printWall(x+4,y+10);printWall(3+4,8+10);printBox(3+4,5+10);printBox(4+4,4+10);printBox(4+4,6+10);printBox(5+4,5+10);printBox(5+4,3+10);printDestination1(3+4,7+10,&win,&pw);printDestination1(4+4,7+10,&win,&pw);printDestination1(5+4,7+10,&win,&pw);printDestination1(4+4,8+10,&win,&pw);printDestination1(5+4,8+10,&win,&pw);printMan(2+4,2+10);return win;}void moveBoxSpacetoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]=0;printf(" ");printBox(x+2,y);status[x+2][y]='b';break;case 'l':status[x][y-1]=0;printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]=0;printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxDestoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]='m';printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]='m';printf(" ");printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]='m';printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxSpacetoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]=0;printf(" ");printBoxDes(x+2,y);printMan(x+1,y);status[x+2][y]='i';break;case 'l':status[x][y-1]=0;printf(" ");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]=0;printf(" ");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}void moveBoxDestoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]='m';printf(" ");printBoxDes(x+2,y);printMan(x+1,y);status[x+2][y]='i';break;case 'l':status[x][y-1]='m';printf(" ");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]='m';printf(" ");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}int judge(int x,int y){int i;switch(status[x][y]){case 0:i=1;break;case 'w':i=0;break;case 'b':i=2;break;case 'i':i=4;break;case 'm':i=3;break;default:break;}return i;}void move(int x,int y,char a) {switch(a){case 'u':if(!judge(x-1,y)){gotoxy(y,x);break;}else if(judge(x-1,y)==1||judge(x-1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x-1,y);break;}else{printf(" ");printMan(x-1,y);break;}}else if(judge(x-1,y)==2){if(judge(x-2,y)==1){moveBoxSpacetoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxSpacetoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}else if(judge(x-1,y)==4){if(judge(x-2,y)==1){moveBoxDestoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxDestoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}case 'd':if(!judge(x+1,y)){gotoxy(y,x);break;}else if(judge(x+1,y)==1||judge(x+1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x+1,y);break;}else{printf(" ");printMan(x+1,y);break;}}else if(judge(x+1,y)==2){if(judge(x+2,y)==1){moveBoxSpacetoSpace(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxSpacetoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}else if(judge(x+1,y)==4){if(judge(x+2,y)==1){moveBoxDestoSpace(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxDestoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}case 'l':if(!judge(x,y-1)){gotoxy(y,x);break;}else if(judge(x,y-1)==1||judge(x,y-1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y-1);break;}else{printf(" ");printMan(x,y-1);break;}}else if(judge(x,y-1)==2){if(judge(x,y-2)==1){moveBoxSpacetoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxSpacetoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}else if(judge(x,y-1)==4){if(judge(x,y-2)==1){moveBoxDestoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxDestoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}case 'r':if(!judge(x,y+1)){gotoxy(y,x);break;}else if(judge(x,y+1)==1||judge(x,y+1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y+1);break;}else{printf(" ");printMan(x,y+1);break;}}else if(judge(x,y+1)==2){if(judge(x,y+2)==1){moveBoxSpacetoSpace(x,y,'r');if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxSpacetoDes(x,y,'r');if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);}elsegotoxy(y,x);break;}else if(judge(x,y+1)==4){if(judge(x,y+2)==1){moveBoxDestoSpace(x,y,'r');if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxDestoDes(x,y,'r');if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);}elsegotoxy(y,x);break;}default:break;}}void reset(int i){switch(i){case 0:init();initStep1();break;case 1:init();initStep2();break;case 2:init();initStep3();break;case 3:init();initStep4();break;default:break;}}void main(){int key;int x;int y;int s;int i=0;winer *win;winer *pw;_AL=3;_AH=0;geninterrupt(0x10);init();win=initStep1();do{_AH=3;geninterrupt(0x10);x=_DH+1;y=_DL+1;while(bioskey(1)==0);key=bioskey(0);switch(key){case 0x4800:move(x,y,'u');break;case 0x5000:move(x,y,'d');break;case 0x4b00:move(x,y,'l');break;case 0x4d00:move(x,y,'r');break;case 0x3920:reset(i);break;default:break;}s=0;pw=win;while(pw){if(status[pw->x][pw->y]=='m')s++;pw=pw->p;}if(s==0){free(win);gotoxy(15,20);printf("congratulate! You have done this step!");getch();i++;switch(i){case 1:init();win=initStep2();break;case 2:init();win=initStep3();break;case 3:init();win=initStep4();break;case 4:gotoxy(15,21);printf("Congratulation! \n");gotoxy(15,22);printf("You have done all the steps, You are very clever!");key=0x011b;getch();break;default:break;}}}while(key!=0x011b);_AL=3;_AH=0;geninterrupt(0x10); }。

推箱子-C语言源码

推箱子-C语言源码

#include <stdio.h>#include <stdlib.h>//用到system()#include <conio.h>//因为需要用到getch()函数//推箱子_C语言版//写了一下午,第一个C语言游戏终于写好了,想想有点小激动....//蔚蓝之链编写//分享给大家参考下(在看得懂的情况下...),代码写得很凌乱.....,勿喷#define X 20#define Y 20#define REN 2 //表示人#define XIANGZI 3 //箱子#define WEIZHI 4 //箱子要推到的位置#define KONG 0 //空位#define QIANG 1 //墙#define RW 6 //人在箱子要推到的位置上#define XW 5 //箱子在箱子要推到的位置上#define bool short#define false 0#define true 1//数组内部数值表示内容:0表示空位,1表示墙体,2表示人,3表示箱子,4表示箱子要推到的位置,5表示既有箱子又有又位置,6表示既有位置又有人//01,2,3,4符号表示://0 :" " 1:■2:♀3:回4:◎void create_map_arr(int _x,int _y,int (*map_arr)[Y]);//创建地图数组void creata_map(int _x,int _y, int (*map)[Y]);//创建地图,重绘游戏画面void amend_map(int _x,int _y,char key , int (* arr_map)[Y]);//接收输入,修改游戏画面bool is_finish(int _x, int _y,int _total ,int (*arr_map)[Y]);//判断是否完成推箱子(地图x,地图y,总箱子数,地图数组)int main(void){int map_arr[X][Y];//存储地图的二维数组create_map_arr(X,Y,map_arr);//创建数组printf("C语言_推箱子v1.0 蔚蓝之链编写\n\n");printf("控制键:W↑,A←,S↓,D→\n");printf("提示:玩本游戏前请先关闭输入法\n");system("pause");system("cls");while(1){creata_map(X,Y,map_arr);//创建地图,重绘游戏画面if(is_finish(X,Y,2,map_arr) == true){printf("恭喜哦,推箱子完成!\n");system("pause");return 0;}amend_map(X,Y,getch(),map_arr);//接收输入,修改画面system("cls");//清空画面}getchar();return 0;}void create_map_arr (int _x,int _y,int (*map_arr)[Y])//创建地图数组{int x,y;for(x=0;x<_x;x++){for (y=0;y<_y;y++){if(y==0 || x==0 || x==_x-1 || y==_y-1){*(*(map_arr+x)+y) = QIANG;//printf ("■");}else{*(*(map_arr+x)+y) = KONG;//printf (" ");}}//printf("\n");}//这里可以修改箱子创建的位置,人创建的位置及箱子要达到的位置,默认两个箱子*(*(map_arr+3)+7)=REN;//人*(*(map_arr+5)+6)=XIANGZI;//箱子*(*(map_arr+5)+7)=XIANGZI;//箱子*(*(map_arr+1)+8)=WEIZHI;//箱子要推到的位置*(*(map_arr+2)+8)=WEIZHI;//箱子要推到的位置}void creata_map(int _x,int _y, int (*map)[Y])//创建地图(地图数组[][]){int x,y;for(x = 0; x < _x; x++){for (y = 0; y < _y; y++){if (*(*(map + x) + y) == QIANG){printf ("■");}else if(*(*(map + x) + y) == KONG){printf (" ");}else if(*(*(map + x) + y)==REN || *(*(map + x) + y)==RW){printf ("♀");}else if (* (*(map + x) + y)==XIANGZI){printf("□");}else if (*(*(map + x) + y)==WEIZHI){printf("◎");}else if(*(*(map + x) + y) == XW){printf("回");}}printf ("\n");}}//这个函数写的太乱了,自己看着也头大,不过还好,总算完成了^_^void amend_map(int _x,int _y,char key , int (* arr_map)[Y])//移动(地图x,地图y,按键值,地图数组){int x,y;for(x = 0; x < _x; x++){for (y = 0; y < _y; y++){if(*( * (arr_map + x ) + y) == REN || *( * (arr_map + x ) + y) == RW){if(key == 'a' || key == 'A')//←{if (y > 0){if(*(*(arr_map + x) +(y)) == RW)//人在最终位置{if(*( * (arr_map + x ) + (y-1)) == WEIZHI)//人左边还是最终位置{*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + y) = WEIZHI;}else if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + x) + (y - 1)) == KONG){*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + x ) + (y-1)) == WEIZHI)//人左边是最终位置{*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + x) + (y - 1)) == KONG){*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + x ) + (y-1)) == XW || *( * (arr_map + x ) + (y-1)) == XIANGZI)//人左边有箱子或箱子在最终位置{if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + x) + (y-1)) == WEIZHI){*(*(arr_map + x) + (y-1)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + x) + (y-1)) == KONG){*(*(arr_map + x) + (y-1)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//←else if(key == 'd' || key == 'D')//→{if (y < _y){if(*(*(arr_map + x) +(y)) == RW)//人在最终位置{if(*( * (arr_map + x ) + (y+1)) == WEIZHI)//人右边还是最终位置{*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + y) = WEIZHI;}else if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + x) + (y + 1)) == KONG){*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + x ) + (y+1)) == WEIZHI)//人右边是最终位置{*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + x) + (y + 1)) == KONG){*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + x ) + (y+1)) == XW || *( * (arr_map + x ) + (y+1)) == XIANGZI)//人右边有箱子或箱子在最终位置{if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + x) + (y+1)) == WEIZHI){*(*(arr_map + x) + (y+1)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + x) + (y+1)) == KONG){*(*(arr_map + x) + (y+1)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//→else if(key == 'w' || key == 'W')//↑{if (x-1 > 0){if(*(*(arr_map + (x)) +(y)) == RW)//人在最终位置{if(*( * (arr_map + (x-1) ) + (y)) == WEIZHI)//人上边还是最终位置{*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + (x) ) + y) = WEIZHI;}else if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW){if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + (x) ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + (x-1) ) + (y)) == WEIZHI)//人上边是最终位置{*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW)//人上边是箱子或在最终位置的箱子{if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + (x-1) ) + (y)) == XW || *( * (arr_map + (x-1) ) + (y)) == XIANGZI)//人上边有箱子或箱子在最终位置{if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW){if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + (x-1)) + (y)) == WEIZHI){*(*(arr_map + (x-1)) + (y)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*(*(arr_map + (x-1)) + (y)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//↑else if(key == 's' || key == 'S')//↓{if (x < _x+1 > 0){if(*(*(arr_map + (x)) +(y)) == RW)//人在最终位置{if(*( * (arr_map + (x+1) ) + (y)) == WEIZHI)//人下边还是最终位置{*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + (x) ) + y) = WEIZHI;}else if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + (x) ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + (x+1) ) + (y)) == WEIZHI)//人下边是最终位置{*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + (x+1) ) + (y)) == XW || *( * (arr_map + (x+1) ) + (y)) == XIANGZI)//人下边有箱子或箱子在最终位置{if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + (x+1)) + (y)) == WEIZHI){*(*(arr_map + (x+1)) + (y)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*(*(arr_map + (x+1)) + (y)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//↓return;}}}}bool is_finish(int _x, int _y,int _total ,int (*arr_map)[Y])//判断是否完成推箱子(地图x,地图y,总箱子数,地图数组){int x,y,temp = 0;for (x = 0; x < _x; x++){for (y = 0; y < _y; y++){if (*(*(arr_map+x)+y) == XW){temp++;}}}if (_total == temp){return true;}return false;}。

C语言代码实现推箱子小游戏

C语言代码实现推箱子小游戏

C语⾔代码实现推箱⼦⼩游戏本⽂实例为⼤家分享了C语⾔实现推箱⼦⼩游戏的具体代码,供⼤家参考,具体内容如下本次游戏是个推箱⼦第⼀关最简单的⼩游戏有详细注释,下⾯是做出来的游戏界⾯游戏操作说明和功能说明:1、按wasd控制⼩⼈的上下左右移动。

2、按 r 重新开始游戏3、游戏开始有操作介绍4、游戏结束有胜利提⽰游戏原理分析1、游戏开始时的星星个数 = 箱⼦在星星上的个数时,游戏胜利。

2、按 r 键重新开始游戏,我们需要定义⼀个量 map_1[8][8] 来保存游戏初始时的界⾯,操作时我们将其赋值给 map[8][8] 来进⾏操作,以便重新归位,这⾥就⽤到了memcpy()函数。

3、wasd 代表上下左右操作⼈物移动,分别需要为其定义⼀个函数。

4、判断⼈物移动之前我们需要定位到⼈物的位置,这⾥我们⽤find()函数来定义。

⾸先从头⽂件开始介绍:#include<stdio.h>#include<stdlib.h> //malloc()函数#include<string.h> //memcpy()函数#include<conio.h> //getch()函数函数名称:malloc函数原型: void * malloc(unsigned size);函数功能: 分配size字节的存储区函数返回: 所分配的内存区地址,如果内存不够,返回0函数函数:memcpy函数原型:void *memcpy(void *dest, const void *src, size_t n);函数功能:从源src所指的内存地址的起始位置开始拷贝n个字节到⽬标dest所指的内存地址的起始位置中函数返回:函数返回指向dest的指针。

函数名称:getch函数原型: int getch(void);函数功能: 从控制台读取⼀个字符,但不显⽰在屏幕上函数返回: 读取的字符上图是简单的8*8的图,我们定义⼀个⼆维数组来保存整张图,⽤0,1,2,3,4 来代表游戏界⾯中的每个符号。

C语言实现推箱子Visualc++6.0

C语言实现推箱子Visualc++6.0

C语言实现推箱子Visualc++6.0#include#include#include#include#include#include#include#define qiang 1#define ren 2#define xiang 3#define jia 6#define men 8#define oxiang 9int v[13][17]={0};int fuben[13][17];int num_guan; //现在的关数int num_xiang; //箱子的个数int move_num=0; //人行走的次数int push_num=0; //人推箱子的次数int M[2]; //M[0] 是人所在位置的横坐标M[1]是人所在位置的纵坐标int door[2]; //门所在的坐标int tem=0;int cc;void play();void chushi2();void chushihua_guan1();void chushihua_guan2();void chushihua_guan3();void chushihua_guan4();void chushihua_guan5();void chushihua_guan6();void chushihua_guan7();void chushihua_guan8();void chushihua_guan9();void chushihua_guan10();void chushihua_guan11();void chushihua_guan12();void chushihua_guan13();void chushihua_guan14();void dayin();int control();int wancheng();/**********************************************************主方法******************************************************************* ******/int main(){void jiemian();void shuoming();void xuanguan();void chushi(int nn);void chushi1();int i;char c;chushi1();for(i=0;i<=999;i++){ jiemian();c=getche(); if(c=='1') play();else if(c=='2') xuanguan();else if(c=='3') shuoming();else if(c=='4') break;else system("cls");} }/*************************打印游戏主界面******************************/void jiemian(){printf("\n\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);printf(" ╭╮ ■ ■ □ □ ◆◆◆◆◆◆◆◆ \n");printf(" ││ ■ ■ □□□□ □□□□ ◆ \n");printf(" ╭─┘└╮ ■ ■■■■ □ ■ □ ■ ◆ ◆ \n");printf(" ╰─┐┌ ■ ■ ■ □ ┏━━━┓ ◆ ◆ \n");printf(" ││ ■ ■■■ □□□□┃┏━┓┃ ◆◆ \n");printf(" ╭─┘└╮ ■ ■ □ ┃┏━┓┃ ┏━━━━◆━━┓\n");printf(" ╰─┐┌╯ ■ ■■■ □□ ┃┏━┓┃ ┗━━━━◆━━┛\n");p rintf(" ╭─┘│ ■ ■ □ □ □┃┗━┛┃ ◆ \n");printf(" ╰──╯ ■■■■■ □ □ □┗━━━┛ ◆◆◆◆◆◆ \n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf(" >>1 开始游戏\n\n");printf(" >>2 选择关卡\n\n");printf(" >>3游戏帮助\n\n");printf(" >>4 退出游戏\n\n");}void chushi1(){int i,j;for(i=0;i<=12;i++)for (j=0;j<=16;j++)fuben[i][j]=0;}void chushi2(){int i,j;move_num=0;push_num=0;for(i=0;i<=12;i++)for(j=0;j<=16;j++) {v[i][j]=0;fuben[i][j]=0;}}/*************************游戏规则及说明******************************/void shuoming(){int i;char c;for(i=0;i<=999;i++){system("cls");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("\t\t\t╭───────────╮\n");printf("\t\t\t│ 游戏规则及说明│\n");printf("\t\t\t╰───────────╯\n\n ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),BACKGROUND_INTENSITY|FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE|FOREGROUND_RED|FOREGROUND_RED);printf("囧");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDL E),FOREGROUND_INTENSITY| FOREGROUND_RED |FOREGROUND_GREEN | FOREGROUND_BLUE);printf(" ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("别看了,这就是你了,还挺cool的吧,嘿嘿\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);printf(" █ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是墙,放心,不是豆腐渣工程,要不你可以去撞撞看,\n");printf(" 你会懂得......\n\n\n");printf(" ■ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是箱子,就是要你去推的那个,不要试图一下推两个箱\n");printf(" 子,更不要想推着箱子把墙撞开,没用的...不要沮丧,再免\n");printf(" 费给你一句忠告,要是把箱子推到了死角,呵呵,直接下一\n");printf(" 关吧\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAN DLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_RED);printf(" ★ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这里就是箱子们的最终归宿了,箱子要是回不到家,哼哼,\n");printf(" 你自己掂量着办吧。

推箱子(C语言小游戏代码)

推箱子(C语言小游戏代码)

#include<stdio.h> #include<stdlib.h>#include<windows.h>#include<conio.h>//打印地图void DrawMap();//设置颜色void SetColor(int nColor);//获取玩家坐标POINT GetGamerPosition();//上void Up();//下void Down();//左void Left();//右void Right();//获取空箱子个数int GetSpareBox();//地图//0:空地;3箱子的目的地4箱子// 6人7箱子与目的地的集合//1:墙壁;9人在箱子的目的地int g_map[2][10][12]={{{0,0,0,0,1,1,1,0,0,0,0,0 },{0,0,0,0,1,3,1,0,0,0,0,0 },{0,0,0,0,1,0,1,1,1,1,1,1 },{1,1,1,1,1,4,0,4,0,0,3,1 },{1,3,0,0,0,4,6,1,1,1,1,1 },{1,1,1,1,1,1,4,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,3,1,0,0,0,0 },{0,0,0,0,0,1,1,1,0,0,0,0 },},{{1,1,1,1,1,0,0,0,0,0,0,0 },{1,0,0,0,1,0,1,1,1,0,0,0 },{1,0,4,0,1,0,1,1,1,1,1,1 },{1,0,4,6,1,0,1,0,0,0,3,1 },{1,1,1,4,1,1,1,0,0,0,3,1 },{1,1,1,0,0,0,0,0,0,0,3,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,1,1,1,1,1,1 },{1,1,1,1,1,1,0,0,0,0,0,0 },}};int g_nCurrentLevel = 0;//当前管卡int main(){//设置窗口大小system("mode con cols=26 lines=11");//设置标题//SetConsoLeTitle("推箱子");char nInput = 0;//输入字符while(1){//如果箱子推完了过关if (0==GetSpareBox())g_nCurrentLevel++;//清屏system("cls");//如果没有关卡,那就恭喜你成功通关。

c语言开发推箱子小游戏

c语言开发推箱子小游戏

目录1 摘要.............................................................. .. (3)1.1设计题目............................................. (3)1.2设计内容.............................................. .. (3)1.3开发工具........................................................ . (3)1.4应用平台....................................................... .. (3)2 详细设计 (3)2.1程序结构...................................................... (3)2.2主要功能..................................................... (4)2.3函数实现..................................................... .. (4)2.4开发日志.................................................... (5)3 程序调试及运行 (5)3.1程序运行结果............................................... .. (6)3.2程序使用说明............................................. . (6)3.3程序开发总结.............................................. (7)4 附件(源程序)........................................... . (7)1 摘要1.1 设计题目推箱子游戏1.2 设计内容推箱子是一款经典的益智小游戏,目的是在训练人的逻辑思考能力。

推箱子c语言程序代码

推箱子c语言程序代码

推箱子c语言程序代码推箱子游戏是一款益智类游戏,在游戏中需要利用角色推动箱子到指定位置,并通过通关来获得胜利。

在这里,我将为您介绍一个简单的推箱子的C语言程序代码。

首先,我们需要定义一些变量和常量来描述游戏中的元素和状态:```c#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <windows.h>#define ROW 10 //地图行数#define COL 15 //地图列数//定义角色、箱子和目标的符号#define PLAYER 'P'#define BOX '$'#define TARGET '*'#define WALL '#'#define SPACE ' '接下来,我们需要定义一个结构体来表示地图上的每一个位置,包含如下元素:- `type`:表示该位置的类型,可以是空格、墙、箱子、目标或者角色。

- `hasBox`: 表示该位置上是否有箱子,是一个布尔值。

```cstruct Position {char type;bool hasBox;};```- `map`:表示地图,是一个二维数组,每个元素都是一个 `Position` 结构体。

- `playerRow` 和 `playerCol`:表示角色当前的位置。

- `boxRow` 和 `boxCol`:表示当前推动的箱子的位置。

```cint playerRow, playerCol, boxRow, boxCol;Position map[ROW][COL];```接下来,我们需要编写地图的初始化函数。

在这个函数中,我们先将地图上所有的位置都标记为空格,并将所有的箱子标记为 `false`。

然后,我们可以将墙、箱子、目标和角色放置到正确的位置上。

c语言推箱子代码

c语言推箱子代码

#i n c l u d e<s t d i o.h> #include <stdlib.h>void map1();void map2();void map3();void show();void move();void main(int argc,char*argv[]){time_t a,b;time(&a);system("color 1E");loop1: {system("cls");//刷屏printf("\n\n\n\n\n\n\t\t\t\t欢迎进入推箱子游戏\n\n\t\t\t\t 请按a b c选择游戏\n\n\t\t\t\t 按 d 键结束");int t=getch();if(t=='d'){printf("\n\t\t\t "); }else{if(t=='a'){map1();goto loop1;}if(t=='b'){map2();goto loop1;}if(t=='c'){map3();goto loop1;}else{printf("\n\n\t\t\t\t 请重新输入:");goto loop1;}}}time(&b);printf("\n\n\n\n\t\t\t\t 游戏总共用时%.3d秒\n\t\t\t ",b-a);getch();//等待读取回车符以结束程序}void map1(){time_t p,q,o;time(&p);int i=4,j=4,m=6,n=2,t=1,k=3; system("cls");//刷屏system("color 2E");int x=1,y=7;char z,a[9][9]={ /*为背景设置初始资料*/ {'#','#','#','#','#','#','#','#','#'},{'#','#','#', 3, 0 , 0 , 0 , 1 ,'#'},{'#','#', 0 , 0 , 0 ,'#', 0 ,'#','#'},{'#', 0 , 0 ,'#', 0 , 0 , 5 , 0 ,'#'},{'#', 0 , 5 , 5 , 3 , 0 , 0 , 0 ,'#'},{'#','#', 0 ,'#', 0 , 0 ,'#','#','#'},{'#','#', 3 , 0 , 0 , 0 ,'#','#','#'},{'#','#','#','#','#','#','#','#','#'},{'#','#','#','#','#','#','#','#','#'}};show(a);//调用输出函数move(a,&x,&y,i,j,m,n,t,k,&z);if(a[i][j]=='!'&&a[m][n]=='!'&&a[t][k]=='!'){system("cls");//刷屏show(a);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13)//判断是否为回车符{return;}else{goto loop1;}}system("cls");//刷屏show(a);//调用输出函数goto loop;}}void map2(){time_t p,q,o;time(&p);int i=2,j=1,m=1,n=3,t=4,k=6;system("cls");//刷屏system("color 2E");int x=4,y=1;char z,b[9][9]={ /*为背景设置初始资料*/ {'#','#','#','#','#','#','#','#','#'},{'#','#','#', 3 , 0 ,'#','#','#','#'},{'#', 3 ,'#', 0 , 0 , 0 , 0 ,'#','#'},{'#', 0 , 5 , 0 ,'#','#', 0 , 0 ,'#'},{'#', 1 , 0 , 5 , 0 , 0 , 3 , 0 ,'#'},{'#', 0 , 0 , 0 , 0 , 0 , 0 ,'#','#'},{'#','#','#', 0 , 0 , 5 ,'#','#','#'},{'#','#','#','#', 0 , 0 ,'#','#','#'},{'#','#','#','#','#','#','#','#','#'}};show(b);//调用输出函数move(b,&x,&y,i,j,m,n,t,k,&z);if(b[i][j]=='!'&&b[m][n]=='!'&&b[t][k]=='!'){system("cls");//刷屏show(b);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13)//判断是否为回车符{return;}else{goto loop1;}}system("cls");//刷屏show(b);//调用输出函数goto loop;}}void map3(){time_t p,q,o;time(&p);int i=2,j=1,m=6,n=2,t=7,k=6;system("cls");//刷屏system("color 2E");int x=3,y=6;char z,c[9][9]={ /*为背景设置初始资料*/ {'#','#','#','#','#','#','#','#','#'},{'#','#','#','#','#', 0 , 0 , 0 ,'#'},{'#', 3 ,'#', 0 , 0 , 0 , 0 , 0 ,'#'},{'#', 0 , 5 , 0 , 5 ,'#', 1 ,'#','#'},{'#', 0 , 0 , 0 , 0 , 0 , 5 , 0 ,'#'},{'#','#', 0 , 0 , 0 , 0 , 0 , 0 ,'#'},{'#','#', 3 , 0 ,'#', 0 ,'#', 0 ,'#'},{'#','#','#', 0 , 0 , 0 , 3 ,'#','#'},{'#','#','#','#','#','#','#','#','#'} };show(c);//调用输出函数move(c,&x,&y,i,j,m,n,t,k,&z);if(c[i][j]=='!'&&c[m][n]=='!'&&c[t][k]=='!'){system("cls");//刷屏show(c);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13)//判断是否为回车符{return;}else{goto loop1;}}system("cls");//刷屏show(c);//调用输出函数goto loop;}}void show(char b[9][9]){printf("\t\t\t\t***************************\n");printf("\t\t\t\t 推箱子游戏");printf("\n\t\t\t\t***************************\n");printf("\t\t\t\t 人:★墙:■\n\t\t\t\t 箱子:◎目的地:□\n\t\t\t\t 按d 键退出游戏\n\t\t\t\t 限时30秒!");printf("\n\t\t\t\t***************************\n");int i;int k;for(i=0;i<9;i++) //使用循环将地图转换输出{printf("\t\t\t\t ");for(k=0;k<9;k++){if(b[i][k]=='#'){printf("■");}else{if(b[i][k]==3){printf("□");}else{if(b[i][k]==5){printf("◎");}else{if(b[i][k]==1){printf("★");}else{if(b[i][k]==0){printf(" ");}else{if(b[i][k]=='!'){printf("⊙");}elseprintf("%c ",b[i][k]);}}}}}}printf("\n");}printf("\t\t\t\t***************************\n");}void move(char c[9][9],int *x,int *y,int X,int Y,int A,int B,int N,int M,char *t){*t=getch();//用getch()得到上下左右键,要调用二次getch(),上键72,下键80,左键75,右键77.if(*t=='d'){}//遇到d返回if(*t==75)//左键{if(c[*x][*y-1]=='#'||c[*x][*y-1]==5&&c[*x][*y-2]==5||c[*x][*y-1]==5&&c[*x][*y-2]=='!'){}//遇到墙或推两个箱子else{if(c[*x][*y-1]=='!')//把箱子推出目的地{if(c[*x][*y-2]!=0){}else{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}else{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]=='#'){}//推不动箱子else{if(c[*x][*y-1]==c[X][Y]||c[*x][*y-1]==c[A][B]||c[*x][*y-1]==c[N][M])//进到目的地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*y-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x][*y-1]='!';}}if(*t==77)//右键{if(c[*x][*y+1]=='#'||c[*x][*y+1]==5&&c[*x][*y+2]==5||c[*x][*y+1]==5&&c[*x][*y+2]=='!'){}//遇到墙或推两个箱子else{if(c[*x][*y+1]=='!')//把箱子推出目的地{if(c[*x][*y+2]!=0){}else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{if(c[*x][*y+1]==5&&c[*x][*y+2]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{if(c[*x][*y+1]==5&&c[*x][*y+2]=='#'){}//推不动箱子else{if(c[*x][*y+1]==c[X][Y]||c[*x][*y+1]==c[A][B]||c[*x][*y+1]==c[N][M])//进到目的地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*y+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x][*y+1]='!';}}if(*t==72)//上键{if(c[*x-1][*y]=='#'||c[*x-1][*y]==5&&c[*x-2][*y]==5||c[*x-1][*y]==5&&c[*x-2][*y]=='!'){}//遇到墙或推两个箱子else{if(c[*x-1][*y]=='!')//把箱子推出目的地{if(c[*x-2][*y]!=0){}else{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}}else{if(c[*x-1][*y]==5&&c[*x-2][*y]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}else{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}}else{if(c[*x-1][*y]==5&&c[*x-2][*y]=='#'){}//推不动箱子else{if(c[*x-1][*y]==c[X][Y]||c[*x-1][*y]==c[A][B]||c[*x-1][*y]==c[N][M])//进到目的地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*x-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x-1][*y]='!';}}if(*t==80)//下键{if(c[*x+1][*y]=='#'||c[*x+1][*y]==5&&c[*x+2][*y]==5||c[*x+1][*y]==5&&c[*x+2][*y]=='!'){}//遇到墙或推两个箱子else{if(c[*x+1][*y]=='!')//把箱子推出目的地{if(c[*x+2][*y]!=0){}else{c[*x][*y]=0;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}}else{if(c[*x+1][*y]==5&&c[*x+2][*y]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //推着箱子离开目的地{c[*x][*y]=3;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}else{c[*x][*y]=0;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}}else{if(c[*x+1][*y]==5&&c[*x+2][*y]=='#'){}//推不动箱子else{if(c[*x+1][*y]==c[X][Y]||c[*x+1][*y]==c[A][B]||c[*x+1][*y]==c[N][M])//进到目的地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]) //离开目的地{c[*x][*y]=3;*x+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x+1][*y]='!';}}}。

推箱子游戏C语言程序

推箱子游戏C语言程序

#include"stdio.h"#include"bios.h"#define LEFT 75#define RIGHT 77#define UPPER 72#define DOWN 80#define ESC 27struct Boxss /*定义箱子结构体,其中包含坐标属性*/{int x,y;};union keyboard /*定义读取键盘码的共用体类型*/{unsigned int iKeyInfo;char chKeyBit[2];};int fnGetKey(void) /*定义读取键盘码的函数*/{union keyboard uniKey1; /*定义读取键盘码的共用体变量*/while(bioskey(1)==0); /*检测用户是否按键*/uniKey1.iKeyInfo=bioskey(0); /*读取按键信息*/return(uniKey1.chKeyBit[0]==0?uniKey1.chKeyBit[1]:uniKey1.chKeyBit[0]); /*返回ASCII码或扩充码*/}void main(){int iKey,x=11,y=6,tx=11,ty=6; /*x,y为人物移动后坐标,tx,ty为人物移动前坐标*/struct Boxss Box[4]; /*定义箱子数量*/int chMap[10][10]={ /*用二维数组定义地图*/{0,0,0,0,0,0,0,0,0,0}, /*0表示墙1表示路2表示目标*/{0,1,0,0,0,0,1,1,1,0},{0,1,0,2,0,0,1,0,1,0},{0,1,0,1,0,0,1,0,1,0},{0,1,1,1,0,0,1,0,1,0},{0,1,0,0,0,0,1,0,1,0},{0,1,1,1,1,1,1,0,1,0},{0,1,0,1,0,0,0,0,2,0},{0,2,0,1,1,1,1,2,0,0},{0,0,0,0,0,0,0,0,0,0},};int i,j;Box[0].x=13; /*定义箱子的坐标属性*/Box[1].x=11;Box[2].x=14;Box[3].x=18;Box[0].y=8;Box[1].y=7;Box[2].y=13;Box[3].y=7;while(1) /*反复进行求移动的坐标运算*/{for(i=0;i<10;i++) /*输出新地图(刷新地图)*/{gotoxy(10,5+i);for(j=0;j<10;j++){if(chMap[i][j]==0)printf("#");if(chMap[i][j]==1)printf(" ");if(chMap[i][j]==2)printf("X");}}j=0; /*判断是否所有箱子都在目标坐标上*/for(i=0;i<4;i++)if(chMap[Box[i].y-5][Box[i].x-10]==2)j++;if(j==4) /*如果所有箱子都就位输出"YOU WIN!"退出*/{clrscr();printf("You Win!");break;}for(i=0;i<4;i++) /*在起始(或移动后)的坐标输出箱子*/{gotoxy(Box[i].x,Box[i].y);printf("0");}gotoxy(x,y); /*在起始(或移动后)的坐标输出人*/printf("*\b");tx=x; /*记录本次移动前的坐标*/ty=y;iKey=fnGetKey();if(iKey==LEFT&&chMap[y-5][x-1-10]!=0) /*按读取的按键信息改变坐标如果改变的坐标和墙(0)重合则不改变*/x--;if(iKey==RIGHT&&chMap[y-5][x+1-10]!=0)x++;if(iKey==UPPER&&chMap[y-1-5][x-10]!=0)y--;if(iKey==DOWN&&chMap[y+1-5][x-10]!=0)y++; /*输入ESC退出并输出"YOU LOST"*/if(iKey==ESC){clrscr();printf("You Lost");break;}for(i=0;i<4;i++) /*如果移动后的人的坐标与箱子坐标重合,则改变箱子坐标向前一格*/if(Box[i].x==x&&Box[i].y==y){Box[i].x+=(x-tx);Box[i].y+=(y-ty);if(chMap[Box[i].y-5][Box[i].x-10]==0) /*如果移动后的箱子坐标会出现在墙上,则使箱子坐标和人坐标都返回移动前的值*/{Box[i].x-=(x-tx);Box[i].y-=(y-ty);x=tx;y=ty;}break;}clrscr();}getch();}。

推箱子游戏C程序

推箱子游戏C程序

4 推箱子游戏【游戏规则】 这是个模拟推箱子拾金币的游戏,进入每一个关卡后,你会发现有一些金币摆放在不同的位置,还有一些相同数量的箱子摆放在不同的位置,你可以通过键盘上的方向键,控制关卡中的搬运工人移动到不同位置上,并推动其前方的箱子移动,只要将箱子推倒金币所在位置上,就相当于将箱子前方金币装入了箱子内,将关卡内的全部金币都装入箱子内,就算是过了一关,可以进入下一关。

注意,搬运工人推箱子只能在位于移动方向上箱子的后面才能推动箱子,而且关卡中的墙体可能会对你顺利地推动箱子造成障碍,如果你将箱子推倒了一个墙角,那么你可能就再也无法移动箱子了,所以你要像下棋一样全盘考虑,才能赢。

每个关卡都是由15行15列的数据组成,如图3-12所示,对应的关卡图形如图3-13所示。

图3-12 第10关的关卡数据 图3-13 图3-12中关卡数据对应的关卡图形000000000000000000000000000000000000000000000000000000000000000222222222000000232232232220002204004004020022040040040020021403403403020022232232232220000222222222000000000000000000000000000000000000000000000000000000000000000C语言大学实用教程学习指导·268·#include <stdio.h>#include <graphics.h>#include <stdlib.h>#include <dos.h>#define TRUE 1#define FALSE 0#define DOWN 80#define LEFT 75#define RIGHT 77#define UP 72#define ENTER 28#define ESC 1#define Q 16#define N 49#define R 19#define B 48#define BKCOLOR 0#define SIZE 15#define MX 5#define MY 5#define MAX_LEVEL 12#define MAX_COINS 14void *MenuDisplay();void SubMenuDisplay();int GetRow(int row, void *lightbar);int GetKey();struct COIN{int x;int y;第3章学习指导·269·}coin[MAX_COINS]; /*记录金币的x,y坐标*/int data[SIZE][SIZE]; /*记录关卡数据*/int x, y; /*搬运工人的当前坐标位置*/main(){int driver = VGA;int mode = VGAHI;int act = 0;int row = 0;int gameOver = FALSE;int gameDone = FALSE;int workerFace = 1;int levelNum = 1;int coinNum = 0;void *lightbar;initgraph(&driver, &mode, "");MENU:levelNum = 1;lightbar = MenuDisplay();do{row = GetRow(row, lightbar);if (row == 0) /*游戏开始*/{cleardevice();setbkcolor(BKCOLOR);coinNum = ReadFile(data, levelNum, coin);do{act = GetKey();switch (act){C语言大学实用教程学习指导·270·case DOWN: /*向下移动*/workerFace = 1;gameDone=MoveBox(x,y+1,x,y+2,workerFace, coinNum);if (gameDone){gameDone = FALSE;coinNum = NextLevel(&levelNum);}break;case UP: /*向上移动*/workerFace = 2;gameDone=MoveBox(x,y-1,x,y-2,workerFace,coinNum);if (gameDone){gameDone = FALSE;coinNum = NextLevel(&levelNum);}break;case LEFT: /*向左移动*/workerFace = 3;gameDone=MoveBox(x-1,y,x-2,y,workerFace,coinNum);if (gameDone){gameDone = FALSE;coinNum = NextLevel(&levelNum);}break;case RIGHT: /*向右移动*/workerFace = 4;gameDone=MoveBox(x+1,y,x+2,y,workerFace, coinNum);if (gameDone){gameDone = FALSE;第3章学习指导·271·coinNum = NextLevel(&levelNum);}break;case ESC: /*返回主菜单*/cleardevice();goto MENU;case R: /*恢复当前关卡的初始状态*/cleardevice();coinNum = ReadFile(data, levelNum, coin);break;case N: /*循环递增关卡序号*/if (levelNum == MAX_LEVEL) levelNum = 1;else levelNum++;cleardevice();coinNum = ReadFile(data, levelNum, coin);break;case B: /*循环递减关卡序号*/if (levelNum == 1) levelNum = MAX_LEVEL;else levelNum--;cleardevice();coinNum = ReadFile(data, levelNum, coin);break;case Q: /*直接退出游戏*/gameOver = TRUE;closegraph();break;default: break;}}while(!gameOver);break;}else /*退出程序*/{C语言大学实用教程学习指导·272·gameOver = TRUE;closegraph();}}while(!gameOver);free(lightbar);}int GetKey(){union REGS rg;rg.h.ah = 0;int86(0x16, &rg, &rg);return rg.h.ah;}int GetRow(int row, void *lightbar){int key, done = FALSE;do{key = GetKey();switch (key){case DOWN: /*向下移动亮条*/putimage(232, 341+row*30, lightbar, XOR_PUT);row = row==1 ? 0 :1;putimage(232, 341+row*30, lightbar, XOR_PUT);break;case UP: /*向上移动亮条*/putimage(232, 341+row*30, lightbar, XOR_PUT);row = row==0 ? 1 :0;putimage(232, 341+row*30, lightbar, XOR_PUT);break;第3章学习指导·273·case ENTER: /*选中当前两条指定的操作*/done = TRUE;break;}}while (!done);return row; /*返回当前两条指定的操作序号*/}/*显示游戏开始前的主菜单*/void *MenuDisplay(){unsigned int size;void *lightbar;int menuNum = 2;int workerFace = 1;int i, k;setlinestyle(0, 0, 3);setcolor(WHITE);rectangle(2, 2, 638, 478);/*显示标题*/setcolor(15);rectangle(120, 60, 500, 140);setlinestyle(0, 0, 1);settextstyle(4, HORIZ_DIR, 7);settextjustify(1, 2);setcolor(15);outtextxy(310,60, "Push box");for (i=11; i<20; i++) DrawWall(i*20, 180);for (i=11; i<20; i++) DrawWall(i*20, 280);for (i=10; i<14; i++) DrawWall(200, i*20);for (i=10; i<14; i++) DrawWall(400, i*20);C语言大学实用教程学习指导·274·DrawWorker(300, 210, workerFace);for (i=12; i<19; i++) DrawBox(i*20, 240);for (i=13; i<18; i++) DrawBox(i*20, 260);/*显示菜单*/setcolor(LIGHTBLUE);setfillstyle(1,YELLOW);for (k=0; k<menuNum; k++){rectangle(230, 340+30*k, 390, 370+30*k);bar(231, 341+30*k, 389, 369+30*k);}settextstyle(1, HORIZ_DIR, 2);settextjustify(1, 2);setcolor(LIGHTRED);outtextxy(310, 342, " New Game");outtextxy(310, 372, "Exit");/*设置显示亮条*/size = imagesize(0,0,158,28);if (size != 1) lightbar = malloc(size);setfillstyle(1, 12);bar(4, 4, 162, 32);getimage(4, 4, 162, 32, lightbar);setfillstyle(1, BLACK);bar(4, 4, 162, 32);/*将显示亮条放到NewGame菜单上*/putimage(232,341,lightbar,XOR_PUT);return lightbar;第3章学习指导·275·}/*显示游戏开始后屏幕右侧的子菜单*/void SubMenuDisplay(){setlinestyle(0, 0, 3);setcolor(15);rectangle(2, 2, 638, 478);setcolor(7);rectangle(460, 10, 630, 470);setfillstyle(1, 6);bar(462, 12, 628, 468);settextstyle(4, HORIZ_DIR, 4);setcolor(15);outtextxy(545, 50, "Good Luck");settextstyle(1, HORIZ_DIR, 2);setcolor(14);outtextxy(490, 120, "ESC:");outtextxy(490, 180, "R :");outtextxy(490, 240, "N :");outtextxy(490, 300, "B :");outtextxy(490, 360, "Q :");settextstyle(0, HORIZ_DIR, 1);setcolor(11);outtextxy(545, 155, " return to MENU");outtextxy(545, 215, " Reload level ");outtextxy(545, 275, " Next level");outtextxy(545, 335, " Past level");outtextxy(545, 395, " Quit directly");}C语言大学实用教程学习指导·276·/*从文件level.txt中读取关卡数据*/int ReadFile(int data[SIZE][SIZE],int levelNum,struct COIN coin[MAX_COINS]){int i, j;int workerFace = 1;int coinNum = 0;FILE *fl = NULL;char test;for (i=0; i<MAX_COINS; i++){coin[i].x = 0;coin[i].y = 0;}if ((fl = fopen("level.txt", "rt")) == NULL){settextstyle(1,HORIZ_DIR,2);setcolor(RED);outtextxy(300,250,"Make sure a level.txt in the same dir.");return 0;}SubMenuDisplay();for (i=1; i<=SIZE*(levelNum-1); i++){do{test = fgetc(fl);}while (test != '\n');}for (i=0; i<SIZE; i++)for (j=0; j<SIZE; j++){test = fgetc(fl);if (test == '\n'){j--;continue;}data[i][j] = test - 48;switch(data[i][j]){case 1: /*在y行x列上绘制搬运工人,并记录其当前位置(x,y)*/x = j;y = i;DrawWorker((j+MX)*20, (i+MY)*20, workerFace);break;case 2: /*绘制墙体*/DrawWall((j+MX)*20, (i+MY)*20);break;/*绘制金币,金币枚数记录于coinNum,金币的位置记录于数组coin*/ case 3:coin[coinNum].x = j;coin[coinNum].y = i;coinNum++;DrawCoins((j+MX)*20, (i+MY)*20);break;case 4: /*绘制箱子*/DrawBox((j+MX)*20, (i+MY)*20);break;}}fclose(fl);return coinNum;}/*绘制墙体*/int DrawWall(int xd, int yd){char fillpattern[40] = {0xfb,0xfb,0x00,0xbf,0xbf,0xbf,0x00,0xfb};setfillstyle(1, BKCOLOR);bar(xd, yd, xd+19, yd+19);setfillpattern(fillpattern, 7);bar(xd, yd, xd+19, yd+19);}/*绘制黄色与浅灰色相间的箱子,表示未装金币、仍可搬动的箱子*/ int DrawBox(int xd,int yd){char fillpattern[40] = {0x55,0x10,0x55,0x10,0x55,0x10,0x55,0x10};setfillstyle(1, BKCOLOR);bar(xd, yd, xd+19, yd+19);setlinestyle(0, 0, 1);setcolor(8);rectangle(xd, yd, xd+19, yd+19);setfillstyle(1, YELLOW);bar(xd+1, yd+1, xd+18, yd+18);rectangle(xd+4, yd+4, xd+15, yd+15);setfillstyle(1, 8);bar(xd+5, yd+5, xd+14, yd+14);setlinestyle(0, 0, 1);setfillpattern(fillpattern, YELLOW);pieslice(xd+10, yd+9, 0, 90, 4);pieslice(xd+9, yd+9, 90, 180, 4);pieslice(xd+9, yd+10, 180, 270, 4);pieslice(xd+10, yd+10, 270, 360, 4);setlinestyle(0, 0, 3);setcolor(YELLOW);line(xd+5, yd+5, xd+14, yd+14);line(xd+5, yd+14, xd+14, yd+5);}/*绘制浅红色与浅灰色相间的箱子,表示已装金币、不能再搬动的箱子*/ int DrawHBox(int xd, int yd){char fillpattern[40] = {0x55,0x10,0x55,0x10,0x55,0x10,0x55,0x10};setfillstyle(1, BKCOLOR);bar(xd, yd, xd+19, yd+19);setlinestyle(0, 0, 1);setcolor(8);rectangle(xd, yd, xd+19, yd+19);setfillstyle(1, 12);bar(xd+1, yd+1, xd+18, yd+18);rectangle(xd+4, yd+4, xd+15, yd+15);setfillstyle(1, 8);bar(xd+5, yd+5, xd+14, yd+14);setlinestyle(0, 0, 1);setfillpattern(fillpattern, 12);pieslice(xd+10, yd+9, 0, 90, 4);pieslice(xd+9, yd+9, 90, 180, 4);pieslice(xd+9, yd+10, 180, 270, 4);pieslice(xd+10, yd+10, 270, 360, 4);setlinestyle(0, 0, 3);setcolor(12);line(xd+5, yd+5, xd+14, yd+14);line(xd+5, yd+14, xd+14, yd+5);}/*绘制圆形的金币*/int DrawCoins(int xd, int yd){setlinestyle(0, 0, 1);setfillstyle(1, BKCOLOR);bar(xd, yd, xd+19, yd+19);setcolor(LIGHTRED);setfillstyle(1, RED);pieslice(xd+10, yd+10, 0, 180, 5);pieslice(xd+10, yd+10, 180, 360, 5);}/*绘制搬运工人*/int DrawWorker(int xd, int yd, int workerFace){setfillstyle(1, BKCOLOR);bar(xd, yd, xd+19, yd+19);setcolor(11);setlinestyle(0, 0, 1);circle(xd+10, yd+10, 9);setcolor(10);setfillstyle(1, 10);pieslice(xd+10, yd+10, 0, 360, 8);setfillstyle(1, BLACK);setcolor(BLACK);switch (workerFace){case 1: /*向下移动时是其正面的面孔*/ pieslice(xd+7, yd+8, 0, 360, 2);pieslice(xd+13, yd+8, 0, 360, 2);arc(xd+10, yd+9, 220, 320, 5);break;case 2: /*向上移动时是其背面的面孔*/break;case 3: /*向左移动时是其左侧面的面孔*/pieslice(xd+7, yd+8, 0, 360, 2);arc(xd+2, yd+9, 260, 320, 5);break;case 4: /*向右移动时是其右侧面的面孔*/pieslice(xd+13, yd+8, 0, 360, 2);arc(xd+17, yd+9, 220, 280, 5);break;default:break;}}/*清除移动位置上原来显示的物体*/int DrawClear(int xd,int yd){setfillstyle(1, BKCOLOR);bar(xd, yd, xd+19, yd+19);}/*移动箱子*/int MoveBox(int x1, int y1, int x2, int y2, int workerFace, int coinNum){int i;int gameDone = FALSE;int pickFlag = 0;int count = 0;/*若工人位置与某个金币所在位置重合,即经过金币,则置标志变量pickFlag为真*/ for (i=0; i<coinNum; i++){if ((x == coin[i].x) && (y == coin[i].y))pickFlag = 1;}switch (data[y1][x1]){case 0: /*y1行x1列是背景*/case 3: /*y1行x1列是金币*//*与金币重合后离开,在原(x,y)位置重画金币,并恢复其关卡数据为金币*/if (pickFlag){DrawCoins((x+MX)*20, (y+MY)*20);data[y][x] = 3;}else /*与金币重合,清除原工人位置(x,y)为背景,设置其关卡数据为背景*/{DrawClear((x+MX)*20, (y+MY)*20);data[y][x] = 0;}/*在新移动位置(x1,y1)画工人,设置其关卡数据为工人*/DrawWorker((x1+MX)*20, (y1+MY)*20, workerFace);data[y1][x1] = 1;x = x1; /*记录工人新移动的位置的列坐标*/y = y1; /*记录工人新移动的位置的行坐标*/break;case 4: /*y1行x1列是箱子*/switch(data[y2][x2]){case 0: /*移动方向上箱子的前方位置(x2,y2)是背景*//*若工人曾在金币所在位置推箱子,则离开前要重绘金币*//*否则清除工人原所在位置为背景*/if (pickFlag){DrawCoins((x+MX)*20, (y+MY)*20);data[y][x] = 3;}else{DrawClear((x+MX)*20, (y+MY)*20);data[y][x] = 0;}/*在箱子移动的新位置(x2,y2)上绘制箱子*/DrawBox((x2+MX)*20, (y2+MY)*20);data[y2][x2] = 4;/*在箱子原所在位置(x1,y1)上绘制工人*/DrawWorker((x1+MX)*20,(y1+MY)*20,workerFace);data[y1][x1] = 1;x = x1; /*记录工人新移动的位置的列坐标*/y = y1; /*记录工人新移动的位置的行坐标*/break;case 3: /*移动方向箱子的前方位置(x2,y2)是金币*//*若工人曾在金币所在位置推箱子,则离开前要重绘金币*//*否则清除工人原所在位置为背景*/if (pickFlag){DrawCoins((x+MX)*20, (y+MY)*20);data[y][x] = 3;}else{DrawClear((x+MX)*20, (y+MY)*20);data[y][x] = 0;}/*在原金币位置(x2,y2)画已装金币的箱子,设置其关卡数据为箱子*/DrawHBox((x2+MX)*20, (y2+MY)*20);data[y2][x2] = 4;/*在原箱子所在位置(x1,y1)画工人,设置其关卡数据为工人*/DrawWorker((x1+MX)*20,(y1+MY)*20, workerFace);data[y1][x1] = 1;x = x1; /*记录工人新移动的位置的列坐标*/y = y1; /*记录工人新移动的位置的行坐标*//*金币位置与箱子位置重合时计数器加1*/for (i=0; i<coinNum; i++){if (data[coin[i].y][coin[i].x] == 4)count++;}/*若所有金币都被装入箱子,则游戏结束*/if (coinNum == count)gameDone = TRUE;break;default:break;}break;default:break;}return gameDone;}/*读取下一个关卡数据*/int NextLevel(int *levelNum){int coinNum ;。

C语言推箱子程序

C语言推箱子程序
m[x+a][y+b]=8;
m[x-a][y-b]=0;}
else if(m[x][y]==3) //人在成功点
{
x+=a;
y+=b;
m[x][y]=3;
m[x+a][y+b]=8;
m[x-a][y-b]=4;
}
}
else if(m[x+a][y+b]==5&&m[x+2*a][y+2*b]==4) //人物前面是成功箱子箱子前面是成功点
printf("⊙");
else if(m[i][j]==4)
printf("□");
else if(m[i][j]==5)
{
count++; //每扫描到一个成功值计数加1
printf("★");}
}
printf("\n");
}
return count;
}
void key() //按键操作
{
char z;
{
x+=a;
y+=b;
m[x][y]=3;
m[x+a][y+b]=5;
m[x-a][y-b]=0;
}
}
void game()
{
m[2][1]=2; //初始化人物模型点
for(;;)
{
prnmap(); //地图打印
key(); //操作指令
move(); //移动判断函数
if(prnmap()==4) //通关条件
{x+=a;
y+=b;

CC++编写推箱子小游戏

CC++编写推箱子小游戏

CC++编写推箱⼦⼩游戏本⽂实例为⼤家分享了C/C++编写推箱⼦⼩游戏的具体代码,供⼤家参考,具体内容如下我们⽤' #'来代表墙,‘O'来代表箱⼦,' * '代表终点,‘@'代表箱⼦已经到达终点,‘S来表⽰⼈'。

注意:W,A,S,D为⽅向键,⽽且要在英⽂格式下运⾏⽰例:以下为完整代码:/*推箱⼦(⽰例⼆)*/#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <conio.h>int main(){char a[12][12]={"##########","## ###","##O### #","# S O O #","# **# O ##","##**# ##","##########",};int x=3,y=2;int i;char ch;for(i=0;i<7;i++)puts(a[i]);while(a[4][2]!='@'||a[4][3]!='@'||a[5][2]!='@'||a[5][3]!='@'){ch=getch();if(ch=='s'){if(a[x+1][y]!='#'&&a[x+1][y]!='O'&&a[x+1][y]!='@') //如果只有⼈,没有箱⼦{a[x][y]=' ';x++;a[x][y]='S';}else if(a[x+2][y]!='#'&&a[x+1][y]=='O'&&a[x+2][y]!='*') //如果是箱⼦和⼈⼀起⾛{a[x][y]=' ';x++;a[x][y]='S';a[x+1][y]='O';}else if(a[x+2][y]!='#'&&(a[x+1][y]=='O'||a[x+1][y]=='@')&&a[x+2][y]=='*') //如果⼈和箱⼦⼀起⾛,并且前⾯是终点 {a[x][y]=' ';x++;a[x][y]='S';a[x+1][y]='@';}}if(ch=='w'){if(a[x-1][y]!='#'&&a[x-1][y]!='O'&&a[x-1][y]!='@') //如果只有⼈,没有箱⼦{a[x][y]=' ';x--;a[x][y]='S';}else if(a[x-2][y]!='#'&&a[x-1][y]=='O'&&a[x-2][y]!='O') //如果是箱⼦和⼈⼀起⾛{a[x][y]=' ';x--;a[x][y]='S';a[x-1][y]='O';}else if(a[x-2][y]!='#'&&(a[x-1][y]=='O'||a[x-1][y]=='@')&&a[x-2][y]=='*') //如果⼈和箱⼦⼀起⾛,并且前⾯是终点{a[x][y]=' ';x--;a[x][y]='S';a[x-1][y]='@';}}if(ch=='a'){if(a[x][y-1]!='#'&&a[x][y-1]!='O'&&a[x][y-1]!='@') //如果只有⼈,没有箱⼦{a[x][y]=' ';y--;a[x][y]='S';}else if(a[x][y-2]!='#'&&a[x][y-1]=='O') //如果是箱⼦和⼈⼀起⾛{a[x][y]=' ';y--;a[x][y]='S';a[x][y-1]='O';}else if(a[x][y-2]!='#'&&(a[x][y-1]=='O'||a[x][y-1]=='@')&&a[x][y-2]=='*') //如果⼈和箱⼦⼀起⾛,并且前⾯是终点{a[x][y]=' ';y--;a[x][y]='S';a[x][y-1]='@';}}if(ch=='d'){if(a[x][y+1]!='#'&&a[x][y+1]!='O'&&a[x][y+1]!='@') //如果只有⼈,没有箱⼦{a[x][y]=' ';y++;a[x][y]='S';}else if(a[x][y+2]!='#'&&a[x][y+1]=='O'&&a[x][y+2]!='O') //如果是箱⼦和⼈⼀起⾛{a[x][y]=' ';y++;a[x][y]='S';a[x][y+1]='O';}else if(a[x][y+2]!='#'&&(a[x][y+1]=='O'||a[x][y+1]=='@')&&a[x][y+2]=='*'&&a[x][y+2]!='O') //如果⼈和箱⼦⼀起⾛,并且前⾯是终点 {a[x][y]=' ';y++;a[x][y]='S';a[x][y+1]='@';}}if(a[4][2]!='@'&&a[4][2]!='S'&&a[4][2]!='*'){a[4][2]='*';}if(a[4][3]!='@'&&a[4][3]!='S'&&a[4][3]!='*'){a[4][3]='*';}if(a[5][2]!='@'&&a[5][2]!='S'&&a[5][2]!='*'){a[5][2]='*';}if(a[5][3]!='@'&&a[5][3]!='S'&&a[5][3]!='*'){a[5][3]='*';}if(a[1][2]=='O'||a[1][6]=='O'||a[2][8]=='O'||a[3][8]=='O'||a[5][5]=='O'||a[5][7]=='O') //判断失败的情况 {break;}system("cls");for(i=0;i<7;i++)puts(a[i]);}system("cls");if(a[4][2]!='@'||a[4][3]!='@'||a[5][2]!='@'||a[5][3]!='@')printf("YOU LOSE!");elseprintf("YOU WIN!");Sleep(3000);return 0;}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

推箱子程序:#include <stdio.h>#include <stdlib.h>#include <Windows.h>#define U 1#define D 2#define L 3#define R 4 //按键状态,U:上;D:下;L:左R:右#define ofx 10#define ofy 9 //x,y方向偏移量typedef struct MAP //地图{int wall[50][2]; //墙int box[9][2]; //箱子int des[9][2]; //终点int overlap[9][2];//箱子与终点的重叠位置int all,now; //总箱子个数,到位箱子个数int x,y; //起点}MAP,*map;//全局变量//int player[2]; //玩家位置int nextp[2],nextb[2]; //玩家下一步位置,箱子下一步位置MAP Pass[5]; //关卡数组map Group,p; //关卡数组指针,当前关卡指针int level; //关卡等级int status; //玩家状态int boxc[9][2],overlapc[9][2]; //箱子状态栈,重叠箱子状态栈int nowbox; //当前到位箱子个数int regame; //是否重玩//声明全部函数//void Pos(int x,int y); //设置光标位置void welcometogame(); //开始界面void initgame(); //初始化游戏void startgame(); //开始游戏void copybox(int copy[9][2],int b[9][2]); //复制箱子信息void loadmap(); //加载地图void printfoverlap(int b[9][2]); //打印重叠箱子位置void move(); //玩家移动void trans(int p[2],int q[2]); //状态传递void inputmap(int a[][2]); //输入地图数据void gamecircle(); //控制游戏int main(){system("mode con cols=80 lines=30"); //控制台宽度80,高度30welcometogame();initgame();for(level=0;level<4;level++){p=Group+level;startgame();}system("cls");Pos(32,14);printf("恭喜你已通过所有关卡!\n");system("pause");}void Pos(int x,int y) //设置光标位置{COORD pos;HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos);}void welcometogame() //开始界面{system("title C语言程序游戏之推箱子");Pos(31,13);printf("欢迎来到推箱子游戏!");Pos(55,25);printf(" 作者:小葱\n");system("pause");system("cls");Pos(29,12);printf("用↑.↓.←.→控制人物的移动。

");Pos(29,13);printf("按F2重新开始此局。

");Pos(29,14);printf("按ESC结束游戏。

\n");system("pause");system("cls");}void startgame() //开始游戏{while(1){system("cls");Pos(25,5);printf("第%d关",level+1);Pos(40,2);printf("按F2重新开始此局。

");Pos(40,3);printf("按ESC结束游戏。

");loadmap();gamecircle();if(regame==0) //regame为0不重玩break;else if(regame==1) //regame为1重玩regame=0;}Sleep(300);}void loadmap() //加载地图{int i;Pos(2*(p->wall[0][0]+ofx),p->wall[0][1]+ofy);printf("■");for(i=1;p->wall[i][0]!=0 || p->wall[i][1]!=0;i++)//打印墙{Pos(2*(p->wall[i][0]+ofx),p->wall[i][1]+ofy);printf("■");}for(i=0;p->des[i][0]!=0 || p->des[i][1]!=0;i++)//打印终点{Pos(2*(p->des[i][0]+ofx),p->des[i][1]+ofy);printf("×");}for(i=0;p->box[i][0]!=0 || p->box[i][1]!=0;i++)//打印箱子{Pos(2*(p->box[i][0]+ofx),p->box[i][1]+ofy);printf("◇");}printfoverlap(p->overlap); //打印重叠箱子位置Pos(2*(p->x+ofx),p->y+ofy); //打印玩家位置printf(":)");Pos(79,29);}void printfoverlap(int b[9][2]) //打印重叠箱子位置{int i;for(i=0;i<9;i++){if(b[i][0]==0&&b[i][1]==0)continue;Pos(2*(b[i][0]+ofx),b[i][1]+ofy);printf("□");}}void gamecircle() //控制游戏{copybox(boxc,p->box);copybox(overlapc,p->overlap);player[0]=p->x;player[1]=p->y;nowbox=p->now; //还原游戏初始状态while(1){Sleep(80);if(GetAsyncKeyState(VK_ESCAPE)){system("cls");Pos(34,14);printf("欢迎下次再玩。

");Sleep(1500);exit(0);}if(GetAsyncKeyState(VK_F2)){regame=1;break;}else if(GetAsyncKeyState(VK_UP)){status=U;}else if(GetAsyncKeyState(VK_DOWN)){status=D;}else if(GetAsyncKeyState(VK_LEFT)){status=L;}else if(GetAsyncKeyState(VK_RIGHT)){status=R;}if(nowbox==p->all) //完成此局break;move();}}void copybox(int copy[9][2],int b[9][2]) //复制箱子信息{int i;for(i=0;i<9;i++){copy[i][0]=b[i][0];copy[i][1]=b[i][1];}}void move() //玩家移动{int i,j,k,con;//i用于遍历,j保存要移动的箱子编号,k标记重叠箱子//con:0下一步有墙重合,1下一步没有墙,2下一步没有墙没有箱子//,3下一步有箱子,4箱子下一步没有墙,5箱子下一步有墙//,6箱子下一步没有墙没有箱子,7箱子下一步有其他箱子if(status!=0)//status不等于0执行移动{trans(nextp,player);//更新下一步for(i=1,con=1;p->wall[i][0]!=0||p->wall[i][1]!=0;i++)//判断玩家下一步是否与墙重合{if(p->wall[i][0]==nextp[0]&&p->wall[i][1]==nextp[1]){con=0;//重合使con为0break;}}if(con==1)//1下一步没有墙{for(i=0,con=2;boxc[i][0]!=0||boxc[i][1]!=0;i++)//判断下一步是否有箱子{if(boxc[i][0]==nextp[0]&&boxc[i][1]==nextp[1]){con=3;//下一步有箱子使con为3j=i;//保存要移动的箱子break;}}if(con==3)//3下一步有箱子{trans(nextb,nextp);//更新盒子下一步for(i=1,con=4;p->wall[i][0]!=0||p->wall[i][1]!=0;i++)//判断箱子下一步是否与墙重合{if(p->wall[i][0]==nextb[0]&&p->wall[i][1]==nextb[1]){con=5;//箱子下一步与墙重合使con为5break;}}if(con==4)//4箱子下一步没有墙{for(i=0,con=6;boxc[i][0]!=0||boxc[i][1]!=0;i++)//判断箱子下一步是否有其他箱子{if(boxc[i][0]==nextb[0]&&boxc[i][1]==nextb[1]){con=7;//箱子下一步有其他箱子使con为7break;}}if(con==6)//6箱子下一步没有墙并且也没有其他箱子{Pos(2*(nextb[0]+ofx),nextb[1]+ofy);printf("◇");Pos(2*(nextp[0]+ofx),nextp[1]+ofy);printf(":)");Pos(2*(player[0]+ofx),player[1]+ofy);printf(" ");for(i=0;p->des[i][0]!=0||p->des[i][1]!=0;i++)//当前玩家位置是否有终点标记{if(p->des[i][0]==player[0]&&p->des[i][1]==player[1]){Pos(2*(player[0]+ofx),player[1]+ofy);printf("×");break;}}for(i=0;p->des[i][0]!=0||p->des[i][1];i++)//判断要移动的箱子是否在终点上{if(p->des[i][0]==boxc[j][0]&&p->des[i][1]==boxc[j][1]){nowbox--; //当前到位的箱子数减一for(k=0;k<9;k++) //从箱子重叠表中删除离开终点的箱子{if(overlapc[k][0]==p->des[i][0]&&overlapc[k][1]==p->des[i][1]){overlapc[k][0]=0;overlapc[k][1]=0;break;}}break;}}for(i=0;p->des[i][0]!=0||p->des[i][1];i++)//判断下一个箱子是否在终点上{if(p->des[i][0]==nextb[0]&&p->des[i][1]==nextb[1]){nowbox++;//当前到位的箱子数减加一for(k=0;k<9;k++) //从箱子重叠表中加入移入终点的箱子{if(overlapc[k][0]==0&&overlapc[k][1]==0){overlapc[k][0]=p->des[i][0];overlapc[k][1]=p->des[i][1];break;}}break;}}printfoverlap(overlapc);Pos(79,29);boxc[j][0]=nextb[0];boxc[j][1]=nextb[1];player[0]=nextp[0];player[1]=nextp[1];}}}else if(con==2)//2下一步没有墙没有箱子{Pos(2*(nextp[0]+ofx),nextp[1]+ofy);printf(":)");Pos(2*(player[0]+ofx),player[1]+ofy);printf(" ");for(i=0;p->des[i][0]!=0||p->des[i][1]!=0;i++)//当前玩家位置是否有终点标记{if(p->des[i][0]==player[0]&&p->des[i][1]==player[1]){Pos(2*(player[0]+ofx),player[1]+ofy);printf("×");break;}}Pos(79,29);player[0]=nextp[0];player[1]=nextp[1];}}status=0;}}void trans(int p[2],int q[2]) //状态传递{if(status==U){p[0]=q[0];p[1]=q[1]-1;}else if(status==D){p[0]=q[0];p[1]=q[1]+1;}else if(status==L){p[0]=q[0]-1;p[1]=q[1];}else if(status==R){p[0]=q[0]+1;p[1]=q[1];}void initgame() //初始化游戏{//[0]是玩家起始位置,[1]是箱子个数和到位个数,//之后是重叠箱子位置,终点位置,箱子位置,墙位置int a0[60][2]={{4,4},{4,0},{1,4},{3,1},{4,6},{6,3},{3,3},{3,4},{4,5},{5,3}, {0,3},{0,4},{0,5},{1,3},{1,5},{2,0},{2,1},{2,2},{2,3},{2,5},{3,0},{3,5},{3,6},{3,7},{4,0},{4,1},{4,2},{4,7},{5,2},{5,4},{5,5},{5,6},{5,7},{6,2},{6,4},{7,2},{7,3},{7,4},{999,999}},a1[60][2]={{1,1},{3,0},{7,3},{7,4},{7,5},{2,2},{2,3},{3,2},{0,0},{0,1},{0,2},{0,3},{0,4},{1,0},{1,4},{1,5},{1,6},{1,7},{1,8},{2,0},{2,4},{2,5},{2,8},{3,0},{3,8},{4,0},{4,1},{4,2},{4,3},{4,4},{4,8},{5,4},{5,6},{5,7},{5,8},{6,2},{6,3},{6,4},{6,7},{7,2},{7,7},{8,2},{8,3},{8,4},{8,5},{8,6},{8,7},{999,999}},a2[60][2]={{2,3},{4,0},{2,4},{2,5},{3,4},{3,5},{2,2},{4,3},{6,4},{7,3},{0,2},{0,3},{0,4},{0,5},{1,0},{1,1},{1,2},{1,5},{1,6},{2,0},{2,6},{3,0},{3,2},{3,6},{4,0},{4,2},{4,4},{4,5},{4,6},{5,0},{5,2},{5,6},{6,0},{6,6},{7,0},{7,1},{7,6},{8,1},{8,4},{8,5},{8,6},{9,1},{9,2},{9,3},{9,4},{999,999}},a3[60][2]={{1,2},{5,1},{3,6},{1,5},{1,6},{2,6},{3,6},{4,6},{2,2},{2,3},{2,5},{3,4},{3,6},{0,1,},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{1,0},{1,1},{1,3},{1,4},{1,7},{2,0},{2,7},{3,0},{3,7},{4,0},{4,1},{4,2},{4,3},{4,7},{5,3},{5,4},{5,5},{5,6},{5,7},{999,999}};Group=Pass;p=Group+0;inputmap(a0);p=Group+1;inputmap(a1);p=Group+2;inputmap(a2);p=Group+3;inputmap(a3);}void inputmap(int a[][2]) //输入地图数据{int i,k;p->x=a[0][0];p->y=a[0][1]; //玩家坐标p->all=a[1][0];p->now=a[1][1]; //箱子状态for(k=0,i=2;i<2+p->now;i++,k++) //输入重叠箱子信息{p->overlap[k][0]=a[i][0];p->overlap[k][1]=a[i][1];for(k=0;i<2+p->now+p->all;i++,k++) //输入终点信息{p->des[k][0]=a[i][0];p->des[k][1]=a[i][1];}for(k=0;i<2+p->now+2*p->all;i++,k++) //输入箱子信息{p->box[k][0]=a[i][0];p->box[k][1]=a[i][1];}for(k=0;a[i][0]!=999;i++,k++) //输入墙的信息{p->wall[k][0]=a[i][0];p->wall[k][1]=a[i][1];}}。

相关文档
最新文档