[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] MOUSEBUTTONDOWN question



srowley wrote:
Just making a quick game where you trace out a shape on the screen while
holding the mouse button.
Trying to check for a collision with the mouse while the button is down,
but for some reason it seems to only check for collision when the button
initially goes down, but doesn't keep checking for collision when the
mouse button is held down.
But I think it should since this is all in the event loop


if event.type is MOUSEBUTTONDOWN:
	pos=pygame.mouse.get_pos()
      mouseRect=[[pos[0],pos[1]],[5,5]]
      mouseRect=Rect(mouseRect)
      m=mouseRect.collidelist(shape)
	if m==-1:
		print "oops! Your not on the shape anymore"
you only get this MOUSEBUTTODOWN event when the button is first pressed, not every frame while it is held down. what you'll want to do is keep track of the MOUSEMOVEMENT events, when the mouse has been pressed.

something more like this..

if event.type == MOUSEBUTTONDOWN:
pressing = 1
if Rect(event.pos, (5, 5)).collidelist(shape) == -1:
print "oops!"
elif event.type == MOUSEBUTTONUP:
pressing = 0
elif event.type == MOUSEMOTION and pressing:
if Rect(event.pos, (5, 5)).collidelist(shape) == -1:
print "oops!"


note that your 'mouserect' here is 5x5 pixels but not centered on the mouse position. you may want to use a little helper function to create centered rectangles...

def create_mouse_rect(pos, size):
r = Rect((0, 0), size)
r.center = pos
return r

with that you can easily create centered mouseRects with a call like this..
mouseRect = create_mouse_rect(event.pos, (5, 5))