C++实现俄罗斯方块(实现俄罗斯方块(linux版本)版本)
本文实例为大家分享了C++实现俄罗斯方块的具体代码,供大家参考,具体内容如下
主程序主程序
RussiaBlock.cpp
//
// Created by adl on 2020/7/18.
//
#include "Block.h"
#include "Table.h"
#include <thread>
#include <mutex>
#include "hierarchical_mutex.h"
#include "fstream"
using namespace std;
thread_local uint64_t
hierarchical_mutex::this_thread_hierarchical_value = ULONG_MAX;
int main(int argc, char **argv) {
int level = 1;
if (argc == 2) {
if ((level = atoi(argv[1])) == 0) {
cerr << "./a.out number " << endl;
exit(-1);
}
}
static int flag = 1;//全局变量
static Table tab(20, 20, level); //构造一个15,20的棋盘
static Block bl; //构造一个落下方块
hierarchical_mutex table_mtx(2);
hierarchical_mutex mtx(1);
thread getkey([&]() {
unsigned char buf[2];
struct termios saveterm, nt;
fd_set rfds, rs;
struct timeval tv;
int i = 0, q, r, fd = 0;//标准输入
tcgetattr(fd, &saveterm);
nt = saveterm;
nt.c_lflag &= ~ECHO;
nt.c_lflag &= ~ISIG;
nt.c_lflag &= ~ICANON;
tcsetattr(fd, TCSANOW, &nt);
FD_ZERO(&rs);
FD_SET(fd, &rs);
tv.tv_usec = 0;
tv.tv_sec = 0;
while (1) {
read(0, buf, 1);
buf[1] = ' ';
r = select(fd + 1, &rfds, nullptr, nullptr, &tv);
if (r < 0) {
write(fileno(stderr), "select error.", sizeof("select error."));
}
rfds = rs;
std::unique_lock<hierarchical_mutex> table_lock(table_mtx);
//上下左右
switch (buf[0]) {
case 'A': {
//旋转
tab.clr_block(bl);//
if (bl.get_type() == 5)continue;
bl.rotate();