Jennifer_28 2023-11-30 11:50 采纳率: 0%
浏览 50

pygame.sprite.Sprite.add() argument after * must be an iterable, not AlienInvasion

错误提示: TypeError: pygame.sprite.Sprite.add() argument after * must be an iterable, not AlienInvasion
看着两段代码是一样的,为什么自己写的代码运行出现错误提示,而范例代码确可以运行? 两段代码差异在哪里?

自己写的代码:
import pygame
from pygame.sprite import Sprite

class Alien(Sprite):

def __int__(self, ai_game):
    super().__init__()
    self.screen = ai_game.screen

    self.image = pygame.image.load('images/alien.bmp')
    self.rect = self.image.get_rect()

    self.rect.x = self.rect.width
    self.rect.y = self.rect.height

    self.x = float(self.rect.x)

范例代码:
import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
"""A class to represent a single alien in the fleet."""

def __init__(self, ai_game):
    """Initialize the alien and set its starting position."""
    super().__init__()
    self.screen = ai_game.screen

    # Load the alien image and set its rect attribute.
    self.image = pygame.image.load('images/alien.bmp')
    self.rect = self.image.get_rect()

    # Start each new alien near the top left of the screen.
    self.rect.x = self.rect.width
    self.rect.y = self.rect.height

    # Store the alien's exact horizontal position.
    self.x = float(self.rect.x)
  • 写回答

4条回答 默认 最新

  • zy2652696 2024-01-16 15:19
    关注

    因为你的初始化函数名字写错了
    一个是 int(self, ai_game):
    一个是 init(self, ai_game):

    评论

报告相同问题?

问题事件

  • 创建了问题 11月30日

悬赏问题

  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?