本游戏共两个文件:blocks.py和main.py。blocks.py定义各类方块,main.py定义游戏画面。
blocks.py
import randomfrom collections import namedtuplePoint = namedtuple('Point', 'X Y')Shape = namedtuple('Shape', 'X Y Width Height')Block = namedtuple('Block', 'template start_pos end_pos name next')# 方块形状的设计,我最初我是做成 4 × 4,因为长宽最长都是4,这样旋转的时候就不考虑怎么转了,就是从一个图形替换成另一个# 其实要实现这个功能,只需要固定左上角的坐标就可以了#更多小游戏请微信关注:Python代码大全# S形方块S_BLOCK = [Block(['.OO', 'OO.', '...'], Point(0, 0), Point(2, 1), 'S', 1), Block(['O..', 'OO.', '.O.'], Point(0, 0), Point(1, 2), 'S', 0)]# Z形方块Z_BLOCK = [Block(['OO.', '.OO', '...'], Point(0, 0), Point(2, 1), 'Z', 1), Block(['.O.', 'OO.', 'O..'], Point(0, 0), Point(1, 2), 'Z', 0)]# I型方块I_BLOCK = [Block(['.O..', '.O..', '.O..', '.O..'], Point(1, 0), Point(1, 3), 'I', 1), Block(['....', '....', 'OOOO', '....'], Point(0, 2), Point(3, 2), 'I', 0)]# O型方块O_BLOCK = [Block(['OO', 'OO'], Point(0, 0), Point(1, 1), 'O', 0)]# J型方块J_BLOCK = [Block(['O..', 'OOO', '...'], Point(0, 0), Point(2, 1), 'J', 1), Block(['.OO', '.O.', '.O.'], Point(1, 0), Point(2, 2), 'J', 2), Block(['...', 'OOO', '..O'], Point(0, 1), Point(2, 2), 'J', 3), Block(['.O.', '.O.', 'OO.'], Point(0, 0), Point(1, 2), 'J', 0)]# L型方块L_BLOCK = [Block(['..O', 'OOO', '...'], Point(0, 0), Point(2, 1), 'L', 1), Block(['.O.', '.O.', '.OO'], Point(1, 0), Point(2, 2), 'L', 2), Block(['...', 'OOO', 'O..'], Point(0, 1), Point(2, 2), 'L', 3),