class TetrixBoard

Constants

BoardHeight
BoardWidth

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File examples/widgets/tetrix/tetrixboard.rb, line 37
def initialize(parent = nil)
    super(parent)
    setFrameStyle(Qt::Frame::Panel | Qt::Frame::Sunken)
    setFocusPolicy(Qt::StrongFocus)
    @isStarting = false
    @isPaused = false
    @board = []
    clearBoard()

    @curPiece = TetrixPiece.new
    @nextPiece = TetrixPiece.new
    @nextPiece.setRandomShape()
    @timer = Qt::BasicTimer.new
end

Public Instance Methods

clearBoard() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 187
def clearBoard()
    (0...BoardWidth*BoardHeight).each do |i|
        @board[i] = TetrixPiece::NoShape
    end
end
drawSquare(painter, x, y, shape) click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 328
def drawSquare(painter, x, y, shape)
    colorTable = [  0x000000, 0xCC6666, 0x66CC66, 0x6666CC,
                    0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00 ]

    color = Qt::Color.fromRgb(colorTable[shape])
    painter.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2,
                     Qt::Brush.new(color))

    painter.pen = color.light
    painter.drawLine(x, y + squareHeight() - 1, x, y)
    painter.drawLine(x, y, x + squareWidth() - 1, y)

    painter.pen = color.dark
    painter.drawLine(x + 1, y + squareHeight() - 1,
                     x + squareWidth() - 1, y + squareHeight() - 1)
    painter.drawLine(x + squareWidth() - 1, y + squareHeight() - 1,
                     x + squareWidth() - 1, y + 1)
end
dropDown() click to toggle source
keyPressEvent(event) click to toggle source
Calls superclass method
# File examples/widgets/tetrix/tetrixboard.rb, line 149
def keyPressEvent(event)
    if !@isStarting || @isPaused || @curPiece.shape == TetrixPiece::NoShape
        super
        return
    end

    case event.key
    when Qt::Key_Left
        tryMove(@curPiece, @curX - 1, @curY)
    when Qt::Key_Right
        tryMove(@curPiece, @curX + 1, @curY)
    when Qt::Key_Down
        tryMove(@curPiece.rotatedRight, @curX, @curY)
    when Qt::Key_Up
        tryMove(@curPiece.rotatedLeft, @curX, @curY)
    when Qt::Key_Space
        dropDown()
    when Qt::Key_D
        oneLineDown()
    else
        super(event)
    end
end
minimumSizeHint() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 73
def minimumSizeHint()
    return Qt::Size.new(BoardWidth * 5 + frameWidth() * 2,
                  5 + frameWidth() * 2)
end
newPiece() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 273
def newPiece()
    @curPiece = @nextPiece
    @nextPiece.setRandomShape()
    showNextPiece()
    @curX = BoardWidth / 2 + 1
    @curY = BoardHeight - 1 + @curPiece.minY()

    if !tryMove(@curPiece, @curX, @curY)
        @curPiece.shape = TetrixPiece::NoShape
        @timer.stop()
        @isStarting = false
    end
end
nextPieceLabel=(label) click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 64
def nextPieceLabel=(label)
    @nextPieceLabel = label
end
oneLineDown() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 206
def oneLineDown()
    if !tryMove(@curPiece, @curX, @curY - 1)
        pieceDropped(0)
    end
end
paintEvent(e) click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 113
def paintEvent(e)
    painter = Qt::Painter.new(self)
    rect = contentsRect()

    drawFrame(painter)

    if @isPaused
        painter.drawText(rect, Qt::AlignCenter, tr("Pause"))
        painter.end
        return
    end

    boardTop = rect.bottom() - BoardHeight*squareHeight()

    (0...BoardHeight).each do |i|
        (0...BoardWidth).each do |j|
            shape = shapeAt(j, BoardHeight - i - 1)
            if shape != TetrixPiece::NoShape
                drawSquare(painter, rect.left() + j * squareWidth(),
                           boardTop + i * squareHeight(), shape)
            end
        end
    end

    if @curPiece.shape() != TetrixPiece::NoShape
        (0...4).each do |i|
            x = @curX + @curPiece.x(i)
            y = @curY - @curPiece.y(i)
            drawSquare(painter, rect.left() + x * squareWidth(),
                       boardTop + (BoardHeight - y - 1) * squareHeight(),
                       @curPiece.shape())
        end
    end
    painter.end
end
pause() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 99
def pause()
    if !@isStarting
        return
    end

    @isPaused = !@isPaused
    if @isPaused
        @timer.stop()
    else
        @timer.start(timeoutTime(), self)
    end
    update()
