In order to fish at work, I made Tetris in Python?

Many people ask me that I started from scratch. Can I learn Python?

First of all, zero foundation can learn python. Many programming gods choose to learn Python before they get started, so if you want to learn, learn it boldly. Anyone who didn't learn before is not zero foundation. Even if you make up your mind to learn now, it's not too late to learn python.

Before I studied Python myself, I was a programming white man without any foundation. I can't get a college major anyway, but now I have a good income as a python programmer. I think your current state is similar to that of me at the beginning, but you should believe in your choice and firm your choice.

This is a Tetris game I play in Python for fishing at work

Source sharing

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)
        # Pause ing
        self.is_paused = False
        # 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()
        # focus
        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 Odes')
        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 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)
    '''suspend/No 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 event '''
    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 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 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()
        # Rapid fall
        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_())

I'm also a past person. Now it seems that I took many detours in learning python. Let me share my experience with you.

We must practice more! We must practice more! We must practice more! Learning any programming language is based on practice. If you don't practice, it's like spending half a day learning the theory of shooting. As a result, you don't have a chance to touch the gun. Do you think you can shoot accurately? If you want to learn Python well, you must practice more. Without the bonus of proficiency, no matter how much you learn, it's just on paper. Once you face the real problem, you won't be able to start.

Learning through materials

Python learning route

Python is easy to learn but difficult to master. It can't be mastered in depth overnight. The "framework diagram of learning Python" I collected systematically combs the necessary knowledge points for getting started with Python, and it is recommended to save it.

👉 Python learning route in all directions 👈

The technical points in all directions of Python are sorted out to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.

👉 Python essential development tools 👈

If a worker wants to do well, he must sharpen his tools first. The development software commonly used to learn Python is here, which saves you a lot of time.

👉 Python boutique PDF eBook 👈

Read some books or handwritten notes compiled by predecessors. These notes record their understanding of some technical points in detail. These understandings are unique and can learn different ideas.

👉 Python learning video 👈

Watching zero basic learning video is the quickest and most effective way to learn. It's still easy to get started with the teacher's ideas in the video from basic to in-depth.

👉 Actual combat cases 👈

Optical theory is useless. We should learn to knock together and practice, so as to apply what we have learned to practice. At this time, we can make some practical cases to learn.

👉 Python exercises 👈

Check the learning results.

👉 Interview materials 👈

We must learn Python in order to find a high paying job. The following interview questions are the latest interview materials from front-line Internet manufacturers such as Ali, Tencent and byte, and Ali has given authoritative answers. After brushing this set of interview materials, I believe everyone can find a satisfactory job.

summary
I have all the information ready for you. If you need partners, please scan the csdn official QR code below to get it for free.

I hope my learning experience can help you to make more friends with people in a circle and facilitate entrepreneurship in the future. After all, there is no end to working for people all your life.

Keywords: Python

Added by paul_r on Thu, 06 Jan 2022 02:37:17 +0200