gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:48:57 26 4
gpt4 key购买 nike

我一直在关注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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com