python - 错误消息 -(AttributeError : Worm instance has no attribute 'vx' ) mean and how can i fix it?

标签 python pygame

我一直在关注a tutorial但不断收到以下错误
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)

----------改变--------

代码:

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'

最佳答案

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)蠕虫.__init__()。将您的代码与教程页面上改进的游戏标题下的代码进行比较。当您遇到更多错误时,请将您的类定义与教程的进行比较。

添加到__init__()

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

关于python - 错误消息 -(AttributeError : Worm instance has no attribute 'vx' ) mean and how can i fix it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856843/

相关文章:

java - 在 Python 中解密由 Java 以 3DES 加密的数据

python - 机器人框架同时点击多个对象

python - Pygame 墙 Sprite 碰撞

python - Pygame 地面碰撞检测和位置重置

java - 在 Ubuntu 10.04 上安装 PyLucene 3.0.3

python - 嵌套元组的最大值

python - 如何绘制一定粗细的抗锯齿圆形线?如何设置 pygame.gfx.aacircle() 的宽度?

python - 我在使用 pygame 显示表面变化时遇到问题

python - 每组的 pandas 计算两个类别的比率,并使用 .pipe() 作为新列附加到数据框

python - 存在 midi 文件但无法使用 pygame 加载