我有两个问题要提出。在我正在从事的项目中,我创建了一个类,用于处理所创建的所有实体(敌人)。这是课程(如果您需要其他什么,请告诉我):
class entity():
def __init__(self, main):
self.main = main
self.main.update_list.append(self.update)
self.main.draw_list.append(self.on_draw)
self.entity_spawn = []
self.ent_made = False
def create_entity(self, pos_x, pos_y, obj):
data = libraries.pokémon_data(obj)
entity = OrderedDict()
image = self.__resource(obj, "img")
entity["name"] = obj
entity["tile"] = 0
entity["image"] = OrderedDict()
entity["texture"] = OrderedDict()
entity["sprite"] = OrderedDict()
for obj, item in data["img"]["battle"].items():
entity["tile"] = item["tile"]
entity["image"][obj] = OrderedDict()
entity["texture"][obj] = OrderedDict()
entity["sprite"][obj] = OrderedDict()
id = 0
for idy in range(len(item["y"])):
y = item["tile"] * item["y"][idy]
list_frame_img = []
list_frame_texture = []
for idx in range(len(item["x"])):
x = item["tile"] * item["x"][idx]
frame = image.get_region(x=x, y=y, width=item["tile"], height=item["tile"])
list_frame_img.append(frame)
texture = frame.get_texture()
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
if self.main.dimension != self.main.min:
tile = self.__tiles(48)
else:
tile = 48
texture.width = tile
texture.height = tile
texture.anchor_x = texture.width // 2
texture.anchor_y = texture.height // 2
list_frame_texture.append(texture)
if len(list_frame_img) > 1:
entity["image"][obj][id] = pyglet.image.Animation.from_image_sequence(list_frame_img, 0.5, True)
else:
entity["image"][obj][id] = list_frame_img[0]
if len(list_frame_texture) > 1:
entity["texture"][obj][id] = pyglet.image.Animation.from_image_sequence(list_frame_texture, 0.5, True)
else:
entity["texture"][obj][id] = list_frame_texture[0]
entity["sprite"][obj][id] = pyglet.sprite.Sprite(entity["texture"][obj][id])
entity["sprite"][obj][id].x = pos_x
entity["sprite"][obj][id].y = pos_y
id += 1
return entity
def __tiles(self, obj):
list = [self.main.min, self.main.max]
res = []
for i in range(len(list)):
x = list[i][0]
y = list[i][1]
ds = (x - y) / (y / 100)
dk = (obj / 100) * ds
tile = obj + int(dk)
res.append(tile)
if (res[0] % res[1]) == 0:
return res[0]
else:
return max(res[0], res[1])
def __resource(self, obj, type):
res = type + obj
if res in resources[type]:
return resources[type][type+obj][1]
else:
self.__error(res)
return resources[type][type+"0000"][1]
def __error(self, obj):
date = datetime.datetime.now()
self.main.error_engine.append([date.strftime("%d-%m-%Y %H:%M:%S"), obj + " missing"])
def on_draw(self):
for entity in self.entity_spawn:
entity["sprite"]["idle"][0].draw()
def update(self):
if self.ent_made:
if len(self.entity_spawn) < 10:
x = random.randint(50, self.main.dimension[0] - 50)
y = random.randint(50, self.main.dimension[1] - 50)
entity = self.create_entity(x, y, "0001")
self.entity_spawn.append(entity)
第一个问题:当我在主类中将self.ent_made
的状态(从False
更改为True
)时,FPS从60急剧下降到4。实体创建功能占用了太多的RAM,那么如何改进它以避免FPS的急剧下降?
第二个问题:我该如何生成实体,以免它们彼此重叠?
这是发生的情况的演示。 FPS之所以是40,是因为我当时在录音,否则我是60。另外,对于第二个问题,当创建的实体很多时,它首先会发生。只有10个(当时仅测试极限),很难发生: