0

为了实现 Frustum Culling 算法,我尝试做的是从取决于玩家 x 和 y 位置的位置开始 render() 函数中的嵌套 for 循环,这样代码只会循环通过.tmx 文件的一小部分必须渲染的部分。现在的问题是,如何从取决于我的 Player 坐标的位置开始循环?在此先感谢您的帮助。

import pygame
import pytmx
pygame.init()

class Map():
    def __init__(self,filename):
        tm=pytmx.load_pygame(filename,pixelalpha=True)
        self.width=tm.width * tm.tilewidth
        self.height=tm.height*tm.tileheight
        self.tmxdata=tm

    def render(self,surface):
        ti=self.tmxdata.get_tile_image_by_gid
        for layer in self.tmxdata.visible_layers:
            if isinstance(layer,pytmx.TiledTileLayer):
                for x,y,gid in layer:
                    tile = ti(gid)
                    if tile
                        surface.blit(tile,(x*self.tmxdata.tilewidth,y*self.tmxdata.tileheight))
    def make_map(self):
        temp_surface=pygame.Surface((self.width,self.height))
        self.render(temp_surface)
        return temp_surface
4

1 回答 1

1

如果tilemap 大小是恒定的(即每个tile 具有相同的宽度和高度),您可以计算世界上某个点的当前tilemap 坐标。

假设我们有 20 像素宽和 20 像素高的图块,并且您的玩家位于 (432, 36) 位置。然后我们可以通过整数除法(除法和截断/“删除小数”)找到他/她所在的瓷砖。

tile_x = player_x // tile_width
tile_y = player_y // tile_height

在我们的例子中,玩家将站在21x 轴 ( 432 // 20 = 21) 上的图块上,y 轴 ( ) 上的图块 1 上36 // 20 = 1

从那里你可以定义你想要在玩家周围有多大的可见区域。如果要在每个方向渲染 1 个图块(包括玩家站立的图块),则必须遍历x = 20 to 22y = 0 to 2

于 2019-08-31T07:10:26.783 回答