一化北溟 2023-03-14 19:05 采纳率: 100%
浏览 58
已结题

俄罗斯方块怎样不满行

C++,俄罗斯方块怎样不满行,就能消除
比如空一个格,这一行也能消除
这代码怎么改


#include<iostream>
#include<math.h>
#include<Windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
 
enum DIR
{
    UP,
    RIGHT,
    DOWN,
    LEFT
};
 
time_t start = 0, finish = 0;
 
int _x = 6, _y = 1;//图形生成位置
 
int map[30][16] = { 0 };
 
int sharp[20][8] = {
    {0,0,0,0,0,0,0,0},
    //I形
    {0,0,0,1,0,2,0,3},
    {0,0,1,0,2,0,3,0},
    //■形
    {0,0,1,0,0,1,1,1},
    //L形
    {0,0,0,1,0,2,1,2},
    {0,0,0,1,1,0,2,0},
    {0,0,1,0,1,1,1,2},
    {0,1,1,1,2,0,2,1},
    //J形
    {0,2,1,0,1,1,1,2},
    {0,0,0,1,1,1,2,1},
    {0,0,0,1,0,2,1,0},
    {0,0,1,0,2,0,2,1},
    //Z形
    {0,0,1,0,1,1,2,1},
    {0,1,0,2,1,0,1,1},
    //S形
    {0,1,1,0,1,1,2,0},
    {0,0,0,1,1,1,1,2},
    //T形
    {0,1,1,0,1,1,2,1},
    {0,0,0,1,0,2,1,1},
    {0,0,1,0,1,1,2,0},
    {0,1,1,0,1,1,1,2}
};
 
 
class Game
{
public:
    int score;//游戏分数
    int _id;//图形编号
    int top;//最高点高度
    int speed;//下落速度
 
    Game();
    void showMenu();//显示菜单
    void showGround();//显示游戏界面
    void gameOver();//游戏结束界面
    void Run();//运行游戏
    void sharpDraw(int id,bool show = false);//绘制图形
    void keyControl();//键盘控制
    bool move(int dir,int id);//移动判断
    bool downSet(int id);//下落
    void Turn(int id);//旋转
    void clean();//消行
};
 
void SetPos(int i, int j)//控制光标位置, 列, 行
{
    COORD pos = { i,j };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
 
int main()
{
    CONSOLE_CURSOR_INFO cursor;
    GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
    cursor.bVisible = 0;    //这四行用来设置光标不显示
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
 
    srand((unsigned)time(NULL));
 
    Game game;
    game.showMenu();
    return 0;
}
 
Game::Game()
{
    score = 0;
    _id = 0;
    top = 58;
    speed = 1000;
}
 
void Game::showMenu()
{
    for (int i = 0; i < 30; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            if ((i == 0 || i == 29) || (j == 0 || j == 25))
            {
                cout << "■";
            }
            else
            {
                cout << "  ";
            }
        }
        cout << endl;
    }
 
    SetPos(17, 8);
    cout << "俄 罗 斯 方 块" << endl;
    SetPos(13, 12);
    cout << "↑旋转方块  ↓加速下滑" << endl;
    SetPos(12, 14);
    cout << "← →左右移动  空格  暂停" << endl;
    SetPos(15, 20);
    cout << "0 退出  Enter 开始" << endl;
 
    while (1)
    {
        int select = _getch();
        if (select == 13)
        {
            system("cls");
            this->Run();
        }
        else if (select = 48)
        {
            system("cls");
            exit(0);
        }
    }
}
 
void Game:: showGround()
{
    for (int i = 0; i < 30; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            if ((i == 0 || i == 29) || (j == 0 || j == 25 || j==15))
            {
                cout << "■";
            }
            else if (i == 15 && j > 15)
            {
                cout << "■";
            }
            else
            {
                cout << "  ";
            }
        }
        cout << endl;
    }
 
    SetPos(31, 2);
    cout << "下 个图形" << endl;
    SetPos(31, 17);
    cout << "当 前得分" << endl;
 
    for (int i = 0; i < 30; i++)
    {
        for (int j = 0; j < 16; j++)
        {
            if ((i == 0 || i == 29) || (j == 0 || j == 15))
            {
                map[i][j] = 1;
            }
            else
            {
                map[i][j] = 0;
            }
        }
    }
}
 
void Game:: gameOver()
{    
    for (int i = 5; i < 15; i++)
    {
        SetPos(1, i);
        cout << "                            " << endl;
    }
 
    SetPos(8, 7);
    cout << "G a m e   O v e r" << endl;
 
    SetPos(3, 10);
    cout << "0 退出   Enter 重新开始" << endl;
 
    while (1)
    {    
        int select = _getch();
        if (select == 13)
        {
            system("cls");
            this->Run();
        }
        else if (select == 48)
        {
            system("cls");
            exit(0);
        }
    }
 
}
 
