C语言推箱子完整代码

C语言推箱子完整代码

#include<stdio.h>
#include<conio.h> 
#include<windows.h>
 
int map[9][11]={
	{0,1,1,1,1,1,1,1,1,1,0},
	{0,1,0,0,0,1,0,0,0,1,0},
	{0,1,0,0,3,0,0,0,0,1,0},
	{0,1,0,3,0,3,3,3,0,1,1},
	{0,1,0,0,0,2,0,0,0,0,1},
	{1,1,0,0,1,1,1,0,3,0,1},
	{1,0,4,4,0,4,0,0,0,0,1},
	{1,0,4,4,0,4,4,3,0,1,1},
	{1,1,1,1,1,1,1,1,1,1,0}
	};//原始的图表,五行六列,其中 0 代表着空白的地方; 1 代表着墙;2 代表着人;
	              //3 代表着箱子;4 代表着箱子的终点位置。 
 
                      //图的变化要靠自己来编写数组,通过数字来进行图的构造。
int drawmain();
int tuidong();
int winshu();
 
int main()//主函数 
{
	while(1)
    {
    	system("cls");//对其进行清屏 
         drawmain();
         tuidong();
       
    }
    printf("shuchu \n");
	return 0;
 } 
//把图形刻画出来
 
int drawmain()
{	
  int i,j;
    winshu();//调用输赢的函数 
	for(i=0;i<9;i++)
	{
	   for(j=0;j<11;j++)
	   	   {
	   	   	   switch(map[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");
	}	   
} 
 
 //进行小人的移动,整个移动的过程就是数组变化的过程 
int tuidong()
{
	int count,caw=0;//行和列 
	int i,j,tui;
	for(i=0;i<9;i++){
		for (j=0;j<11;j++)
		{
			if(map[i][j]==2||map[i][j]==6)
			{
				count=i;
				caw=j;
			}
		}	   
	}

	tui=getch();//与getchar()有区别的是:getchar()输入一个字符后需要回车来进行下一个字符的输入,
	                //比较麻烦 ,getch()则不需要回车就能连续输入多个字符。 
    switch(tui)
	{//上
		case 'W':
		case 72:
			// 1.人的前面是空地;
		    // 2.人的前面是终点位置;
			// 3.人的前面是箱子
		    //3.1.箱子的前面是空地;
			//3.2.箱子的前面是终点位置。
		 if(map[count-1][caw]==0||map[count-1][caw]==4)
			{
				map[count][caw]-=2;
				map[count-1][caw]+=2;
			} 
		 else if(map[count-1][caw]==3||map[count-1][caw]==7)
			{
				if(map[count-2][caw]==0||map[count-2][caw]==4)
				{
			      map[count][caw]-=2;
				  map[count-1][caw]-=1;
				  map[count-2][caw]+=3;
				}
			}
		break;
	
//下 
	    case 'S':
	    case 80://键值 
	    	 if(map[count+1][caw]==0||map[count+1][caw]==4)
			{
				map[count][caw]-=2;
				map[count+1][caw]+=2;
			}
		
			 else if(map[count+2][caw]==0||map[count+2][caw]==4)
			{
			   	if(map[count+1][caw]==3||map[count+1][caw]==7)
				{
			      map[count][caw]-=2;
				  map[count+1][caw]-=1;
				  map[count+2][caw]+=3;
				}
			}
			break;
//左 
	    case 'A':
	    case 75:
	    		 if(map[count][caw-1]==0||map[count][caw-1]==4)
			{
				map[count][caw]-=2;
				map[count][caw-1]+=2;
			}
		
			  else if(map[count][caw-2]==0||map[count][caw-2]==4)
			{
			   	if(map[count][caw-1]==3||map[count][caw-1]==7)
				{
			      map[count][caw]-=2;
				  map[count][caw-1]-=1;
				  map[count][caw-2]+=3;
				}
			}
        	break;
//右 
	    case 'D':
		case 77:
		    	 if(map[count][caw+1]==0||map[count][caw+1]==4)
			{
				map[count][caw]-=2;
				map[count][caw+1]+=2;
			}
		
			  else if(map[count][caw+2]==0||map[count][caw+2]==4)
			{
			     if(map[count][caw+1]==3||map[count][caw+1]==7)
				{
			      map[count][caw]-=2;
				  map[count][caw+1]-=1;
				  map[count][caw+2]+=3;
				}
			}
		    break;
		     
}
} 

int winshu() 
{
	int k = 0;//初始化
	int j,i;
	for(i=0;i<9;i++)
	{
		for (j=0;j<11;j++)
		{
			 if(map[i][j]==3)
			     k++;
		}
	}
	if(k==0)
		printf("恭喜你,你赢了!\n");
}
 
  • 89
    点赞
  • 399
    收藏
    觉得还不错? 一键收藏
  • 38
    评论
当然,下面是一个简单的 C 语言推箱子游戏的完整代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAX_ROW 10 #define MAX_COL 10 // 定义地图元素类型 typedef enum { EMPTY, WALL, BOX, TARGET, PLAYER } element; // 定义地图结构体 typedef struct { int row; int col; } position; // 初始化地图 void initializeMap(element map[MAX_ROW][MAX_COL], position *player, position targets[], int *numTargets) { element initialMap[MAX_ROW][MAX_COL] = { {WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL}, {WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL}, {WALL, EMPTY, BOX, BOX, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL}, {WALL, EMPTY, BOX, TARGET, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL}, {WALL, PLAYER, EMPTY, EMPTY, EMPTY, TARGET, EMPTY, BOX, EMPTY, WALL}, {WALL, EMPTY, BOX, BOX, EMPTY, EMPTY, TARGET, TARGET, TARGET, WALL}, {WALL, EMPTY, BOX, EMPTY, TARGET, EMPTY, TARGET, PLAYER, EMPTY, WALL}, {WALL, EMPTY, BOX, EMPTY, TARGET, BOX, TARGET, TARGET, EMPTY, WALL}, {WALL, EMPTY, BOX, TARGET, TARGET, TARGET, TARGET, TARGET ,EMPTY ,WALL}, {WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL} }; for (int i = 0; i < MAX_ROW; i++) { for (int j = 0; j < MAX_COL; j++) { map[i][j] = initialMap[i][j]; if (map[i][j] == PLAYER) { player->row = i; player->col = j; } if (map[i][j] == TARGET) { targets[*numTargets].row = i; targets[*numTargets].col = j; (*numTargets)++; } } } } // 渲染地图 void renderMap(element map[MAX_ROW][MAX_COL]) { for (int i = 0; i < MAX_ROW; i++) { for (int j = 0; j < MAX_COL; j++) { switch(map[i][j]) { case WALL: printf("#"); break; case EMPTY: printf(" "); break; case BOX: printf("$"); break; case TARGET: printf("."); break; case PLAYER: printf("@"); break; } } printf("\n"); } } // 移动玩家 void movePlayer(element map[MAX_ROW][MAX_COL], position *player, int row, int col) { int newRow = player->row + row; int newCol = player->col + col; if (map[newRow][newCol] == EMPTY || map[newRow][newCol] == TARGET) { map[player->row][player->col] = (map[player->row][player->col] == TARGET) ? TARGET : EMPTY; player->row = newRow; player->col = newCol; map[player->row][player->col] = PLAYER; } else if (map[newRow][newCol] == BOX) { int newBoxRow = newRow + row; int newBoxCol = newCol + col; if (map[newBoxRow][newBoxCol] == EMPTY || map[newBoxRow][newBoxCol] == TARGET) { map[player->row][player->col] = (map[player->row][player->col] == TARGET) ? TARGET : EMPTY; player->row = newRow; player->col = newCol; map[player->row][player->col] = PLAYER; map[newBoxRow][newBoxCol] = (map[newBoxRow][newBoxCol] == TARGET) ? BOX : TARGET; } } } int main() { element map[MAX_ROW][MAX_COL]; position player; position targets[MAX_ROW * MAX_COL]; int numTargets = 0; initializeMap(map, &player, targets, &numTargets); while (1) { system("cls"); renderMap(map); if (numTargets == 0) { printf("You win!\n"); break; } char input = getch(); switch(input) { case 'w': movePlayer(map, &player, -1, 0); break; case 's': movePlayer(map, &player, 1, 0); break; case 'a': movePlayer(map, &player, 0, -1); break; case 'd': movePlayer(map, &player, 0, 1); break; case 'q': printf("Goodbye!\n"); exit(0); } } return 0; } ``` 这段代码实现了一个简单的推箱子游戏。你可以使用WASD键来控制玩家的移动,目标是将所有的箱子推到目标位置上。运行代码后,地图会显示在控制台上,直至完成游戏或按下Q键退出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值