I am having a problem with an entity class. Below is the script for the entity class:
import uuid, pyglet, random
class overworld_0001():
resources = {}
def __init__(self, engine, position, type_p, gender, **kwargs):
super(overworld_0001, self).__init__(**kwargs)
self.engine = engine
self.type_entity = "pk"
self.id = uuid.uuid4()
self.type_p = type_p
self.gender = gender
self.x = position[0]
self.y = position[1]
self.target_x = self.x
self.target_y = self.y
self.stat = "stand"
self.direction = 3
self.overworld = True
self.speed = engine.dict_pokemon["0001"]["overworld"]["speed"]
resource = engine.resouces["0001"]
self.resources["overworld"] = {}
if not type_p + "-" + gender in resource["overworld"]:
typ = resource["overworld"][type_p]
else:
typ = resource["overworld"][type_p + "-" + gender]
for stat in typ:
self.resources["overworld"][stat] = {}
for count in typ[stat]:
sprite = pyglet.sprite.Sprite(img=typ[stat][count], x=self.x, y=self.y)
sprite.target_x = self.target_x
sprite.target_y = self.target_y
self.resources["overworld"][stat][count] = sprite
self.current_sprite = self.resources["overworld"][self.stat][self.direction]
self.width = self.current_sprite.width
self.height = self.current_sprite.height
self.size = max(self.width, self.height)
def update(self, dt):
self.entity = self.engine.list_sprite_layers["3"]
if self.overworld:
self.update_overworld(dt)
def is_tile_free(self, x, y):
for entity in self.engine.list_sprite_layers["3"]:
if entity.id != self.id:
if (x < entity.x + entity.width and
x + self.width > entity.x and
y < entity.y + entity.height and
y + self.height > entity.y):
return False
return True
def update_overworld(self, dt):
if self.stat == "stand":
directions = {
0: (0, self.size),
1: (self.size, 0),
2: (-self.size, 0),
3: (0, -self.size)
}
possible_moves = []
for direction, (dx, dy) in directions.items():
new_x = self.x + dx
new_y = self.y + dy
if self.is_tile_free(new_x, new_y):
possible_moves.append((direction, new_x, new_y))
if possible_moves:
chosen_move = random.choice(possible_moves)
self.direction, self.target_x, self.target_y = chosen_move
self.stat = "run"
self.current_sprite.x = self.x
self.current_sprite.y = self.y
else:
self.stat = "stand"
if self.stat == "run":
if self.target_x != self.x:
direction_x = (self.target_x - self.x) / abs(self.target_x - self.x)
self.x += direction_x * self.speed * dt
if abs(self.target_x - self.x) < self.speed * dt:
self.x = self.target_x
if self.target_y != self.y:
direction_y = (self.target_y - self.y) / abs(self.target_y - self.y)
self.y += direction_y * self.speed * dt
if abs(self.target_y - self.y) < self.speed * dt:
self.y = self.target_y
if self.x == self.target_x and self.y == self.target_y:
self.stat = "stand"
self.current_sprite.x = self.x
self.current_sprite.y = self.y
#self.current_sprite = self.resources["overworld"][self.stat][self.direction]
self.current_sprite.x = self.x
self.current_sprite.y = self.y
def on_draw(self):
self.current_sprite.draw()
I know the rules say to put a script with the error in order to reproduce it, and I would like to do that, but unfortunately I don’t know what the error is.
As is, it works and has no problems: Image
But if in update_overworld
I insert self.current_sprite = self.resources["overworld"][self.stat][self.direction]
this is what i get: Image
Now, I don’t know what the error is, but I assume there could be two, this is because the entity works when it is single, but when I create two or more it starts to do what you see in the image (and the more I create entities, the worse it gets).
-
Or in the init, when the texture taken from the loaded resources is taken and transformed into a sprite, instead of creating a single sprite, each entity shares the sprite and each entity modifies those of the others.
-
In the update_overworld function of each entity, in addition to modifying itself, it also modifies those of the others.
These two hypotheses come to me, mostly because if the entity is single, the problem does not occur. Do you have any opinions or help? What I am using is Pyglet 2.0.16 and Python 3.12