void Game::Run()
{
    score = 0;
    _id = 0;
    top = 58;
    _x = 6;
    _y = 1;
    showGround();
    start = clock();
    int new_id = rand() % 19 + 1;
 
    while (1)
    {
        sharpDraw(_id);        
        keyControl();
 
        if (downSet(_id))
        {
            sharpDraw(-new_id, 1);
            _id = new_id;
            new_id = rand() % 19 + 1;
            sharpDraw(new_id, 1);
            clean();
        }
 
        SetPos(34, 20);
        cout << score << endl;
    }
}
 
void Game::sharpDraw(int id, bool show)
{    
    int x, y;
 
    if (show == true)
    {
        if (id > 0)
        {
            for (int i = 0; i < 4; i++)
            {
                x = 19 + sharp[id][2 * i];
                y = 6 + sharp[id][2 * i + 1];
                SetPos(2 * x, y);
                cout << "■";
            }
        }
        else
        {
            for (int i = 0; i < 4; i++)
            {
                x = 19 + sharp[-id][2 * i];
                y = 6 + sharp[-id][2 * i + 1];
                SetPos(2 * x, y);
                cout << "  ";
            }
        }
        return;
    }
 
 
    if (id > 0)
    {
        for (int i = 0; i < 4; i++)
        {
            x = _x + sharp[id][2 * i];
            y = _y + sharp[id][2 * i + 1];
            SetPos(2 * x, y);
            cout << "■";
        }
    }
    else
    {
        for (int i = 0; i < 4; i++)
        {
            x = _x + sharp[- id][2 * i];
            y = _y + sharp[- id][2 * i + 1];
            SetPos(2 * x, y);
            cout << "  ";
        }
    }
    return;
 
}
 
bool Game:: downSet(int id)
{
    if (id == 0)
        return true;
 
    finish = clock();
 
    if (finish - start < speed)
    {
        return false;
    }
 
    start = clock();
 
    if (!move(DOWN,_id))
    {
        int x, y;
        for (int i = 0; i < 4; i++)
        {
            x = _x + sharp[id][2 * i];
            y = _y + sharp[id][2 * i + 1];
            map[y][x] = 1;
 
            if (y < top)
            {
                top = y;
            }
            if (top <= 1)
            {
                gameOver();
            }
        }
        _x = 6;
        _y = 1;
        return true;
    }
        
    sharpDraw(-id);
    _y++;
    sharpDraw(id);
    return false;
}
 
bool Game:: move(int dir,int id)
{
    int x, y;
    switch (dir)
    {
    case UP:
        for (int i = 0; i < 4; i++)
        {
            x = _x + sharp[id][2 * i];
            y = _y + sharp[id][2 * i + 1];
            if (map[y][x] == 1)
            {
                return false;
            }
        }
        break;
    case DOWN:
    {
        for (int i = 0; i < 4; i++)
        {
            x = _x + sharp[id][2 * i];
            y = _y + sharp[id][2 * i + 1];
            if (map[y + 1][x] == 1)
            {
                return false;
            }
        }
    }
    break;
    case RIGHT:
    {
        for (int i = 0; i < 4; i++)
        {
            x = _x + sharp[id][2 * i];
            y = _y + sharp[id][2 * i + 1];
            if (map[y][x + 1] == 1)
            {
                return false;
            }
        }
    }
    break;
    case LEFT:
    {
        for (int i = 0; i < 4; i++)
        {
            x = _x + sharp[id][2 * i];
            y = _y + sharp[id][2 * i + 1];
            if (map[y][x - 1] == 1)
            {
                return false;
            }
        }
    }
    break;
    default:
        break;
    }
    return true;
}
 
void Game:: Turn(int id)
{
    switch (id)
    {
    case 1:id++; break;
    case 2:id--; break;
 
    case 3: break;
 
    case 4:id++; break;
    case 5:id++; break;
    case 6:id++; break;
    case 7:id -= 3; break;
 
    case 8:id++; break;
    case 9:id++; break;
    case 10:id++; break;
    case 11:id -= 3; break;
 
    case 12:id++; break;
    case 13:id--; break;
 
    case 14:id++; break;
    case 15:id--; break;
 
    case 16:id++; break;
    case 17:id++; break;
    case 18:id++; break;
    case 19:id -= 3; break;
 
    default:
        break;
    }
 
    if (!move(UP, id))
    {
        return;
    }
 
    sharpDraw(-_id);
    _id = id;
}
 
