简体   繁体   English

为什么我的程序在启动后不起作用?

[英]Why my program doesn't work after launch?

I want to add endless clouds, but my program doesn't work, what's wrong?我想添加无尽的云,但是我的程序不起作用,怎么了?

class Cloud(pygame.sprite.Sprite):
    def __init__(self):
        super(Cloud, self).__init__()
        self.image = clouds_jpg
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = random.randint(0, WIDTH)
        self.rect.centery = random.randint(HEIGHT - 550, HEIGHT)


    def update(self):
        self.rect.move_ip(-2, 0)
        if self.rect.right <= 0:
            self.rect.left = screen.get_width() 

all_sprites = pygame.sprite.Group()
player = Player()
cloud = Cloud()
all_sprites.add(player, cloud)
...
elif event.type == ADDCLOUD:
            new_cloud = Cloud()
            cloud.add(new_cloud)
            all_sprites.add(new_cloud)

Error:错误:

Traceback (most recent call last): File "C:\Users\дом\My project\111.py", line 117, in cloud.add(new_cloud) File "C:\Users\дом\AppData\Local\Programs\Python\Python36\lib\site-packages\pygame\sprite.py", line 133, in add self.add(*group) TypeError: add() argument after * must be an iterable, not Cloud回溯(最后一次调用):文件“C:\Users\дом\My project\111.py”,第 117 行,在 cloud.add(new_cloud) 文件“C:\Users\дом\AppData\Local\Programs\ Python\Python36\lib\site-packages\pygame\sprite.py",第 133 行,在 add self.add(*group) TypeError: add() argument after * must be an iterable, not Cloud

cloud is a pygame.sprite.Sprite . cloudpygame.sprite.Sprite You only can add a pygame.sprite.Sprite object toto apygame.sprite_Group :您只能将pygame.sprite.Sprite object 添加到pygame.sprite_Group

clouds = pygame.sprite.Group()
elif event.type == ADDCLOUD:
    new_cloud = Cloud()
    
    # cloud.add(new_cloud)
    clouds.add(new_cloud)

    all_sprites.add(new_cloud)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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