在之前C语言基础入门48篇_31_二维数组应用之游戏中的碰撞检测(二维数组初始化边界,利用边界值判断是否可移动)中我们实现了游戏边界的检测功能,本篇将会使用结构体对游戏角色进行封装。
关于游戏封装的思考
1.原代码
#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结构体中。
2.封装后代码
#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;
}
运行结果:
3. 学习视频地址:使用结构体封装游戏角色