我在python3.4.3(Win8.1)上使用pygame1.9.2。在
我的游戏窗口由一个使用48x48px平铺的地图组成。游戏的框架根据玩家的输入,使用游戏framwork类的以下方法移动瓷砖:
def moveTiles(self):
xmove = self.parent.dt * self.panning[0] * self.panning_speed
ymove = self.parent.dt * self.panning[1] * self.panning_speed
for tile in self.game_map:
tile.x += xmove
tile.y += ymove
tile.rect.x = int(tile.x)
tile.rect.y = int(tile.y)
tile.pos = (tile.rect.x, tile.rect.y)
同时“自摇摄速度“是不言自明的,自我平移是一个包含x和y平移的两个值的列表(例如[0,0]=不平移,[-1,1]=向左下平移,等等)。在
tile.x/tile.y值被“int'ed”用作rect和pos(后者用于blitting)的x-/y值。在
我给每个平铺的x/y值加上完全相同的量,然后我在它们上使用“int”,据我所知,它总是将浮点值下移到下一个较低的int值上。所以从技术上看,每个平铺的x/y值的相对变化与任何其他平铺的x/y值完全相同。在
但是:我仍然时不时地在行或列之间找到间隙!这些间隙不规则地出现,但非常明显,当您在看到间隙时停止平移时,该间隙将一直保持到您再次平移。在
请参见此图以获取示例: See this image for an example (black line in the red square is the gap; gold and pink are the tiles)
在我的代码中,这些空白会出现在哪里?在
这里有一个更好的解决方案。当玩家移动时,只需将
velocity
添加到panning
向量中,而不要移动瓷砖。然后,当分片被blitting以获得正确的位置时,平移被添加到tiles的rect.topleft
位置。在将平移添加到平铺位置之前,必须先将平移转换为整数(创建另一个矢量,在示例中称为offset
),否则仍将获得间隙。在另一种选择是在程序开始时将瓷砖快速地放到一个大的背景表面上。这样你就不会有差距,这也会提高性能。在
我认为问题是}的情况,它会使预期结果和获得的结果不同。在
float
不能保留每个值,而且我们有{您只能使用整数值来更改所有平铺的位置,即
这样你只会丢失很小的值(小于一个像素),但是如果你累积了很多小值,你就可以得到很少的像素。在
所以你可以尝试累积浮点值和整数值之间的差异。在
我不想这样做,但如果我做不到的话
^{pr2}$我认为主要的问题是你的游戏资产没有填充。在
填充在你的资产中非常重要,尤其是当你使用相机和浮动值时。在
有时使用摄影机时,浮点值无法提供所需的精度,并可能导致纹理出血或在游戏中不时看到的线条。在
解决方案是给你的资产添加填充。在
我的方法如下:
下载并安装GIMP
安装可以从here添加填充的插件。
使用GIMP中的插件加载每个资产并添加填充。
无需更改任何资产的名称。只需添加填充和覆盖。在
我希望这对你有帮助,如果你有任何进一步的问题,请随时在下面发表评论!在
相关问题 更多 >
编程相关推荐