1

i dont know what to do, ive googled it and looked at other answers but nothing ive tried is working, im trying to create a sprite, and then move it, i was able to get the sprite to move but it would immiedietly go back to its starting position. after inti this class i get NameError: name 'image' is not defined

import pygame
import os
import sys

pygame.init()
screen = pygame.display.set_mode((448, 576))
done = False

y = 320
x = 216

clock = pygame.time.Clock()
PACMANSPRITE = pygame.image.load("pacman.png").convert_alpha()

class pacman(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        self.image = image
        self.y=y
        self.x=x
        screen.blit((image), (x,y))


    def movement(self):
        pressed= pygame.key.get_pressed()
        if pressed[pygame.K_UP]:
            self.y=y-10
        if pressed[pygame.K_DOWN]:
            self.y=y+10
        if pressed[pygame.K_LEFT]:
            self.x=x-10
        if pressed[pygame.K_RIGHT]:
            self.x=x+10
        screen.blit(image, (x,y))

while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
                        pygame.quit()
                        sys.exit()

        screen.fill((0, 0, 0))


        pacman(PACMANSPRITE, x ,y)
        pacman.movement()



        pygame.display.flip()
        clock.tick(60)
cdd
  • 109
  • 11
  • Possible duplicate of [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – Carcigenicate Aug 13 '17 at 12:58

1 Answers1

1

You called the method directly on the class:

pacman.movement()

Perhaps you wanted to store the instance you created on the line preceding that call?

sprite = pacman(PACMANSPRITE, x ,y)
sprite.movement()

You probably want to create one instance outside of your loop:

sprite = pacman(PACMANSPRITE, x ,y)
while not done:
    # ...
    sprite.movement()

Next, your movement method references a global image variable that is not defined; presumably you meant the image attribute on the instance, referenceable with self.image:

def movement(self):
    # ...
    screen.blit(self.image, (x,y))
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343