简体   繁体   中英

Why am I getting an Attribute Error for my code when it should be working

I have a class ScrollingCredits. In that, I have a method load_credits. Please have a look at the code

class ScrollingCredits:
    def __init__(self):


        self.load_credits("end_credits.txt")

        (self.background, self.background_rect) = load_image("starfield.gif", True)

        self.font = pygame.font.Font(None, FONT_SIZE)

        self.scroll_speed = SCROLL_SPEED

        self.scroll_pause = SCROLL_PAUSE

        self.end_wait = END_WAIT

        self.reset()

        def load_credits(self, filename):

            f = open(filename)

            credits = []

            while 1:

                line = f.readline()

                if not line:

                    break
            line = string.rstrip(line)

            credits.append(line)

            f.close()

            self.lines = credits

The first line after defining the function is where my attribute problem occurs I get this brought up when I try to run it: AttributeError: 'ScrollingCredits' object has no attribute 'load_credits'

If anyone would be able to help me it would be much appreciated

There is function definition and calling issue for load_credits , if you want to access the function with self Make the load_credits outside the __init__ function like below.

class ScrollingCredits:
    def __init__(self):
        self.load_credits("end_credits.txt")
............

    def load_credits(self, filename):
............

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM