今天来完毕绘制矢量图形。

没有读过前几章的同学,请先阅读前几章:

Python游戏引擎开发(一):序

Python游戏引擎开发(二):创建窗体以及重绘界面

Python游戏引擎开发(三):显示图片

Python游戏引擎开发(四):TextField文本类

Python游戏引擎开发(五):Sprite精灵类和鼠标事件

Python游戏引擎开发(六):动画的小小研究

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,
"lineColor" : None,
"fillColor" : color,
"fillAlpha" : 255 * alpha,
"joins" : None,
"caps" : None,
"miterLimit" : None
}

開始绘制命令须要例如以下几个參数:图形填充色、填充色透明度。

在上面的代码中,我们初始化了__currentGraphics属性。能够看到,他是一个dict对象。当中的path成员是一个QPainterPath对象。这个对象来自Qt,通过调用这个类中的一些方法,能够创建一些图形,然后调用QPainterdrawPath方法就能够把这个对象里创建的全部图形画出来。

增加endFill方法。用于把当前图形保存到__drawingList中,保存到__drawingList后。就能够使其显示出来:

def endFill(self):
if not self.__currentGraphics:
return self.__currentGraphics["path"].setFillRule(QtCore.Qt.WindingFill) self.__drawingList.append(self.__currentGraphics)

然后是_show方法,在前面的章节中介绍过,每一个显示在界面上的对象都有这种方法,用于显示自身:

def _show(self, c):
for item in self.__drawingList:
if not isinstance(item, dict):
return path = item["path"] if not path:
continue lineWidth = item["lineWidth"]
lineColor = item["lineColor"]
fillColor = item["fillColor"]
joins = item["joins"]
caps = item["caps"]
miterLimit = item["miterLimit"]
fillAlpha = item["fillAlpha"]
lineAlpha = item["lineAlpha"] brush = None
pen = QtGui.QPen() c.save() if lineWidth:
pen.setWidth(lineWidth)
else:
pen.setWidth(0) if lineColor:
color = getColor(lineColor) if isinstance(color, QtGui.QColor):
if lineAlpha:
color.setAlpha(lineAlpha) pen.setColor(color)
else:
pen.setColor(getColor("transparent")) if joins:
pen.setJoinStyle(joins) if caps:
pen.setCapStyle(caps) if miterLimit:
pen.setMiterLimit(miterLimit) if fillColor:
color = getColor(fillColor) if fillAlpha and hasattr(color, "setAlpha"):
color.setAlpha(fillAlpha) brush = QtGui.QBrush(color) brush.setStyle(QtCore.Qt.SolidPattern) c.setBrush(brush) c.setPen(pen)
c.drawPath(path) c.restore()

当中,我们遍历了__drawingList,从中读取每一个图形数据,然后依据数据进行一些图形样式设置。最后drawPath画出图形。

上面的代码主要完毕了基础的一些部分,眼下我们仅仅有開始画图和结束画图命令。

还差设置样式以及增加图形的命令,通过下面代码增加:

def lineStyle(self, thickness = 1, color = "black", alpha = 1, joints = None, caps = None, miterLimit = 3):
if not self.__currentGraphics:
return if color == "transparent":
alpha = 0 if joints == JoinStyle.ROUND:
joints = QtCore.Qt.RoundJoin
elif joints == JoinStyle.MITER:
joints = QtCore.Qt.MiterJoin
elif joints == JoinStyle.BEVEL:
joints = QtCore.Qt.BevelJoin if caps == CapsStyle.NONE:
caps = QtCore.Qt.FlatCap
elif caps == CapsStyle.SQUARE:
caps = QtCore.Qt.SquareCap
elif caps == CapsStyle.ROUND:
caps = QtCore.Qt.RoundCap self.__currentGraphics["lineWidth"] = thickness
self.__currentGraphics["lineColor"] = color
self.__currentGraphics["lineAlpha"] = 255 * alpha
self.__currentGraphics["joints"] = joints
self.__currentGraphics["caps"] = caps
self.__currentGraphics["miterLimit"] = miterLimit def moveTo(self, x, y):
if not self.__currentGraphics:
return self.__currentGraphics["path"].moveTo(x, y) def lineTo(self, x, y):
if not self.__currentGraphics:
return self.__currentGraphics["path"].lineTo(x, y) def drawRect(self, x, y, width, height):
if not self.__currentGraphics:
return self.__currentGraphics["path"].addRect(x, y, width, height) def drawCircle(self, x, y, radius):
self.drawEllipse(x - radius, y - radius, radius * 2, radius * 2) def drawEllipse(self, x, y, width, height):
if not self.__currentGraphics:
return self.__currentGraphics["path"].addEllipse(x, y, width, height)

有了这些命令,就能够进行画图操作了。

Sprite上使用Graphics

Graphics主要是在Sprite上使用,例如以下代码所看到的:

layer = Sprite()
layer.graphics.beginFill("#FF0000")
layer.graphics.drawRect(0, 0, 200, 200)
layer.graphics.endFill()
addChild(layer)

可见我们须要为Sprite增加一个graphics属性,用于操作矢量图形,所以在Sprite构造器中增加例如以下代码:

