C++推箱子游戏(可以撤回)

C++推箱子小游戏制作

	期末了,需要交一个C++大作业,就准备了一下写了一个推箱子小游戏,内容借鉴于网友,
不过进行了一些修改添加了一些新的内容,下面放出源码和效果.

第一关
第二关
一共有五个关卡,具体内容在下面代码中的注释

/*-----推箱子游戏-----created by 汤圆*/
/*
	======待完成项目======
	一.在死局的情况下重开关卡
	二.自动生成新地图(目前只有5张地图,自己可以通过给MAP添加数组进行新地图的添加)
*/
//障碍为█,箱子为□,玩家为♀,目的地为◎,到达目的地之后为★,空地为" ",人在目的地上表示☆;
#include "pch.h"
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
#include <string.h>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
#define ROW 12
#define COL 12
char but;//按键
//定义结构体玩家位置,x,y为玩家的坐标
struct pos {
	int x;
	int y;
}player;
struct MAP {
	const int row;
	const int col;
	char map[ROW][COL];
};
//颜色
void color(int x)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
//地图
struct MAP map1[5] = { { 8, 8, { { 0, 0, 1, 1, 1, 0, 0, 0 },  //地图一
{ 0, 0, 1, 3, 1, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 2, 0, 2, 3, 1 },
{ 1, 3, 0, 2, 4, 1, 1, 1 },
{ 1, 1, 1, 1, 2, 1, 0, 0 },
{ 0, 0, 0, 1, 3, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 1, 0, 0 } } },
{ 9, 9, { {1,1,1,1,1,0,0,0,0},   //地图二
{1,4,0,0,1,0,0,0,0},
{1,0,2,2,1,0,1,1,1},
{1,0,2,0,1,0,1,3,1},
{1,1,1,0,1,1,1,3,1},
{0,1,1,0,0,0,0,3,1},
{0,1,0,0,0,1,0,0,1},
{0,1,0,0,0,1,1,1,1},
{0,1,1,1,1,1,0,0,0} } },
{ 7, 10, { { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },  //地图三
{ 0, 1, 0, 0, 0, 0, 0, 1, 1, 1 },
{ 1, 1, 2, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 4, 0, 2, 0, 0, 2, 0, 1 },
{ 1, 0, 3, 3, 1, 0, 2, 0, 1, 1 },
{ 1, 1, 3, 3, 1, 0, 0, 0, 1, 0 },
{0,1,1,1,1,1,1,1,1,0} } },
{ 8, 6, { { 0, 1, 1, 1, 1, 0 },    //地图四
{ 1, 1, 0, 0, 1, 0 },
{ 1, 4, 2, 0, 1, 0 },
{ 1, 1, 2, 0, 1, 1 },
{ 1, 1, 0, 2, 0, 1 },
{ 1, 3, 2, 0, 0, 1 },
{ 1, 3, 3, 5, 3, 1 },
{ 1, 1, 1, 1, 1, 1 } } },
{ 8, 8, { { 0, 1, 1, 1, 1, 1, 0, 0 },  //地图五
{ 0, 1, 0, 4, 0, 1, 1, 1 },
{ 1, 1, 0, 1, 2, 0, 0, 1 },
{ 1, 0, 5, 3, 0, 3, 0, 1 },
{ 1, 0, 0, 2, 2, 0, 1, 1 },
{ 1, 1, 1, 0, 1, 3, 1, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 0 } } } };   //在这里增加地图,行数,列数,以及地图元素

/*
*
*
*生成地图
*
*
*/

