错误信息-(AttributeError: Worm实例没有属性'vx')是什么意思,我该如何修复?

1 投票
2 回答
1172 浏览
提问于 2025-04-17 04:47

我一直在跟着一个教程学习,但总是遇到以下错误
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'

2 个回答

0

dir_xdir_y 其实就是 vx 和 vy,你应该把它们改成 vx 和 vy...

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))

更新

现在你在 Worm.vx 上遇到了 AttributeError,因为你在 Worm.__init__() 中缺少了这个属性(还有 vy)。你可以把你的代码和教程页面上改进后的游戏部分的代码进行对比。当你遇到其他错误时,也可以把你的类定义和教程中的进行对比。

添加到 __init__()

def __init__(self, surface):
    ...
    ...
    self.vx = 0
    self.vy = -1
    ...
    ...

撰写回答