Python Forum
AttributeError : method has no attribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError : method has no attribute
#1
Hi I'm trying to make a basic alien invasion game but when I try to load a ship image into the midbottom of the game screen I get an error. Here is the code I'm using to create the ship:

class Ship:
    """A class to manage the ship."""

    def __init__(self, ai_game):
        """Initialise the ship and its starting position."""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        # Load the ship image and get its rect.
        self.image = pygame.image.load(r'C:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\Images\ship.bmp') 
        self.rect = self.image.get_rect
        # Start each new ship at the bottom of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

    def blitme(self):
        """Draw the ship at its current location."""
        self.screen.blit(self.image, self.rect)
I'm then getting this error

Error:
File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\alien_invasion.py", line 41, in <module> ai = AlienInvasion() File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\alien_invasion.py", line 22, in __init__ self.ship = Ship(self) File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\ship.py", line 15, in __init__ self.rect.midbottom = self.screen_rect.midbottom AttributeError: 'builtin_function_or_method' object has no attribute 'midbottom'
But when I looked online at pygame tutorials it says that midbottom should be a built in attribute. Can someone help me with this please?
Reply
#2
that means that your rect is not a pygame rect and the first thing i see is your get_rect method needs parenethesis

from
Quote:
self.rect = self.image.get_rect
to this
self.rect = self.image.get_rect()
Recommended Tutorials:
Reply
#3
Thanks, that's sorted it.
metulburr likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tetris - AttributeError: 'list' object has no attribute 'y' abscorpy 6 6,562 Feb-28-2019, 05:20 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020