我一直关注a tutorial,但一直收到以下错误
AttributeError: Worm instance has no attribute '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)
----------改变--------
代码:
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'
答案 0 :(得分:3)
AttributeError
表示您试图访问未在对象的类定义中定义的对象上的属性或方法。
看起来您在教程代码中没有取得足够的进展来定义Worm.move()
方法。它出现在本教程的第43行,就在Worm.draw()
之前。您将前往AttributeError
方法的另一个draw()
,因为您尚未定义该方法。只需将这两个添加到Worm
类定义中。
43 def move(self):
44 """ Move the worm. """
45 self.x += self.vx
46 self.y += self.vy
47
48 if (self.x, self.y) in self.body:
49 self.crashed = True
50
51 self.body.insert(0, (self.x, self.y))
52
53 if (self.grow_to > self.length):
54 self.length += 1
55
56 if len(self.body) > self.length:
57 self.body.pop()
58
59 def draw(self):
60 #for x, y in self.body:
61 # self.surface.set_at((x, y), self.color)
62 x, y = self.body[0]
63 self.surface.set_at((x, y), self.color)
64 x, y = self.body[-1]
65 self.surface.set_at((x, y), (0, 0, 0))
<强>更新强>
您现在收到AttributeError
上的Worm.vx
,因为您错过了vy
的该属性(也是Worm.__init__()
)。将您的代码与教程页面上改进游戏标题下的代码进行比较。当您遇到更多错误时,请将您的班级定义与教程进行比较。
添加到__init__()
def __init__(self, surface):
...
...
self.vx = 0
self.vy = -1
...
...
答案 1 :(得分:0)
dir_x
和dir_y
是vx和vy你应该改变它们......到vx和vy ......