Home > Enterprise >  'pygame.Surface' object has no attribute 'update'
'pygame.Surface' object has no attribute 'update'

Time:11-20

I get the message : 'pygame.Surface' object has no attribute 'update'. But as you can see, i have an update function in the code. wha did i wrong? I looked around but i didn't fina a simular question.

class Createparticle:
    def __init__(self, xx, yy,img):
        self.x = xx
        self.y = yy
        self.img = img
        self.particlelist = []
        self.verzoegerung = 0        
        self.scale_k = 0.1
        self.img = scale(img, self.scale_k)
        self.alpha = 255
        self.alpha_rate = 3
        self.alive = True
        self.vx = 0
        self.vy = 4   random.randint(-10, 10) / 10
        self.k = 0.01 * random.random() * random.choice([-1, 1])

    def update(self):
        self.x  = self.vx
        self.vx  = self.k
        self.y -= self.vy
        self.vy *= 0.99
        self.scale_k  = 0.005
        self.alpha -= self.alpha_rate           
        self.img = scale(self.img, self.scale_k)
        self.img.set_alpha(self.alpha)
        self.particlelist = [i for i in self.particlelist if i.alive]      
        self.verzoegerung  = 1
        if self.verzoegerung % 2 == 0:
            self.verzoegerung = 0
            self.particlelist.append(self.img)        
        for i in self.particlelist:
            i.update()
 
    def draw(self):
        for i in self.particlelist:
            screen.blit(self.img, self.img.get_rect(center=(self.x, self.y)))
  
createparticle = Createparticle(500,300,basisbild)

while True:       
    screen.fill((0, 0, 0))
    createparticle.update()
    createparticle.draw()      
    pygame.display.update()
    clock.tick(FPS)

CodePudding user response:

you have a list of surfaces called particlelist, in your update function you are calling the update function on each item in that list. Since theses are of the 'surface' type they don't have a update function. This is where the error is coming from.

CodePudding user response:

The error is caused by i.update(). i is an element from self.particlelist. In your case self.particlelist is an image (pygame.Surface). A pygame.Surface object has no update method. Probably i should not be a pygame.Surface, but you add pygame.Surface objects to the list:

self.particlelist.append(self.img)

So this line of code is obviously wrong and should be like this instead (note: Particle is a guess of mine, but I don't know how you named your classes.):

self.particlelist.append(Particle(self.img))
  • Related