Pygame Rect clase

¿Cómo defino una clase rectangular en Pygame?

class square(pygame.Rect)
    def __init__(self):
        pygame.Rect.__init__(self)

El código anterior, que utilizará para definir una clase de sprite, no funciona.

preguntado Oct 07 '14, 14:10

1 Respuestas

Creo que lo que quieres es esto:

class Rectangle(object):
    def __init__(self, top_corner, width, height):
        self._x = top_corner[0]
        self._y = top_corner[1]
        self._width = width
        self._height = height

    def get_bottom_right(self):
        d = self._x + self.width
        t = self._y + self.height
        return (d,t)

Puedes usar esto así:

# Makes a rectangle at (2, 4) with width
# 6 and height 10
rect = new Rectangle((2, 4), 6, 10) 

# Returns (8, 14)
bottom_right = rect.get_bottom_right

Además, probablemente podría ahorrarse algo de tiempo haciendo una clase Point

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

Respondido 07 Oct 14, 14:10

Aunque esto no era exactamente lo que estaba buscando, (casi) resolvió mi problema. 'rect = new Rectangle((2, 4), 6, 10)' no es compatible con python, pero esto también podría ser una falla de mi versión de python. - Berendschot

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.