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.