C零基础视频-31-二维数组应用之游戏中的碰撞检测

目录

没有碰撞检测的版本

#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 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':
                ClearPlayer(nRow, nCol);
                nCol -= 1;
                ShowPlayer(nRow, nCol);
                break;
            case 'w':
                ClearPlayer(nRow, nCol);
                nRow -= 1;
                ShowPlayer(nRow, nCol);
                break;
            case 's':
                ClearPlayer(nRow, nCol);
                nRow += 1;
                ShowPlayer(nRow, nCol);
                break;
            case 'd':
                ClearPlayer(nRow, nCol);
                //改变坐标并移动、打印
                nCol += 1;
                ShowPlayer(nRow, nCol);
                break;
            default:
                break;
            }
        }

    }

    return 0;
}

碰撞检测

#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 };

int IsCanMove(int nToRow, int nToCol)
{
    if (g_chBackground[nToRow][nToCol] == 1)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

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 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;
}

猜你喜欢

转载自www.cnblogs.com/shellmad/p/11695586.html