我正在使用Pygame和Python 3制作视觉进化模拟器。我的代码用于运行模拟器的文件:
#!/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)
每当我创建一个新的Organism精灵时,我都会收到一条错误消息
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文件中,或者我没有正确使用它,可能是后者。
答案 0 :(得分:0)
它需要是 super().__init__()
而不是 super().__init__(self)
super().__init__(self)
super().__init__()