0
class Powup(pg.sprite.Sprite):
    def __init__(self,x ,y ,color):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((10,10))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.vx = 0
        self.vy = 0
        self.state = False
        self.power = 1
        self.timeout = 5000
        #self.last_true = pg.time.get_ticks()




    def update(self):

        if p.rect.left < self.rect.centerx < p.rect.right and self.rect.bottom >= 560:
            self.kill()
            self.last_true = pg.time.get_ticks()
            self.state = True
            p.image.fill(red)

        if ( pg.time.get_ticks() - self.last_true ) > 5000  :
            p.image.fill(black)
            self.state = False



        self.rect.y += self.vy

when I run the program it says

if ( pg.time.get_ticks() - self.last_true ) > 5000 : AttributeError: 'Powup' object has no attribute 'last_true'

This makes no sense to me. Could someone please explain this to me?

1
  • Even if you uncomment the line where you set it? Commented May 14, 2016 at 13:25

2 Answers 2

1

You have "self.last_true = pg.time.get_ticks()" commented out in your object initialization. If the first if statement in your update is false it never gets set properly to be tested in the second if statement. Therefore you're testing the value of something you've never set.

Sign up to request clarification or add additional context in comments.

2 Comments

yes i tested that and i saw that. but i want the pg.time.get_ticks() to start when the first if statement is True can you suggest anything?
If you're dead set on waiting until that statement is true before starting your "self.last_true" from being set, you have to have that second if statement hiding behind a bool toggle that gets flipped the first time the first statement is true.
0

Going by your comment to the other answer, you could potentially do it like this and ignore it if there is an error.

def update(self):

    if p.rect.left < self.rect.centerx < p.rect.right and self.rect.bottom >= 560:
        self.kill()
        self.last_true = pg.time.get_ticks()
        self.state = True
        p.image.fill(red)

    try:
        if ( pg.time.get_ticks() - self.last_true ) > 5000  :
            p.image.fill(black)
            self.state = False
    except AttributeError:
        pass


    self.rect.y += self.vy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.