VS2019 C++ SFML 俄罗斯方块
SFML配置方法和素材:https://www.bilibili.com/video/av80956260
代码来源:https://www.bilibili.com/video/BV1uK4y187zT?from=search&seid=3828192988339151057
完整代码如下:
#include <SFML/Graphics.hpp> //图形
#include <SFML/Audio.hpp> //声音
#include <time.h>
using namespace sf;
//写在main函数外的是全局变量
Sound sou;
int blocks[7][4] = //7种方块,每种4格
{
1, 3, 5, 7, //1型
2, 4, 5, 7, //Z 1型
3, 5, 4, 6, //Z 2型
3, 5, 4, 7, //T型
2, 3, 5, 7, //L
3, 5, 7, 6, //J
2, 3, 4, 5, //田
}; //每种方块中方格的序号
const int ROW_COUNT = 20;
const int COL_COUNT = 10;
//游戏区域的表示
//若table[i][j]==0,表示第i行第j列这个格子是空白的
//table[i][j]==1,表示有方块,且这个格子是第一种方块(第一种形式的方块);等于2就是第二种形式的方块
int table[ROW_COUNT][COL_COUNT] = { 0 }; //方块运动的区域
int blockIndex; //当前方块的种类
struct Point //方格的坐标
{
int x;
int y;
} curBlock[4], bakBlock[4]; //当前的坐标,备份
//下降速度(单位秒)
const float SPEED_NORMAL = 0.5;
const float SPEED_QUICK = 0.05;
float delay = SPEED_NORMAL;
bool check() //检查当前方块合法性
{
for (int i = 0; i < 4; i++)
{
if (curBlock[i].x < 0
|| curBlock[i].x >= COL_COUNT
|| curBlock[i].y >= ROW_COUNT
|| table[curBlock[i].y][curBlock[i].x] != 0)
{
return false;
}
}
return true;
}
void moveLeftRight(int dx)
{
for (int i = 0; i < 4; i++)
{
bakBlock[