void mapInit(char(*map)[COL], const int *row, const int *col) {
	for (int i = 0; i < *row; i++) {
		cout << "\t\t\t\t\t";
		for (int j = 0; j < *col; j++) {
			switch ((map[i][j])) {
			case 0: color(15); cout << "  ";
				break;
			case 1:color(27); cout << "█";
				break;
			case 2:color(4); cout << "□";
				break;
			case 3:color(5); cout << "◎";
				break;
			case 4:color(6); cout << "♀";
				break;
			case 5:color(8); cout << "★";
				break;
			case 7:color(9); cout << "☆";
				break;
			}
		}
		cout << endl;
	}
	color(27);
	cout << "\n\t\t\t\t\t█";
	color(15);
	cout << "障碍\n";
	color(4);
	cout << "\t\t\t\t\t□"; 
	color(15);
	cout << "箱子\n";
	color(5); 
	cout << "\t\t\t\t\t◎";
	color(15);
	cout << "目标点\n";
	color(6);
	cout << "\t\t\t\t\t♀";
	color(15);
	cout << "人物\n";
	color(8);
	cout << "\t\t\t\t\t★";
	color(15);
	cout << "箱子在目标点上\n";
	color(9);
	cout << "\t\t\t\t\t☆";
	color(15);
	cout << "人物在目标点上\n";




	cout << "\n\t\t\t\t\tW A S D控制人物移动\n\t\t\t\t\tR键撤销移动,ESC键退出" << endl;
}

/*
*
*
*锁定玩家位置
*
*
*/
void lockPlayerPosition(char(*map)[COL], const int *row, const int *col, int *x, int *y) {
	for (int i = 0; i < *row; i++) {
		for (int j = 0; j < *col; j++) {
			if ((map[i][j]) == 4 || (map[i][j] == 7)) {
				*x = i;
				*y = j;
				return;
			}
		}
	}
}
/*
*
*
*判断是否胜利
*
*
*/
int isWin(char(*map)[COL], const int*row, const int *col) {
	int count = 0;
	for (int i = 0; i < *row; i++) {
		for (int j = 0; j < *col; j++) {
			if (map[i][j] == 2 || map[i][j] == 7) {
				return 0;
			}
		}
	}
	return 1;
}
/*
*
*
*控制玩家移动
*
*
*/
void playerMov(char(*map)[COL], int *x, int *y) {
	char CheckBut = but;
	switch (but = _getch()) {
		//退出
	case 'Q':
	case 'q':
	case 27: {
		exit(1);
	}
		//撤回
	case 'R':
	case 'r': { 
		if (CheckBut == 0) {
			cout << "\n之前没有进行任何移动操作,请移动之后再试" << endl;
		}
		//上一次操作按的是W键
		else if (CheckBut == 'w') {
			//(1)上次人物要到的下一个位置是空地
			if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 4&& map[(*x) - 1][*y] != 2 && map[(*x) - 1][*y] != 5) {
				map[*x][*y] = 0;
				map[(*x) + 1][*y] = 4;
				++*x;
			}
			//(2)上次人物要到的下一个位置是空目的地
			else if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 7&&map[(*x) - 1][*y] != 5 && map[(*x) - 1][*y] != 2) {
				map[*x][*y] = 3;
				map[(*x) + 1][*y] =4 ;
				++*x;
			}
			//(3)上次人物要推着箱子到一个空地
			else if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 4 && map[(*x) - 1][*y] == 2) {
				map[*x][*y] = 2;
				map[(*x) - 1][(*y)] = 0;
				map[(*x) +1 ][(*y)] =4 ;
				
			}
			//(4)上次人物要推着箱子到一个空目的地
			else if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 4 && map[(*x) - 1][*y] == 5) {
				map[*x][*y] = 2;
				map[(*x) - 1][*y] = 3;
				map[(*x) + 1][*y] = 4;
			}
			//(5)上次人物要推着目的地中的箱子到空地
			else if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 7 && map[(*x) - 1][*y] == 2) {
				map[*x][*y] = 5;
				map[(*x) - 1][*y] = 0;
				map[(*x) +1][*y] = 4;
				
			}
			//(6)上次人物要推着目的地中的箱子到新的目的地中
			else if (map[(*x) - 1][*y] == 5 && map[*x][*y] == 7 && map[(*x) + 1][*y] == 0) {
				map[*x][*y] = 5;
				map[(*x) - 1][*y] = 3;
				map[(*x) +1][*y] = 4;
				
			}
			//(7)上次人物在目的地中到空地
			else if (map[(*x) + 1][*y] == 3 && map[*x][*y] == 4&&map[(*x) - 1][*y] != 5) 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值