视频地址: 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;
}