C零基础视频-41-使用结构体封装游戏角色

本文探讨了游戏开发中封装的重要性,通过实例展示了如何将游戏角色的坐标信息封装到结构体中,从而提高代码的可读性和可维护性。文章详细介绍了封装前后的代码对比,以及如何在游戏循环中使用封装后的结构体进行角色移动。
摘要由CSDN通过智能技术生成


视频地址: https://www.bilibili.com/video/av73896303

关于游戏封装的思考

原代码:

#include <windows.h>
#include <conio.h>
#include <stdio.h>

void MoveCursorTo(int nRow, int nCol)
{
    COORD crdLocation;
    crdLocation.X = 2 * nCol;
    crdLocation.Y = nRow;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}

char g_chBackground[20][20] = { 0 };

void InitBackground()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (nRow == 0
                || nCol == 0
                || nRow == 19
                || nCol == 19)
            {
                g_chBackground[nRow][nCol] = 1;
            }
            else
            {
                g_chBackground[nRow][nCol] = 0;
            }
        }
    }
}

void ShowBackGround()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (g_chBackground[nRow][nCol] == 1)
            {
                MoveCursorTo(nRow, nCol);
                printf("■");
            }
            else
            {

            }
        }
    }
}

void ClearPlayer(int nRow, int nCol)
{
    MoveCursorTo(nRow, nCol);
    printf(" ");
}

void ShowPlayer(int nRow, int nCol)
{
    MoveCursorTo(nRow, nCol);
    printf("×");
}

int IsCanMove(int nToRow, int nToCol)
{
    if (g_chBackground[nToRow][nToCol] == 1)
    {
        return 0; //不能移动
    }
    else
    {
        return 1; //可以移动
    }
}

int main(int argc, char* argv[])
{
    char chInput = 0;
    InitBackground();
    ShowBackGround();

    int nRow = 10;
    int nCol = 10;

    ShowPlayer(nRow, nCol);

    while (1)
    {
        if (_kbhit() != 0)
        {
            chInput = _getch();
            switch (chInput)
            {
            case 'a':
                if (IsCanMove(nRow, nCol - 1))
                {
                    ClearPlayer(nRow, nCol);
                    nCol -= 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            case 'w':
                if (IsCanMove(nRow - 1, nCol))
                {
                    ClearPlayer(nRow, nCol);
                    nRow -= 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            case 's':
                if (IsCanMove(nRow + 1, nCol))
                {
                    ClearPlayer(nRow, nCol);
                    nRow += 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            case 'd':
                if (IsCanMove(nRow, nCol + 1))
                {
                    ClearPlayer(nRow, nCol);
                    //改变坐标并移动、打印
                    nCol += 1;
                    ShowPlayer(nRow, nCol);
                }
                break;
            default:
                break;
            }
        }

    }
    return 0;
}

我们选择封装玩家,将行、列坐标封装到player结构体中。

封装后代码

#include <windows.h>
#include <conio.h>
#include <stdio.h>

struct tagPlayer{
    int nRow;
    int nCol;
};

void MoveCursorTo(int nRow, int nCol)
{
    COORD crdLocation;
    crdLocation.X = 2 * nCol;
    crdLocation.Y = nRow;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}

char g_chBackground[20][20] = { 0 };

void InitBackground()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (nRow == 0
                || nCol == 0
                || nRow == 19
                || nCol == 19)
            {
                g_chBackground[nRow][nCol] = 1;
            }
            else
            {
                g_chBackground[nRow][nCol] = 0;
            }
        }
    }
}

void ShowBackGround()
{
    for (size_t nRow = 0; nRow < 20; nRow++)
    {
        for (size_t nCol = 0; nCol < 20; nCol++)
        {
            if (g_chBackground[nRow][nCol] == 1)
            {
                MoveCursorTo(nRow, nCol);
                printf("■");
            }
            else
            {

            }
        }
    }
}

void ClearPlayer(tagPlayer *pPlayer)
{
    MoveCursorTo(pPlayer->nRow, pPlayer->nCol);
    printf(" ");
}

void ShowPlayer(tagPlayer *pPlayer)
{
    MoveCursorTo(pPlayer->nRow, pPlayer->nCol);
    printf("×");
}

int IsCanMove(int nRow, int nCol)
{
    if (g_chBackground[nRow][nCol] == 1)
    {
        return 0; //不能移动
    }
    else
    {
        return 1; //可以移动
    }
}

int main(int argc, char* argv[])
{
    char chInput = 0;
    InitBackground();
    ShowBackGround();

    tagPlayer player = { 10, 10 };

    ShowPlayer(&player);

    while (1)
    {
        if (_kbhit() != 0)
        {
            chInput = _getch();
            switch (chInput)
            {
            case 'a':
                if (IsCanMove(player.nRow, player.nCol - 1))
                {
                    ClearPlayer(&player);
                    player.nCol -= 1;
                    ShowPlayer(&player);
                }
                break;
            case 'w':
                if (IsCanMove(player.nRow - 1, player.nCol))
                {
                    ClearPlayer(&player);
                    player.nRow -= 1;
                    ShowPlayer(&player);
                }
                break;
            case 's':
                if (IsCanMove(player.nRow + 1, player.nCol))
                {
                    ClearPlayer(&player);
                    player.nRow += 1;
                    ShowPlayer(&player);
                }
                break;
            case 'd':
                if (IsCanMove(player.nRow, player.nCol + 1))
                {
                    ClearPlayer(&player);
                    //改变坐标并移动、打印
                    player.nCol += 1;
                    ShowPlayer(&player);
                }
                break;
            default:
                break;
            }
        }

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值