Python/Pygame: All Instances Still On Screen After Restart -



Python/Pygame: All Instances Still On Screen After Restart -

i'm working on side-scrolling shooter in python , pygame. @ origin of play, create bunch of lists such enemies[] , shots[]. when game restarts, have tried can think of, instances still on screen lastly playthrough!

in restart code have this...

for item in enemies: enemies.remove(item) del item item in shots: shots.remove(item) del item

i have added code after reinitialize list, so:

enemies=[] shots=[]

but on new playthrough still there. help!

your main problem lists exist in more 1 context (another method or function)

when enemies = [] clear local variable named "enemies" - ceases point previous object, used in funtcion/method (although don't list code here).

your other approach, using for tries iterate on list while modifying - should (and does) break in unexpected ways.

simply do:

enemies[:] = [] shots[:] = []

instead: these alter object referenced names, not create new objects , utilize them locally. way, contents on whatever other function uses these lists emptied well.(it not magic: [:] piece syntax addresses elements of list - first last, , attribution makes elements replaced contents of empty list: nothing)

while work now, if using enemies, shots , other info structures across several functions/methods, maybe you'd have cleaner design using class, holding info structures attributes of class, , promote functions methods (even if otherwise not making other uses of object oriented design). way, explicit trying modify same objects used in other parts of code.

another advice: read docs, , familiarized pygame groups (and sprites) - quite handy clean design, , can perform improve doing list objects above (for example, if enemies group, you'd phone call enemies.empty() in case)

python pygame restart instances

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -