performance - Shortcut in Python for repetitive actions with variables? -
i wondering if there way express following code in shorter way using python. there placedwall
, placedflyer
, placedspring
, , placeddiamonds
. and, placedwalllist
, placedflyerlist
, placedspringlist
, , placeddiamondslist
. want because going add more objects same repetitive actions, make code unnecessarily long.
for placedwall in placedwalllist: if placedwall.rect.collidepoint(mousepos): placedsprites.remove(placedwall) placedwalllist.remove(placedwall) placedflyer in placedflyerlist: if placedflyer.rect.collidepoint(mousepos): placedsprites.remove(placedflyer) placedflyerlist.remove(placedflyer) placedspring in placedspringlist: if placedspring.rect.collidepoint(mousepos): placedsprites.remove(placedspring) placedspringlist.remove(placedspring) placeddiamonds in placeddiamondslist: if placeddiamonds.rect.collidepoint(mousepos): placedsprites.remove(placeddiamonds) placeddiamondslist.remove(placeddiamonds)
you might want put lists 1 dictionary {'walls': […], 'flyers': […], …}
, try this:
for group, items in placed_objects.items(): placed_objects[group] = [x x in items if not x.collidepoint(mousepos)]
Comments
Post a Comment