[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] more list problems
Eric Hunter schrieb:
so I have been able to write pygame.Rect rectangles into rect-type
lists. but now I'm trying to delete unwanted rectangles that are
printed to the screen.
when I draw one at a time, I can only delete the last drawn rectangle
before deleting the previous ones. I want to be able to delete any
rectangle at any time without problems. I've come close but have run
into a wall.
below is the "deleting rect" section. if anyone needs more then what's
provided i can provide it upon request.
thanks in advance.
[code]
if event.button == 3:
mousexTEMP, mouseyTEMP =
pygame.mouse.get_pos()
for i in range(0, len(rect)):
if
rect[i].collidepoint(mousexTEMP, mouseyTEMP):
del rect[i]
numRect -= 1
[/code]
Hi
1. use event.pos to get the mouseposition
2. be always carefull when deleting from a list while iterating over it
3. normally use a plural form for a list: rects = [] # list of rectangles
Here, how I would do it. It deletes all rects colliding with the mouse
position (untested code):
[code]
if event.button == 3:
mouserect = pygame.Rect(event.pos, (1, 1)) # perhaps a Rect(pos,
(0,0)) would work too
# since you actually want 1 pixel size
i = mouserect.collidelist(rects)
while i != -1:
del rects[i]
i = mouserect.collidelist(rects)
numRect = len(rects) # really needed?
[/code]
I hope that helps.
~DR0ID