Python小游戏之俄罗斯方块

简介: 俄罗斯方块

俄罗斯方块

玩法:童年经典 应该都会玩 在这里就不多介绍了

源代码分享:

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 = self.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_())

_69043821/article/details/124349978

目录
相关文章
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
Python
python小游戏4
python小游戏4
366 1
|
Python
python小游戏7
python小游戏7
313 0
|
Python
python小游戏6
python小游戏6
277 0
|
Python
python小游戏5
python小游戏5
246 0
|
Python
python小游戏1
python小游戏1
279 0
|
Python
python小游戏3
python小游戏3
347 0
|
存储 人工智能 运维
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
819 48
【01】做一个精美的打飞机小游戏,浅尝阿里云通义灵码python小游戏开发AI编程-之飞机大战小游戏上手实践-优雅草央千澈-用ai开发小游戏尝试-分享源代码和游戏包
|
人工智能 Python
【02】做一个精美的打飞机小游戏,python开发小游戏-鹰击长空—优雅草央千澈-持续更新-分享源代码和游戏包供游玩-记录完整开发过程-用做好的素材来完善鹰击长空1.0.1版本
【02】做一个精美的打飞机小游戏,python开发小游戏-鹰击长空—优雅草央千澈-持续更新-分享源代码和游戏包供游玩-记录完整开发过程-用做好的素材来完善鹰击长空1.0.1版本
894 7
|
Python
python小游戏
python小游戏
305 2

推荐镜像

更多