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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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语言实现推箱子游戏完整代码

C语⾔实现推箱⼦游戏完整代码C语⾔实现推箱⼦游戏完整代码,供⼤家参考,具体内容如下前⾔⾃⼰做的,可能有些代码不够⼯整,或者有些⼩问题,但游戏的基本操作是可以实现的代码效果代码⼀共分为8个部分,4个控制上下左右移动,2个判断输赢,1个统计归为的个数,⼀个作图。

⼿动设置地图⽤'0'表⽰空格,“1”表⽰墙,“2”表⽰箱⼦,“3”表⽰⼈,“4”表⽰终点这样可以提⾼代码的移植性如需改为⼿动输⼊地图可以直接定义⼀个⼆维数组,在给他赋值就可以了int screen[9][11]={{0,1,1,1,1,1,1,1,1,0,0},{0,1,0,0,0,1,0,0,0,1,0},{0,1,0,2,2,2,2,2,0,1,0},{0,1,0,2,0,2,0,2,0,1,1},{0,1,0,0,0,3,0,0,2,0,1},{1,1,0,1,1,1,1,0,2,0,1},{1,0,4,4,4,4,4,1,0,0,1},{1,0,4,4,4,4,4,0,0,1,1},{1,1,1,1,1,1,1,1,1,1,0}};//定义为全局变量(地图) i表⽰⾏,j表⽰列计算地图中终点的个数这⼀步主要是为了后⾯判断游戏输赢的int cum(){int i,j,k=0;for(i=0;i<9;i++){for(j=0;j<11;j++){if(screen[i][j]==2){k++;}}}//遍历整个⼆维数组return k;}//计算地图中有多少个终点打印地图函数通过switch函数对⼆维数组中的值进⾏可视化,也就是画出地图注意:这⾥还定义出了6和7,是通过重叠的关系来算的,就是箱⼦在终点上,这个位置⼜有箱⼦⼜有终点2个标识,所以让两个的数值加起来,⽅便理解,也⽅便后⾯的计算void print(){int i,j;printf("请⽤wsad代表上下左右来进⾏游戏\n");for(i=0;i<9;i++){for(j=0;j<11;j++){switch(screen[i][j]){case 0:printf(" ");//空break;case 1:printf("■");//墙break;case 2:printf("★");//箱⼦break;case 3:printf("♀");//⼈break;case 4:printf("○");//终点break;case 6:printf("★");break;//箱⼦和终点case 7://⼈和终点显⽰⼈printf("♀");break;}}printf("\n");}}判断游戏输赢这⾥我写了2个函数,⼀个判断赢,⼀个判断输,并返回值,然后在主函数的最后⾯通过判断返回值来确定游戏的输赢判断赢int win(){int i,j,k=0;int t=0;for(i=0;i<9;i++){for(j=0;j<11;j++){if(screen[i][j]==6){k++;}}}//遍历整个⼆维数组,计算箱⼦在终点上的个数if(k==cum()){t=1;}//如果个数等于前⾯计算出的终点个数,则说明所有终点都放了箱⼦,说明游戏胜利return t;} //判断赢判断输int lose(){int i,j;int k=0;for(i=0;i<9;i++){for(j=0;j<11;j++){if(i>0 && j>0 ){if(screen[i][j] == 2 || screen[i][j] == 6){if(((screen[i-1][j] == 1 || screen[i-1][j] == 2 || screen[i-1][j] == 6) && (screen[i][j-1] == 1 || screen[i][j-1] == 2 || screen[i][j-1] == 6))|| ((screen[i][j-1] == 1 || screen[i][j-1] == 2 || screen[i][j-1] == 6) && (screen[i+1][j] == 1 || screen[i+1][j] == 2 || screen[i+1][j] == 6))|| ((screen[i+1][j] == 1 || screen[i+1][j] == 2 || screen[i+1][j] == 6) && (screen[i][j+1] == 1 || screen[i][j+1] == 2 || screen[i][j+1] == 6))|| ((screen[i][j+1] == 1 || screen[i][j+1] == 2 || screen[i][j+1] == 6) && (screen[i-1][j] == 1 || screen[i-1][j] == 2 || screen[i-1][j] == 6))){k++;}}}}/*这⾥也是遍历了整个数组,判断所有的箱⼦四个⽅向的情况,如果有三个⽅向被堵住了说明箱⼦⽆法移动了,也表明这个箱⼦失效了,⽤k来记录失效的个数,当全部失效时游戏失败(这是游戏的玩法,其实有⼀个被堵住就已经不可能胜利了)*/}if(k==cum()){k=1;return k;//返回1说明游戏失败}接下来是最重要的四个控制函数向上移动通过数字的变化来控制⼆维数组的变化,进⽽控制地图的更新这⾥⾮常重要的就是要理解:加1,加2,加3减3都是什么意思加1:箱⼦的值是2,⼈的值是3,所以箱⼦的位置变成⼈需要加1来实现加2:空地的值是0,箱⼦的值是2,箱⼦和终点在⼀起的值是6,所以在推箱⼦的时候,前⽅的空格或者终点放上箱⼦后数值会加2加3减3:⼈的值是3,⼈要动的话,它原先在的格⼦就会因为⼈⾛了导致数值减3,⾛到的那个格⼦就会因为站了⼈⽽加3如果这个理解的话,代码就⾮常简单了void movew(){if(x>0){if(screen[x-1][y]==1){return ;/*如果箱⼦的上⾯是墙,则地图不会发⽣变化,因为推不动嘛*/}else if(screen[x-1][y]==0){screen[x-1][y]+=3;screen[x][y]-=3;x--;/*如果前⾯是空地,则需要向前移动⼀格,也就是原先⼈的位置变成空地,前⽅的空地变成⼈,空地(0)变成⼈(3)需要加3,⼈变成空地需要减3*/}else if(screen[x-1][y]==4){screen[x-1][y]+=3;screen[x][y]-=3;x--;}//⼀样的else if(screen[x-1][y]==2||screen[x-1][y]==6){if(screen[x-2][y]==0){screen[x-2][y]+=2;//箱⼦前⾯的格变成箱⼦(2)screen[x-1][y]+=1;//箱⼦的位置变成⼈(3)screen[x][y]-=3;/*如果前⾯是空地,则需要向前移动⼀格,也就是原先是箱⼦的格⼦变成⼈,⼈的位置变成空地,原先的空地变成箱⼦,箱⼦(2)变成⼈(3)需要减3,空地变成⼈*/x--;}else if(screen[x-2][y]==1){return ;}else if(screen[x-2][y]==2){return;//如果箱⼦的前⾯是墙或者其他的箱⼦,则箱⼦推不动}else if(screen[x-2][y]==4){screen[x-2][y]+=2;screen[x-1][y]+=1;screen[x][y]-=3;x--;}//这个情况别漏了}}}其他三个⽅向的代码思路和这个是⼀样的向下移动void moves(){if(x<9){if(screen[x+1][y]==1){return ;}else if(screen[x+1][y]==0){screen[x+1][y]+=3;screen[x][y]-=3;x++;}else if(screen[x+1][y]==4){screen [x+1][y]+=3;screen[x][y]-=3;}else if(screen[x+1][y]==2||screen[x+1][y]==6){ if(screen[x+2][y]==1){return;}else if(screen[x+2][y]==0){screen[x+2][y]+=2;screen[x+1][y]+=1;screen[x][y]-=3;x++;}else if(screen[x+2][y]==2){return ;}else if(screen[x+2][y]==4){screen[x+2][y]+=2;screen[x+1][y]+=1;screen[x][y]-=3;x++;}}}}向左移动void movea(){if(y>0){if(screen[x][y-1]==1){return;}else if(screen[x][y-1]==4){screen[x][y-1]+=3;screen[x][y]-=3;y--;}else if(screen[x][y-1]==0){screen[x][y-1]+=3;screen[x][y]-=3;y--;}else if(screen[x][y-1]==2||screen[x][y-1]==6){ if(screen[x][y-2]==0){screen[x][y-2]+=2;screen[x][y-1]+=1;screen[x][y]-=3;y--;}else if(screen[x][y-2]==1){return;}else if(screen[x][y-2]==2){return;}else if(screen[x][y-2]=4){screen[x][y-2]+=2;screen[x][y-1]+=1;screen[x][y]-=3;y--;}}}}向右移动void moved(){if(y<9){if(screen[x][y+1]==1){return;}else if(screen[x][y+1]==4){screen[x][y+1]+=3;screen[x][y]-=3;y++;}else if(screen[x][y+1]==0){screen[x][y+1]+=3;screen[x][y]-=3;y++;}elseif(screen[x][y+1]==2||screen[x][y+1]==6){if(screen[x][y+2]==0){screen[x][y+2]+=2;screen[x][y+1]+=1;screen[x][y]-=3;y++;}else if(screen[x][y+2]==4){screen[x][y+2]+=2;screen[x][y+1]+=1;screen[x][y]-=3;y++;}else if(screen[x][y+2]==2){return;}else if(screen[x][y+2]==1){return;}}}}主函数这个主函数写的有点乱,直接看注释吧int main(){int n,t;int j,k;int b=1;here:system("cls");//printf("开始游戏请按1\n退出游戏请按2\n");scanf("%d",&j);if(j==1){printf("请⽤wsad代表上下左右来进⾏游戏\n");//这个就引导进⼊游戏while(1){system("cls");/*在每⼀次移动过后都清除上⼀个地图,不然就会每⾛⼀步⽣成⼀个图*/print();//先打印地图scanf("%c",&n);//读⼊⽤户的操作switch(n){case 'w':movew();break;case 's':moves();break;case 'a':movea();break;case 'd':moved();break;} //控制⼈移动t=win();if(t==1){goto there;}//每次操作完先判断游戏是否胜利,如果胜利了直接跳到函数最后if(b == lose()){system("cls");print();printf("游戏失败");return 0;} //游戏失败提⽰}}else {system("cls");printf("您确认要退出游戏吗\n确认退出按1\t返回上⼀层按2\n");scanf("%d",&k);if(k==1){printf("你已退出游戏,期待你的再次到来,谢谢");return 0;}else {goto here;}}//这⼀块是最前⾯⽤户进⼊游戏那⾥的,如果⽤户选择退出游戏执⾏的操作 there:printf("恭喜你通过了游戏!");return 0;}//主函数所有的代码就到这⾥了,如果需要完整代码可以留⾔以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

