Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Objects list : How to use it?
#15
I think I found important things about what I want in the cide

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?
Reply


Messages In This Thread
Objects list : How to use it? - by System64 - 12-19-2020, 05:20 AM
RE: Objects list : How to use it? - by megamarc - 12-19-2020, 06:09 PM
RE: Objects list : How to use it? - by System64 - 12-19-2020, 09:57 PM
RE: Objects list : How to use it? - by megamarc - 12-19-2020, 10:27 PM
RE: Objects list : How to use it? - by System64 - 12-19-2020, 11:29 PM
RE: Objects list : How to use it? - by megamarc - 12-19-2020, 11:59 PM
RE: Objects list : How to use it? - by System64 - 12-20-2020, 12:07 AM
RE: Objects list : How to use it? - by megamarc - 12-20-2020, 12:15 AM
RE: Objects list : How to use it? - by System64 - 12-20-2020, 02:58 AM
RE: Objects list : How to use it? - by megamarc - 12-20-2020, 03:14 AM
RE: Objects list : How to use it? - by System64 - 12-20-2020, 04:14 AM
RE: Objects list : How to use it? - by megamarc - 12-20-2020, 08:05 AM
RE: Objects list : How to use it? - by System64 - 12-20-2020, 09:25 AM
RE: Objects list : How to use it? - by megamarc - 12-20-2020, 05:17 PM
RE: Objects list : How to use it? - by System64 - 12-20-2020, 10:06 PM
RE: Objects list : How to use it? - by megamarc - 12-21-2020, 03:14 AM
RE: Objects list : How to use it? - by System64 - 12-21-2020, 04:13 AM
RE: Objects list : How to use it? - by megamarc - 12-21-2020, 07:41 AM
RE: Objects list : How to use it? - by System64 - 12-21-2020, 07:53 AM
RE: Objects list : How to use it? - by megamarc - 12-21-2020, 08:23 AM
RE: Objects list : How to use it? - by System64 - 12-21-2020, 09:15 AM
RE: Objects list : How to use it? - by megamarc - 12-24-2020, 06:47 AM
RE: Objects list : How to use it? - by System64 - 12-24-2020, 06:56 AM
RE: Objects list : How to use it? - by megamarc - 12-24-2020, 04:33 PM
RE: Objects list : How to use it? - by System64 - 12-24-2020, 09:43 PM
RE: Objects list : How to use it? - by megamarc - 12-25-2020, 07:31 AM
RE: Objects list : How to use it? - by System64 - 12-25-2020, 08:59 AM
RE: Objects list : How to use it? - by megamarc - 12-27-2020, 05:05 PM
RE: Objects list : How to use it? - by System64 - 12-27-2020, 09:44 PM
RE: Objects list : How to use it? - by megamarc - 12-28-2020, 05:58 PM
RE: Objects list : How to use it? - by System64 - 12-28-2020, 09:13 PM
RE: Objects list : How to use it? - by megamarc - 12-28-2020, 10:42 PM
RE: Objects list : How to use it? - by System64 - 12-29-2020, 01:31 AM
RE: Objects list : How to use it? - by megamarc - 12-31-2020, 10:21 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)