我罗斯

作业描述 详情
这个作业属于哪个课程 2020面向对象程序设计
这个作业要求在哪里 我罗斯方块最终篇
这个作业的目标 代码的 git 仓库链接
运行截图/运行视频。
代码要点
收获与心得。
依然存在的问题
成员 031902328薛颂
作业正文 如下
其他参考文献 俄罗斯方块

运行截图

首先是刚开始运行:

这里有个bug,右边的界面方块只能下降到最后两格,因为这是个单人方块改的双人方块,所以这个问题牵一发而动全身,尝试改了几次,最后连游戏都不能正常运行,只能放弃,所以之后的消行和此消彼长看起来会有点奇怪。:

然后是消行和此消彼长,但是我这里能实现的只有对方界面的下面随机生成一行,做不到在生成一行之前先使对方底下的方块全部上升一行,下边四个图,两个是左边消行,两个是右边消行。


右边消行

代码要点

初始化窗口:

void initialWindow(HANDLE hOut)
{
    SetConsoleTitle("我罗斯方块");\\获取窗口标题
    COORD size = { 80, 25 };
    SetConsoleScreenBufferSize(hOut, size);\\更改指定缓冲区大小
    SMALL_RECT rc = { 0, 0, 79, 24 };
    SetConsoleWindowInfo(hOut, true, &rc);\\设置控制台窗口信息
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(hOut, &cursor_info);\\用来设置指定bai的控制台光标的大小和可见性,可以让光标不再一闪一闪
}

方块:

int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } };
int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } };
int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } };
int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } };
int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } };
int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } };
int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } };
int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } };
int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } };
int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } };
int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } };
int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } };
int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,0 } };

不同的方块是根据左上角的数字,即blockXX[0][0]的值来区分的,这个代码也根据这个数字在打印方块时上了不同的色
上色代码:

SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);

随机生成方块并打印到下一个方块位置:

void roundBlock(HANDLE hOut, int block[4][4])
{
    clearBlock(hOut, block, 5, 15);\\消除方块
    switch (rand() % 19)\\随机数生成
    {
    case 0:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block00[i][j];
            }
        }
        break;
    case 1:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block01[i][j];
            }
        }
        break;
    case 2:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block02[i][j];
            }
        }
        break;
    case 3:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block03[i][j];
            }
        }
        break;
    case 4:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block04[i][j];
            }
        }
        break;
    case 5:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block05[i][j];
            }
        }
        break;
    case 6:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block06[i][j];
            }
        }
        break;
    case 7:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block07[i][j];
            }
        }
        break;
    case 8:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block08[i][j];
            }
        }
        break;
    case 9:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block09[i][j];
            }
        }
        break;
    case 10:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block10[i][j];
            }
        }
        break;
    case 11:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block11[i][j];
            }
        }
        break;
    case 12:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block12[i][j];
            }
        }
        break;
    case 13:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block13[i][j];
            }
        }
        break;
    case 14:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block14[i][j];
            }
        }
        break;
    case 15:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block15[i][j];
            }
        }
        break;
    case 16:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block16[i][j];
            }
        }
        break;
    case 17:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block17[i][j];
            }
        }
        break;
    case 18:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block18[i][j];
            }
        }
        break;
    default:
        break;
    }
    printBlock(hOut, block, 5, 15);
}

敲击键盘的响应

  if (_kbhit())\\检查当前是否有键盘输入
        {
            key = _getch();\\不回显读取且不用按下其他键
            switch (key)
            {
            case 72:\\即↑键
                myUp(hOut, blockB, map, positionX, positionY);
                break;
            case 'w':
                	 myUp(hOut, blockB2, map2, positionX2, positionY2);
                break;
            case 75:即←健
                myLeft(hOut, blockB, map, positionX, positionY);
                break;
               case 'a':
                myLeft(hOut, blockB2, map2, positionX2, positionY2);
                break; 
            case 77:即→键
                myRight(hOut, blockB, map, positionX, positionY);
                break;
            case 'd':
                myRight(hOut, blockB2, map2, positionX2, positionY2);
                break;
            case 80:即↓键
                switch (myDown(hOut, blockB, map, positionX, positionY))
                {
                case 0:
                    check = false;
                    break;
                case 1:
                    check = true;
                    break;
                case 2:
                    gameOver(hOut, blockB, map);
                    goto initial;
                default:
                    break;
                }
                break;
                  case 's':
                switch (myDown(hOut, blockB2, map2, positionX2, positionY2))
                {
                case 0:
                    check2 = false;
                    break;
                case 1:
                    check2 = true;
                    break;
                case 2:
                    gameOver(hOut, blockB2, map2);
                    goto initial;
                default:
                    break;
                }
                break;
            case 32:
                myStop(hOut, blockA);
                break;
            case 27:
                exit(0);
            default:
                break;
            }
        }

收获与心得

网上的教程是五花八门,试过很多种方法去自学,但最后还是只能把单人方块去修改成双人方块(让我自己写一个实在是寸步难行),这也导致了代码长度几乎增加一倍以及有一些牵一发而动全身的bug。通过这次学习,我大概懂了去做一个设计的流程,虽然是一个人做的,但差不多也了解了如何团队协作以及团队协作的重要性。因为以后的话去做什么东西,肯定都是要多人协作去完成的,一个人实在是吃不消,而且一个人容易没有动力去做。这次作业也为以后的毕业设计和实习时的团队协作积累了一次宝贵的经验,这是平常写题时得不到的。

依然存在的问题

bug自然不用说,玩家2的方块只能下落到倒数第三格真的是致命的,还有此消彼长也不能完全实现。
代码很长,因为很多函数我都要稍微改一下来符合双人的需求,这就导致会有两个差不多一样的函数,代码长度几乎增加一倍。
另外代码也没分块

posted @ 2020-06-23 14:28  xtzc  阅读(...)  评论(...编辑  收藏