I would have two questions to propose. In the project I am working on, I have created a class that deals with all the entities (enemies) that are created. This is the class (if you need anything else, tell me):
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)
First question: When, with the main class, I change the status of self.ent_made
(from False
to True
), the FPS from 60 drops drastically to 4. I assume that the entity creation function eats too much RAM, so how can I improve it to avoid this drastic drop in FPS?
Second question: how can I generate the entities, so that they are not superimposed on each other?
Here is a demonstration of what happens. The FPS are at 40 simply because I was recording, otherwise I am at 60. Moreover, for the second question, it happens above all when the created entities are many. With 10 (limit put only to test at the time) it is difficult to happen: