Package pyzzle :: Module RelativeRect
[hide private]
[frames] | no frames]

Source Code for Module pyzzle.RelativeRect

 1  """A Rect style object whose coordinates are given 
 2  as a fraction of another rect's width and height""" 
 3  from pygame.rect import Rect 
 4   
5 -class RelativeRect:
6 """A Rect style object whose coordinates are given 7 as a fraction of another rect's width and height 8 9 Pygame uses the Rect class to store rectangular coordinates. 10 One problem with this representation is that coordinates are measured in pixels. 11 This presents problems in a Myst game - what if you want to adjust the resolution 12 of your image files? Using only rect, you would have to re-adjust the rects of the 13 Hotspots inside that slide. 14 15 The RelativeRect addresses this ploblem. It stores coordinates as 16 a fraction of another rect's width and height. In our example, the Hotspot's 17 coordinates would be stored as a fraction of the rect from the Slide in which it 18 resides. 19 """
20 - def __init__(self, rect, reference=None):
21 """Creates a RelativeRect 22 @type rect: Rect or (float,float,float,float) 23 @param rect: The Rect that output will represent, or a tuple 24 (left,top,width,height) representing relative coordinates 25 of this instance. 26 @type reference: Rect 27 @param reference: the Rect that the instance will be in relation to. 28 This parameter is required if a Rect is supplied in the previous 29 parameter. 30 """ 31 if reference: 32 self.left =float(rect.left-reference.left) / reference.width 33 self.top =float(rect.top- reference.top) / reference.height 34 self.width =float(rect.width) / reference.width 35 self.height =float(rect.height) / reference.height 36 else: 37 self.left,self.top,self.width,self.height = rect 38 39 for coordinate in 'left', 'top', 'width', 'height': 40 if not getattr(self,coordinate): 41 setattr(self,coordinate,0)
42
43 - def absolute(self, reference):
44 """Gets a pygame Rect that is equivalent to RelativeRect 45 when in relation to another Rect. 46 47 @type reference: Rect 48 @param reference: the Rect that output will be in relation to 49 @rtype: Rect 50 """ 51 return Rect(self.left *reference.width +reference.left, 52 self.top *reference.height +reference.top, 53 self.width *reference.width, 54 self.height *reference.height)
55