Why do group lists in pygame have to have “update” functions, and not any other?

大兔子大兔子 提交于 2021-01-28 07:16:21

问题


I made a small particles application, but I was testing it and the function at the bottom has to be called "update". I thought the function name, just like variables, was just a name. I thought it didn't matter what it was named, as long as it's the same when you call it. Apparently I was wrong. It will only recognize "update". If I change the function to "move", it will throw an error. Could someone explain why it's like this?

import pygame
import random
pygame.init()
win_height=600
win_width=800
win=pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("List practice")
white=(255,255,255)
black=(0,0,0)
clock=pygame.time.Clock()

class particle_class(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.Surface((25,25))
        self.image.fill(white)
        self.rect=self.image.get_rect()
        self.speed=0
    def update(self):
        self.rect.y+=self.speed
        
particles=pygame.sprite.Group()

for i in range(10):
    particle=particle_class()
    particle.speed=random.randrange(5,11)
    particle.rect.y=0
    particle.rect.x=random.randrange(0,win_width+1)
    particles.add(particle)

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
    win.fill(black)
    particles.update()
    particles.draw(win)
    pygame.display.update()
    for particle in particles:
        if particle.rect.y>win_height:
            particle.rect.y=0
            particle.speed=random.randrange(5,11)
            particle.rect.x=random.randrange(0,win_width+1)


回答1:


It will only recognize "update". If I change the function to "move", it will throw an error.

Yes of course. Read the documentation of pygame.sprite.Group.

pygame.sprite.Group.update() and pygame.sprite.Group.draw() are methods which are provided by pygame.sprite.Group.
The former delegates the to the update method of the contained pygame.sprite.Sprites - you have to implement the method.

pygame.sprite.Group.update()

Calls the update() method on all Sprites in the Group.

The later uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects - you have to ensure that the pygame.sprite.Sprites have the required attributes

pygame.sprite.Group.draw()

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect for the position.


If you want a similar mechanism with your own method, then you have to implement your own class derived from pygame.sprite.Group. For instance:

class MyGroup(pygame.sprite.Group):

    def __init__(self, *args):
        super().__init__(*args) 

    def move(self):
        for sprite in self:
            sprite.move()
class particle_class(pygame.sprite.Sprite):
    # [...]

    def move(self):
        self.rect.y+=self.speed
particles = MyGroup()

for i in range(10):
    particle=particle_class()
    particle.speed=random.randrange(5,11)
    particle.rect.y=0
    particle.rect.x=random.randrange(0,win_width+1)
    particles.add(particle)
particles.move()


来源:https://stackoverflow.com/questions/64835155/why-do-group-lists-in-pygame-have-to-have-update-functions-and-not-any-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!