Python3 programming actual Tetris robot (project structure)

zhoutk
中文

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.

\ptetris
|---- .editorconfig
|---- .gitignore
|---- .vscode
|    |---- launch.json
|    |---- settings.json
|---- LICENSE.rst
|---- README.md
|---- tetris
|    |---- app.py
|    |---- block.py
|    |---- config.py
|    |---- game.py
|    |---- tetris.py
|    |---- __init__.py
|    |---- __main__.py

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

__init__.py

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

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

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

__main__.py

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

import tetris
tetris.main()

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\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\zhoutk\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "d:\codes\ptetris\tetris\__main__.py", line 7, in <module>
    import tetris
  File "d:\codes\ptetris\tetris\tetris.py", 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 __init__.py 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 __main__.py, 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 __main__.py 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 __main__.py 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

https://gitee.com/zhoutk/ptetris
或
https://github.com/zhoutk/ptetris

Operation method

1. install python3, git
2. git clone https://gitee.com/zhoutk/ptetris (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:

https://gitee.com/zhoutk/qtetris
阅读 1.4k

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

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

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

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

2.6k 声望
1.2k 粉丝
文章目录
宣传栏