Pygame Type error: Argument must be a rect style object

Viewed 448

I created this class so I could work with better

class Block:
  def __init__(self, top, left):
    self.Rect = pygame.Rect((top,left),(10,10))

Then I wanted to create a simple function that clips a rect like so

def clip(rect_obj):
  return pygame.Rect.clip(rect_obj)

But when I pass the rect into my function I get the error in the headline. I even printed the type

my_block = Block()
print(type(my_block.Rect))    --> <Class Pygame.Rect>
my_rect = clip(my_block.Rect) --> TypeError: Argument must be a rect style Object

I want to know if there is a difference between a Rect Style Object and a Rect Object because I'm so confused. Also would drawing rects on a screen be the best way to layout a tic tac toe GUI or is it simpler to draw lines.

2 Answers

This should work:

my_block = Block(25, 25)
my_block1 = Block(25, 25)
my_block1.Rect.clip(my_block.Rect)

Note: clip() is a pygame.Rect method. Also, maybe it would be easier to inherit from pg.Rect:

class Block(pygame.Rect):
    def __init__(self, rect):
        super().__init__(rect)

and add attributes you need. Surely there are better ways, this is just an idea.

The error is the line pygame.Rect.clip(rect_obj)

def clip(rect_obj):
   return pygame.Rect.clip(rect_obj)  

This line does not make any sense at all, because pygame.Rect.clip is a instance method. pygame.Rect.clip():

Returns a new rectangle that is cropped to be completely inside the argument Rect. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned.

The mehtod intersects 2 pygame.Rect objects:

rect_1 = pygame.Rect(0, 0, 20, 20)
rect_2 = pygame.Rect(10, 10, 30, 30)
rect_and = rect_1.clip(rect_2 )

rect_and is the rectangle at (10, 10), with a size of (10, 10).

Probably you try to set the clipping region to the display surface. This can be done by pygame.Surface.set_clip(). For instance:

screen = display.set_mode((width, height))

clip_rect = pygame.Rect(x, y, w, h)
screen.set_clip(clip_rect )
Related