Python3 programming actual Tetris robot (project structure)


Series entry

"Python3 Programming Actual Tetris Robot"

Project structure

The structure of the project is as follows. The code is organized as a module package in the form of a directory, and execution in a directory or package is supported at the same time. Both methods have a unified entry.

|---- .editorconfig
|---- .gitignore
|---- .vscode
|    |---- launch.json
|    |---- settings.json
|---- LICENSE.rst
|---- tetris
|    |----
|    |----
|    |----
|    |----
|    |----
|    |----
|    |----

Implementation details

Our program is located in the tetris directory (package) and can be executed as a folder:

python tetris

It can also be executed as a package:

python -m tetris

If python treats a folder as a Package, then this folder must contain a file named, even if it is empty.
We define main() in this file as the unified entrance of the software.

from import start   # 这个tetris目录下的文件都位于tetris包中了

def main():
    print("Hi I'm Tetris!")
    start()                    #真实入口在app.py中

For python to run a folder, this folder must contain a file named
Call main() in in to unify the program entry.

import tetris

There is no problem running in package mode at this time, but an error will occur when running in directory mode:

Traceback (most recent call last):
  File "C:\Users\zhoutk\AppData\Local\Programs\Python\Python39\lib\", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\zhoutk\AppData\Local\Programs\Python\Python39\lib\", line 87, in _run_code
    exec(code, run_globals)
  File "d:\codes\ptetris\tetris\", line 7, in <module>
    import tetris
  File "d:\codes\ptetris\tetris\", line 2, in <module>
    from tetris.config import *
ModuleNotFoundError: No module named 'tetris.config'; 'tetris' is not a package

The reason is that the first search path of sys.path is a package and the other is an empty string. For the calling method of python -m tetris, since is loaded in advance, the python interpreter already knows that this is a package.
Therefore the current path (empty string) is included in sys.path. Then call, then import pkg package will be no problem.
For the calling method of python tetris, the python interpreter does not know that it is working under a Package. By default, the python interpreter adds the current path tetris of to sys.path, and then looks for the tetris module under this path, so an error occurs.
Add the following code at the beginning of to solve this problem:

import os, sys

if not __package__:
  path = os.path.join(os.path.dirname(__file__), os.pardir)
  sys.path.insert(0, path)

vscode configuration

vscode is still very good to write python, do two-step operation, you can write python programs well.

Install plugin

Just install the ms-python.python plug-in, and the other related ones will be installed automatically.

Configure launch.json

"configurations": [
            "name": "Tetris",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/tetris",
            "console": "integratedTerminal"

In this way, you can use vscode to open the ptetris directory and press F5 to run the program.

project address

Operation method

1. install python3, git
2. git clone (or download and unzip source code)
3. cd ptetris
4. python3 tetris

This project surpport windows, linux, macOs

on linux, you must install tkinter first, use this command:  
sudo apt install python3-tk

Related items

C++ version has been implemented, project address:
阅读 1.4k

自由程序员,技术路线c,delphi, c++,c#,java,php,node.js,python,golang,typescript;超喜欢re...


2.6k 声望
1.2k 粉丝
0 条评论


2.6k 声望
1.2k 粉丝