Write the box pushing game in C
- 2020-06-19 11:14:50
- OfStack
This article is an example to share the C language push box game specific implementation code for your reference, the specific content is as follows
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <conio.h>
// Rows and columns
#define ROW 10
#define COL 11
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/**
*
*
*/
// The map
char map[ROW][COL] = {
"##########",//0
"### ##",//1
"### ##",//2
"##AX # ##",//3
"### ## ",//4
"##### #",//5
"## #",//6
"# ####",//7
"### ",//8
"##########" //9
//A: people . X Box:
} ;
// Print the map
void showMap();
// Receive the direction of the SIMS
char enterDirection();
// The way the little man moves up
void moveToUp();
// The way the little man moves down
void moveToDown();
// The method by which the villain moves to the right
void moveToRight();
// The way the little man moves to the left
void moveToLeft();
// Current lilliputians coordinates
int currentPersonRow = 3;
int currentPersonCol = 2;
// The coordinates of the current box
int currentBoxRow = 3;
int currentBoxCol = 3;
int main(int argc, char *argv[]) {
//system("clear");
printf(" Click enter to start the game ^_^\n\n");
//1 On behalf of the run 0 stop
int flag = 1;
while(flag==1){
// According to the map
showMap();
// Receive the direction of the SIMS
char dir = enterDirection();
switch(dir){
// The little man moves up
case 'w':
case 'W':
moveToUp();
break;
// The little man moves down
case 's':
case 'S':
moveToDown();
break;
// The little man moves to the right
case 'd':
case 'D':
moveToRight();
break;
// The little man moves to the left
case 'a':
case 'A':
moveToLeft();
break;
// Stop running
case 'q':
case 'Q':
printf(" What a low IQ you have! T_T\n");
flag = 0;
break;
}
showMap();
if(currentBoxRow==8& because tBoxCol==9){
printf(" You have a high IQ ^_^!!!");
flag = 0;
}
}
}
/*
Method implementation
*/
// Print the map
void showMap(){
int i;
for(i = 0;i < ROW; i++){
printf("%s\n",map[i]);
}
printf("\n\n\n\n\n");
printf("W: On, S :, A : left, D : right. Q : quit ");
printf("\n\n\n\n\n");
}
// Receive the direction of the SIMS
char enterDirection(){
// remove SCANF Buffer in
rewind(stdin);
char dir;
dir = getch();
//scanf("%c",&dir);
return dir;
}
// The way the little man moves up
void moveToUp(){
// The man under 1 A coordinate
int nextPersonCol = currentPersonCol;
int nextPersonRow = currentPersonRow - 1;
// Under the case of 1 A coordinate
int nextBoxRow = currentBoxRow - 1;
int nextBoxCol = currentBoxCol;
// If the little man's bottom 1 The coordinates are the paths
if(map[nextPersonRow][nextPersonCol]==' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
}
// If the little man's bottom 1 The coordinates are the walls
if(map[nextPersonRow][nextPersonCol]=='#'){
// Nothing
}
// If the little man's bottom 1 The coordinates are the boxes
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol] == ' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
map[nextBoxRow][nextBoxCol] = 'X';
map[currentBoxRow][currentBoxCol] = 'A';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
currentBoxRow = nextBoxRow;
currentBoxCol = nextBoxCol;
}
}
}
// The way the little man moves down
void moveToDown(){
// The man under 1 A coordinate
int nextPersonCol = currentPersonCol;
int nextPersonRow = currentPersonRow + 1;
// Under the case of 1 A coordinate
int nextBoxRow = currentBoxRow + 1;
int nextBoxCol = currentBoxCol;
// If the little man's bottom 1 The coordinates are the paths
if(map[nextPersonRow][nextPersonCol]==' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
}
// If the little man's bottom 1 The coordinates are the walls
if(map[nextPersonRow][nextPersonCol]=='#'){
// Nothing
}
// If the little man's bottom 1 The coordinates are the boxes
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol] == ' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
map[nextBoxRow][nextBoxCol] = 'X';
map[currentBoxRow][currentBoxCol] = 'A';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
currentBoxRow = nextBoxRow;
currentBoxCol = nextBoxCol;
}
}
}
// The method by which the villain moves to the right
void moveToRight(){
// The man under 1 A coordinate
int nextPersonCol = currentPersonCol + 1;
int nextPersonRow = currentPersonRow;
// Under the case of 1 A coordinate
int nextBoxRow = currentBoxRow;
int nextBoxCol = currentBoxCol + 1;
// If the little man's bottom 1 The coordinates are the paths
if(map[nextPersonRow][nextPersonCol]==' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
}
// If the little man's bottom 1 The coordinates are the walls
if(map[nextPersonRow][nextPersonCol]=='#'){
// Nothing
}
// If the little man's bottom 1 The coordinates are the boxes
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
map[nextBoxRow][nextBoxCol] = 'X';
map[currentBoxRow][currentBoxCol] = 'A';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
currentBoxRow = nextBoxRow;
currentBoxCol = nextBoxCol;
}
}
}
// The way the little man moves to the left
void moveToLeft(){
// The man under 1 A coordinate
int nextPersonCol = currentPersonCol - 1;
int nextPersonRow = currentPersonRow;
// Under the case of 1 A coordinate
int nextBoxRow = currentBoxRow;
int nextBoxCol = currentBoxCol - 1;
// If the little man's bottom 1 The coordinates are the paths
if(map[nextPersonRow][nextPersonCol]==' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
}
// If the little man's bottom 1 The coordinates are the walls
if(map[nextPersonRow][nextPersonCol]=='#'){
// Nothing
}
// If the little man's bottom 1 The coordinates are the boxes
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==' '){
map[nextPersonRow][nextPersonCol] = 'A';
map[currentPersonRow][currentPersonCol] = ' ';
map[nextBoxRow][nextBoxCol] = 'X';
map[currentBoxRow][currentBoxCol] = 'A';
currentPersonRow = nextPersonRow;
currentPersonCol = nextPersonCol;
currentBoxRow = nextBoxRow;
currentBoxCol = nextBoxCol;
}
}
}