The overloaded functions in pygame.Rect

The overloaded functions in pygame.Rect

Postby jedi_mind_ » Sat Jun 28, 2014 1:00 am

From the site pygame.org:
pygame object for storing rectangular coordinates
Rect(left, top, width, height) -> Rect
Rect((left, top), (width, height)) -> Rect
Rect(object) -> Rect


How would that be implemented in code. I'm trying to make a derived class but I don't know how to overload the functions.
jedi_mind_
 
Posts: 11
Joined: Sun Jun 08, 2014 5:31 pm

Re: The overloaded functions in pygame.Rect

Postby Mekire » Sat Jun 28, 2014 5:22 am

Those "alternate constructors" are already implemented in the parent class. There is no point in reimplementing them yourself.

Code: Select all
import pygame


class MyRect(pygame.Rect):
    def __init__(self, *args):
        pygame.Rect.__init__(self, *args)
Usage:
Code: Select all
>>> a = MyRect(1,2,3,4)
>>> a
<rect(1, 2, 3, 4)>
>>> a = MyRect((1,2), (3,4))
>>> a
<rect(1, 2, 3, 4)>
>>> a = MyRect((1,2,3,4))
>>> a
<rect(1, 2, 3, 4)>
>>> a = MyRect(pygame.Rect(1,2,3,4))
>>> a
<rect(1, 2, 3, 4)>
>>>

-Mek
User avatar
Mekire
 
Posts: 1140
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: Bing [Bot] and 3 guests