今天来完成绘制矢量图形。
没有读过前几章的同学,请先阅读前几章:
Python游戏引擎开发(五):Sprite精灵类和鼠标事件
Graphics类
首先我们创建Graphics
类用于创建矢量图形:
class Graphics(DisplayObject):
def __init__(self):
super(Graphics, self).__init__()
# 存储所有图形数据的列表
self.__drawingList = []
# 用于储存当前图形数据
self.__currentGraphics = None
由于我们的窗口界面是在不断清除,然后重绘的,所以加入__drawingList
属性来储存所有图形的数据。而__currentGraphics
用于储存当前图形数据。
在flash
中,我们使用beginFill
方法来下达开始绘制命令。加入该方法:
def beginFill(self, color = "transparent", alpha = 1):
if color == "transparent":
alpha = 0
self.__currentGraphics = {
"path" : QtGui.QPainterPath(),
"lineAlpha" : 255,
"lineWidth" : None