推箱子游戏的C++实现

// 1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <stdio.h>//■
#include <cstdlib>
#include <conio.h>
using std::cout;
const int row = 12;
const int col = 13;
int x = 1;
int y = 11;
int map[row][col] = {
    {2,2,2,2,2,2,2,2,2,2,2,2,2},
    {2,0,0,0,0,0,0,0,0,0,0,5,2},
    {2,0,0,0,0,0,0,0,0,0,3,0,2},
    {2,0,0,0,0,0,0,0,0,0,0,0,2},
    {2,0,0,0,0,0,0,0,1,0,0,0,2},
    {2,0,0,0,0,0,0,0,0,0,0,0,2},
    {2,0,0,0,0,0,0,0,0,0,0,0,2},
    {2,0,0,0,0,0,0,0,0,0,0,0,2},
    {2,0,0,0,0,0,0,0,0,0,0,0,2},
    {2,0,0,0,0,0,0,0,0,0,0,0,2},
    {2,0,0,0,0,0,0,0,0,0,0,0,2},
    {2,2,2,2,2,2,2,2,2,2,2,2,2},
};

void draw() {
    int i;
    for (i = 0; i < 12; i++)
    {
        for (int j = 0; j < 13; j++)
        {
            switch (map[i][j])
            {
            case 0:
            {
                cout << "  ";//0代表可以走的空格
                break;
            }
            case 1:
            {
                cout << "★";//1代表箱子,用我就★代替了
                break;
            }
            case 2:
            {
                cout << "■";//2代表了墙,不能逾越
                break;
            }
            case 3:
            {
                cout << "☆";
                break;
            }

            case 5:
            {
                cout << "♀";//5代表了人,可移动
                break;
            }
            }
        }
        cout << "\n";
    }
}

void move(int _x, int _y) {
    if (map[x + _x][y + _y] == 2 || map[x + _x][y + _y] == 3 ||
        map[x + _x][y + _y] == 5) {//判断边界人不能走出墙外
        return;
    }
    if (map[x + _x][y + _y] == 1) {
        if (map[x + 2 * _x][y + 2 * _y] == 2) {
            return;
        }
        map[x + _x][y + _y] = 0;
        map[x + 2 * _x][y + 2 * _y] = 1;
        move(_x, _y);
        return;
    }
    map[x][y] = 0;
    x += _x;
    y += _y;
    map[x][y] = 5;
    draw();

}
void step(char o) {
    switch (o) {
    case 'w': {//就像cf,cs那样,wsad代表上下左右移动
        
        move(-1, 0);
        break;
    }
    case 's': {
        move(1, 0);
        break;
    }
    case 'd': {
        move(0, 1);
        break;
    }
    case 'a': {
        move(0, -1);
        break;
    }
    }
}

int main()
{
    while (true) {        
        draw();//根据二维数组的值来进行绘图
        step(_getch());//直接获取屏幕输入
        system("cls");//清屏来实现动画效果
    }
}
简易推箱子 #include "Map.h" #include <conio.h> int XDest = 4; int YDest = 4; void CMap::Init() { int i,j; CPoint cPoint; CSprite *pSphte; CPlayer cPlay; CBox cBox; CDest cDest; int XPlayer = 4; int YPlayer = 6; int XBox = 4; int YBox = 5; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (i == 0 || j == 0 || i == 9 || j == 9) { cPoint.SetPoint(i, j); pSphte = new CWall(cPoint); m_nMap[i][j] = pSphte; } else if (i == XPlayer && j == YPlayer) { cPoint.SetPoint(i,j); pSphte = new CPlayer(cPoint); m_nMap[i][j] = pSphte; } else if (i == XBox && j == YBox) { cPoint.SetPoint(i,j); pSphte = new CBox(cPoint); m_nMap[i][j] = pSphte; } else if (i == XDest && j == YDest) { cPoint.SetPoint(i,j); pSphte = new CDest(cPoint); m_nMap[i][j] = pSphte; } else { cPoint.SetPoint(i, j); pSphte = new CSpace(cPoint); m_nMap[i][j] = pSphte; } } cout<<endl; } } void CMap::Draw() { int i,j; for (i=0; i<10; i++) { for (j=0; j<10; j++) { cout<<m_nMap[i][j]->GetSprite(); } cout<<endl; } } void CMap::Logic() { int n = getch(); //获取输入的数字 char ch = char(n);//将获取到的数字进行强制类型转换 CPlayer *pPlayer = GetPlayer(); int XPlayer = pPlayer->GetX(); int YPlayer = pPlayer->GetY(); CBox *pBox = GetBox(); int XBox = pBox->GetX(); int YBox = pBox->GetY(); CDest *pDest = GetDest(); switch(ch) { case 'a': { int yPlayer = pPlayer->GetY(); YPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer++; YBox++; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'd': { int yPlayer = pPlayer->GetY(); YPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer--; YBox--; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'w': { int xPlayer = pPlayer->GetX(); XPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer++; XBox++; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; case 's': { int xPlayer = pPlayer->GetX(); XPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer--; XBox--; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; default: break; } if ((pPlayer->GetX() == XDest && pPlayer->GetY() == YDest) || (pBox->GetX() == XDest && pBox->GetY() == YDest)) { int i,j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { m_nMap[i][j]->SetSprite(" "); } } } } else if (m_nMap[XDest][YDest]->GetSprite() == " ") { m_nMap[XDest][YDest]->SetSprite("◎"); } if (m_nMap[XDest][YDest]->GetSprite() == "■") { m_nMap[XDest][YDest]->SetSprite("●"); // cout<<"游戏结束"<<endl; } if (pBox->GetX() != XDest || pBox->GetY() != YDest) { pBox->SetSprite("■"); } } CPlayer *CMap::GetPlayer(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "♀") { return (CPlayer *)m_nMap[i][j]; } } } } CBox *CMap::GetBox(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "■" || m_nMap[i][j]->GetSprite() == "●") { return (CBox *)m_nMap[i][j]; } } } } CDest *CMap::GetDest(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { return (CDest *)m_nMap[i][j]; } } } }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dareu_4523

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

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

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

打赏作者

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

抵扣说明:

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

余额充值