推箱子游戏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>#include <Windows.h>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 U=1,D=2,L=3,R=4; //定义按键状态//int jump;int ofx=10,ofy=9; //定义xy偏移量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 reset;//是否重玩//声明全部函数//void Pos(int x,int y)//设置光标位置{COORD pos;HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos);system("color 3B");}void welcometogame()//开始界面{system("title 2017程序设计之推箱子");Pos(28,3);printf("欢迎来到推箱子!");Pos(50,24);printf(" 软件1603 刘刈16110572087\n");Pos(27,9);printf("用↑.↓.←.→控制人物的移动。

C语言推箱子代码

C语言推箱子代码

#include<stdio.h>#include<stdlib.h>int i,j,steps = 0,gk = 1;char command,t,m = 0,n = 0;int map_1[8][11],map[8][11] = {{0,0,0,0,0,0,0,0,0,0,0},{1,1,1,1,0,0,0,0,0,0,0},{1,0,0,1,1,1,1,1,0,0,0},{1,0,0,6,0,0,4,1,0,0,0},{1,3,0,2,0,0,4,1,0,0,0},{1,0,0,1,1,1,1,1,0,0,0},{1,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0}};int map_2[8][11] = {{0,0,0,1,1,1,1,0,0,0,0},{0,0,0,1,4,0,1,0,0,0,0},{1,1,1,1,0,0,1,1,0,0,0},{1,0,0,0,3,2,0,1,0,0,0},{1,0,1,1,0,0,0,1,0,0,0},{1,0,6,0,0,0,0,1,1,0,0},{1,1,1,1,0,0,0,4,1,0,0},{0,0,0,1,1,1,1,1,1,0,0}};int map_3[8][11] = {{0,0,0,1,1,1,1,0,0,0,0},{0,0,0,1,4,4,1,0,0,0,0},{1,1,1,1,0,0,1,1,0,0,0},{1,0,0,0,0,0,0,1,0,0,0},{1,0,0,0,0,0,0,1,1,0,0},{1,1,1,0,1,6,2,0,1,0,0},{0,0,1,0,0,3,0,0,1,0,0},{0,0,1,1,1,1,1,1,1,0,0}};typedef struct position{int x;int y;}location;location agg[3];//全部通过void clearance(){if(map[agg[1].x][agg[1].y] == 5&&map[agg[2].x + n][agg[2].y + m] == 5&&gk == 3) {printf("你赢了!\n");system("pause");exit(0);}}//判断是否通过void control_adopt(){if(map[agg[1].x][agg[1].y] == 5&&map[agg[2].x + n][agg[2].y + m] == 5) {if(gk != 3){for(i = 0;i < 8;i++)for(j = 0;j < 11;j++){map[i][j] = map_2[i][j];map_2[i][j] = map_3[i][j];map_1[i][j] = map[i][j];}gk++;}steps = 0;}}//重新开始void control_reset(char reset_1){if(reset_1 == 'f'){for(i = 0;i < 8;i++)for(j = 0;j < 11;j++)map[i][j] = map_1[i][j];steps = 0;}}//结束void control_end(char end_0){if(end_0 == 't')exit(0);}//控制方向+ 箱子堵住陷阱void control_direction(char direction_){if(direction_ == 'd'||direction_ == 'w'||direction_ == 'a'||direction_ == 's'){if(direction_ == 'd')m = 1;if(direction_ == 'a')m = -1;if(direction_ == 'w')n = -1;if(direction_ == 's')n = 1;if(agg[0].x + n == agg[1].x&&(agg[0].y + m) == agg[1].y&&map[agg[1].x + n][agg[1].y + m] != 1&&map[agg[1].x][agg[1].y] != 5){if(map[agg[1].x + n][agg[1].y + m] == 4){map[agg[1].x][agg[1].y] = 0;map[agg[1].x + n][agg[1].y + m] = 5;agg[1].y = agg[1].y + m;agg[1].x = agg[1].x + n;}else{t = map[agg[1].x + n][agg[1].y + m];map[agg[1].x + n][agg[1].y + m] = map[agg[1].x][agg[1].y];map[agg[1].x][agg[1].y] = t;agg[1].y = agg[1].y + m;agg[1].x = agg[1].x + n;}}//第二个箱子的判断条件if(agg[0].x + n == agg[2].x&&(agg[0].y + m) == agg[2].y&&map[agg[2].x + n][agg[2].y + m] != 1&&map[agg[2].x][agg[2].y] != 5){if(map[agg[2].x + n][agg[2].y + m] == 4){map[agg[2].x][agg[2].y] = 0;map[agg[2].x + n][agg[2].y + m] = 5;agg[2].y = agg[2].y + m;agg[2].x = agg[2].x + n;}else{t = map[agg[2].x + n][agg[2].y + m];map[agg[2].x + n][agg[2].y + m] = map[agg[2].x][agg[2].y];map[agg[2].x][agg[2].y] = t;agg[2].y = agg[2].y + m;agg[2].x = agg[2].x + n;}}if(map[agg[0].x + n][agg[0].y + m] == 0){t = map[agg[0].x + n][agg[0].y + m];map[agg[0].x + n][agg[0].y + m] = map[agg[0].x][agg[0].y];map[agg[0].x][agg[0].y] = t;agg[0].y = agg[0].y + m;agg[0].x = agg[0].x + n;steps++;}m = 0;n = 0;}}//输出,寻找1,2,3void control_output(){printf("关卡%d:\n",gk);printf("步数:%d\n",steps);for(i = 0;i < 8;i++){for(j = 0;j < 11;j++){if(map[i][j] == 1)printf("■");if(map[i][j] == 0)printf("");if(map[i][j] == 2){printf("□");agg[1].x = i;agg[1].y = j;}if(map[i][j] == 3){printf("⊙");agg[0].x = i;agg[0].y = j;}if(map[i][j] == 4)printf("☆");if(map[i][j] == 5)printf("★");if(map[i][j] == 6){printf("□");agg[2].x = i;agg[2].y = j;}}printf("\n");}printf("w,a,s,d控制方向,t退出游戏,f重新开始!\n");}//输入指令void input_command(){while(1){scanf("%c",&command);if(command == 'w'||command == 'a'||command == 's'||command == 'd'||command == 't'||command == 'f')break;}}main(){for(i = 0;i < 8;i++){for(j = 0;j < 12;j++){map_1[i][j] = map[i][j];}}while(1){control_output();input_command();control_direction(command);control_reset(command);control_end(command);control_adopt();system("cls");clearance();}}【下载本文档,可以自由复制内容或自由编辑修改内容,更多精彩文章,期待你的好评和关注,我将一如既往为您服务】。

