俄罗斯方块C++实现(基于QT)(5)– I型方块代码

  • A+
所属分类:C++

俄罗斯方块C++实现(基于QT)(5)-- Z型方块代码

I型方块代码实现(iblock.h, iblock.cpp)

iblock.h代码:

// iblock.h
#ifndef IBLOCK_H
#define IBLOCK_H

#include "tetris.h"

// 长条类
class IBlock : public Tetris
{
public:
    IBlock(int const &style, int const &w, int const &h);
    void rotate();
};
#endif // IBLOCK_H

iblock.cpp代码:

// iblock.cpp
#include "iblock.h"

IBlock::IBlock(int const &style, int const &w, int const &h)
{
    bwide = w;
    bhigh = h;
    this->style = style;
    // 初始化方块为长条
    for (int i = 0; i != 4; ++i)
    {
        CurPosi[0].x = bwide/2-1;
        CurPosi[1].x = bwide/2-1;
        CurPosi[2].x = bwide/2-1;
        CurPosi[3].x = bwide/2-1;
        CurPosi[0].y = 0;
        CurPosi[1].y = 1;
        CurPosi[2].y = 2;
        CurPosi[3].y = 3;
    }
}

void IBlock::rotate()
{
    if (CurPosi[1].y == CurPosi[0].y) // 横长条
    {
        TmpPosi[0].x = CurPosi[0].x;
        TmpPosi[1].x = CurPosi[0].x;
        TmpPosi[2].x = CurPosi[0].x;
        TmpPosi[3].x = CurPosi[0].x;
        TmpPosi[0].y = CurPosi[0].y;
        TmpPosi[1].y = CurPosi[0].y+1;
        TmpPosi[2].y = CurPosi[0].y-1;
        TmpPosi[3].y = CurPosi[0].y-2;
    }
    else // 竖长条
    {
        TmpPosi[0].x = CurPosi[0].x;
        TmpPosi[1].x = CurPosi[0].x-1;
        TmpPosi[2].x = CurPosi[0].x+1;
        TmpPosi[3].x = CurPosi[0].x+2;
        TmpPosi[0].y = CurPosi[0].y;
        TmpPosi[1].y = CurPosi[0].y;
        TmpPosi[2].y = CurPosi[0].y;
        TmpPosi[3].y = CurPosi[0].y;
    }
}

查看下一篇文章俄罗斯方块C++实现(基于QT)(6)

查看上一篇文章俄罗斯方块C++实现(基于QT)(4)

百分购

发表评论

您必须才能发表评论!