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

Re: [pygame] Rect attribute on pygame.draw.rect(screen, color, Rect, width=0)



On Sun, 2012-09-02 at 13:52 -0700, mr_Roboman4321 wrote:
> Hello. I'm trying to assign the rect attribute in a pygame.draw.rect()
> statement to a mouse position. here's my code (it's inside a while True:
> loop):
> 
>  Mouse = pygame.mouse.get_pos()
>  MouseRect = pygame.draw.rect(screen, RED, (Mouse, 100, 100), 1)


pygame.draw.rect(screen, Color("red"), (Mouse, (100, 100)), 1)

will work. The rect argument must be either 4 numbers, or 2 sequences
giving 2 numbers each, i.e. (x,y,w,h) or ((x,y), (w,h)). Mouse is a 2
item sequence.

I also imagine you want it centred so you will also need to change:
Mouse = pygame.mouse.get_pos()
Mouse = (Mouse[0] - 50, Mouse[1] - 50)

> what I am trying to do with this is to see if the mouse has passed over any
> of my sprite rects. If there is an easier way to do this, show that way.


That code only draws a rectangle, it does not do any kind of collision
detection, this will not tell you (or the program) if the mouse is over
a sprite. If you want to actually check collision in your code, you need
something like:

m = pygame.mouse.get_pos()
for s in sprites:
    if s.rect.collidepoint(m):
        print "Colliding with", s

Attachment: signature.asc
Description: This is a digitally signed message part