1

I have put in the code self.rect = pygame.Rect(x, y, 100, 100). There is an AttributeError: type object 'MobRow1' has no attribute 'rect'. Why does this error keep popping up when I have put the rect in.

I have tried self.rect = pygame.Rect(self.x, self.y, 100, 100) This still doesn't work.

        class MobRow1(pygame.sprite.Sprite):
            def __init__(self, x, y):
                pygame.sprite.Sprite.__init__(self)
                self.image = pygame.image.load(badguy_file).convert_alpha()
                self.image = pygame.transform.scale(self.image, (50, 50))
                self.x = x
                self.y = y
                self.rect = pygame.Rect(x, y, 100, 100)
                self.mask = pygame.mask.from_surface(self.image)

            def update(self):
                self.rect.y += self.speedy
                self.rect.x += self.speedx
Max COSSETTO
  • 103
  • 7
  • 4
    Please create an [MCVE](https://stackoverflow.com/help/mcve) and add the full error traceback to your question. – Klaus D. Aug 14 '18 at 08:32
  • There must be another piece of code missing here. Is your function `update` called anywhere in your script ? – Val Berthe Aug 14 '18 at 08:32
  • @ValBerthe it isn't called anywhere. Would you like me to show you all of the code? – Max COSSETTO Aug 14 '18 at 08:36
  • @MaxCOSSETTO please add the error traceback first – Val Berthe Aug 14 '18 at 08:42
  • @ValBerthe Traceback (most recent call last): File "C:\Users\maxco\AppData\Local\Programs\Python\Python36\codingandcaring github\Basketball 2.2.3.py", line 143, in MobRow1.update(screen_rect) File "C:\Users\maxco\AppData\Local\Programs\Python\Python36\codingandcaring github\Basketball 2.2.3.py", line 140, in update self.rect.y += self.speedy AttributeError: 'pygame.Rect' object has no attribute 'rect' – Max COSSETTO Aug 14 '18 at 08:56
  • Seems like in Basketball 2.2.3.py you're calling `update` without instanciating your object. Did you create a `MobRow1()`object first ? – Val Berthe Aug 14 '18 at 09:12
  • @ValBerthe by object, do you mean sprite? Because I have made other sprites before this one, but no objects named `MobRow1()` – Max COSSETTO Aug 14 '18 at 09:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177997/discussion-between-val-berthe-and-max-cossetto). – Val Berthe Aug 14 '18 at 09:31

1 Answers1

2

You call MobRow1.update(screen_rect), which will result in self being screen_rect.

So when self.rect.y += self.speedy is called, it will fail with an AttributeError because self, which is screen_rect, which is a Rect instance, does not have a rect attribute.


What you should do, is calling update on an instance of MobRow1, not on the MobRow1 class itself. Also, if you do this, you either have to add another parameter to its update function or remove the screen_rect argument from the function call.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • Can you please give an example and where would I put the code (eg. Main, Sprite etc.) – Max COSSETTO Aug 14 '18 at 10:00
  • Because the suggested code isn't working for me currently. – Max COSSETTO Aug 14 '18 at 11:31
  • 1
    @MaxCOSSETTO I can't give you an example because I don't know what the rest of your code looks like, what `MobRow1` actually represents and how it should work in your game. Is there only ever one `MobRow1`? Is it always there? Then you should probably create it before your main loop, something like `mob = MobRow1(x, y)` and then call `mob.update()` – sloth Aug 14 '18 at 13:33