Create Entities Reduce FPS And Overlapping









up vote
0
down vote

favorite












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:



enter image description here










share|improve this question

























    up vote
    0
    down vote

    favorite












    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:



    enter image description here










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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:



      enter image description here










      share|improve this question













      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:



      enter image description here







      python pyglet python-3.7






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 23:21









      BlackFenix06

      127212




      127212



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254239%2fcreate-entities-reduce-fps-and-overlapping%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254239%2fcreate-entities-reduce-fps-and-overlapping%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto