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

2024-09-30 05:21:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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)

在我的代码中,这些空白会出现在哪里?在


Tags: therectself游戏fordtparentint
3条回答

这里有一个更好的解决方案。当玩家移动时,只需将velocity添加到panning向量中,而不要移动瓷砖。然后,当分片被blitting以获得正确的位置时,平移被添加到tiles的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不能保留每个值,而且我们有{}的情况,它会使预期结果和获得的结果不同。在

您只能使用整数值来更改所有平铺的位置,即

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

这样你只会丢失很小的值(小于一个像素),但是如果你累积了很多小值,你就可以得到很少的像素。在

所以你可以尝试累积浮点值和整数值之间的差异。在

我不想这样做,但如果我做不到的话

^{pr2}$

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

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

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

解决方案是给你的资产添加填充。在

我的方法如下:

  1. 下载并安装GIMP

  2. 安装可以从here添加填充的插件。

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

无需更改任何资产的名称。只需添加填充和覆盖。在

我希望这对你有帮助,如果你有任何进一步的问题,请随时在下面发表评论!在

相关问题 更多 >

    热门问题