python - Updating multiple objects without creating separate variables for them -
in game, "enemies" created objects class level loads.
when screen refreshes, each need update x , y positions. since there multiple enemies, need create long piece of code telling every enemy object update.
for example:
enemy1 = enemy() enemy2 = enemy() enemy3 = enemy() ... enemy1.update() enemy2.update() enemy3.update() ...
i need way create these objects when needed without needing create variable every single one, , function make objects in enemy class update when screen refreshes.
you can that:
class enemy(object): def __init__(self): pass def update(self): pass # call function during updates. enemies = [] iter in range(10): enemies.append(enemy()) # creating 10 enemies , appending list enemy in enemies: enemy.update() #updating every enemy in list
answer easy. during creating enemy append list. later, if need calculate x/y position iterate on list. remember remove
or del
enemy list if don't need anymore.
-edit- recommend book:
python programming absolute beginner
by michael dawson. it's book. written people wanted learn how create games in python. should read it.
Comments
Post a Comment