C语言推箱子小游戏

24 篇文章 2 订阅
4 篇文章 0 订阅
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int x = 0, y = 0;
// save map
int map[10][10] = { 0 };
// map 1
int map_1[10][10] = {
	{0,0,1,1,1,0,0,0},
	{0,0,1,4,1,0,0,0},
	{0,0,1,0,1,1,1,1},
	{1,1,1,0,0,2,4,1},
	{1,4,2,2,0,1,1,1},
	{1,1,1,3,2,1,0,0},
	{0,0,0,1,4,1,0,0},
	{0,0,0,1,1,1,0,0}
};
// map 2
int map_2[10][10] = {
	{0,0,0,1,1,1,1,1,1,1},
	{0,0,1,1,0,0,1,0,3,1},
	{0,0,1,0,0,0,1,0,0,1},
	{0,0,1,2,0,2,0,2,0,1},
	{0,0,1,0,2,1,1,0,0,1},
	{1,1,1,0,2,0,1,0,1,1},
	{1,4,4,4,4,4,0,0,1,0},
	{1,1,1,1,1,1,1,1,1,0}
};
// win?
int finish();
// move man
void move(int x1, int y1, int x2, int y2);
// print map
int render();
// find man
void find();
// choose map 1 or 2
void setmap(int n);
// help
void help(void);

int main(void){
	int n;
	char dir;
	char c;

	help();
	printf("Please choose (1 or 2, q to exit): ");
	while(scanf("%d", &n)){
		getchar();
		// n==0, exit
		if(n == 0){
			printf("game over\n");
			break;
		}
		system("cls");
		if(n == 1 || n == 2){
			setmap(n);	// choose map
			render();
			while((dir = getch()) != EOF){
				system("cls");
				find();
				switch(dir){
					case 'w': move(x - 1, y, x - 2, y); break;
					case 's': move(x + 1, y, x + 2, y); break;
					case 'a': move(x, y - 1, x, y - 2); break;
					case 'd': move(x, y + 1, x, y + 2); break;
					case 'r': setmap(n); break;
					case 'q': return 0;		  
				}
				render();
				if(finish()){
					printf("You Win!\n");
					printf("Please choose (1 or 2, q to exit): ");
					break;
				}
			}
		}
		else{
			printf("Please try again.\n");
			printf("Please choose (1 or 2, q to exit): ");
		}
	}
	system("pause");
	return 0;
}
void help(void)
{
	printf("*************Weclome to the box game*************\n"
			"*\t\t\t\t\t\t*\n"
			"*\t-------------------------------\t\t*\n"
			"*\t\tPowered By X\t\t\t*\n"
			"*\t-------------------------------\t\t*\n"
			"*\t\t\t\t\t\t*\n"
			"*\t\t+ : man\t\t\t\t*\n"
			"*\t\t$ : address\t\t\t*\n"
			"*\t\t@ : box\t\t\t\t*\n"
			"*\t\t\t\t\t\t*\n"
			"*\t\thelp\t\t\t\t*\n"
			"*\t\tmove : wasd\t\t\t*\n"
			"*\t\texit : q\t\t\t*\n"
			"*\t\trestart : r\t\t\t*\n"
			"*************************************************\n");
}

void move(int x1, int y1, int x2, int y2){
	if(map[x][y] == 3)	// find man
	{
		// In front of the person is the box. The Box is on the space
		if(map[x1][y1] == 2){
			// In front of the box is space
			if(map[x2][y2] == 0) {
				map[x][y] = 0;
				map[x1][y1] = 3;
				map[x2][y2] = 2;
			}
			// In front of the box is the position
			else if(map[x2][y2] == 4){
				map[x][y] = 0;
				map[x1][y1] = 3;
				map[x2][y2] = 5;
			}
		}
		// In front of you is the box. The Box is in position
		else if(map[x1][y1] == 5){
			// In front of the box is space
			if(map[x2][y2] == 0) {
				map[x][y] = 0;
				map[x1][y1] = 6;
				map[x2][y2] = 2;
			}
			// In front of the box is the position
			else if(map[x2][y2] == 4){
				map[x][y] = 0;
				map[x1][y1] = 6;
				map[x2][y2] = 5;
			}
		}
		// Blank in front of the person
		if(map[x1][y1] == 0){
			map[x1][y1] = 3;
			map[x][y] = 0;
		}
		else if(map[x1][y1] == 4){
			map[x][y] = 0;
			map[x1][y1] = 6;
		}
	}
	else if(map[x][y] == 6) // People in position
	{
		// Before the position is the box, the box is on the space
		if(map[x1][y1] == 2)
		{
			// Boxes are preceded by Spaces
			if(map[x2][y2] == 0){
				map[x][y] = 4;
				map[x1][y1] = 3;
				map[x2][y2] = 2;
			}
			// In front of the box is the position
			else if(map[x2][y2] == 4){
				map[x][y] = 4;
				map[x1][y1] = 3;
				map[x2][y2] = 5;
			}
		}
		// Before the position is the box, the box is in the position
		else if(map[x1][y1] == 5){
			// Boxes are preceded by Spaces
			if(map[x2][y2] == 0){
				map[x][y] = 4;
				map[x1][y1] = 6;
				map[x2][y2] = 2;
			}
			// In front of the box is the position
			else if(map[x2][y2] == 4){
				map[x][y] = 4;
				map[x1][y1] = 6;
				map[x2][y2] = 5;
			}
		}
		// Position is in front of people
		if(map[x1][y1] == 4){
			map[x][y] = 4;
			map[x1][y1] = 6;
		}
		// People are preceded by Spaces
		else if(map[x1][y1] == 0){
			map[x][y] = 4;
			map[x1][y1] = 3;
		}
	}
}

void find(void){
	for(x = 0; x < 10; x++){
		for(y = 0; y < 10; y++){
			if(map[x][y] == 3 || map[x][y] == 6){
				return;
			}
		}
	}
}

int render(void){
	for(x = 0; x < 10; x++){
		for(y = 0; y < 10; y++){
			if(map[x][y] == 1){
				printf("*");	// The output frame
			}
			else if(map[x][y] == 3){
				printf("+");	// Output character location
			}
			else if(map[x][y] == 2){
				printf("@");	// Output box
			}
			else if(map[x][y] == 4){
				printf("$");	// Output destination
			}
			else if(map[x][y] == 0){
				printf(" ");	// Output space
			}
			else if(map[x][y] == 5){
				printf("@");	// Output the icon after reaching the destination location
			}
			else if(map[x][y] == 6){
				printf("+");	// People arrvice at the destination after the output character
			}
		}
		printf("\n");
	}
	return 0;
}

void setmap(int n){
	if(n == 1){
		memcpy(map, map_1, sizeof(map_1));
	}
	else if(n == 2){
		memcpy(map, map_2, sizeof(map_2));
	}
}

int finish(){
	for(x = 0; x < 10; x++){
		for(y = 0; y < 10; y++){
			if(map[x][y] == 2)
				return 0;
		}
	}
	return 1;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小老弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值