俄罗斯方块c++linux版本

这篇博客介绍了如何使用C++在Linux环境下编写俄罗斯方块游戏,涉及了random数生成、成员指针函数表、互斥锁的权重加锁策略防止死锁、线程内的函数、select监控标准输入、终端控制以及毫秒级睡眠等功能的实现。作者反思了没有使用继承来优化方块类设计的问题。
摘要由CSDN通过智能技术生成

主程序
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] = '\0';
            r = select(fd + 1, &rfds, nullptr, nullptr, &tv);
            if (r < 0) {
   
                write(fileno(stderr), "select error.\n", sizeof("select error.\n"));
            }
            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();
                    if (tab.set_block(bl) == -1) {
   
                        bl.rotate_back();
                        tab.set_block(bl);
                        continue;
                    }
                    break;
                }
                case 'B': {
   
                    //向下(加速)
                    tab.clr_block(bl);
                    bl.move(Block::DOWN);
                    if (tab.set_block(bl) == -1) {
   
                        bl.move(Block::UP);
                        tab.set_block(bl);
                    }
                    break;
                }
                case 'C': {
   
                    /*向右*/
                    tab.clr_block(bl);
                    bl.move(Block::RIGHT);
                    if (tab.set_block(bl) == -1) {
   
                        bl.move(Block::LEFT);
                        tab.set_block(bl);
                    }
                    break;
                }
                case 'D': {
   
                    //左
                    tab.clr_block(bl);
                    bl.move(Block::LEFT);
                    if (tab.set_block(bl) == -1) {
   
                        bl.move(Block::RIGHT);
                        tab.set_block(bl);
                    }
                    break;
                }
                default:
                    break;
            }
            table_lock.unlock();
            std::unique_lock<hierarchical_mutex> lock(mtx);
            if (flag == 2 || buf[0] == 113) {
   

                lock.unlock();

                tcsetattr(fd, TCSANOW, &saveterm);
                std::cout << "game over" << std::endl;
                exit(0);
            } else {
   
                lock.unlock();
            }
        }

        tcsetattr(0, TCSANOW, &saveterm);
    });
    thread printloop([&]() {
   
        while (1) {
   
            system("clear");
            std::unique_lock<hierarchical_mutex> table_lock(table_mtx);
            tab.paint();
            table_lock.unlock();
            this_thread::sleep_for(std::chrono::milliseconds(200 / tab.getLevel()));
            std::unique_lock<hierarchical_mutex> lock(mtx);

            if (flag == 2) {
   
                cout << "任意键退出" << endl;
                lock.unlock();
                break;
            } else
                lock.unlock();
        }
    });
    getkey.detach();
    printloop.detach();
    int dir, i, c;
    while (true) {
   
        //生成方块
        std::unique_lock<hierarchical_mutex> table_lock(table_mtx);
//        std::unique_lock<std::mutex>ta
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>