Hey I'm having a problem with an attribute error that i was wondering how to fix

the code I have is

import pygame
pygame.init()
#---------------------------------------------------------------------------------------------------------------------------------------------#
""" This is the small box which the players will be able to stand on"""
class Ground(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((768, 18))
        self.image.fill((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.bottomleft = (0, 240)
#----------------------------------------------------------------------------------------------------------------------------------------------#
"""The Background stage at this early version it will not be able to be chosen"""
class Level(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("SPRITES/BACKGROUND/Stage_Adon.gif")
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        
#-----------------------------------------------------------------------------------------------------------------------------------------------#
"""Player 1"""
class Player1(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.STANDING = 0
        self.RUNNING = 1

        self.loadImages()
        self.image = self.imageStand
        self.rect = self.image.get_rect()
        self.rect.x = 20
        self.rect.y = 150
        self.frame = 0
        self.delay = 3
        self.pause = 0
        self.state = self.STANDING
        direction = ""
    def checkKeys(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            direction = "RIGHT"
            self.rect.x -= 5
            self.state = self.RUNNING
            print self.state, self.rect.x
        elif keys[pygame.K_d]:
            direction = "LEFT"
            self.rect.x += 5
            self.state = self.RUNNING
            print self.state, self.rect.x
            
            
    def update(self):

        self.checkKeys()
        if self.state ==  self.STANDING:
            self.image = self.imageStand

        elif self.state == self.RUNNING:
            self.pause += 1
            if self.pause >= self.delay:
                self.pause = 0
                self.frame += 1
                if self.frame >= len(self.runImages):
                    self.frame = 0
                    self.state = self.STANDING
                    self.image = self.imageStand
                else:
                    self.image = self.runImages[self.frame]
    
    def loadImages(self):
        if self.direction == "RIGHT":
            
            self.imageStand = pygame.image.load("SPRITES/WALKING_RHS/000.gif")
            self.imageStand = self.imageStand.convert()
            transColor = self.imageStand.get_at((1, 1))
            self.imageStand.set_colorkey(transColor)

            self.runImages = []
            for i in range(5):
                imgName = "SPRITES/WALKING_RHS/00%d.gif" % i
                tmpImage = pygame.image.load(imgName)
                tmpImage = tmpImage.convert()
                transColor = tmpImage.get_at((1, 1))
                tmpImage.set_colorkey(transColor)
                self.runImages.append(tmpImage)

        elif self.direction == "LEFT":
            self.imageStand = pygame.image.load("SPRITES/WALKING_LHS/000.gif")
            self.imageStand = self.imageStand.convert()
            transColor = self.imageStand.get_at((1, 1))
            self.imageStand.set_colorkey(transColor)

            self.runImages = []
            for i in range(5):
                imgName = "SPRITES/WALKING_LHS/00%d.gif" % i
                tmpImage = pygame.image.load(imgName)
                tmpImage = tmpImage.convert()
                transColor = tmpImage.get_at((1, 1))
                tmpImage.set_colorkey(transColor)
                self.runImages.append(tmpImage)

#-----------------------------------------------------------------------------------------------------------------------------------------------#
"""This is Player 2"""
class Player2(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.STANDING = 0
        self.RUNNING = 1

        self.loadImages()
        self.image = self.imageStand
        self.rect = self.image.get_rect()
        self.rect.x = 728
        self.rect.y = 150
        self.frame = 0
        self.delay = 3
        self.pause = 0
        self.state = self.STANDING
        
            
    def checkKeys(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.rect.x -= 5
            self.state = self.RUNNING
            print self.state, self.rect.x
        elif keys[pygame.K_RIGHT]:
            self.rect.x += 5
            self.state = self.RUNNING
            print self.state, self.rect.x
            
            
    def update(self):

        self.checkKeys()
        
        if self.state ==  self.STANDING:
            self.image = self.imageStand

        elif self.state == self.RUNNING:
            self.pause += 1
            if self.pause >= self.delay:
                self.pause = 0
                self.frame += 1
                if self.frame >= len(self.runImages):
                    self.frame = 0
                    self.state = self.STANDING
                    self.image = self.imageStand
                else:
                    self.image = self.runImages[self.frame]

    def loadImages(self):
        self.imageStand = pygame.image.load("SPRITES/WALKING_LHS/000.gif")
        self.imageStand = self.imageStand.convert()
        transColor = self.imageStand.get_at((1, 1))
        self.imageStand.set_colorkey(transColor)

        self.runImages = []
        for i in range(5):
            imgName = "SPRITES/WALKING_LHS/00%d.gif" % i
            tmpImage = pygame.image.load(imgName)
            tmpImage = tmpImage.convert()
            transColor = tmpImage.get_at((1, 1))
            tmpImage.set_colorkey(transColor)
            self.runImages.append(tmpImage)


def main():     
    screen = pygame.display.set_mode((768, 240))
    pygame.display.set_caption("Fast Running")

    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 0))
    screen.blit(background, (0, 0))

    level = Level()
    player1 = Player1()
    player2 = Player2()
    ground = Ground()
    allSprites = pygame.sprite.Group(ground, level, player1, player2)

    clock = pygame.time.Clock()
    keepGoing = True
   
    
    while keepGoing:
        clock.tick(30)  

        

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            
        allSprites.clear(screen, background)
        allSprites.update()
        allSprites.draw(screen)
        pygame.display.flip()

if __name__ == "__main__":
    main()

when i run it it tells me player 1 has no attribute "direction"
any help would be much appreciated.

Try writing and testing your programs on piece at a time. I've commented everything after the __init__ function for Player1 below. Run it that way first and see if that helps. Then add the functions one by one and test them. The problem is not that Player1 doesn't have an attribute named direction, the problem is that you don't have any idea where and what is wrong. Next, you might want to figure out or ask how to use one Player class for any number of players.

#"""Player 1"""
class Player1(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.STANDING = 0
        self.RUNNING = 1

        self.loadImages()
        self.image = self.imageStand
        self.rect = self.image.get_rect()
        self.rect.x = 20
        self.rect.y = 150
        self.frame = 0
        self.delay = 3
        self.pause = 0
        self.state = self.STANDING
        direction = ""

    """--------------------  COMMENTED for testing-----------------------------------------------
    def checkKeys(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            direction = "RIGHT"
            self.rect.x -= 5
            self.state = self.RUNNING
            print self.state, self.rect.x
        elif keys[pygame.K_d]:
            direction = "LEFT"
            self.rect.x += 5
            self.state = self.RUNNING
            print self.state, self.rect.x
            
            
    def update(self):

        self.checkKeys()
        if self.state ==  self.STANDING:
            self.image = self.imageStand

        elif self.state == self.RUNNING:
            self.pause += 1
            if self.pause >= self.delay:
                self.pause = 0
                self.frame += 1
                if self.frame >= len(self.runImages):
                    self.frame = 0
                    self.state = self.STANDING
                    self.image = self.imageStand
                else:
                    self.image = self.runImages[self.frame]
    
    def loadImages(self):
        if self.direction == "RIGHT":
            
            self.imageStand = pygame.image.load("SPRITES/WALKING_RHS/000.gif")
            self.imageStand = self.imageStand.convert()
            transColor = self.imageStand.get_at((1, 1))
            self.imageStand.set_colorkey(transColor)

            self.runImages = []
            for i in range(5):
                imgName = "SPRITES/WALKING_RHS/00%d.gif" % i
                tmpImage = pygame.image.load(imgName)
                tmpImage = tmpImage.convert()
                transColor = tmpImage.get_at((1, 1))
                tmpImage.set_colorkey(transColor)
                self.runImages.append(tmpImage)

        elif self.direction == "LEFT":
            self.imageStand = pygame.image.load("SPRITES/WALKING_LHS/000.gif")
            self.imageStand = self.imageStand.convert()
            transColor = self.imageStand.get_at((1, 1))
            self.imageStand.set_colorkey(transColor)

            self.runImages = []
            for i in range(5):
                imgName = "SPRITES/WALKING_LHS/00%d.gif" % i
                tmpImage = pygame.image.load(imgName)
                tmpImage = tmpImage.convert()
                transColor = tmpImage.get_at((1, 1))
                tmpImage.set_colorkey(transColor)
                self.runImages.append(tmpImage)
    """

## under def main() after player1=Player1()
    print  player1.STANDING,
            player1.RUNNING,
            player1.rect.x,
            player1.rect.y,
            player1.frame,
            player1.delay,
            player1.pause,
            player1.direction
    ## or whatever is relevant

Try writing and testing your programs on piece at a time. I've commented everything after the __init__ function for Player1 below. Run it that way first and see if that helps. Then add the functions one by one and test them. The problem is not that Player1 doesn't have an attribute named direction, the problem is that you don't have any idea where and what is wrong. Next, you might want to figure out or ask how to use one Player class for any number of players.

#"""Player 1"""
class Player1(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.STANDING = 0
        self.RUNNING = 1

        self.loadImages()
        self.image = self.imageStand
        self.rect = self.image.get_rect()
        self.rect.x = 20
        self.rect.y = 150
        self.frame = 0
        self.delay = 3
        self.pause = 0
        self.state = self.STANDING
        direction = ""

    """--------------------  COMMENTED for testing-----------------------------------------------
    def checkKeys(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            direction = "RIGHT"
            self.rect.x -= 5
            self.state = self.RUNNING
            print self.state, self.rect.x
        elif keys[pygame.K_d]:
            direction = "LEFT"
            self.rect.x += 5
            self.state = self.RUNNING
            print self.state, self.rect.x
            
            
    def update(self):

        self.checkKeys()
        if self.state ==  self.STANDING:
            self.image = self.imageStand

        elif self.state == self.RUNNING:
            self.pause += 1
            if self.pause >= self.delay:
                self.pause = 0
                self.frame += 1
                if self.frame >= len(self.runImages):
                    self.frame = 0
                    self.state = self.STANDING
                    self.image = self.imageStand
                else:
                    self.image = self.runImages[self.frame]
    
    def loadImages(self):
        if self.direction == "RIGHT":
            
            self.imageStand = pygame.image.load("SPRITES/WALKING_RHS/000.gif")
            self.imageStand = self.imageStand.convert()
            transColor = self.imageStand.get_at((1, 1))
            self.imageStand.set_colorkey(transColor)

            self.runImages = []
            for i in range(5):
                imgName = "SPRITES/WALKING_RHS/00%d.gif" % i
                tmpImage = pygame.image.load(imgName)
                tmpImage = tmpImage.convert()
                transColor = tmpImage.get_at((1, 1))
                tmpImage.set_colorkey(transColor)
                self.runImages.append(tmpImage)

        elif self.direction == "LEFT":
            self.imageStand = pygame.image.load("SPRITES/WALKING_LHS/000.gif")
            self.imageStand = self.imageStand.convert()
            transColor = self.imageStand.get_at((1, 1))
            self.imageStand.set_colorkey(transColor)

            self.runImages = []
            for i in range(5):
                imgName = "SPRITES/WALKING_LHS/00%d.gif" % i
                tmpImage = pygame.image.load(imgName)
                tmpImage = tmpImage.convert()
                transColor = tmpImage.get_at((1, 1))
                tmpImage.set_colorkey(transColor)
                self.runImages.append(tmpImage)
    """

## under def main() after player1=Player1()
    print  player1.STANDING,
            player1.RUNNING,
            player1.rect.x,
            player1.rect.y,
            player1.frame,
            player1.delay,
            player1.pause,
            player1.direction
    ## or whatever is relevant

I know what the code is doing. I also discovered that you can't share attributes between classes without returning the variable value into an external entity, that was the problem. Thanks for help

Actually your problem was that you called loadImages() before assigning some meaningful value to the direction:

self.direction = "RIGHT"   # ""
        self.loadImages()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.