I believe you are sure to complete the small game of pushing boxes. The gameplay is very simple, that is, one person to push all the boxes to the corresponding position that can win. So let’s see what we can do with the box pushing game.
char map[10][10]= { {'#','#','#','#','#','#','#','#','#','#'}, {'#','#','#','#',' ',' ','! ',' ',' ','#'}, {'#',' ',' ',' ',' ','o',' ',' ',' ','#'}, {'#',' ',' ',' ',' ',' ',' ',' ',' ','#'}, {' # ', '#', ', '#', '#', '#', ', '#', '#', '#'}, {' # ', ' ' ' ', ', ', ', ', ', ', '#'}, {' # ', 'a', ' ', ' 'and', ' 'and', ' 'and', '#'}, {' # ', '#', ', '#', ', '#', '#', '#', '#', '#'}, {'#','#',' ',' ',' ',' ',' ','x','#','#'}, {'#','#','#','#','#','#','#','#','#','#'} }; void DrawMap(){ for(int i=0; i<=9; i++){ for(int j=0; j<=9; j++){ cout<<map[i][j]; } cout<<endl; }}Copy the code
First draw the map, where # is the wall, X is the end point, o is the box to push, and then! People. Empty characters are channels. Don’t look at the interface is simple, we learn algorithm, as long as the thought of learning, literally transplanted to which interface procedures are ok.
int r,l; For (int I =0; i<=9; i++){ for(int j=0; j<=9; j++){ if(map[i][j]=='! '){ r=i; //r, l equals the width and length of the map l=j; }}}Copy the code
So at the beginning we need to go over and get the coordinates of the person.
cout<<"please enter w(up) s(down) a(left) b(right)"<<endl; char ch; cin>>ch; If (ch = = 'w' | | ch = = 'w') {if (map [r - 1] [l] = = ' ') {/ / go up without obstacle map [r] [l] = = ' '; map[r-1][l]='! '; / / go up} the if (map [r - 1] [l] = = 'o') {/ / if the above box if (map - 2 [r] [l] = = '| | map - 2 [r] [l] = =' x ') {/ / for pushing the box away, Map [r-2][l]='o'; map[r-2][l]='o'; map[r-1][l]='! '; map[r][l]=' '; } } } if(ch=='s'||ch=='S'){ if(map[r+1][l]==' '){ map[r][l]=' '; map[r+1][l]='! '; } if(map[r+1][l]=='o'){ if(map[r+2][l]==' '||map[r+2][l]=='x'){ map[r+2][l]='o'; map[r+1][l]='! '; map[r][l]=' '; } } } if(ch=='a'||ch=='A'){ if(map[r][l-1]=' '){ map[r][l]=' '; map[r][l-1]='! '; } if(map[r][l-1]=='o'){ if(map[r][l-2]==' '||map[r][l-2]=='x'){ map[r][l-2]='o'; map[r][l-1]='! '; map[r][l]=' '; } } } if(ch=='d'||ch=='D'){ if(map[r][l+1]==' '){ map[r][l+1]='! '; map[r][l]=' '; } if(map[r][l+1]=='o'){ if(map[r][l+2]==' '||map[r][l+2]=='x'){ map[r][l+2]='o'; map[r][l+1]='! '; map[r][l]=' '; }}}Copy the code
This is wSAD up, down, left, and right. Then the operation needs to judge whether the person can move. If it’s a wall, you can’t move. If it is a box, there is also a judgment of the box in front of the box can move, if you can directly push the box.
Overall the game is not too difficult. For multiple levels, you can optionally add a level map profile. Then let the game designer configure it. Designing the map of the game is where the difficulty lies.
If you want to learn how to make games, you can pay attention to the public number: code like poetry, find me to learn together.