我有一个250+敌方外星人的滚动游戏,我想实现基本的群体行为,甚至可能是目标导向的寻路。 为此,我认为实现一个tile系统将是最好的。问题是,获取每个外星人的位置并使用它来“阻止”它所在的磁贴,对于我的实现来说太慢了。 您能否帮助我更好地实施或了解如何实现更好的性能
我创建了一个tilemap,它是一个数组self.tiles
,包含作为字典的tile,每个tile是一个16px的矩形。(每个外星人大约16像素宽)。
为了检索每个磁贴,我有一个数组self.chunks_location
,包含所有磁贴坐标
每个外星人将其(x,y)传递给下面的函数,将其(x,y)转换为16的multipel。
然后在self.chunks_location
中查找转换后的(x,y),并使用检索到的索引来阻止self.tiles
中相应的磁贴
功能:转换(x,y)
(该函数最初用于输出多个磁贴坐标,如果外星人正好位于两个磁贴之间,那么现在看起来有点奇怪。)
def get_chunks_from_cord(self, x, y):
"""
Returns the chunk or chunks for the given location on the map, based on the tilesize of the map.
Being in between chunks leads to multiple chunks being returned.
(float or int) -> [tuple(int, int)]
(20, 58) -> [[16, 68]]
"""
"""
Adds the cords separate rounded to 16 to tilesize coordinates to the blocked list.
"""
decimal_16 = [x % self.chunksize, y % self.chunksize]
blocked = [[], []] # [[x values], [y values]]
for cnt, cord in enumerate((x, y)):
if decimal_16[cnt] >= self.chunksize_05:
blocked[cnt].append(cord + self.chunksize - decimal_16[cnt])
else:
blocked[cnt].append(cord - decimal_16[cnt])
result = []
for array in blocked[0]:
for var in blocked[1]:
result.append((round(array), round(var)))
return result
功能块磁贴
基于先前转换的坐标块平铺
def block_chunks(self, array):
if len(array) == 1:
target = self.chunks_location.index(array[0])
self.chunks[target]["blocked"] = True
self.chunks_blocked.append(self.chunks[target])
else:
for cords in array:
target = self.chunks_location.index(cords)
self.chunks[target]["blocked"] = True
self.chunks_blocked.append(self.chunks[target])
目前没有回答
相关问题 更多 >
编程相关推荐