From: <ch...@us...> - 2009-03-08 11:12:14
|
Revision: 285 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=285&view=rev Author: chozone Date: 2009-03-08 11:12:02 +0000 (Sun, 08 Mar 2009) Log Message: ----------- New avatar: multidir_parts, the last one(for now...). Also really tiny fix in multidir, and comment updates in multidir and multiparts. Modified Paths: -------------- branches/VP-Grounds/modules/playground/avatar-multidir.py branches/VP-Grounds/modules/playground/avatar-multipart.py Added Paths: ----------- branches/VP-Grounds/modules/playground/avatar-multidir_parts.py Modified: branches/VP-Grounds/modules/playground/avatar-multidir.py =================================================================== --- branches/VP-Grounds/modules/playground/avatar-multidir.py 2009-03-03 19:53:06 UTC (rev 284) +++ branches/VP-Grounds/modules/playground/avatar-multidir.py 2009-03-08 11:12:02 UTC (rev 285) @@ -54,7 +54,8 @@ def move(self, pos): """ Call this def to move this avatar to pos x=pos[1], y=pos[2]. - Will update rect, and make playground module update our old+new area + Will update rect, and make playground module update our old+new area. + Will also change our direction if needed. """ oldRect=self.rect.__copy__() @@ -91,6 +92,8 @@ if not self.fullImage: return #Do we have base image? + self.currentDir=dir + #Actually change image self.blitRect.x = self.blitRect.w * dir self.image=self.fullImage.subsurface(self.blitRect) Added: branches/VP-Grounds/modules/playground/avatar-multidir_parts.py =================================================================== --- branches/VP-Grounds/modules/playground/avatar-multidir_parts.py (rev 0) +++ branches/VP-Grounds/modules/playground/avatar-multidir_parts.py 2009-03-08 11:12:02 UTC (rev 285) @@ -0,0 +1,123 @@ +from functions import * + +class Object(): + def __init__(self, data, share): + self.sh = share + + self.currentDir=0 # 0=d, 1=l, 2=u, 3=r + + self.imageList=data['Avatar'][1:] + self.imageNameList=data['Avatar'][1:] + self.rect = Rect(data['Pos'][1], data['Pos'][2], 0, 0) + self.zorder = self.rect.top + self.rect.height + + def show(self): + """ + Start the whole process of: + checking whether avatar is downloaded, [download it], return avatar, and finally: blit ourselves. + This has to be a seperate def. If you put this right into __init__, we will make playground blit + ourself, before the initialisation is ready. And so, if that's not ready, we're not in the objects + list of the playground, and we will not be blitted. + """ + + #We have multiple images, so loop through them, to download them all + for image in self.imageList: + self.sh['Downloader'].getImage(image, self.imageUpdate, image) + + def imageUpdate(self, image, *arg): + """ + The downloader will call this when he's finished. + Will load in the image, and when we have all our images, we wil build ourselved. + """ + imageName=arg[0] + image=image + + self.imageList[self.imageNameList.index(imageName)]=image + + #Does our list still contain strings? + #(wich indicates we are still awaiting some downloads) + for img in self.imageList: + if type(img)!=tuple: return + + self.buildAvatar(True) + + def update(self, screen, rect): + if self.rect.colliderect(rect): #Check if the rect collide with ours + rect = rect.move(-self.rect[0], -self.rect[1]) #Move rect to our position + myPos = (self.rect[0] + rect[0], self.rect[1] + rect[1]) #Calculate our position from the rect + screen.blit(self.image, myPos, rect) #Blit + + + def move(self, pos): + """ + Call this def to move this avatar to pos x=pos[1], y=pos[2]. + Will update rect, and make playground module update our old+new area. + Will also change our direction if needed. + """ + oldRect=self.rect.__copy__() + + #Change avatar direction + if self.rect.x < pos[1]: self.setDir('r') + elif self.rect.x > pos[1]: self.setDir('l') + elif self.rect.y < pos[2]: self.setDir('d') + elif self.rect.y > pos[2]: self.setDir('u') + + self.rect.x=pos[1] + self.rect.y=pos[2] + self.zorder = self.rect.top + self.rect.height + + updateRect=oldRect.union(self.rect) + + self.sh['Playground'].blit(updateRect) + + + def buildAvatar(self, blit=False): + oldRect=self.rect.__copy__() #So we can combine it later with our new rect, so we can also update te old area + + #Update Width(w) and height(h) + self.rect.w=max([rect.w/4 for img,rect in self.imageList]) + self.rect.h=0 + for img, rect in self.imageList: self.rect.h+=rect.h + + #And the zorder('cause we have new height) + self.zorder=self.rect.top+self.rect.height + + #Create clean surface for our avatar + self.image=pygame.Surface(self.rect.size, SRCALPHA, 32) + + #Blit all parts on this fresh surface + y=0 + for img,rect in self.imageList: + w=rect.w/4 #width of real image(there are 4 images in this one, for directions) + x=self.rect.w/2-w/2 + + self.image.blit(img, (x,y), (w*self.currentDir,0,w,rect.h)) + y+=rect.h + + if blit: + #Combine rects, and update area. It's time to show ourselves! :D + updateRect=self.rect.union(oldRect) + self.sh['Playground'].blit(updateRect) + + + def setDir(self, dir, blit=False): + """ + Change avatar direction. + `dir` can be either int(0,1,2,3) or str('d','l','u','r'). + """ + + #Change to correct value, or exit + if type(dir)!=int: + if dir=='d': dir=0 + elif dir=='l': dir=1 + elif dir=='u': dir=2 + elif dir=='r': dir=3 + else: return + + if dir < 0 or dir > 3: return #Valid number? + if dir == self.currentDir: return #Not the same as old direction? + + self.currentDir=dir + + #Actually change image + self.buildAvatar(blit) Modified: branches/VP-Grounds/modules/playground/avatar-multipart.py =================================================================== --- branches/VP-Grounds/modules/playground/avatar-multipart.py 2009-03-03 19:53:06 UTC (rev 284) +++ branches/VP-Grounds/modules/playground/avatar-multipart.py 2009-03-08 11:12:02 UTC (rev 285) @@ -25,7 +25,7 @@ def imageUpdate(self, image, *arg): """ The downloader will call this when he's finished. - Will load in the image, and make playground blit our rect. + Will load in the image, and when we have all our images, we wil build ourselved. """ imageName=arg[0] image=image This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |