[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Set_position method



> def Set_position(self, pos):
> print "old position: ", self.rect.topleft
> self.rect = self.rect.move(pos)
> print "new position: ", self.rect.topleft

hey chris. the Rect.move() function actually moves the
rectangle to its current relative position. with the new
python2 assignent operators this isn't as useful as i
first thought. your Set_position is effectively doing this

def Set_Position(self, pos):
    self.rect.topleft += pos


i think the easiest thing to do is simply change the
Set_position to look like this...

def Set_position(self, pos):
    print "old position: ", self.rect.topleft
    self.rect.topleft = pos
    print "new position: ", self.rect.topleft


assigning the rectangle's topleft corner will move the
rectangle so the topleft corner is positioned where you
assign it. the Rect object has a lot of members like this
you can use to reposition the rectangle. for example
>>> self.rect.center = pos

would center your rectangle on the given position. you can
also access individual elements. for example, to move a 
rectangle to the bottom of the screen.
>>> rect.bottom = screen.get_height()
or
>>> rect.bottom = screen.get_rect().bottom


the Rect.move function is good for moving a rectangle from its
current position. for example...
>>> r = Rect(10, 20, 100, 200)
>>> r.move(5, 5)
<Rect(15, 25, 100, 200)>


hopefully this clears up some grey areas and gives you some good
ideas on how to best use the Rect objects. good luck



____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org