void Game:: keyControl()
{
    if (!_kbhit())
        return;
 
    int key = _getch();
 
    switch (key)
    {
    case 72:
        Turn(_id);
        break;
    case 80:
        if (move(DOWN, _id))
        {
            sharpDraw(-_id);
            _y++;
        }
        break;
    case 75:
        if (move(LEFT, _id))
        {
            sharpDraw(-_id);
            _x --;
        }
        break;
    case 77:
        if (move(RIGHT, _id))
        {
            sharpDraw(-_id);
            _x ++;
        }
        break;
    case 32:
    {
        for (int i = 5; i < 15; i++)
        {
            SetPos(1, i);
            cout << "                            " << endl;
        }
 
        SetPos(10, 7);
        cout << "游 戏 暂 停" << endl;
 
        SetPos(3, 10);
        cout << "0 返回菜单  回车 继续游戏" << endl;
 
        while (1)
        {        
            int select = _getch();
 
            if (select == 13)
            {
                for (int i = 5; i < 15; i++)
                {
                    SetPos(1, i);
                    cout << "                            " << endl;
                }
                break;
            }
            else if (select == 48)
            {
                system("cls");
                showMenu();
            }
        }
 
    }
    default:
        break;
    }
}
 
void Game:: clean()
{
    int n = -1;
    int line = -1;
    while (1)
    {
        for (int i = 28; i > 0; i--)
        {
            for (int j = 1; j < 15; j++)
            {
                line = i;
                if (map[i][j] == 0)
                {
                    line = -1;
                    break;
                }
            }
            if (line != -1)
                break;
        }
 
        if (line == -1)
            break;
 
        for (int i = line; i > 0; i--)
        {
            for (int j = 1; j < 15; j++)
            {
                if (i == 1)
                    map[i][j] = 0;
                else
                {
                    map[i][j] = map[i - 1][j];
                    SetPos(2 * j, i);
                    if (map[i][j] == 1)
                        cout << "■";
                    else
                        cout << "  ";
                }
            }
        }
        top++;
        n++;
    }
 
    if (n >= 0)
    {
        score += n * n * 100 + 100;
        if (speed > 100)
            speed = 1000 - score / 10;
    }
}

版权声明:本文为南方孤旅博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/nanfang0/article/details/122028686

  • 写回答

3条回答 默认 最新

  • 以山河作礼。 新星计划导师 2023-03-14 19:58
    关注

    该回答引用gpt:
    在俄罗斯方块中,当一行被填满时,需要将该行消除。如果您想让一行不满也能消除,可以考虑在游戏逻辑中添加一些额外的规则。

    一种简单的实现方法是,在方块落下到底部时,检查该行是否可以消除。如果该行上有空格,则该行不能消除;否则,该行可以消除。如果您想让玩家手动控制方块落下的位置,可以在方块移动到底部时进行检查。

    以下是一个示例实现方法:

    // 定义一个游戏区域,包含所有已经落下的方块
    vector<vector<int>> gameBoard;
    
    // 定义一个函数,用于检查并消除一行
    void checkAndClearRow(int row) {
        bool canClear = true;
        for (int j = 0; j < gameBoard[row].size(); j++) {
            if (gameBoard[row][j] == 0) {
                canClear = false;
                break;
            }
        }
        if (canClear) {
            // 将该行清空
            for (int j = 0; j < gameBoard[row].size(); j++) {
                gameBoard[row][j] = 0;
            }
            // 将该行上面的方块下移一行
            for (int i = row - 1; i >= 0; i--) {
                for (int j = 0; j < gameBoard[i].size(); j++) {
                    gameBoard[i + 1][j] = gameBoard[i][j];
                }
            }
            // 将第一行清空
            for (int j = 0; j < gameBoard[0].size(); j++) {
                gameBoard[0][j] = 0;
            }
        }
    }
    
    // 定义一个函数,用于将方块落下到底部
    void dropBlockToBottom() {
        // 将方块逐行下落,直到不能继续下落为止
        while (canMoveDown()) {
            moveDown();
        }
        // 检查最后一行是否可以消除
        checkAndClearRow(gameBoard.size() - 1);
    }
    

    在上面的代码中,gameBoard是一个二维向量,用于表示游戏区域中所有已经落下的方块。checkAndClearRow函数用于检查并消除一行,如果该行可以消除,则将该行上面的方块下移一行,并将第一行清空。dropBlockToBottom函数用于将方块落下到底部,首先将方块逐行下落,然后检查最后一行是否可以消除。

    请注意,上面的代码仅供参考,您需要根据实际情况进行修改和优化。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 修改了问题 3月14日
  • 创建了问题 3月14日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果