PyGame:平移平铺地图会导致间隙



我在Python 3.4.3(Win8.1)上使用PyGame 1.9.2。

我的游戏窗口由使用 48x48 像素图块的平铺地图组成。游戏的框架使用游戏框架类的以下方法根据玩家输入移动瓷砖:

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)

虽然"self.panning_speed"是不言自明的,但self.panning是一个包含x和y平移两个值的列表(例如,[0, 0] = 无平移,[-1, 1] = 左下平移等)。

然后将tile.x/tile.y值"int'ed"用作rect和pos(后者用于块传输)的x-/y值。

我在每个瓷砖的 x/y 值中添加完全相同的数量,然后在它们上使用"int",据我所知,它总是将浮点数降低到下一个较低的 int。因此,从技术上看,每个图块的 x/y 值的相对变化与任何其他图块的 x/y 值完全相同。

然而:我仍然不时在行或列之间出现间隙!这些间隙看起来不规则,但非常明显,当您停止平移时,间隙可见时,该间隙将一直保持到您再次平移。

有关示例,请参阅此图像: 有关示例,请参阅此图像(红色方块中的黑线是间隙;金色和粉红色是瓷砖)

在我的代码中,这些差距会出现在哪里?

这是一个应该效果更好的解决方案。只需在玩家移动时将velocity添加到panning矢量中,切勿移动图块。然后,当拼贴被拼接以获得正确的位置时,将平移添加到磁贴的rect.topleft位置。必须先将平移转换为整数(创建另一个矢量,在示例中称为offset),然后再将其添加到磁贴位置,否则仍会出现间隙。

import itertools
import pygame as pg
from pygame.math import Vector2

TILE_SIZE = 44
TILE_IMG1 = pg.Surface((TILE_SIZE, TILE_SIZE))
TILE_IMG1.fill(pg.Color('dodgerblue3'))
TILE_IMG2 = pg.Surface((TILE_SIZE, TILE_SIZE))
TILE_IMG2.fill(pg.Color('aquamarine3'))

class Tile(pg.sprite.Sprite):
def __init__(self, pos, image):
super().__init__()
self.image = image
self.rect = self.image.get_rect(topleft=pos)
self.pos = Vector2(pos)

def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
dt = 0
image_cycle = itertools.cycle((TILE_IMG1, TILE_IMG2))
game_map = pg.sprite.Group()
# Create tile instances and add them to the game map group.
for y in range(16):
for x in range(20):
image = next(image_cycle)
game_map.add(Tile((x*TILE_SIZE, y*TILE_SIZE), image))
next(image_cycle)
panning = Vector2(0, 0)
velocity = Vector2(0, 0)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_a:
velocity.x = 100
elif event.key == pg.K_d:
velocity.x = -100
elif event.type == pg.KEYUP:
if event.key == pg.K_d and velocity.x < 0:
velocity.x = 0
elif event.key == pg.K_a and velocity.x > 0:
velocity.x = 0
# Add the velocity to the panning when we're moving.
if velocity.x != 0 or velocity.y != 0:
panning += velocity * dt
game_map.update()
screen.fill((30, 30, 30))
# Assigning screen.blit to a local variable improves the performance.
screen_blit = screen.blit
# Convert the panning to ints.
offset = Vector2([int(i) for i in panning])
# Blit the tiles.
for tile in game_map:
# Add the offset to the tile's rect.topleft coordinates.
screen_blit(tile.image, tile.rect.topleft+offset)
pg.display.flip()
dt = clock.tick(30) / 1000

if __name__ == '__main__':
pg.init()
main()
pg.quit()

另一种选择是在程序启动时将瓷砖布块放在大背景表面上。这样你就不会得到差距,这也提高了性能。

我认为问题是float无法保留每个值,我们的情况是0.1 + 0.2 != 0.3它可以在预期结果和获得的结果之间产生差异。

您只能使用整数值来更改所有磁贴的位置,即。

tile.rect.x = int(xmove)
tile.rect.y = int(ymove)

这样,您只能丢失小值(小于一个像素),但是如果您累积了许多小值,则可以获得很少的像素。

因此,您可以尝试累积浮点值和整数值之间的差异。

我无法检查这是否正常工作,但我会做这样的事情

def moveTiles(self):
# add moves to already cumulated differences betweeen floats and integers
self.xcululated += self.parent.dt * self.panning[0] * self.panning_speed
self.ycululated += self.parent.dt * self.panning[1] * self.panning_speed
# get only integer values
xmove = int(self.xcululated)
ymove = int(self.ycululated)
# remove integers from floats
self.xcululated += xmove
self.ycululated += ymove
for tile in self.game_map:
# use the same integer values for all tiles
tile.rect.x += xmove
tile.rect.y += tmove

我认为主要问题是您的游戏资产没有填充

资产中的填充非常重要,尤其是在使用相机和浮点值时。

有时,使用摄像机时,浮点值无法提供所需的精度,并可能导致纹理渗色或您在游戏中不时看到的线条。

解决方案是向资产添加填充。

我这样做的方式如下:

  1. 下载并安装 GIMP

  2. 安装一个可以从此处添加填充的插件。

  3. 加载每个资产并使用 GIMP 中的插件添加填充。

无需更改任何资产的名称。只需添加填充并覆盖即可。

我希望这对您有所帮助,如果您有任何其他问题,请随时在下面发表评论!

最新更新