C语言完整游戏项目推箱子详细代码

C语言完整游戏项目推箱子详细代码

C语⾔完整游戏项⽬推箱⼦详细代码话不多说我们今天就来创造出属于我们⾃⼰的《推箱⼦》,GOGOGO直接开始吧⾸先是我们⽤⼆维数组特定的数字描绘出这个地图int cas = 0;int map[3][8][8] ={1,1,1,1,1,1,1,1,1,3,4,0,0,4,3,1,1,0,1,3,0,1,0,1,1,0,1,4,0,1,0,1,1,0,0,5,0,0,0,1,1,0,1,0,0,1,0,1,1,3,4,0,0,4,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,4,0,0,4,3,1,1,0,1,3,0,1,0,1,1,0,1,4,0,1,0,1,1,3,4,5,0,0,0,1,1,0,1,0,0,1,0,1,1,3,4,0,0,4,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,4,0,0,4,3,1,1,0,1,3,0,1,0,1,1,0,1,4,0,1,0,1,1,3,4,5,0,4,3,1,1,0,1,0,0,1,0,1,1,3,4,0,0,4,3,1,1,1,1,1,1,1,1,1};然后来绘制我们的推箱⼦地图void drawGraph(){for (int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){//算贴图的坐标int x = 50 * j;int y = 50 * i;switch (map[cas][i][j]){case 0://⼀个汉字符号占⽤两个位置//printf(" ");putimage(x, y, img + 0);break;case 1:putimage(x, y, img + 1);//printf("■");break;case 3:putimage(x, y, img + 2);//printf("☆");break;case 4:putimage(x, y, img + 3);//printf("★");break;case 5:case 8:putimage(x, y, img + 4);//printf("⼈");break;case 7:putimage(x, y, img + 5);//printf("●");break;}}//printf("\n");}}之后就是我们的游戏函数,怎样去⽤什么按键去控制我们的⾓⾊void keyDown(){int userKey = _getch(); //不可见输⼊//定位:找到⼈的位置int i = 0;int j = 0;for (i = 1; i < 8; i++){for (j = 1; j < 8; j++){if (map[cas][i][j] == 5 || map[cas][i][j] == 8){goto NEXT;}}}NEXT://我们这个游戏⽤什么按键去玩switch (userKey){case 'W':case 'w':case 72:if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3){map[cas][i][j] -= 5;map[cas][i - 1][j] += 5;}if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7){if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3){map[cas][i][j] -= 5;map[cas][i - 1][j] += 1;map[cas][i - 2][j] += 4;}}break;case 's':case 'S':case 80:if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3)map[cas][i][j] -= 5;map[cas][i + 1][j] += 5;}if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7) {if (map[cas][i + 2][j] == 0 || map[cas][i +2][j] == 3) {map[cas][i][j] -= 5;map[cas][i + 1][j] += 1;map[cas][i + 2][j] += 4;}}break;case 'a':case 'A':case 75:if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3) {//a+=1 a=a+1 复合赋值运算符map[cas][i][j] -= 5;map[cas][i][j - 1] += 5;}if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7) {if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3) {map[cas][i][j] -= 5;map[cas][i][j - 1] += 1;map[cas][i][j - 2] += 4;}}break;case 'd':case 'D':case 77:if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3) {map[cas][i][j] -= 5;map[cas][i][j + 1] += 5;}if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7) {if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3) {map[cas][i][j] -= 5;map[cas][i][j + 1] += 1;map[cas][i][j + 2] += 4;}}break;}}再然后就是我们如何去判断游戏的结果//胜负的判断:int gameOver(){//地图上没有箱⼦就可以结束for (int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){if (map[cas][i][j] == 4){return 0;}}}return 1;}最后运⾏我们的主函数就⾏啦int main()loadResource();mciSendString("open 1.mp3", 0, 0, 0);mciSendString("play 1.mp3 repeat", 0, 0, 0);initgraph(50 * 8, 50 * 8);while (1){drawGraph();if (gameOver()){cas++; //变换关卡if(cas==3)break;}keyDown();//system("cls");}closegraph();//printf("GameOver!\n");return 0;}其实代码并不是很多,当然啦,如果同学们想更加完善,可以增加关卡设定,再优化⼀下我们的开始界⾯以及游戏界⾯也是可以的,⼤家快去尝试吧希望看完了的同学可以获得⾃⼰想要的知识,也感谢⼤家的耐⼼观看,在这⾥想得到⼤家⼀波关注,后续UP主还会发布更多的项⽬源码以及学习资料,有什么问题可以回帖留⾔,我尽量回答。

C++实现推箱子游戏源代码

C++实现推箱子游戏源代码

#define qiang 1#define ren 2#define xiangzi 3#define jia 6#define men 8#define space 9int map[13][17] = {0};int fuben[13][17];int num_guan; //现在的关数int num_xiangzi; //箱子的个数int move_num = 0; //人行走的次数int push_num = 0; //人推箱子的次数int people[2]; //M[0] 是人所在位置的横坐标M[1]是人所在位置的纵坐标int door[2]; //门所在的坐标int tem = 0;int cc;void play();void chushi2();void guan1();void guan2();void guan3();void guan4();void guan5();void guan6();void guan7();void guan8();void guan9();void guan10();void guan11();void guan12();void guan13();void guan14();void dayin();int control();int finish();/****************************************************** ****主方法******************************************************* ******************/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_HAND LE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);printf(" ╭╮■■□□◆◆◆◆◆◆◆◆\n");printf(" ││■■□□□□□□□□◆ \n");printf(" ╭─┘└╮■■■■■□■□■◆◆ \n");printf(" ╰─┐┌■■■□┏━━━┓◆◆ \n");printf(" ││■■■■□□□□┃┏━┓┃◆◆ \n");printf(" ╭─┘└╮■■□┃┏━┓┃┏━━━━◆━━┓\n");printf(" ╰─┐┌╯■■■■□□┃┏━┓┃┗━━━━◆━━┛\n");printf(" ╭─┘│■■□□□┃┗━┛┃◆ \n");printf(" ╰──╯■■■■■□□□┗━━━┛◆◆◆◆◆◆ \n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),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++){map[i][j] = 0;fuben[i][j] = 0;}}/*************************游戏规则及说明******************************/void shuoming(){int i;char c;for(i = 0;i<=999;i++){system("cls");LE),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_HAND LE),BACKGROUND_INTENSITY|FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN |BACKGROUND_BLUE|FOREGROUND_RED|FOREGROUND_RED);printf("囧");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf(" ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("别看了,这就是你了,还挺cool的吧,嘿嘿\n\n\n");LE),FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);printf(" █ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是墙,放心,不是豆腐渣工程,要不你可以去撞撞看,\n");printf(" 你会懂得......\n\n\n");printf(" ■ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是箱子,就是要你去推的那个,不要试图一下推两个箱\n");printf(" 子,更不要想推着箱子把墙撞开,没用的...不要沮丧,再免\n");printf(" 费给你一句忠告,要是把箱子推到了死角,呵呵,直接下一\n");printf(" 关吧\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_RED);printf(" ★ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这里就是箱子们的最终归宿了,箱子要是回不到家,哼哼,\n");printf(" 你自己掂量着办吧。

推箱子-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语言推箱子课程设计(附报告)

C语言推箱子课程设计(附报告)

(注:VC运行时,分别新建两个文件BotIt.cpp和Maps.h,再在同一目录下运行其中cpp)代码部分文件“BoxIt.cpp”如下:#include <stdio.h>#include <conio.h>#include <stdlib.h>#include "Maps.h"#define AISLE "" //走廊#define N_AISLE 0#define W ALL "■" //墙壁#define N_W ALL 1#define AIM "※" //未放箱子的目标#define N_AIM 2#define BOX "□" //未在目标位置的箱子#define N_BOX 3#define MAN "♀" //人#define N_MAN 4#define BOX_OK "▢" //■已经在目标位置的箱子[即已放箱子的目标]#define N_BOX_OK 5#define MAN_IN "* "#define N_MAN_IN 6#define ESC 0x1B#define UP 'w'#define DOWN 's'#define LEFT 'a'#define RIGHT 'd'#define RESTART 'r'#define SA VE 'x'#define PF(a) printf("%s",a)struct Point{int x;int y;}man_p;/*小人坐标*///struct Filename{int num;char name[20];}filename[10];//int N;/*存档总人数*/int M;/*关数*/int step;/*步数*/char x;void welcome();void ingame();void playgame(int map[14][16]);/*玩游戏*/void displaymap(int map[14][16]);/*展示当前游戏界面*/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]);/*人移动*/ void movebox(Point box_p,Point boxf_p,int map[14][16]);/*移箱子*/int win(int map[14][16]);/*判断当无※时赢*/void choosemap(int map[14][16],int i);/*选关*/void changemap(int map[14][16],int mapx[14][16]);/*换地图*/void save();/*保存当前*/int read();/*读取存档*/int a=0,b=0,c=0,d=0;//system("color a");int main(){x='0';while(x!='4'){welcome();if(x=='3'&&read()==0) continue;if(x!='4') ingame();}return 0;}void displaymap(int map[14][16]){int i,j;system("cls");printf("\t\t\tMisson %d\n\n",M);for(i=0;i<14;i++){for(j=0;j<16;j++){ switch(map[i][j]){case N_AISLE:PF(" ");break;case N_W ALL:PF(W ALL);break;case N_AIM:PF(AIM);break;case N_BOX:PF(BOX);break;case N_MAN:PF(MAN);break;case N_BOX_OK:PF(BOX_OK);break;case N_MAN_IN:PF(MAN_IN);default:;}}if(i==2) printf("Press the key:");if(i==3) printf("'w'、'a'、's'、'd' to control ♀");if(i==5) printf("'r' to replay");if(i==7) printf("'x' to save");if(i==9) printf("'ESC' back to the menu");if(i==12) printf("Step:%d",step);PF("\n");}}void playgame(int map[14][16]){ int X,Y;Point manf_p,manff_p;//findman(man_p.x,man_p.y,map);displaymap(map);step=0;while(win(map)==0){findman(man_p.x,man_p.y,map);//printf("输入:");switch(getKey()){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;manf_p.y=man_p.y-1,manff_p.y=man_p.y-2;X=0,Y=-1;break;case LEFT://printf("\n左\n");manf_p.x=man_p.x-1;manff_p.x=man_p.x-2;manf_p.y=man_p.y;manff_p.y=man_p.y;X=-1,Y=0;break;case RIGHT://printf("\n右\n");manf_p.x=man_p.x+1;manff_p.x=man_p.x+2;manf_p.y=man_p.y;manff_p.y=man_p.y;X=1,Y=0;break;case SA VE:save();continue;case RESTART:changemap(map,map0);M--;continue;case ESC:system("cls");changemap(map,map0);M=44;continue;default:;}//printf("\n人位置[%d %d]\n",man_p.y,man_p.x);//printf("\n人前方[%d %d]人前前方[%d %d]\n",manf_p.y,manf_p.x,manff_p.y,manff_p.x);switch(getmap(manf_p,map)){case N_AISLE://0case N_AIM://2moveman(man_p,manf_p,X,Y,map);break;case N_BOX://3case N_BOX_OK://5if(getmap(manff_p,map)==N_AISLE||getmap(manff_p,map)==N_AIM){movebox(manf_p,manff_p,map);moveman(man_p,manf_p,X,Y,map);}break;default:;}displaymap(map);}if(M==42) printf("\n\tCongratulation! Pass All!!!\n\n\n\t");}void findman(int &a,int &b,int map[14][16]){int i,j;for(i=0;i<14;i++)for(j=0;j<16;j++)if(map[i][j]==N_MAN||map[i][j]==N_MAN_IN)a=j,b=i;}unsigned char getKey(){unsigned char key;do{key = getch();}while(key != RESTART && key != ESC && key != LEFT && key != RIGHT && key != UP && key != DOWN && key!='x'&& key!=0x1B &&key!='r');return key;}int getmap(Point p,int map[14][16]){//printf("\n前方值%d\n",map[p.y][p.x]);return map[p.y][p.x];}void moveman(Point man_p,Point manf_p,int X,int Y,int map[14][16]){map[manf_p.y][manf_p.x]=(map[manf_p.y][manf_p.x]==N_AIM||map[manf_p.y][manf_p.x]==N_BOX _OK)?N_MAN_IN:N_MAN;map[man_p.y][man_p.x]=map[man_p.y][man_p.x]==N_MAN_IN?N_AIM:N_AISLE; //printf("\n移动后—小人位置值%d\n",map[manf_p.y][manf_p.x]);//printf("\n移动后—原小人位置值%d\n",map[man_p.y][man_p.x]);man_p.x+=X,man_p.y+=Y;step++;}void movebox(Point box_p,Point boxf_p,int map[14][16]){map[boxf_p.y][boxf_p.x]=map[boxf_p.y][boxf_p.x]==N_AISLE?N_BOX:N_BOX_OK; map[box_p.y][box_p.x]=map[box_p.y][box_p.x]==N_BOX?N_AISLE:N_AIM;}int win(int map[14][16]){int i,j;for(i=0;i<14;i++)for(j=0;j<16;j++)if(map[i][j]==2||map[i][j]==6)return 0;return 1;}void choosemap(int map[14][16],int i){switch(i){case 1:changemap(map,map1);break;case 2:changemap(map,map2);break;case 3:changemap(map,map3);break;case 4:changemap(map,map4);break;case 5:changemap(map,map5);break;case 6:changemap(map,map6);break;case 7:changemap(map,map7);break;case 8:changemap(map,map8);break;case 9:changemap(map,map9);break;case 10:changemap(map,map10);break;case 11:changemap(map,map11);break;case 12:changemap(map,map12);break;case 13:changemap(map,map13);break;case 14:changemap(map,map14);break;case 15:changemap(map,map15);break;case 16:changemap(map,map16);break;case 17:changemap(map,map17);break;case 18:changemap(map,map18);break;case 19:changemap(map,map19);break;case 20:changemap(map,map20);break;case 21:changemap(map,map21);break;case 22:changemap(map,map22);break;case 23:changemap(map,map23);break;case 24:changemap(map,map24);break;case 25:changemap(map,map25);break;case 26:changemap(map,map26);break;case 27:changemap(map,map27);break;case 28:changemap(map,map28);break;case 29:changemap(map,map29);break;case 30:changemap(map,map30);break;case 31:changemap(map,map31);break;case 32:changemap(map,map32);break;case 33:changemap(map,map33);break;case 34:changemap(map,map34);break;case 35:changemap(map,map35);break;case 36:changemap(map,map36);break;case 37:changemap(map,map37);break;case 38:changemap(map,map38);break;case 39:changemap(map,map39);break;case 40:changemap(map,map40);break;case 41:changemap(map,map41);break;case 42:changemap(map,map42);break;default:;}}void changemap(int map[14][16],int mapx[14][16]) {int i,j;for(i=0;i<14;i++)for(j=0;j<16;j++)map[i][j]=mapx[i][j];}void save(){int i,j;FILE *in;/*char filename[10][20];printf("put in the filename:");scanf("%s",filename);*/if((in=fopen("save game","w"))==NULL){printf("cannot open this file\n");exit(0);}for(i=0;i<14;i++)for(j=0;j<16;j++)fwrite(&map[i][j],sizeof(int),1,in);fwrite(&M,sizeof(int),1,in);fwrite(&step,sizeof(int),1,in);printf("\nGame Misson %d saved!\n",M);system("pause");fclose(in);}int read(){int i,j;FILE *out;/*char filename[10][20];printf("put in the filename:");scanf("%s",filename);*/if((out=fopen("save game","r"))==NULL){printf("\ncannot open this file\n");return 0;}else{for(i=0;i<14;i++)for(j=0;j<16;j++)fread(&map[i][j],sizeof(int),1,out);fread(&M,sizeof(int),1,out);fread(&step,sizeof(int),1,out);printf("\nM=%d\n",M);printf("Game read!\n");fclose(out);return 1;}}void welcome(){printf("\t\t ♀Box It \n");printf("\t\t1:New Game.\n\t\t2:Select Misson.\n\t\t3:Read Record.\n\t\t4:Exit Game.\n Choose-- ");switch(x=getch()){case '1':printf("1:");M=1;choosemap(map,M);break;case '2':printf("2:");printf("1-42 Misson to Choose:");scanf("%d",&M);choosemap(map,M);break;case '3':printf("3:");if(read()==1) printf("\n%d misson opened\n",M);break;case '4':printf("4:Exit Game.\n\n"); break;default:printf("Not 1-4\n");}}void ingame(){while(M<43){playgame(map);step=0;M++;choosemap(map,M);}}文件“Maps.h”如下:int map[14][16];int map0[14][16]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };int map1[14][16]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,2,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1,0,1,0,0}, {1,0,0,0,0,0,0,3,0,0,0,3,0,1,0,0}, {1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0}, {0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };int map37[14][16]={{0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0}, {0,1,2,2,1,0,0,0,0,0,0,1,2,2,1,0}, {0,1,2,2,1,1,1,1,1,1,1,1,2,2,1,0}, {0,1,0,3,0,2,2,2,2,2,2,3,2,3,1,0}, {0,1,3,0,3,0,3,0,3,0,3,0,3,0,1,0}, {0,1,0,3,0,3,0,3,4,3,0,3,0,3,1,0}, {0,1,3,0,3,0,3,0,3,0,3,0,3,0,1,0}, {0,1,0,3,0,3,0,3,0,3,0,3,0,3,1,0}, {0,1,3,2,3,2,2,2,2,2,2,0,3,0,1,0}, {0,1,2,2,1,1,1,1,1,1,1,1,2,2,1,0}, {0,1,2,2,1,0,0,0,0,0,0,1,2,2,1,0}, {0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0},。

c语言推箱子代码精编版

c语言推箱子代码精编版

#include <stdio.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);//调用输出函数loop://语句标号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);//调用输出函数loop://语句标号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);//调用输出函数loop://语句标号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]=='#'){}//推不动箱子elseif(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){}elsec[*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<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语言推箱子代码

c语言推箱子代码推箱子是一款经典的益智游戏,也是计算机科学中常见的算法案例。

在这篇文章中,我们将通过C语言来实现推箱子游戏的代码。

推箱子游戏中,玩家需要控制一个人物,将箱子推到指定位置。

箱子只能被推,不能被拉,也不能被穿过其他箱子或墙壁。

游戏的目标是将所有的箱子推到指定位置,完成所有的关卡。

我们需要定义游戏中的元素,包括墙壁、箱子、玩家和目标位置。

我们可以使用二维数组来表示游戏地图,其中0表示空地,1表示墙壁,2表示箱子,3表示玩家,4表示目标位置。

例如,下面是一个简单的游戏地图:int map[5][5] = {{1, 1, 1, 1, 1},{1, 0, 0, 0, 1},{1, 0, 2, 4, 1},{1, 0, 3, 0, 1},{1, 1, 1, 1, 1}};接下来,我们需要实现玩家的移动。

玩家可以向上、下、左、右四个方向移动,但是需要避免撞到墙壁或箱子。

我们可以使用循环和条件语句来实现玩家的移动,例如:int x = 2; // 玩家的初始位置int y = 2;char input; // 读取玩家的输入while (1) { // 游戏循环// 打印游戏地图for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; 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("."); }printf("\n");}// 读取玩家的输入scanf("%c", &input);// 根据玩家的输入移动玩家int new_x = x, new_y = y;if (input == 'w') new_x--;if (input == 's') new_x++;if (input == 'a') new_y--;if (input == 'd') new_y++;// 判断新位置是否可行if (map[new_x][new_y] == 0 || map[new_x][new_y] == 4) { map[x][y] = 0;map[new_x][new_y] = 3;x = new_x;y = new_y;} else if (map[new_x][new_y] == 2) {// 判断箱子的移动int new_box_x = new_x, new_box_y = new_y;if (input == 'w') new_box_x--;if (input == 's') new_box_x++;if (input == 'a') new_box_y--;if (input == 'd') new_box_y++;if (map[new_box_x][new_box_y] == 0 || map[new_box_x][new_box_y] == 4) {map[x][y] = 0;map[new_x][new_y] = 3;map[new_box_x][new_box_y] = 2;x = new_x;y = new_y;}}// 判断游戏是否结束int count = 0;for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; j++) {if (map[i][j] == 2) count++;}}if (count == 0) {printf("Congratulations! You win!\n");break;}}我们需要添加一些辅助功能,例如读取游戏地图和保存游戏进度。

推箱子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`。

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

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)。
}
if (map[i][j]==6||map[i][j]==9)
break;
}
printf("\n\t\t\t您的当前坐标(%d,%d)",i,j);
printf("\n\n\t\t\t加油哦!^-^");
case 6:
printf("♂"); //数字6代表人
break;
case 8:
printf("\t\t\t"); //数字6代表人
{
//如果人前面是箱子,而箱子前面是空地。
if (map[i+2][j]==0)
{
map[i+2][j]=4;
//下面是对箱子原地进行判断
if(map[i+1][j]==7) //如果箱子原地为目的地。
if(map[i][j]==9)
map[i][j]=3;
else
map[i][j]=0;
}
//如果人的前面是箱子,而箱子前面是目的地。
else if (map[i-2][j]==3)
switch(map[i][j])
{
case 0:
printf(" "); //数字0代表道路
{
map[i-2][j]=7;
count++;
//下面是对箱子原先位置的判断,同上。
if(map[i-1][j]==7)
map[i-1][j]=9;
else
map[i-1][j]=6;
else
map[i][j]=0;
}
}
break;
case 's':
//如果人前面是空地。
if(map[i+1][j]==0)
{
map[i+1][j]=6+0;
map[i][j]=3;
else
map[i][j]=0;
}
else if(map[i-1][j]==3)
{
map[i-1][j]=6+3;
if(map[i][j]==9)
map[i+1][j]=6+3;
if(map[i][j]==9)
map[i][j]=3;
else
map[i][j]=0;
}
//如果人前面是箱子。
else if(map[i+1][j]==4)
int map[10][13] =
{
{8,2,2,2,2,2,1,1,1,1,1,2,2},
{8,1,1,1,1,2,1,0,0,0,1,1,2},
{8,1,0,0,1,1,1,0,1,0,0,1,2},
{8,1,0,4,3,3,3,3,3,1,0,1,1},
{8,1,0,0,1,1,3,3,3,4,0,0,1},
{8,1,0,0,0,0,4,1,1,4,1,0,1},
{8,1,0,4,1,4,0,0,0,4,0,0,1},
{8,1,1,0,6,0,1,1,1,4,1,0,1},
{8,2,1,1,1,1,1,2,1,0,0,0,1},
{8,2,2,2,2,2,2,2,1,1,1,1,1}
};
while (1)
{
system("color 0A");
system("CLS");
draw_map(map);
printf("\n\n\t\t\t当前得分:%d\n",count);
void draw_map(int map[10][13])
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<13;j++)
{
case 2:
printf(" "); //数字2是游戏边框的空白部分
break;
case 3:
break;
case 9:
printf("♂"); //数字9代表人进入目的地
break;
}
printf("×"); //数字3代表目的地
break;
case 4:
printf("●"); //数字4代表箱子
else if(map[i-1][j]==7)
{
//如果人前面是已经进入某目的地的箱子,而箱子前面是空地。
if(map[i-2][j]==0)
{
count--;
map[i-2][j]=4;
map[i-1][j]=9;
if(map[i][j]==9)
map[i][j]=3;
else
map[i][j]=0;
}
//如果人前面是目的地。
else if(map[i+1][j]==3)
{
}
printf("\n"); //分10行输出
}
}
void first()
{
char input;
int count=0; //定义记分变量
for (i=0;i<10;i++)
{
for (j=0;j<12;j++)
{
if (map[i][j]==6||map[i][j]==9)
break;
map[i+1][j]=9;
else
map[i+1][j]=6;
//下面是对人原地进行判断
if(map[i][j]==9) //如果之前是目的地。
map[i][j]=3;
#include"stdafx.h"
#include <stdio.h> //标准输入输出头文件
#include <stdlib.h> //system函数的头文件
#include <conio.h> //getch函数的头文件
int i,j;
void draw_map(int map[10][13]); //声明画图函数
input = getch();
switch (input)
{
case 'w':
//如果人前面是空地。
if(map[i-1][j]==0)
{
map[i-1][j]=6+0;
if(map[i][j]==9)
break;
case 7:
printf("㊣"); //数字7代表箱子进入目的地
break;
if(map[i-2][j]==3)
{
map[i-2][j]=7;
map[i-1][j]=9;
//下面是对人原先站立地进行判断,方法同上。
if(map[i][j]==9)
map[i][j]=3;
else
map[i][j]=0;
map[i][j]=3;
else
map[i][j]=0;
}
//如果人前面是箱子。
else if(map[i-1][j]==4)
{
//如果人前面是箱子,而箱子前面是空地。
if (map[i-2][j]==0)
//下面是对人原先地进行判断,方法同上。
if(map[i][j]==9)
map[i][j]=3;
else
map[i][j]=0;
}
//如果人前面是已经进入某目的地的箱子,而箱子前面是另一目的地。
//下面是对人原先位置进行判断,同上。
if(map[i][j]==9)
map[i][j]=3;
else
map[i][j]=0;
}
}
//如果人前面是已经进入某目的地的箱子(ID=7)。
break;
case 1:
printf("■"); //数字1代表墙壁
break;
{
map[i-2][j]=4;
if(map[i-1][j]==7)
map[i-1][j]=9;
else
map[i-1][j]=6;
相关文档
最新文档