RelativeRect.py :  » Game-2D-3D » Pyzzle » pyzzle-0.9 » pyzzle » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » Pyzzle 
Pyzzle » pyzzle 0.9 » pyzzle » RelativeRect.py
"""A Rect style object whose coordinates are given
as a fraction of another rect's width and height"""
from pygame.rect import Rect

class RelativeRect:
    """A Rect style object whose coordinates are given
    as a fraction of another rect's width and height
    
    Pygame uses the Rect class to store rectangular coordinates.
    One problem with this representation is that coordinates are measured in pixels.
    This presents problems in a Myst game - what if you want to adjust the resolution
    of your image files? Using only rect, you would have to re-adjust the rects of the
    Hotspots inside that slide.
    
    The RelativeRect addresses this ploblem. It stores coordinates as 
    a fraction of another rect's width and height. In our example, the Hotspot's 
    coordinates would be stored as a fraction of the rect from the Slide in which it
    resides. 
    """
    def __init__(self, rect, reference=None):
        """Creates a RelativeRect
        @type rect: Rect or (float,float,float,float)
        @param rect: The Rect that output will represent, or a tuple 
            (left,top,width,height) representing relative coordinates 
            of this instance. 
        @type reference: Rect
        @param reference: the Rect that the instance will be in relation to.
            This parameter is required if a Rect is supplied in the previous
            parameter.
        """
        if reference:
            self.left   =float(rect.left-reference.left) / reference.width
            self.top    =float(rect.top- reference.top)  / reference.height
            self.width  =float(rect.width)               / reference.width
            self.height =float(rect.height)              / reference.height
        else:
            self.left,self.top,self.width,self.height = rect
            
        for coordinate in 'left', 'top', 'width', 'height':
            if not getattr(self,coordinate):
                setattr(self,coordinate,0)
        
    def absolute(self, reference):
        """Gets a pygame Rect that is equivalent to RelativeRect
        when in relation to another Rect.
        
        @type reference: Rect
        @param reference: the Rect that output will be in relation to
        @rtype: Rect
        """
        return Rect(self.left   *reference.width  +reference.left,
                    self.top    *reference.height +reference.top,
                    self.width  *reference.width,
                    self.height *reference.height)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.