基于ncurses库的C++俄罗斯方块游戏

测试系统 :Ubuntu~22.04

效果预览
在这里插入图片描述

①安装库:

这里只说下Ubuntu的,其他系统可以自己编译源码或者安装。
源码位置:http://ftp.gnu.org/pub/gnu/ncurses/

sudo apt-get install libncurses5-dev libncursesw5-dev -y

②代码

#include <ncurses.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <unistd.h>

using namespace std;

// 方块形状
vector<vector<vector<int>>> tetrominoes = {
   
    {
   {
   1, 1, 1, 1}},              // I
    {
   {
   1, 1}, {
   1, 1}},            // O
    {
   {
   1, 1, 0}, {
   0, 1, 1}},      // Z
    {
   {
   0, 1, 1}, {
   1, 1, 0}},      // S
    {
   {
   1, 1, 1}, {
   0, 1, 0}},      // T
    {
   {
   1, 1, 1}, {
   1, 0, 0}},      // L
    {
   {
   1, 1, 1}, {
   0, 0, 1}}       // J
};

int width = 10, height = 20;
int score = 0;
bool game_over = false;
int delay_us = 50000; // 每次循环的延迟(微秒)

// 游戏界面
vector<vector<int>> board(height, vector<int>(width, 0));

// 初始化颜色对
void initColors() {
   
    start_color();
    init_pair(1, COLOR_WHITE, COLOR_BLACK);   // 白色方块
    init_pair(2, COLOR_CYAN, COLOR_BLACK);    // 预测落点的浅蓝色影子
}

// 检查方块是否可以放置
bool checkPosition(const vector<vector<int>>& tetromino, int x, int y) {
   
    for (int i = 0; i < tetromino.size(); ++i) {
   
        for (int j = 0; j < tetromino[i].size(); ++j) {
   
            if (tetromino[i][j]) {
   
                int newX = x + j;
                int newY = y + i;
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值