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'
AttributeErrorindicates 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 beforeWorm.draw(). You are headed for anotherAttributeErroron thedraw()method, as you’ve not yet defined that one either. Just add both of these to theWormclass definition.Update
You’re now receiving the
AttributeErroronWorm.vxbecause you’re missing that property (alsovy) fromWorm.__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__()