#include <Windows.h>
#include <iostream>
using namespace std;
#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code)&0X8000?1:0)
//场景内节点相关结构体
struct SData
{
int nX;
int nY;
int nXB;
int nYB;
};
int main()
{
//游戏地图数组
int nGameMap[15][11] =
{
1,1,1,1,1,1,1,1,1,1,1,
1,0,2,2,2,2,2,2,2,0,1,
1,0,2,2,2,2,2,2,2,0,1,
1,0,2,2,2,2,2,2,2,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,
};
//创建子弹结构体
SData bullet[100];
//子弹坐标
bullet[0].nX = 5;
bullet[0].nY = 12;
//子弹之前坐标
bullet[0].nXB = 5;
bullet[0].nYB = 12;
//创建箱子结构体
SData arrPlay[3];
//玩家坐标
arrPlay[0].nX = 4;
arrPlay[0].nY = 13;
arrPlay[1].nX = 5;
arrPlay[1].nY = 13;
arrPlay[2].nX = 6;
arrPlay[2].nY = 13;
//判断子弹是否发射
int fight = 0;
//判断时间然后增加层数
int time = 0;
while (true)
{
time++;
CONSOLE_CURSOR_INFO cursor; //定义结构体 控制台光标信息
cursor.bVisible = 0; //设置不显示
cursor.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); //设置光标信息
COORD coord = { 0,0 };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
Sleep(100);
//记录玩家之前坐标
for (int i = 0; i < 3; i++)
{
arrPlay[i].nXB = arrPlay[i].nX;
arrPlay[i].nYB = arrPlay[i].nY;
}
//记录子弹之前坐标
bullet[0].nXB = bullet[0].nX;
bullet[0].nYB = bullet[0].nY;
//控制玩家左移动
if (KEY_DOWN(VK_LEFT))
{
for (int i = 0; i < 3; i++)
{
arrPlay[i].nX--;
}
if (0 == fight)
{
bullet[0].nX--;
}
}
//控制玩家右移动
if (KEY_DOWN(VK_RIGHT))
{
for (int i = 0; i < 3; i++)
{
arrPlay[i].nX++;
}
if (0 == fight)
{
bullet[0].nX++;
}
}
//上键发射子弹
if (KEY_DOWN(VK_UP))
{
fight = 1;
}
//发射后子弹持续上移
if (1 == fight)
{
bullet[0].nY--;
}
//碰到盒,盒销毁,子弹复位
if (2 == nGameMap[bullet[0].nY][bullet[0].nX])
{
nGameMap[bullet[0].nY][bullet[0].nX] = 0;
bullet[0].nX = arrPlay[1].nX;
bullet[0].nY = arrPlay[1].nY - 1;
fight = 0;
}
//碰到墙,子弹复位
if (1 == nGameMap[bullet[0].nY][bullet[0].nX])
{
bullet[0].nX = arrPlay[1].nX;
bullet[0].nY = arrPlay[1].nY - 1;
fight = 0;
}
//玩家撞墙时返回之前坐标
if (1 == nGameMap[13][arrPlay[0].nX] || 1 == nGameMap[13][arrPlay[2].nX])
{
for (int j = 0; j < 3; j++)
{
arrPlay[j].nX = arrPlay[j].nXB;
}
bullet[0].nX = bullet[0].nXB;
}
//一定时间增加一层砖块,同时让已生成砖块下移
if (0 == time % 50)
{
for (int i = 13; i > 0; i--)
{
for (int j = 10; j > 0; j--)
{
if (2 == nGameMap[i][j])
{
nGameMap[i + 1][j] = 2;
nGameMap[i][j] = 0;
}
}
}
for (int i = 2; i < 9; i++)
{
nGameMap[1][i] = 2;
}
}
//渲染地图
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 11; j++)
{
//判断玩家位置
bool bPlaySet = false;
for (int k = 0; k < sizeof(arrPlay) / sizeof(SData); k++)
{
if (i == arrPlay[k].nY && j == arrPlay[k].nX)
{
bPlaySet = true;
}
}
//初始化地图边界
if (1 == nGameMap[i][j])
{
cout << "■";
}
//初始化方块
else if (2 == nGameMap[i][j])
{
cout << "盒";
}
//初始化子弹
else if (i == bullet[0].nY && j == bullet[0].nX)
{
cout << "↑";
}
//初始化玩家
else if (bPlaySet)
{
cout << "台";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
}
利用VS中C++控制台实现小游戏---打砖块
最新推荐文章于 2024-07-19 14:01:03 发布