Pygame Sprite super()__初始化失败

2024-09-30 08:22:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Pygame和Python3制作一个可视化的进化模拟器。运行模拟器的文件的我的代码:

#!/usr/bin/python3

import pygame
from organism import Organism


def main():
    pygame.init()
    screen_width, screen_height = screen_size = (800, 800)
    screen = pygame.display.set_mode(screen_size)
    allOrganisms = pygame.sprite.Group()
    allOmnivores = pygame.sprite.Group()
    allHerbivores = pygame.sprite.Group()
    allCarnivores = pygame.sprite.Group()
    test = Organism(screen, (100, 100), allOrganisms, 0, None)


main()

我的生物体代码(super()初始化时代码失败):

#!/usr/bin/python3

import pygame
import pygame.gfxdraw
from random import randint


class Organism(pygame.sprite.Sprite):
    def __init__(self, screen, position, allOrganisms, generation, parents):
        # screen: pygame.Surface object
        # position: (x, y)
        # groups: tuple of pygame.sprite.Group objects
        # parents: if generation > 1, specify the organism's parents, otherwise use None
        super().__init__(self)
        self.wheelSpeeds = self.wheel1Speed, self.wheel2Speed = 0, 0
        self.size = 10
        self.pos = position
        self.x = position[0]
        self.y = position[1]
        sightRadiusSize = 75
        self.sightRadius = pygame.gfxdraw.aacircle(
            screen, self.x, self.y, sightRadiusSize, (0, 0, 0, 0))
        self.sightRadius = pygame.gfxdraw.filled_circle(
            screen, self.x, self.y, sightRadiusSize, (0, 0, 0, 0))
        self.foodRadiusSize = 25
        self.foodRadius = pygame.gfxdraw.aacircle(
            screen, self.x, self.y, sightRadiusSize, (0, 0, 0, 0))
        self.foodRadius = pygame.gfxdraw.filled_circle(
            screen, self.x, self.y, sightRadiusSize, (0, 0, 0, 0))
        self.generation = generation
        self.parents = parents

        if self.parents is not None:
            if len(self.parents) == 1:
                self.colorOut = self.parents.colorOut
                self.meatDigestibility = self.parents.meatDigestibility
                self.plantDigestibility = self.parents.plantDigestibility
            else:
                self.colorOut = ((self.parents[0].colorOut[0] +
                                  self.parents[1].colorOut[0]) / 2,
                                 (self.parents[0].colorOut[1] +
                                  self.parents[1].colorOut[1]) / 2,
                                 (self.parents[0].colorOut[2] +
                                  self.parents[1].colorOut[2]) / 2)
                self.meatDigestibility = (self.parents[0].meatDigestibility +
                                          self.parents[1].meatDigestibility) / 2
                self.plantDigestibility = (self.parents[0].plantDigestibility +
                                           self.parents[1].plantDigestibility) / 2
        else:
            self.colorOut = (randint(50, 255), randint(50, 255), randint(50, 255))
            self.meatDigestibility = randint(0, 100)
            self.plantDigestibility = randint(0, 100)

        self.add(allOrganisms)
        if abs(meatDigestibility - plantDigestibility) < 10:
            self.add(allOmnivores)
        elif meatDigestibility > plantDigestibility:
            self.add(allCarnivores)
        else:
            self.add(allHerbivores)

每当我创建一个新的有机体精灵时,我都会收到一条错误消息

File "/home/user/Programming/Python/Evolution Simulator/simulator_v2.py", line 18, in <module>
    main()
  File "/home/user/Programming/Python/Evolution Simulator/simulator_v2.py", line 15, in main
    test = Organism(screen, (100, 100), allOrganisms, 0, None)
  File "/home/user/Programming/Python/Evolution Simulator/organism_v2.py", line 14, in __init__
    super().__init__(self)
  File "/usr/local/lib/python3.5/dist-packages/pygame/sprite.py", line 124, in __init__
    self.add(*groups)
  File "/usr/local/lib/python3.5/dist-packages/pygame/sprite.py", line 142, in add
    self.add(*group)
TypeError: add() argument after * must be an iterable, not Organism

在我看来,要么是pygame.sprite.sprite文件中有错误,要么是我没有正确使用它,可能是后者


Tags: importselfaddinitgroupscreenpygamerandint

热门问题