错误消息 -(AttributeError: Worm 实例没有属性 'vx') 是什么意思以及如何修复它?

发布于 2024-12-11 07:46:53 字数 4148 浏览 0 评论 0原文

我一直在关注教程,但不断收到以下错误
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 error
AttributeError: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-12-18 07:46:53

AttributeError 表示您试图访问对象的类定义中未定义的属性或方法。

看起来您在教程代码中还没有取得足够的进展来定义 Worm.move() 方法。它出现在教程的第 43 行,就在 Worm.draw() 之前。您将在 draw() 方法上遇到另一个 AttributeError,因为您还没有定义该错误。只需将这两个添加到 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,因为您缺少该属性(还有 vy) ) 来自 Worm.__init__()。将您的代码与教程页面上改进的游戏标题下的代码进行比较。当您遇到更多错误时,请将您的类定义与教程的进行比较。

添加到__init__()

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

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 before Worm.draw(). You are headed for another AttributeError on the draw() method, as you've not yet defined that one either. Just add both of these to the Worm class definition.

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

Update

You're now receiving the AttributeError on Worm.vx because you're missing that property (also vy) from Worm.__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__()

def __init__(self, surface):
    ...
    ...
    self.vx = 0
    self.vy = -1
    ...
    ...
千里故人稀 2024-12-18 07:46:53

dir_xdir_y 是 vx 和 vy 你应该将它们更改为 vx 和 vy...

dir_x and dir_y are vx and vy you should change them... to vx and vy...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文