错误消息 -(AttributeError: Worm 实例没有属性 'vx') 是什么意思以及如何修复它?
我一直在关注教程,但不断收到以下错误AttributeError:蠕虫实例没有属性“move”
我不确定这到底意味着什么或如何解决它。错误指的是第 44 行,该行是 w.move()
(这个已解决,请看下面)
import pygame
class Worm:
"""A Worm."""
def __init__(self, surface, x, y, length):
self.surface = surface
self.x = x
self.y = y
self.length = length
self.dir_x = 0
self.dir_y = -1
self.body = []
self.crashed = False
def key_event(self, event):
"""Handle Key events that affect the worm."""
if event.key == pygame.K_UP:
self.dir_x = 0
self.dir_y = -1
elif event.key == pygame.K_DOWN:
self.dir_x = 0
self.dir_y = 1
elif event.key == pygame.K_DOWN:
self.dir_x = -1
self.dir_y = 0
elif event.key == pygame.K_DOWN:
self.dir_x = 1
self.dir_y = 0
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), (255, 255, 255))
width = 640
height = 400
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
running = True
w = Worm(screen, width/2, height/2, 200)
while running:
screen.fill((0, 0, 0))
w.move()
w.draw()
if w.crashed or w.x <= 0 or w.x >= width -1 or w.y <= 0 or w.y >= height -1:
print "crash"
running = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
w.key_event(event)
pygame.display.flip()
clock.tick(240)
----------Change - --------
代码:
import pygame
class Worm:
"""A Worm."""
def __init__(self, surface, x, y, length):
self.surface = surface
self.x = x
self.y = y
self.length = length
self.dir_x = 0
self.dir_y = -1
self.body = []
self.crashed = False
def key_event(self, event):
"""Handle Key events that affect the worm."""
if event.key == pygame.K_UP:
self.dir_x = 0
self.dir_y = -1
elif event.key == pygame.K_DOWN:
self.dir_x = 0
self.dir_y = 1
elif event.key == pygame.K_DOWN:
self.dir_x = -1
self.dir_y = 0
elif event.key == pygame.K_DOWN:
self.dir_x = 1
self.dir_y = 0
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), (255, 255, 255))
def move(self):
"""move worm."""
self.x += self.vx
self.y += self.vy
if (self.x, sel.y) in self.body:
self.crashed = True
self.body.insert(0, (self.x, self.y))
if len(self.body) > self.length:
self.body.pop()
def draw(self):
#for x, y self.body:
# self.surface.set_at((x, y),self.color)
x, y = self.body[0]
self.surface.set_at((x, y), self.color)
x, y = self.body[-1]
self.surface.set_at((x, y), (0, 0, 0))
width = 640
height = 400
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
running = True
w = Worm(screen, width/2, height/2, 200)
while running:
screen.fill((0, 0, 0))
w.move()
w.draw()
if w.crashed or w.x <= 0 or w.x >= width -1 or w.y <= 0 or w.y >= height -1:
print "crash"
running = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
w.key_event(event)
pygame.display.flip()
clock.tick(240)
和错误 -
Traceback (most recent call last):
File "C:/Users/Enrique/Dropbox/Public/snakegametutorial.py", line 65, in <module>
w.move()
File "C:/Users/Enrique/Dropbox/Public/snakegametutorial.py", line 34, in move
self.x += self.vx
AttributeError: Worm instance has no attribute 'vx'
I've been following a tutorial but keep getting the following errorAttributeError: Worm instance has no attribute 'move'
I'm not sure exactly what it means or how to fix it. The error refers to line 44 towards the bottom the line is w.move()
(this one's solved look below)
import pygame
class Worm:
"""A Worm."""
def __init__(self, surface, x, y, length):
self.surface = surface
self.x = x
self.y = y
self.length = length
self.dir_x = 0
self.dir_y = -1
self.body = []
self.crashed = False
def key_event(self, event):
"""Handle Key events that affect the worm."""
if event.key == pygame.K_UP:
self.dir_x = 0
self.dir_y = -1
elif event.key == pygame.K_DOWN:
self.dir_x = 0
self.dir_y = 1
elif event.key == pygame.K_DOWN:
self.dir_x = -1
self.dir_y = 0
elif event.key == pygame.K_DOWN:
self.dir_x = 1
self.dir_y = 0
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), (255, 255, 255))
width = 640
height = 400
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
running = True
w = Worm(screen, width/2, height/2, 200)
while running:
screen.fill((0, 0, 0))
w.move()
w.draw()
if w.crashed or w.x <= 0 or w.x >= width -1 or w.y <= 0 or w.y >= height -1:
print "crash"
running = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
w.key_event(event)
pygame.display.flip()
clock.tick(240)
----------Change --------
code:
import pygame
class Worm:
"""A Worm."""
def __init__(self, surface, x, y, length):
self.surface = surface
self.x = x
self.y = y
self.length = length
self.dir_x = 0
self.dir_y = -1
self.body = []
self.crashed = False
def key_event(self, event):
"""Handle Key events that affect the worm."""
if event.key == pygame.K_UP:
self.dir_x = 0
self.dir_y = -1
elif event.key == pygame.K_DOWN:
self.dir_x = 0
self.dir_y = 1
elif event.key == pygame.K_DOWN:
self.dir_x = -1
self.dir_y = 0
elif event.key == pygame.K_DOWN:
self.dir_x = 1
self.dir_y = 0
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), (255, 255, 255))
def move(self):
"""move worm."""
self.x += self.vx
self.y += self.vy
if (self.x, sel.y) in self.body:
self.crashed = True
self.body.insert(0, (self.x, self.y))
if len(self.body) > self.length:
self.body.pop()
def draw(self):
#for x, y self.body:
# self.surface.set_at((x, y),self.color)
x, y = self.body[0]
self.surface.set_at((x, y), self.color)
x, y = self.body[-1]
self.surface.set_at((x, y), (0, 0, 0))
width = 640
height = 400
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
running = True
w = Worm(screen, width/2, height/2, 200)
while running:
screen.fill((0, 0, 0))
w.move()
w.draw()
if w.crashed or w.x <= 0 or w.x >= width -1 or w.y <= 0 or w.y >= height -1:
print "crash"
running = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
w.key_event(event)
pygame.display.flip()
clock.tick(240)
and Error -
Traceback (most recent call last):
File "C:/Users/Enrique/Dropbox/Public/snakegametutorial.py", line 65, in <module>
w.move()
File "C:/Users/Enrique/Dropbox/Public/snakegametutorial.py", line 34, in move
self.x += self.vx
AttributeError: Worm instance has no attribute 'vx'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AttributeError
表示您试图访问对象的类定义中未定义的属性或方法。看起来您在教程代码中还没有取得足够的进展来定义
Worm.move()
方法。它出现在教程的第 43 行,就在Worm.draw()
之前。您将在draw()
方法上遇到另一个AttributeError
,因为您还没有定义该错误。只需将这两个添加到Worm
类定义中即可。更新
您现在在
Worm.vx
上收到AttributeError
,因为您缺少该属性(还有vy
) ) 来自Worm.__init__()
。将您的代码与教程页面上改进的游戏标题下的代码进行比较。当您遇到更多错误时,请将您的类定义与教程的进行比较。添加到
__init__()
AttributeError
indicates that you attempted to access a property or method on an object that was not defined in the object's class definition.It just seems like you have not progressed far enough in the tutorial code to have defined the
Worm.move()
method. It occurs at line 43 of the tutorial, just beforeWorm.draw()
. You are headed for anotherAttributeError
on thedraw()
method, as you've not yet defined that one either. Just add both of these to theWorm
class definition.Update
You're now receiving the
AttributeError
onWorm.vx
because you're missing that property (alsovy
) fromWorm.__init__()
. Compare your code to the code under the heading The improved game on the tutorial page. When you encounter further errors, compare your class definition to the tutorial's.Add to
__init__()
dir_x
和dir_y
是 vx 和 vy 你应该将它们更改为 vx 和 vy...dir_x
anddir_y
are vx and vy you should change them... to vx and vy...