源码:
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <Windows.h>
using namespace std;
#define HANG 9
#define LIE 9
int x, y;
class MyClass
{
public:
char map[30][30] = {
"######### ",
"#r # #*#",
"# OO# *#",
"# O $",
"### > *#",
" ## #",
" # # #",
" # ||||",
" ########"
};
void sm() {
cout << "welcome to 推箱子" << endl;
cout << "WASD-----------方向" << endl;
cout << "r--------------------人" << endl;
cout << "O------------------箱子" << endl;
cout << "*------------------目标点" << endl;
cout << "@@@@@ 作者:可爱的重云@B站 " << endl;
cout << "本人没写注释,让各位大佬见笑了。" << endl;
cout << "有一个_getch()的地方如果不是MSVC请自己自己删掉下划线" << endl;
}
void xs() {
int i, j;
for (i = 0; i < HANG; i++) {
for (j = 0; j < LIE; j++) {
cout << map[i][j];
}
cout << endl;
}
}
void yd(int x1, int y1, int x2, int y2) {
if (map[x][y] == 'r') {
if (map[x1][y1] == 'O')
{
if (map[x2][y2] == ' ')
{
map[x][y] = ' ';
map[x1][y1] = 'r';
map[x2][y2] = 'O';
}
if (map[x2][y2] == '*')
{
map[x][y] = ' ';
map[x1][y1] = 'r';
map[x2][y2] = '@';
}
}
if (map[x1][y1] == '@')
{
if (map[x2][y2] == ' ')
{
map[x][y] = ' ';
map[x1][y1] = 'R';
map[x2][y2] = 'O';
}
if (map[x2][y2] == '*') {
map[x][y] = ' ';
map[x1][y1] = 'R';
map[x2][y2] = '@';
}
}
if (map[x1][y1] == ' ') {
map[x][y] = ' ';
map[x1][y1] = 'r';
}
if (map[x1][y1] == '*') {
map[x][y] = ' ';
map[x1][y1] = 'R';
}
}
if (map[x][y] == 'R') {
if (map[x1][y1] == 'O') {
if (map[x2][y2] == ' ') {
map[x][y] = '*';
map[x1][y1] = 'r';
map[x2][y2] = 'O';
}
if (map[x2][y2] == '*') {
map[x][y] = '*';
map[x1][y1] = 'r';
map[x2][y2] = '@';
}
}
if (map[x1][y1] == '@') {
if (map[x2][y2] == ' ') {
map[x][y] = '*';
map[x1][y1] = 'R';
map[x2][y2] = 'O';
}
if (map[x2][y2] == '*') {
map[x][y] = '*';
map[x1][y1] = 'R';
map[x2][y2] = '@';
}
}
if (map[x1][y1] == ' ') {
map[x][y] = '*';
map[x1][y1] = 'r';
}
if (map[x1][y1] == '*') {
map[x][y] = '*';
map[x1][y1] = 'R';
}
}
}
void find() {
for (x = 0; x < HANG; x++) {
for (y = 0; y < LIE; y++) {
if (map[x][y] == 'r' || map[x][y] == 'R') {
return;
}
}
}
}
int over() {
int i, j;
for (i = 0; i < HANG; i++) {
for (j = 0; j < LIE; j++) {
if (map[i][j] == 'O') {
return 0;
}
}
}
return 1;
}
};
int main(void) {
MyClass myclass;
system("color b");
char fx;
myclass.sm();
myclass.xs();
while (1)
{
myclass.find();
fx = _getch();
switch (fx) {
case 72:
case 'w':
case 'W':
myclass.yd(x - 1, y, x - 2, y); break;
case 80:
case 's':
case 'S':
myclass.yd(x + 1, y, x + 2, y); break;
case 75:
case 'A':
case 'a':
myclass.yd(x, y - 1, x, y - 2); break;
case 77:
case 'D':
case 'd':
myclass.yd(x, y + 1, x, y + 2); break;
}
system("CLS");
myclass.sm();
myclass.xs();
if (myclass.over() == 1) {
MessageBox(NULL, L"你赢了", L"推箱子Game", MB_OK);
break;
}
}
return 0;
}//完成于2022/8/21
运行结果: