Series entry
"Python3 Programming Actual Tetris Robot"
interface design
This project focuses on algorithm implementation. In order to install simple and have strong cross-platform, so choose the built-in python tkinter. This interface library has some problems. For example, if it is found to have a problem with the thread lock later, it will die inexplicably. In the lockbug branch of the project, I have located the problem and performed a series of log output. Interested students can go to study the specific problem. This problem can be avoided. The interface features used in this project are few, and my proposition is enough.
Design ideas
Use two canvases to display the blocks, one is the game space, and the other is the next block display. Because the interface is relatively simple, it is divided into three parts, one is the game space, the other is the next square display, and the third is the information that is the operation button. These three blocks directly use place for absolute positioning, and the third block uses frame to combine elements such as Label and Button.
Interface effect
Implementation
Window setting
def start():
root = Tk()
root.title("Tetris")
root.geometry('470x630') # 窗口大小,中间是字母x表示乘号
root.resizable(0, 0) # 窗口大小固定
App(root)
root.mainloop()
Two canvas
from tkinter import *
from tkinter import ttk
# 常数CANVASOFFSET,目的是留一点边框空隙,在不同的操作系统上表现不一致,用空隙协调
self.gameCanvas = Canvas(root, bg='black', height=600 + CANVASOFFSET * 2, width=300 + CANVASOFFSET * 2)
self.gameCanvas.place(x=12, y=10) # 绝对定位
# 父窗口 背景设为黑色
self.nextCanvas = Canvas(root, bg='black', height=120 + CANVASOFFSET * 2, width=120 + CANVASOFFSET * 2)
self.nextCanvas.place(x = 330, y = 10)
Information operation unit
Use frame to combine canvas objects and anchor method for layout. Side method cannot be used for this piece. Pay attention to the difference between the two methods.
frame = Frame(root)
frame.place(x = 330, y = 160)
Label(frame, text = "SPEED:").pack(anchor="w") # 使用anchor方式布局
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.set("Start") # 开始游戏按钮
Button(frame, height=1, width=10, command=self.btnStartClicked, textvariable=self.btnStartVar).pack(anchor="w")
# 绑定点击事件
Select playback parameters from the drop-down box:
coboxVar = StringVar # 注意这没有圆括号,与前面的游戏速度参数比较
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.comboxClicked) # 绑定单击事件
Game space binding keyboard response:
self.gameCanvas.bind(sequence="<Key>", func=self.processKeyboardEvent) # 绑定键盘响应
self.game = Game(self.gameCanvas, self.nextCanvas, self) # 初始化游戏对象
self.gameCanvas.focus_set() # 设置游戏空间为焦点对象
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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。