I made Tetris in Python for fishing at work?

yz_ weixiao 2022-06-23 17:54:42 阅读数:565

tetrispythonfishingwork

First of all, the zero basis is to be able to learn python Of , Many programming gods choose to learn before they get started Python, So if you want to learn, learn boldly , Who is not zero foundation before learning , Even if I make up my mind to study now, I'm not afraid , Study Python It's never too late .

I learn python Before , Is a programming white , There is no basis , College majors can't beat the odds , But I am now as a python The programmer , With a good income . I think you are in the same state as I was in the beginning , But you have to believe in your choice , Be firm in your choice .

This is for fishing at work Python Do Tetris games

​ The source code to share

import os
import sys
import random
from modules import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
''' Define Tetris game class '''
class TetrisGame(QMainWindow):
def __init__(self, parent=None):
super(TetrisGame, self).__init__(parent)
# Whether to suspend ing
self.is_paused = False
# Do you want to start ing
self.is_started = False
self.initUI()
''' Interface initialization '''
def initUI(self):
# icon
self.setWindowIcon(QIcon(os.path.join(os.getcwd(), 'resources/icon.jpg')))
# Block size
self.grid_size = 22
# Game frame rate
self.fps = 200
self.timer = QBasicTimer()
# The focus of
self.setFocusPolicy(Qt.StrongFocus)
# Horizontal layout
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 —— Nine songs ')
self.show()
self.setFixedSize(self.external_board.width() + self.side_panel.width(), self.side_panel.height() + self.status_bar.height())
''' The game interface moves to the middle of the screen '''
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) // 2, (screen.height() - size.height()) // 2)
''' Update the interface '''
def updateWindow(self):
self.external_board.updateData()
self.side_panel.updateData()
self.update()
''' Start '''
def start(self):
if self.is_started:
return
self.is_started = True
self.inner_board.createNewTetris()
self.timer.start(self.fps, self)
''' Pause / Don't pause '''
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()
''' Timer events '''
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)
''' Key events '''
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 Key pause
if key == Qt.Key_P:
self.pause()
return
if self.is_paused:
return
# towards the left
elif key == Qt.Key_Left:
self.inner_board.moveLeft()
# towards the right
elif key == Qt.Key_Right:
self.inner_board.moveRight()
# rotate
elif key == Qt.Key_Up:
self.inner_board.rotateAnticlockwise()
# Fall fast
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_())

It's also a person who came over , Now it seems that I learned python I also took many detours , Let me share my experience with you .

Be sure to practice more ! Be sure to practice more ! Be sure to practice more ! Learning any programming language is based on practice , No practice , It's like taking half a day to learn the theory of shooting , As a result, there was no chance to touch the gun at all , Do you think you're accurate ? Want to learn well Python Be sure to practice more , There's no bonus to proficiency , No matter how much you learn, it's just a piece of paper , Once faced with the real problem , You can't do it .

版权声明:本文为[yz_ weixiao]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/01/202201042206463923.html