【发布时间】: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