12-20-2020, 10:06 PM
I think I found important things about what I want in the cide
But I have the impression that Actor.remove and world.object.remove are indefined
Also, seems I need ElementTree for the code, something I probably don't have in other languages. Should I use an external library for that?
Code:
def load_objects(file_name, layer_name, first_gid):
""" loads tiles in object layer from a tmx file.
Returns list of Item objects """
tree = ET.parse(file_name)
root = tree.getroot()
for child in root.findall("objectgroup"):
name = child.get("name")
if name == layer_name:
item_list = list()
for item in child.findall("object"):
gid = item.get("gid")
if gid is not None:
x = item.get("x")
y = item.get("y")
item_list.append(Item(int(gid) - first_gid, int(x), int(y)))
return item_list
return None
Code:
class Item(object):
""" Generic item declared in tilemap object layer awaiting to spawn """
Opossum, Eagle, Frog = range(3)
def __init__(self, item_type, x, y):
self.type = item_type
self.x = x
self.y = y
self.alive = False
def try_spawn(self, x):
""" Tries to spawn an active game object depending on screen position and item type """
if self.alive is False and x < self.x < x + WIDTH:
self.alive = True
if self.type is Item.Eagle:
Eagle(self, self.x, self.y - Eagle.size[1])
elif self.type is Item.Opossum:
Opossum(self, self.x, self.y - Opossum.size[1])
class Actor(object):
""" Generic active game entity base class """
spriteset = None
def __init__(self, item_ref, x, y):
self.x = x
self.y = y
self.sprite = engine.sprites[engine.get_available_sprite()]
self.animation = engine.animations[engine.get_available_animation()]
self.sprite.setup(self.spriteset)
self.item = item_ref
actors.append(self)
def __del__(self):
self.animation.disable()
self.sprite.disable()
if self.item is not None:
self.item.alive = False
def kill(self):
""" definitive kill of active game entity, removing from spawn-able item list too """
world.objects.remove(self.item)
self.item = None
actors.remove(self)
Code:
class World(object):
""" world/play field entity """
def __init__(self):
self.foreground = engine.layers[0]
self.background = engine.layers[1]
self.clouds = 0.0
self.foreground.setup(Tilemap.fromfile("layer_foreground.tmx"))
self.background.setup(Tilemap.fromfile("layer_background.tmx"))
self.x = 0
self.x_max = self.foreground.width - WIDTH
self.objects = load_objects("assets/layer_foreground.tmx", "Capa de Objetos 1", 973)
engine.set_background_color(self.background.tilemap)
actors.append(self)
def pick_gem(self, tiles_list):
""" updates tilemap when player picks a gem """
for tile_info in tiles_list:
if tile_info.type is Tiles.Gem:
self.foreground.tilemap.set_tile(tile_info.row, tile_info.col, None)
Effect(tile_info.col*16, tile_info.row*16, spriteset_vanish, seq_vanish)
sounds.play("pickup", 1)
break
def update(self):
""" updates world state once per frame """
oldx = self.x
if player.x < 240:
self.x = 0
else:
self.x = int(player.x - 240)
if self.x > self.x_max:
self.x = self.x_max
self.clouds += 0.1
if self.x is not oldx:
self.foreground.set_position(self.x, 0)
self.background.set_position(self.x/8, 0)
# spawn new entities from object list
for item in self.objects:
item.try_spawn(self.x)
return True
But I have the impression that Actor.remove and world.object.remove are indefined
Also, seems I need ElementTree for the code, something I probably don't have in other languages. Should I use an external library for that?