Renderização e OO - Código

Como prometi, o código que ilustra a parte de renderização. Python não implementa interfaces explícitas, como em Java, então fiz uma ligeira gambiarra com uma classe abstrata. E sim, eu programo em inglês. Me processem :P


# Object renderer abstract class
class Renderer:
    # Default constructor
    #
    # Raises an exception because this class can't
    # be instantiated. It's used as a base class
    # for other renderer classes
    def __init__(self):
        raise "Can't instantiate this abstract class"

    # Renders an object
    #
    # Renders the object based on its class. It uses
    # an ugly chain of "if else isinstance", because
    # Python can't do method overloading (at least
    # not out of the box). Thanks, guys.
    def render(self, object):
        if isinstance(object, Map):
            self.__renderMap__(object)

# Renderer that uses pygame's routines
class PygameRenderer(Renderer):
    # Default constructor
    #
    # Overrides the base class' constructor
    # to prevent an instantiation exception
    def __init__(self):
        pass

    # Renders a map object
    def __renderMap__(self, map):
        print "Rendering map using PyGame!"

# Renderer that uses OpenGL routines
class OpenGLRenderer(Renderer):
    # Default constructor
    #
    # Overrides the base class' constructor
    # to prevent an instantiation exception
    def __init__(self):
        pass

    # Renders a map object
    def __renderMap__(self, map):
        print "Rendering map using OpenGL!"

1 Comentário »

  1. Sereburo disse,

    26.11.2007 @ 19:45

    Entendi mais ou menos… a sintaxe do python é bem diferente do C++, que eu tou acostumado.

    Tem msn paolo? Daqui a 15 dias eu me formo tb, e pretendia estudar justamente um jogo de plataforma no estilo metroid / castlevania, mas talvez com algum componente online… podíamos trocar idéias.

    sereburo@hotmail.com pra adicionar o meu

RSS feed para comentários neste post · URI do TrackBack

Deixe seu comentário