def keyPressEvent(self, event): if not self.isStarted or BOARD_DATA.currentShape == Shape.shapeNone: super(Tetris, self).keyPressEvent(event) return key = event.key() if key == Qt.Key_P: self.pause() return if self.isPaused: return elif key == Qt.Key_Left: BOARD_DATA.moveLeft() elif key == Qt.Key_Right: BOARD_DATA.moveRight() elif key == Qt.Key_Up: BOARD_DATA.rotateLeft() elif key == Qt.Key_Down: BOARD_DATA.moveDown() elif key == Qt.Key_Space: self.tboard.score += BOARD_DATA.dropDown() else: super(Tetris, self).keyPressEvent(event) self.updateWindow()
def timerEvent(self, event): if event.timerId() == self.timer.timerId(): # 如果TETRIS_AI存在,则由AI进行下一步操作 if TETRIS_AI and not self.nextMove: self.nextMove = TETRIS_AI.nextMove() if self.nextMove: k = 0 while BOARD_DATA.currentDirection != self.nextMove[0] and k < 4: BOARD_DATA.rotateRight() k += 1 k = 0 while BOARD_DATA.currentX != self.nextMove[1] and k < 5: if BOARD_DATA.currentX > self.nextMove[1]: BOARD_DATA.moveLeft() elif BOARD_DATA.currentX < self.nextMove[1]: BOARD_DATA.moveRight() k += 1 # lines = BOARD_DATA.dropDown() lines = BOARD_DATA.moveDown() self.tboard.score += lines if self.lastShape != BOARD_DATA.currentShape: self.nextMove = None self.lastShape = BOARD_DATA.currentShape self.updateWindow() else: super(Tetris, self).timerEvent(event)
def timerEvent(self, event): if event.timerId() == self.timer.timerId(): if TETRIS_AI and not self.nextMove: self.nextMove = TETRIS_AI.nextMove() if self.nextMove: k = 0 while BOARD_DATA.currentDirection != self.nextMove[0] and k < 4: BOARD_DATA.rotateRight() k += 1 k = 0 while BOARD_DATA.currentX != self.nextMove[1] and k < 5: if BOARD_DATA.currentX > self.nextMove[1]: BOARD_DATA.moveLeft() elif BOARD_DATA.currentX < self.nextMove[1]: BOARD_DATA.moveRight() k += 1 # lines = BOARD_DATA.dropDown() lines = BOARD_DATA.moveDown() self.tboard.score += lines if self.lastShape != BOARD_DATA.currentShape: self.nextMove = None self.lastShape = BOARD_DATA.currentShape self.updateWindow() else: super(Tetris, self).timerEvent(event)
def keyPressEvent(self, event): if not self.isStarted or BOARD_DATA.currentShape == Shape.shapeNone: super(Tetris, self).keyPressEvent(event) return key = event.key() if key == Qt.Key_P: self.pause() return if self.isPaused: return elif key == Qt.Key_Left: BOARD_DATA.moveLeft() elif key == Qt.Key_Right: BOARD_DATA.moveRight() elif key == Qt.Key_Down: lineCheck = BOARD_DATA.moveDown() self.tboard.score += lineCheck if lineCheck >= 2: self.tboard.itemNum += int(lineCheck / 2) elif key == Qt.Key_Up: BOARD_DATA.rotateLeft() elif key == Qt.Key_Space: lineCheck = BOARD_DATA.dropDown() self.tboard.score += lineCheck if lineCheck >= 2: self.tboard.itemNum += int(lineCheck / 2) elif key == Qt.Key_Z: if self.tboard.itemNum >= 1: self.tboard.itemNum -= 1 itemRandom = random.randint(1, 2) if itemRandom == 1: BOARD_DATA.createNewPiece() elif itemRandom == 2: BOARD_DATA.clear() elif key == Qt.Key_Escape: self.reGame() else: super(Tetris, self).keyPressEvent(event) self.updateWindow()