end
pieceDropped(dropHeight) click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 212
def pieceDropped(dropHeight)
    (0...4).each do |i|
        x = @curX + @curPiece.x(i)
        y = @curY - @curPiece.y(i)
        setShapeAt(x, y, @curPiece.shape())
    end

    @numPiecesDropped += 1
    if @numPiecesDropped % 25 == 0
        @level += 1
        @timer.start(timeoutTime(), self)
        emit levelChanged(@level)
    end

    @score += dropHeight + 7
    emit scoreChanged(@score)
    removeFullLines()

    if !@isWaitingAfterLine
        newPiece()
    end
end
removeFullLines() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 235
def removeFullLines()
    numFullLines = 0
    (BoardHeight - 1).downto(0) do |i|
        lineIsFull = true

        (0...BoardWidth).each do |j|
            if shapeAt(j, i) == TetrixPiece::NoShape
                lineIsFull = false
                break
            end
        end

        if lineIsFull
            numFullLines += 1
            (i...BoardHeight).each do |k|
                (0...BoardWidth).each do |j|
                    setShapeAt(j, k, shapeAt(j, k + 1))
                end
            end
            (0...BoardWidth).each do |j|
                setShapeAt(j, BoardHeight - 1, TetrixPiece::NoShape)
            end
        end
    end

    if numFullLines > 0
        @numLinesRemoved += numFullLines
        @score += 10 * numFullLines
        emit linesRemovedChanged(@numLinesRemoved)
        emit scoreChanged(@score)

        @timer.start(500, self)
        @isWaitingAfterLine = true
        @curPiece.shape = TetrixPiece::NoShape
        update()
    end
end
setShapeAt(x, y, shape) click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 56
def setShapeAt(x, y, shape)
    @board[(y * BoardWidth) + x] = shape
end
shapeAt(x, y) click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 52
def shapeAt(x, y)
    return @board[(y * BoardWidth) + x]
end
showNextPiece() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 287
def showNextPiece()
    if @nextPieceLabel.nil?
        return
    end

    dx = @nextPiece.maxX() - @nextPiece.minX() + 1
    dy = @nextPiece.maxY() - @nextPiece.minY() + 1

    pixmap = Qt::Pixmap.new(dx * squareWidth(), dy * squareHeight())
    painter = Qt::Painter.new(pixmap)
    painter.fillRect(pixmap.rect(), @nextPieceLabel.palette().background())

    (0...4).each do |i|
        x = @nextPiece.x(i) - @nextPiece.minX()
        y = @nextPiece.y(i) - @nextPiece.minY()
        drawSquare(painter, x * squareWidth(), y * squareHeight(),
                   @nextPiece.shape())
    end
    @nextPieceLabel.pixmap = pixmap
    painter.end
end
sizeHint() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 68
def sizeHint()
    return Qt::Size.new(BoardWidth * 15 + frameWidth() * 2,
                  15 + frameWidth() * 2)
end
squareHeight() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 62
def squareHeight() return contentsRect().height() / BoardHeight end
squareWidth() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 61
def squareWidth() return contentsRect().width() / BoardWidth end
start() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 78
def start()
    if @isPaused
        return
    end

    @isStarting = true
    @isWaitingAfterLine = false
    @numLinesRemoved = 0
    @numPiecesDropped = 0
    @score = 0
    @level = 1
    clearBoard()

    emit linesRemovedChanged(@numLinesRemoved)
    emit scoreChanged(@score)
    emit levelChanged(@level)

    newPiece()
    @timer.start(timeoutTime(), self)
end
timeoutTime() click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 60
def timeoutTime() return 1000 / (1 + @level) end
timerEvent(event) click to toggle source
Calls superclass method
# File examples/widgets/tetrix/tetrixboard.rb, line 173
def timerEvent(event)
    if event.timerId == @timer.timerId
        if @isWaitingAfterLine
            @isWaitingAfterLine = false
            newPiece()
            @timer.start(timeoutTime(), self)
        else
            oneLineDown()
        end
    else
        super(event)
    end
end
tryMove(newPiece, newX, newY) click to toggle source
# File examples/widgets/tetrix/tetrixboard.rb, line 309
def tryMove(newPiece, newX, newY)
    (0...4).each do |i|
        x = newX + newPiece.x(i)
        y = newY - newPiece.y(i)
        if x < 0 || x >= BoardWidth || y < 0 || y >= BoardHeight
            return false
        end
        if shapeAt(x, y) != TetrixPiece::NoShape
            return false
        end
    end

    @curPiece = newPiece
    @curX = newX
    @curY = newY
    update()
    return true
end