Python实现简单的俄罗斯方块游戏
程序员文章站
2022-06-19 18:01:21
本文实例为大家分享了python实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下玩法:童年经典,普通模式没啥意思,小时候我们都是玩加速的。源码分享:import osimport sysimpo...
本文实例为大家分享了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_())
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。