class MyWin(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) self.setupUi(self) self.g = Level() self.rect = self.g.getRect() self.score = self.g.get_score() self.level = 1 self.time = 50 self.timer = QtCore.QTimer() self.timer.timeout.connect(self.timer_tick) self.StartButton.clicked.connect(self.start) def keyPressEvent(self, event): dict = { QtCore.Qt.Key_W: self.up, QtCore.Qt.Key_S: self.down, QtCore.Qt.Key_A: self.left, QtCore.Qt.Key_D: self.right, QtCore.Qt.Key_F: self.rotate } try: dict[event.key()]() except: pass def show_matrix_in_table(self): for row in range(self.g.GameHeight): for col in range(self.g.GameWidth): item = self.g[row, col] cellinfo = QTableWidgetItem(' ') if (row >= self.rect[0][0] and row <= self.rect[0][0] + self.rect[2] - 1) and \ (col >= self.rect[0][1] and col <= self.rect[0][1] + self.rect[1] - 1): cellinfo = QTableWidgetItem('o') # Только для чтения cellinfo.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) cellinfo.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) cellinfo.setFont(QtGui.QFont('SansSerif', 60)) self.tableWidget.setItem(row, col, cellinfo) color = c.my_color[item] self.tableWidget.item(row, col).setBackground( QtGui.QColor(color[0], color[1], color[2])) self.score = self.g.get_score() self.labelScore.setText("Score: " + str(self.score)) self.labelLevel.setText("Level " + str(self.level)) self.labelScore.setFont(QtGui.QFont('SansSerif', 16)) self.labelLevel.setFont(QtGui.QFont('SansSerif', 16)) def decor_update_view(func): def wrapped(self, *args, **kwargs): func(self, *args, **kwargs) self.show_matrix_in_table(*args, **kwargs) return wrapped # Описываем функции @decor_update_view def rotate(self): self.g.update() self.check_new_level() @decor_update_view def up(self): self.g.move_border('up') self.rect = self.g.getRect() @decor_update_view def down(self): self.g.move_border('down') self.rect = self.g.getRect() @decor_update_view def left(self): self.g.move_border('left') self.rect = self.g.getRect() @decor_update_view def right(self): self.g.move_border('right') self.rect = self.g.getRect() def start(self): self.g = Level() self.timer.stop() self.time = 50 self.level = 1 self.g.create_level() self.rect = self.g.getRect() self.show_matrix_in_table() self.label_timer.setText("Time: " + str(self.time)) self.label_timer.setFont(QtGui.QFont('SansSerif', 16)) self.timer.start(1000) def check_new_level(self): self.score = self.g.get_score() if self.score >= 4000: self.new_level() self.timer.stop() self.time = 50 self.timer.start(1000) def new_level(self): self.level += 1 self.g = Level() self.g.create_level() self.score = self.g.get_score() self.rect = self.g.getRect() @decor_update_view def timer_tick(self): self.time -= 1 self.label_timer.setText("Time: " + str(self.time)) self.label_timer.setFont(QtGui.QFont('SansSerif', 16)) if self.time <= 0: self.timer.stop() msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText("Game Over!") msg.setInformativeText("Level " + str(self.level)) msg.setWindowTitle("Game") msg.addButton('Новая игра', QMessageBox.AcceptRole) msg.exec() self.start()
class MyWin(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) self.setupUi(self) # мои переменные self.g = Level(0) self.g.create_level() self.lup = self.g.get_lup() self.score = self.g.get_score() self.level = 1 self.timer = QtCore.QTimer() self.timer.timeout.connect(self.timer_tick) self.buttonStart.clicked.connect(self.start) self.btn_info.clicked.connect(self.information) def keyPressEvent(self, event): dict = { QtCore.Qt.Key_W: self.up, QtCore.Qt.Key_S: self.down, QtCore.Qt.Key_A: self.left, QtCore.Qt.Key_D: self.right, QtCore.Qt.Key_F: self.update } try: dict[event.key()]() except: pass def show_matrix_in_table(self): self.lup = self.g.get_lup() self.score = self.g.get_score() for row in range(self.g.height): for col in range(self.g.width): item = self.g[row, col] cellinfo = QTableWidgetItem(' ') if row == self.lup[0] and col == self.lup[1]: cellinfo = QTableWidgetItem('o') # Только для чтения cellinfo.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) cellinfo.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) cellinfo.setFont(QtGui.QFont('SansSerif', 22)) self.tableWidget.setItem(row, col, cellinfo) color = colors.my_color[item] self.tableWidget.item(row, col).setBackground( QtGui.QColor(color[0], color[1], color[2])) self.label_score.setText("Score: " + str(self.score)) self.label_level.setText("Level " + str(self.level)) self.label_score.setFont(QtGui.QFont('SansSerif', 14)) self.label_level.setFont(QtGui.QFont('SansSerif', 14)) def decor_update_view(func): def wrapped(self, *args, **kwargs): func(self, *args, **kwargs) self.show_matrix_in_table(*args, **kwargs) return wrapped # Описываем функции @decor_update_view def left(self): self.g.move_border('left') @decor_update_view def right(self): self.g.move_border('right') @decor_update_view def down(self): self.g.move_border('down') @decor_update_view def up(self): self.g.move_border('up') @decor_update_view def update(self): self.g.update() self.score = self.g.get_score() self.check_new_level() @decor_update_view def timer_tick(self): self.g.add_row() if self.g.check_game_over(): self.timer.stop() msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText("Game Over!") msg.setInformativeText("Your level " + str(self.level)) msg.setWindowTitle("Game") msg.addButton('Ok', QMessageBox.AcceptRole) msg.exec() self.start() def start(self): self.g = Level(0) self.g.create_level() self.lup = self.g.get_lup() self.score = self.g.get_score() self.level = 1 self.show_matrix_in_table() self.timer.start(2500) def check_new_level(self): if self.score >= 20000: self.new_level() def new_level(self): self.level += 1 self.g = Level(0) self.g.create_level() self.score = self.g.get_score() self.lup = self.g.get_lup() def information(self): msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText("Цель игры: удалять по 4 и больше соеденённых блоков.") msg.setInformativeText( "Управление: (WASDF)\n" "W, A, S, D - двигаемся по полю вверх/влево/вниз/вправо соответственно,\n" "F - удаляем блоки, если это возможно.") msg.setWindowTitle("Game") msg.addButton('Ok', QMessageBox.AcceptRole) msg.exec()
class MyWin(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) self.setupUi(self) # мои переменные self.g = Level(0) self.g.create_level() self.select_col = self.g.get_select_col() self.score = self.g.get_score() self.level = 1 self.timer = QtCore.QTimer() self.timer.timeout.connect(self.timer_tick) self.buttonStart.clicked.connect(self.start) self.btn_Info.clicked.connect(self.info) def keyPressEvent(self, event): dict = { QtCore.Qt.Key_W: self.up, QtCore.Qt.Key_S: self.down, QtCore.Qt.Key_A: self.left, QtCore.Qt.Key_D: self.right } try: dict[event.key()]() except: pass def show_matrix_in_table(self): self.select_col = self.g.get_select_col() for row in range(self.g.height): for col in range(self.g.width): item = self.g[row, col] cellinfo = QTableWidgetItem(' ') if item == -2: cellinfo = QTableWidgetItem('Dinamit') # Только для чтения cellinfo.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.tableWidget.setItem(row, col, cellinfo) color = colors.my_color[item] if col == self.select_col and item == 0: color = (200, 200, 200) self.tableWidget.item(row, col).setBackground( QtGui.QColor(color[0], color[1], color[2])) # 2 таблица cell = QTableWidgetItem(' ') # Только для чтения cell.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.tableWidget_2.setItem(0, 0, cell) sel_block = self.g.get_select_blocks() c = colors.my_color[sel_block['color']] self.tableWidget_2.item(0, 0).setBackground( QtGui.QColor(c[0], c[1], c[2])) self.label.setText("x" + str(sel_block['count'])) self.label_score.setText("Score: " + str(self.score)) self.label_level.setText("Level " + str(self.level)) self.label.setFont(QtGui.QFont('SansSerif', 16)) self.label_score.setFont(QtGui.QFont('SansSerif', 14)) self.label_level.setFont(QtGui.QFont('SansSerif', 14)) def decor_update_view(func): def wrapped(self, *args, **kwargs): func(self, *args, **kwargs) self.show_matrix_in_table(*args, **kwargs) return wrapped # Описываем функции @decor_update_view def left(self): self.g.move_select_column('left') @decor_update_view def right(self): self.g.move_select_column('right') @decor_update_view def down(self): self.g.take_blocks() @decor_update_view def up(self): self.g.update() self.score = self.g.get_score() self.check_new_level() @decor_update_view def timer_tick(self): self.g.add_row() if self.g.check_game_over(): self.timer.stop() msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText("Game Over!") msg.setInformativeText("Level " + str(self.level)) msg.setWindowTitle("Tetris") msg.addButton('Окей', QMessageBox.AcceptRole) msg.addButton('Отмена', QMessageBox.RejectRole) msg.exec() def start(self): self.g = Level(0) self.g.create_level() self.select_col = self.g.get_select_col() self.score = self.g.get_score() self.level = 1 self.show_matrix_in_table() self.timer.start(5000) def check_new_level(self): if self.score >= 2500: self.new_level() def new_level(self): self.level += 1 self.g = Level(0) self.g.create_level() self.score = self.g.get_score() self.select_col = self.g.get_select_col() def info(self): msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText( "Цель игры: соединить 4 и больше одинаковых блоков, чтобы они удалились.\n" "P.S. черный блок забрать нельзя, динамит уничтожает весь стольбец, в который его возвратим." ) msg.setInformativeText("Управление: (WASD)\n" "A, D - передвигаем выбранный столбец,\n" "S - забираем блок,\n" "W - возвращаем блок.") msg.setWindowTitle("Tetris") msg.addButton('Окей', QMessageBox.AcceptRole) msg.exec()