Series entry

Programming Tetris Robot in Python3

Interface design

This project focuses on algorithm implementation. For simple installation and strong cross-platform, we choose Tkinter, which is built into Python. There are some problems with the interface library, such as later found that it works with the thread lock problem, will somehow die. The Lockbug branch of the project, I’ve identified the problem, and I’ve done a series of log outputs, so those of you who are interested can study the specific problem. This problem can be avoided; this project uses very few interface features, and my claim is sufficient.

Design ideas

Use two canvases to display the square, one for the game space and the other for the next square display. Because the interface is relatively simple, it is divided into three parts: the first is the game space, the second is the display of the next box, and the third is the information that is the operation button. These three blocks directly use place for absolute positioning, while the third block uses frame to combine elements such as Label and Button.

Effect of interface

The specific implementation

Set the window

def start(): Root = Tk() root.title("Tetris") root.geometry('470x630') # window size, The letter X in the middle stands for the sign rot.resizable (0, 0) # Window size fixed App(root) rot.mainloop ()

Two canvas

From tkinter import * from tkinter import TTK # CANVASOFFSET, to leave a bit of border space, not uniform on different operating systems, Self. Gamecanvas = Canvas(Root, BG =' Black ', Height =600 + CanvasOffset * 2, Width =300 + CanvasOffset * 2) self. Gamecanvas. Place (x=12, y=10) bg='black', height=120 + CANVASOFFSET * 2, width=120 + CANVASOFFSET * 2) self.nextCanvas.place(x = 330, y = 10)

Information operation unit

Use a Frame to compose the Canvas object, use an Anchor to lay it out. This piece cannot be Side. Note the difference between the two.

frame = Frame(root) frame.place(x = 330, y = 160) Label(frame, Text =" SPEED:").pack(anchor="w") self.speedVar = stringVar () Speed = Label(Frame, Height =1, Width =12, Relief = Sunken, BD =1, textvariable=self.speedVar) speed.pack(anchor="w")

Checkboxes and buttons:

Checkbutton(frame, Text =" AutoPlay").pack(anchor="w") #}} self.btnStartVar = stringVar () self.btnStartVar Button(frame, height=1, width=10, command= self.btnStartClicks, textVariable =self.btnStartVar).pack(anchor="w") # Bind click events

Drop down box to select playback parameters:

CoBoxVar = stringVar # Notice there's no parentheses here, Cobox = ttk.Combobox(frame, textVariable =coboxVar, height=5, width=8, State =" readOnly ") cobox.pack(anchor="w") Cobox ["value"] = ("last", "one", "two", "three") cobox.current(0) # Cobox.bind ("<< ComboBoxSelected >>", Self.com boxClicked) # Bind click events

Game space binding keyboard response:

Self. GameCanvas. Bind sequence = "< Key >", func = self. ProcessKeyboardEvent) # binding keyboard in response to the self. The game = game (self gameCanvas, Self. NextCanvas, self) # Initialize the GameObject self.gamecanvas.focus_set () # Set the GameSpace as the focus object

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