【问题标题】:AttributeError: 'list' object has no attribute 'x'AttributeError:“列表”对象没有属性“x”
【发布时间】:2017-04-19 02:31:35
【问题描述】:

我正在尝试为游戏小行星制作一些岩石,但收到此错误消息。我该如何解决这个问题?

这是我的代码:

class Rock(movable.Movable):
# outline = [(10, 0), (12, 8), (0, 7), (-10, 10), (-5, 0), (-10, -10), 
(0, -8)]
outline = []
points = random.randint(5, 15)
for degrees in range(0, 360, int(360/points)):
    r = random.randint(5, 15)
    angle = math.radians(degrees)
    x = int(r * math.cos(angle))
    y = int(r * math.sin(angle))
    point = (x, y)
    outline.append(point)

x = random.randint(0, SCREENW)
y = random.randint(0, SCREENH)
d = random.randint(0, 359)
movable.Movable.__init__(outline, x, y, dir)
self.setColor(255, 0, 0)

dx = random.randint(1, 4) * random.randint(-1, 1)
dy = random.randint(1, 4) * random.randint(-1, 1)
self.setDX(7)
self.setDY(3)

ddir = random.randint(-20, 20)
self.rotate(ddir)

def paint(self, surface):
    pygame.draw.polygon(surface, (255, 0, 0), outline)

我只是不明白“列表”在哪里被调用?

这是我的引用:

Traceback (most recent call last):
File "pygame_starter.py", line 4, in <module>
import game
File "/Volumes/KINGSTON/CS1410/Asteroids/game.py", line 1, in <module>
import pygame, rocket
File "/Volumes/KINGSTON/CS1410/Asteroids/rocket.py", line 75, in 
<module>
class Rock(movable.Movable):
File "/Volumes/KINGSTON/CS1410/Asteroids/rocket.py", line 90, in Rock
movable.Movable.__init__(outline, x, y, dir)
File "/Volumes/KINGSTON/CS1410/Asteroids/movable.py", line 7, in 
__init__
locatable.Locatable.__init__(self, x, y)
File "/Volumes/KINGSTON/CS1410/Asteroids/locatable.py", line 6, in 
__init__
self.x = int(SCREENW//2)
AttributeError: 'list' object has no attribute 'x'

这是我的Locatable:

def __init__(self, x, y):
    self.x = int(SCREENW//2)
    self.y = int(SCREENH//2)
    self.dx = 0
    self.dy = 0
    self.dir = 0
    self.ddir = 0
    self.life = True
    self.color = (0, 0, 255)
    self.radius = 0

【问题讨论】:

  • 请在问题中包含完整的错误回溯。还请格式化代码的缩进。
  • 它会回到我的定位,但如果我摆脱了摇滚类,它就不会出现
  • locatable.Locatable 对应的__init__ 是什么?
  • 您的Rock 类有一个奇怪且可能不正确的结构。 (除了格式不正确。)它没有构造函数,没有任何方法,也没有任何self 引用。
  • 对于初学者,您仍然没有包含一些重要的代码。 Rock.__init__ 到底在哪里开始和结束?您创建的 movable 实例会发生什么(顺便说一句,通常没有理由显式调用超类,这就是 super 所做的)?我最好的选择是你在movable.__init__ 中混淆了参数顺序,并将outline 放在x 应该去的地方,但是如果你想让人们回答你的问题,不要让他们猜测。

标签: python python-3.x pygame


【解决方案1】:

你的代码很奇怪......但问题几乎肯定在这里:

movable.Movable.__init__(outline, x, y, dir)

__init__ 方法需要 movable.Movable 的实例作为第一个参数。你传递给它outline,这是一个列表。

【讨论】:

    猜你喜欢
    • 2021-07-05
    • 2022-12-15
    • 2018-01-16
    • 2016-05-14
    • 2016-12-21
    • 2022-01-23
    • 2022-01-23
    • 2019-11-01
    • 2021-08-23
    相关资源
    最近更新 更多