为什么pygame中的群组列表必须具有“更新"功能?功能,还有其他功能吗? [英] Why do group lists in pygame have to have "update" functions, and not any other?

查看:45
本文介绍了为什么pygame中的群组列表必须具有“更新"功能?功能,还有其他功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小颗粒的应用程序,但是我正在测试它,并且底部的功能必须称为更新".我认为函数名称就像变量一样,只是一个名称.我以为它的名字无关紧要,只要您调用它的名字是一样的即可.显然我错了.它将仅识别更新".如果我将功能更改为"move",它将引发错误.有人可以解释为什么会这样吗?

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)

推荐答案

它将仅识别更新".如果我将功能更改为"move",则会引发错误.

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

是的.阅读 pygame.sprite.Group .

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

pygame.sprite.Group.update() pygame.sprite.Group.draw() 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()

在组中的所有Sprite上调用update()方法.

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

稍后,它们使用所包含的 pygame.sprite.Sprite image rect 属性绘制对象-您必须确保 pygame.sprite.Sprite 具有必需的属性

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()

将包含的Sprite绘制到Surface参数.这会将Sprite.image属性用作源表面,并将Sprite.rect属性用作位置.

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


如果要使用自己的方法使用类似的机制,则必须实现从 pygame.sprite.Group 派生的自己的类.例如:


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()

这篇关于为什么pygame中的群组列表必须具有“更新"功能?功能,还有其他功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