為了上班摸魚我用Python製作了俄羅斯方塊?

專注的阿熊發表於2022-01-07

import os

import sys

import random

from modules import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

from PyQt5.QtWidgets import *

''' 定義俄羅斯方塊遊戲類 '''

class TetrisGame(QMainWindow):

     def __init__(self, parent=None):

         super(TetrisGame, self).__init__(parent)

         # 是否暫停 ing

         self.is_paused = False

         # 是否開始 ing

         self.is_started = False

         self.initUI()

     ''' 介面初始化 '''

     def initUI(self):

         # icon

         self.setWindowIcon(QIcon(os.path.join(os.getcwd(), 'resources/icon.jpg')))

         # 塊大小

         self.grid_size = 22

         # 遊戲幀率

         self.fps = 200

         self.timer = QBasicTimer()

         # 焦點

         self.setFocusPolicy(Qt.StrongFocus)

         # 水平佈局

         layout_horizontal = QHBoxLayout()

         self.inner_board = InnerBoard()

         self.external_board = ExternalBoard(self, self.grid_size, self.inner_board)

         layout_horizontal.addWidget(self.external_board)

         self.side_panel = SidePanel(self, self.grid_size, self.inner_board)

         layout_horizontal.addWidget(self.side_panel)

         self.status_bar = self.statusBar()

         self.external_board.score_signal[str].connect(self.status_bar.showMessage)

         self.start()

         self.center()

         self.setWindowTitle('Tetris —— 九歌 ')

         self.show()

         self.setFixedSize(self.external_board.width() + self.side_panel.width(), self.side_panel.height() + self.status_bar.height())

     ''' 遊戲介面移動到螢幕中間 '''

     def center(self):

         screen = QDesktopWidget().screenGeometry()

         size = 外匯跟單gendan5.comself.geometry()

         self.move((screen.width() - size.width()) // 2, (screen.height() - size.height()) // 2)

     ''' 更新介面 '''

     def updateWindow(self):

         self.external_board.updateData()

         self.side_panel.updateData()

         self.update()

     ''' 開始 '''

     def start(self):

         if self.is_started:

             return

         self.is_started = True

         self.inner_board.createNewTetris()

         self.timer.start(self.fps, self)

     ''' 暫停 / 不暫停 '''

     def pause(self):

         if not self.is_started:

             return

         self.is_paused = not self.is_paused

         if self.is_paused:

             self.timer.stop()

             self.external_board.score_signal.emit('Paused')

         else:

             self.timer.start(self.fps, self)

         self.updateWindow()

     ''' 計時器事件 '''

     def timerEvent(self, event):

         if event.timerId() == self.timer.timerId():

             removed_lines = self.inner_board.moveDown()

             self.external_board.score += removed_lines

             self.updateWindow()

         else:

             super(TetrisGame, self).timerEvent(event)

     ''' 按鍵事件 '''

     def keyPressEvent(self, event):

         if not self.is_started or self.inner_board.current_tetris == tetrisShape().shape_empty:

             super(TetrisGame, self).keyPressEvent(event)

             return

         key = event.key()

         # P 鍵暫停

         if key == Qt.Key_P:

             self.pause()

             return

         if self.is_paused:

             return

         # 向左

         elif key == Qt.Key_Left:

             self.inner_board.moveLeft()

         # 向右

         elif key == Qt.Key_Right:

             self.inner_board.moveRight()

         # 旋轉

         elif key == Qt.Key_Up:

             self.inner_board.rotateAnticlockwise()

         # 快速墜落

         elif key == Qt.Key_Space:

             self.external_board.score += self.inner_board.dropDown()

         else:

             super(TetrisGame, self).keyPressEvent(event)

         self.updateWindow()

'''run'''

if __name__ == '__main__':

     app = QApplication([])

     tetris = TetrisGame()

     sys.exit(app.exec_())


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2851193/,如需轉載,請註明出處,否則將追究法律責任。

相關文章