self.graphics = new Graphics()
self.graphics.parent = self

通过上面的这些命令,我们就能够创建出很多不同的矢量图形:

绘制矢量图的功能就搞定了~如有不懂之处,欢迎留言。


至此,引擎基本功能就实现了

Github地址:https://github.com/yuehaowang/pylash_engine


欢迎大家继续关注我的博客

转载请注明出处:Yorhom’s Game Box

http://blog.csdn.net/yorhomwang

Python游戏引擎开发(七):绘制矢量图的更多相关文章

  1. Python游戏引擎开发(五):Sprite精灵类和鼠标事件

    本次来实现Sprite类和鼠标事件. 说起这个Sprite啊,涉及过2D游戏研究领域的看官应该都听说过它. 它中文原意是"精灵",只是在不同人的眼中,它所表示的意义不同. 比方说在 ...

  2. 【Cocos2d-x游戏引擎开发笔记(25)】XML解析

    原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/9128819 XML是一种非常重要的文件格式,由于C++对XML的支持非常完善 ...

  3. 【译】在Asp.Net中操作PDF - iTextSharp - 绘制矢量图

    原文 [译]在Asp.Net中操作PDF - iTextSharp - 绘制矢量图 在上一篇iTextSharp文章中讲述了如何将现有的图片插入PDF中并对其进行操作.但有时,你需要在PDF中绘制不依 ...

  4. 推荐一些好用的 HTML5 & JavaScript 游戏引擎开发库

    推荐一些好用的 HTML5 & JavaScript 游戏引擎开发库 0. 引言 如果你是一个游戏开发者,并且正在寻找一个可以与 JavaScript 和 HTML5 无缝工作的游戏引擎.那么 ...

  5. 【python】pandas & matplotlib 数据处理 绘制曲面图

    Python matplotlib模块,是扩展的MATLAB的一个绘图工具库,它可以绘制各种图形 建议安装 Anaconda后使用 ,集成了很多第三库,基本满足大家的需求,下载地址,对应选择pytho ...

  6. 25 个超棒的 HTML5 & JavaScript 游戏引擎开发库

    就像在汽车中,引擎完成主要的工作,使汽车看起来不可思议.游戏引擎同理,游戏开发者完成细节的工作,使游戏看起来真实.吸引人眼球.游戏引擎负责其余的事情.早期,游戏开发者通常从草图做起,花费高昂,且不容易 ...

  7. Photoshop 基础七 位图 矢量图 栅格化

    矢量图(CorelDraw)不是像素组成的,放大不会失真,体积小,颜色比较单一.由直线.曲线构成,画一些直线.曲线.多边形.图标. 位图(Photoshop画的就是位图),又像素组成,放大失真,放的越 ...

  8. Qt使用QPainter绘制矢量图并保存为svg文件

    位图和矢量图: Bitmap: Usually a larger file size Cannot be enlarged into a higher resolution as the image ...

  9. 【Android LibGDX游戏引擎开发教程】第06期:图形图像的绘制(下)图片整合工具的使用

    在上一篇文章中,我们提到了图片必须是2的n次方的问题.但是随着Libgdx的不断完善和发展,使用一些工具就 可以很好的解决了这样一个问题,但是它的功能又不仅仅只限于此,那么下面就来让我们看看Textu ...

随机推荐

  1. lr手工添加关联函数的步骤:

    点击“确定”后: 如何修改已经创建好的关联规则:

  2. es6解构赋值总结

    数组的解构赋值 1.简单的赋值方式 2.多维数组解构赋值 3.默认值,只有当右边对应位置为undefined时候才会选择默认(null不属于undefined) 4.左右不对等,会相应的对号入座,没有 ...

  3. ESLint 使用入门

    在团队协作中,为避免低级 Bug.产出风格统一的代码,会预先制定编码规范.使用 Lint 工具和代码风格检测工具,则可以辅助编码规范执行,有效控制代码质量. 在以前的项目中,我们选择 JSHint 和 ...

  4. 【BZOJ 4104】【THUSC 2015】解密运算

    http://www.lydsy.com/JudgeOnline/problem.php?id=4104 网上题解满天飞,我也懒得写了 #include<cstdio> #include& ...

  5. luogu P1060 开心的金明

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今天 ...

  6. BZOJ 4939 [Ynoi2016]掉进兔子洞(莫队+bitset)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4939 [题目大意] 给出一个数列,每个询问给出三个区间,问除去三个区间共有的数字外, ...

  7. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  8. 【贪心】【堆】AtCoder Grand Contest 018 C - Coins

    只有两维的时候,我们显然要按照Ai-Bi排序,然后贪心选取. 现在,也将人按照Ai-Bi从小到大排序,一定存在一个整数K,左侧的K个人中,一定有Y个人取银币,K-Y个人取铜币: 右侧的X+Y+Z-K个 ...

  9. Java学习笔记(10)

    目前多态情况下不能访问子类特有的成员 如果需要访问子类特有的成员,那么需要进行类型强制转换,转换为子类的类型然后再进行调用 基本数据类型的转换: 小数据类型------------->大数据类型 ...

  10. oracle增加表空间的四种方法

    1. 查看所有表空间大小 select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_nam ...