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

[pygame] Set_position method



Hey all, my last compiling problem was a stupid error - please disregard 
(re: pygame demographics).

Anyway, I'm pretty new to PyGame, and i'm having some problems with my 
Set_position method of my Sprite class. Here's the code for the entire 
Sprite class:

class Sprite:
"""Sprites are objects with images and sounds associated with them."""
cur_frame = 0
images = []
pos_changed = 0
def __init__(self, frames, cur_position=None):
for x in frames:
self.images.append(load_image(x, 0))
self.rect = self.images[0].get_rect() #just use the first image in the 
list for the size
self.Set_position(cur_position)

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

def Get_position(self):
return self.rect.topleft

def Draw(self, screen):
return screen.blit(self.Get_frame(), (self.rect.topleft[0] + 
screen.get_width() / 2, self.rect.topleft[1] + screen.get_height() / 2))

def Erase(self, screen, background):
return screen.blit(background, (self.rect.topleft[0] + 
screen.get_width() / 2, self.rect.topleft[1] + screen.get_height() / 2))

def Get_frame(self):
"""Returns the current Image object"""
if self.cur_frame == len(self.images):
self.cur_frame = 0
return self.images[self.cur_frame]
else:
self.cur_frame = self.cur_frame + 1
return self.images[self.cur_frame - 1]

The problem is, after I Set_position(), the rect's position seems to be 
changed (i check with self.rect.topleft), but it doesn't draw the sprite 
at the new position any more. (if you're wondering about the weird 
division by 2, i have that because in my game the 'origin' is 
effectively the middle of the screen).
Any help on this would be greatly appreciated. Thanks all! :)

--
Chris Armstrong

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