Series entry

Programming Tetris Robot in Python3

The 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 the execution is supported as a directory or a package, both ways having 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

For Python to treat a folder as a Package, the folder must contain a file named __init__. Py, even if it is empty. In this file, we define main() as a unified entry point to the software.

Def main(): print("Hi I'm Tetris!"): print("Hi I'm Tetris!") ) start() # real entry in app.py

__main__.py

In order for Python to run a folder, this folder must contain a file named __main__. Py called main() in __init__. Py to unify the program entry.

import tetris
tetris.main()

Running as a package is fine, but running as a directory causes an error:

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 in sys.path is either a package or an empty string. Because __init__. Py is loaded in advance, the Python interpreter already knows that it is a package, so the current path (an empty string) is included in sys.path. Then we call __main__. Py, and there is no problem with import PKG. In the case of Python Tetris, the Python interpreter does not know that it is working under a Package. By default, the Python interpreter adds tetris, the current path of __main__. Py, to sys.path, and then looks for the module tetris below that path. This problem can be solved by adding the following code to the beginning of __main__. Py:

import os, sys

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

Vscode configuration

VC code to write Python is still very good, do two steps, you can be very good to write Python programs.

Installing a plug-in

Just install the ms-python.python plugin, and the others will be installed automatically.

Configuration launch. Json

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

This will open the PTetris directory with VS Code and press F5 to run the program.

The project address

https://gitee.com/zhoutk/ptetris or 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 projects

C++ version has been implemented, project address:

https://gitee.com/zhoutk/qtetris